素材巴巴 > 程序开发 >

机器人中的坐标变换

程序开发 2023-09-07 12:11:28

机器人中的坐标变换
    参考:机器人学导论 - 第二章

TF 功能包
    5s之前,机器人头部坐标系相对于全局坐标系的关系是什么样的
    机器人夹取的物体相对于机器人中心坐标系的位置
    机器人中心坐标系相对于全局坐标系的位置

TF坐标变换实现
    广播TF变换
    监听TF变换

坐标变化例程
sudo apt-get install ros-kinetic-turtle-tf
kinetic@vm:~$ rospack find turtle_tf
/opt/ros/kinetic/share/turtle_tf

运行例程
kinetic@vm:~$ roslaunch turtle_tf turtle_tf_demo.launch
kinetic@vm:~$ rosrun turtlesim turtle_teleop_key
kinetic@vm:~$ rosrun tf view_frames
在 home目录下生成 frames.pdf

查找两只海龟坐标变化的关系,执行如下命令
kinetic@vm:~$ rosrun tf tf_echo turtle1 turtle2

rviz 可视环境下运行
kinetic@vm:~$ rosrun rviz rviz -d `rospack find turtle_tf` /rviz/turtle_rviz.rviz

实现TF广播器
        定义TF广播器(TransformBroadcaster)
        创建坐标变换值

        发布坐标变换值(sendTransform)

创建tf包
kinetic@vm:~/catkin_ws/src$ catkin_create_pkg learning_tf std_msgs rospy roscpp

~/catkin_ws/src/learning_tf/src/turtle_tf_broadcaster.cpp

  1. #include "ros/ros.h"
  2. #include "tf/transform_broadcaster.h"
  3. #include "turtlesim/Pose.h"
  4. std::string turtle_name;
  5. void poseCallback(const turtlesim::PoseConstPtr &msg)
  6. {
  7. // tf 广播器
  8. static tf::TransformBroadcaster br;
  9. // 根据乌龟当前的位姿,设置相对于世界坐标系的坐标变换
  10. tf::Transform transform;
  11. transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0));
  12. tf::Quaternion q;
  13. q.setRPY(0, 0, msg->theta);
  14. transform.setRotation(q);
  15. // 发布坐标变换
  16. br.sendTransform( tf::StampedTransform( transform, ros::Time::now(), "world", turtle_name));
  17. }
  18. int main(int argc, char **argv)
  19. {
  20. // 初始化节点
  21. ros::init(argc, argv, "my_tf_broadcaster");
  22. if ( argc !=2 )
  23. {
  24. ROS_ERROR("need turtle name as argument");
  25. return -1;
  26. }
  27. turtle_name = argv[1];
  28. // 订阅乌龟的pose信息
  29. ros::NodeHandle node;
  30. ros::Subscriber sub = node.subscribe(turtle_name+"/pose", 10, &poseCallback);
  31. ros::spin();
  32. return 0;
  33. }

实现TF监听器步骤
        定义TF监听器(TransFormListener)
        查找坐标变换(waitForTransform、lookupTransform)

~/catkin_ws/src/learning_tf/src/turtle_tf_listener.cpp

  1. #include "ros/ros.h"
  2. #include "tf/transform_listener.h"
  3. #include "geometry_msgs/Twist.h"
  4. #include "turtlesim/Spawn.h"
  5. int main(int argc, char **argv)
  6. {
  7. // 初始化节点
  8. ros::init(argc, argv, "my_tf_listener");
  9. ros::NodeHandle node;
  10. // 通过服务器调用,产生第二只乌龟turtle2
  11. ros::service::waitForService("spawn");
  12. ros::ServiceClient add_turtle = node.serviceClient("spawn");
  13. turtlesim::Spawn srv;
  14. add_turtle.call(srv);
  15. // 定义 turtle2的速度控制发布器
  16. ros::Publisher turtle_vel = node.advertise("turtle2/cmd_vel", 10);
  17. // tf 监听器
  18. tf::TransformListener listener;
  19. ros::Rate rate(10.0);
  20. while( node.ok() ) {
  21. tf::StampedTransform transform;
  22. try
  23. {
  24. // 查找 turtle2 与 turtle1 的坐标变换
  25. listener.waitForTransform( "turtle2", "turtle1", ros::Time(0), ros::Duration(3.0) );
  26. listener.lookupTransform("turtle2", "turtle1", ros::Time(0), transform);
  27. }
  28. catch (tf::TransformException &ex)
  29. {
  30. ROS_INFO("%s", ex.what() );
  31. ros::Duration(1.0).sleep();
  32. continue;
  33. }
  34. // 根据turtle1 和 turtle2 之间的坐标变换, 计算turtle2需要运动的线速度和角速度
  35. // 并发布速度控制指令,使turtle2向turtle1移动
  36. geometry_msgs::Twist vel_msg;
  37. vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),
  38. transform.getOrigin().x());
  39. vel_msg.linear.x = 0.5 * sqrt( pow(transform.getOrigin().x(), 2) +
  40. pow(transform.getOrigin().y(), 2) );
  41. turtle_vel.publish(vel_msg);
  42. rate.sleep();
  43. }
  44. return 0;
  45. }

修改 ~/catkin_ws/src/learning_tf/CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
  roscpp
  tf
)
add_executable(turtle_tf_broadcaster src/turtle_tf_broadcaster.cpp)
add_executable(turtle_tf_listener src/turtle_tf_listener.cpp)
target_link_libraries( turtle_tf_broadcaster  ${catkin_LIBRARIES} )
target_link_libraries( turtle_tf_listener  ${catkin_LIBRARIES} )

编译
kinetic@vm:~/catkin_ws$ catkin_make

创建luanch
kinetic@vm:~/catkin_ws/src/learning_tf$ mkdir launch
~/catkin_ws/src/learning_tf$ cd launch/start_demo_with_listener.launch

运行
kinetic@vm:~$ roslaunch learning_tf start_demo_with_listener.launch


标签:

上一篇: IDEA 对项目添加watch产生的问题 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。