博客参考:https://www.ncnynl.com/archives/201702/1312.html
说明:
为何增加坐标系
那里增加坐标系
如何添加坐标系
$ roscd learning_tf
$ touch src/frame_tf_broadcaster.cpp
$ vim src/frame_tf_broadcaster.cpp
#include
#include
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;
};
代码解释:
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")
编译运行
$ rosed learning_tf CMakeLists.txt
add_executable(frame_tf_broadcaster src/frame_tf_broadcaster.cpp)
target_link_libraries(frame_tf_broadcaster ${catkin_LIBRARIES})
$ cd ~/catkin_ws
$ catkin_make
编译顺利,在bin目录会生成frame_tf_broadcaster文件
编辑start_demo.launch
$ rosed learning_tf start_demo.launch
增加新代码:
运行:
$ roslaunch learning_tf start_demo.launch
检查结果:
接着上个教程的,如果你驱动turtle1,你注意到,即使我们添加了一个新的坐标系,行为没有改变。 这是因为添加额外的坐标系不会影响其他坐标系,我们的监听器仍然使用先前定义的坐标系。 所以,让我们改变监听器的行为。
编辑src/turtle_tf_listener.cpp,在26-27行用/carrot1替换/turtle1,如:
listener.lookupTransform("/turtle2", "/carrot1",
ros::Time(0), transform);
编译运行:
$ catkin_make
$ roslaunch learning_tf start_demo.launch
应该可以看到,turtle2跟随carrot1变化,而不是turtle1
广播移动的坐标系
$ vim src/frame_tf_broadcaster.cpp
transform.setOrigin( tf::Vector3(2.0*sin(ros::Time::now().toSec()), 2.0*cos(ros::Time::now().toSec()), 0.0) );
transform.setRotation( tf::Quaternion(0, 0, 0, 1) );
$ catkin_make
$ roslaunch learning_tf start_demo.launch
手机扫一扫
移动阅读更方便
你可能感兴趣的文章