标签:
在机器人的控制中,坐标系统是非常重要的,在ROS使用tf软件库进行坐标转换。
相关链接:http://www.ros.org/wiki/tf/Tutorials#Learning_tf
$ rosdep install turtle_tf rviz
$ rosmake turtle_tf rviz
可以发现,第二只乌龟会跟随你移动的乌龟进行移动。
$ rosrun tf view_frames
我们也可以通过rviz的图形界面更加形象的看到这三者之间的关系。
$ rosrun rviz rviz -d `rospack find turtle_tf`/rviz/tule_rviz.rviz
cd catkin_ws/src/
catkin_create_pkg learning_tf tf roscpp rospy turtlesim
建立你的新包roscd之前:
$ cd ~/catkin_ws
$ catkin_make
$ source ./devel/setup.bash
我们首先创建源文件。 我们刚刚创建的包:
$ roscd learning_tf
去 src / 文件夹并打开你最喜欢的编辑器,将下面的代码粘贴到一个新文件 src / turtle_tf_broadcaster.cpp 。
#include <ros/ros.h> #include <tf/transform_broadcaster.h> #include <turtlesim/Pose.h> std::string turtle_name; void poseCallback(const turtlesim::PoseConstPtr& msg){ static tf::TransformBroadcaster br; tf::Transform transform; transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) ); tf::Quaternion q; q.setRPY(0, 0, msg->theta); transform.setRotation(q); br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", turtle_name)); } int main(int argc, char** argv){ ros::init(argc, argv, "my_tf_broadcaster"); if (argc != 2){ROS_ERROR("need turtle name as argument"); return -1;}; turtle_name = argv[1]; ros::NodeHandle node; ros::Subscriber sub = node.subscribe(turtle_name+"/pose", 10, &poseCallback); ros::spin(); return 0; };
现在我们创建代码,允许编译它。 打开 CMakeLists.txt 底部文件,并添加以下行:
add_executable(turtle_tf_broadcaster src/turtle_tf_broadcaster.cpp) target_link_libraries(turtle_tf_broadcaster ${catkin_LIBRARIES})
Build your package; at the top folder of your catkin workspace:
$ catkin_make
创建launch文件start_demo.launch:
<launch> <!-- Turtlesim Node--> <node pkg="turtlesim" type="turtlesim_node" name="sim"/> <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/> <!-- Axes --> <param name="scale_linear" value="2" type="double"/> <param name="scale_angular" value="2" type="double"/> <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle1" name="turtle1_tf_broadcaster" /> <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle2" name="turtle2_tf_broadcaster" /> </launch>
运行:
$ roslaunch learning_tf start_demo.launch
可以看到界面中只有移植乌龟了,打开tf_echo的信息窗口:
$ rosrun tf tf_echo /world /turtle1
world参考系的原点在最下角,对于turtle1的转换关系,其实就是turtle1在world参考系中所在的坐标位置以及旋转角度。
这一步,我们将看到如何使用tf进行参考系转换。首先写一个tf listener(turtle_tf_listener.cpp)
. The turtlesim/Velocity.h header is not used anymore(再也不), it has been replaced by geometry_msgs/Twist.h. Furthermore(此外), the topic /turtle/command_velocity is now called /turtle/cmd_vel. In light of this, a few changes are necessary to make it work:
#include <ros/ros.h> #include <tf/transform_listener.h> #include <geometry_msgs/Twist.h> #include <turtlesim/Spawn.h> int main(int argc, char** argv){ ros::init(argc, argv, "my_tf_listener"); ros::NodeHandle node; ros::service::waitForService("spawn"); ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("spawn"); turtlesim::Spawn srv; add_turtle.call(srv); ros::Publisher turtle_vel = node.advertise<geometry_msgs::Twist>("turtle2/cmd_vel", 10); tf::TransformListener listener; ros::Rate rate(10.0); while (node.ok()){ tf::StampedTransform transform; try{ listener.lookupTransform("/turtle2", "/turtle1", ros::Time(0), transform); } catch (tf::TransformException &ex) { ROS_ERROR("%s",ex.what()); ros::Duration(1.0).sleep(); continue; } geometry_msgs::Twist vel_msg; vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(), transform.getOrigin().x()); vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) + pow(transform.getOrigin().y(), 2)); turtle_vel.publish(vel_msg); rate.sleep(); } return 0; };
修改 CMakeLists.txt
gedit CMakeLists.txt
add_executable(turtle_tf_listener src/turtle_tf_listener.cpp) target_link_libraries(turtle_tf_listener ${catkin_LIBRARIES})
Build your package; at the top folder of your catkin workspace:
$ catkin_make
修改 launch文件start_demo.launch添加:
<launch> ... <node pkg="learning_tf" type="turtle_tf_listener" name="listener" /> </launch>
然后在运行:
$ roslaunch learning_tf start_demo.launch
就可以看到两只turtle了,也就是我们在最开始见到的那种跟随效果。
#include <ros/ros.h> #include <tf/transform_broadcaster.h> int main(int argc, char** argv){ ros::init(argc, argv, "my_tf_broadcaster"); ros::NodeHandle node; tf::TransformBroadcaster br; tf::Transform transform; ros::Rate rate(10.0); while (node.ok()){ transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) ); transform.setRotation( tf::Quaternion(0, 0, 0, 1) ); br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1")); rate.sleep(); } return 0; };
修改 CMakeLists.txt
gedit CMakeLists.txt
add_executable(frame_tf_broadcaster src/frame_tf_broadcaster.cpp)
target_link_libraries(frame_tf_broadcaster ${catkin_LIBRARIES})
Build your package; at the top folder of your catkin workspace:
$ catkin_make
修改 launch文件start_demo.launch添加:
<launch> ... <node pkg="learning_tf" type="frame_tf_broadcaster" name="broadcaster_frame" /> </launch>
然后在运行:
$ roslaunch learning_tf start_demo.launch
发现效果跟以前一样
Open the src/turtle_tf_listener.cpp file, and simple replace "/turtle1" with "/carrot1" in lines 26-27:
listener.lookupTransform("/turtle2", "/carrot1", ros::Time(0), transform);
重新编译
Build your package; at the top folder of your catkin workspace:
$ catkin_make
标签:
原文地址:http://www.cnblogs.com/CZM-/p/5879845.html