完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
创建功能包
cd ~/catkin_workspace/src catkin_create_pkg learning_tf roscpp rospy tf turtlesim 如何实现一个tf广播器 定义TF广播器(TransformBroadcaster) 创建坐标变换值; 发布坐标变换(sendTransform) 创建tf广播器代码(C++) nano ~/catkin_workspace/src/learning_tf/src/turtle_tf_broadcaster.cpp /*********************************************************************** Copyright 2020 GuYueHome (www.guyuehome.com). ***********************************************************************/ /** * 该例程产生tf数据,并计算、发布turtle2的速度指令 */ #include #include #include std::string turtle_name; void poseCallback(const turtlesim::PoseConstPtr& msg) { // 创建tf的广播器 static tf::TransformBroadcaster br; // 初始化tf数据 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); // 广播world与海龟坐标系之间的tf数据 br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", turtle_name)); } int main(int argc, char** argv) { // 初始化ROS节点 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; }; 配置编译规则 nano ~/catkin_workspace/src/learning_tf/CMakeLists.txt add_executable(turtle_tf_broadcaster src/turtle_tf_broadcaster.cpp) target_link_libraries(turtle_tf_broadcaster ${catkin_LIBRARIES}) add_executable(turtle_tf_listener src/turtle_tf_listener.cpp) target_link_libraries(turtle_tf_listener ${catkin_LIBRARIES}) 编译并运行 cd ~/catkin_workspace catkin_make # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtlesim_node # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_tf turtle_tf_broadcaster __name:=turtle1_tf_broadcaster /turtle1 # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_tf turtle_tf_broadcaster __name:=turtle2_tf_broadcaster /turtle2 # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_tf turtle_tf_listener # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtle_teleop_key # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rqt_graph python 编写程序 cd ~/catkin_workspace/src/learning_tf mkdir scripts 1 2 turtle_tf_broadcaster.py nano ~/catkin_workspace/src/learning_tf/scripts/turtle_tf_broadcaster.py 1 #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将请求/show_person服务,服务数据类型learning_service::Person import roslib roslib.load_manifest('learning_tf') import rospy import tf import turtlesim.msg def handle_turtle_pose(msg, turtlename): br = tf.TransformBroadcaster() br.sendTransform((msg.x, msg.y, 0), tf.transformations.quaternion_from_euler(0, 0, msg.theta), rospy.Time.now(), turtlename, "world") if __name__ == '__main__': rospy.init_node('turtle_tf_broadcaster') turtlename = rospy.get_param('~turtle') rospy.Subscriber('/%s/pose' % turtlename, turtlesim.msg.Pose, handle_turtle_pose, turtlename) rospy.spin() turtle_tf_listener.py nano ~/catkin_workspace/src/learning_tf/scripts/turtle_tf_listener.py #!/usr/bin/env python # -*- coding: utf-8 -*- ######################################################################## #### Copyright 2020 GuYueHome (www.guyuehome.com). ### ######################################################################## # 该例程将请求/show_person服务,服务数据类型learning_service::Person import roslib roslib.load_manifest('learning_tf') import rospy import math import tf import geometry_msgs.msg import turtlesim.srv if __name__ == '__main__': rospy.init_node('turtle_tf_listener') listener = tf.TransformListener() rospy.wait_for_service('spawn') spawner = rospy.ServiceProxy('spawn', turtlesim.srv.Spawn) spawner(4, 2, 0, 'turtle2') turtle_vel = rospy.Publisher('turtle2/cmd_vel', geometry_msgs.msg.Twist,queue_size=1) rate = rospy.Rate(10.0) while not rospy.is_shutdown(): try: (trans,rot) = listener.lookupTransform('/turtle2', '/turtle1', rospy.Time(0)) except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException): continue angular = 4 * math.atan2(trans[1], trans[0]) linear = 0.5 * math.sqrt(trans[0] ** 2 + trans[1] ** 2) cmd = geometry_msgs.msg.Twist() cmd.linear.x = linear cmd.angular.z = angular turtle_vel.publish(cmd) rate.sleep() 运行 sudo chmod +x ~/catkin_workspace/src/learning_tf/scripts/turtle_tf_broadcaster.py sudo chmod +x ~/catkin_workspace/src/learning_tf/scripts/turtle_tf_listener.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash roscore # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtlesim_node # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_tf turtle_tf_broadcaster.py __name:=turtle1_tf_broadcaster _turtle:=turtle1 # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_tf turtle_tf_broadcaster.py __name:=turtle2_tf_broadcaster _turtle:=turtle2 # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun learning_tf turtle_tf_listener.py # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rosrun turtlesim turtle_teleop_key # 新建一个终端 source ~/catkin_workspace/devel/setup.bash rqt_graph |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
431 浏览 0 评论
524 浏览 1 评论
1789 浏览 1 评论
3043 浏览 1 评论
synopsys 的design ware:DW_fpv_div,浮点数除法器,默认32位下,想提升覆盖率(TMAX),如果用功能case去提升覆盖率呢?
3771 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-19 10:49 , Processed in 0.916476 second(s), Total 70, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号