ROS-小乌龟跟随

TF2 package:

tf2是tf的替代版,tf2是一个允许用户随时跟踪多个坐标帧的包。tf在时间缓冲的树结构中维护坐标帧之间的关系,并允许用户在任意两个坐标帧之间任意时间点转换点、向量等。典型应用就是:一只乌龟跟随着被控制的乌龟移动。
官方demo:

1. Set Up the Demo
$ sudo apt-get install ros-kinetic-ros-tutorials ros-kinetic-geometry-tutorials ros-kinetic-rviz ros-kinetic-rosbash ros-kinetic-rqt-tf-tree
2.Running the Demo
$ roslaunch turtle_tf turtle_tf_demo.launch

在这里插入图片描述

1 tf Tools

1.1 Using view_frames
view_frames通过ROS上的tf可以创建一个广播帧的diagram;
1.2 Using rqt_tf_tree
可视化基于ROS发布的信息框架,用法如下:

rosrun rqt_tf_tree rqt_tf_tree

Or simply:

rqt &

1.3 Using tf_echo
打印任何两个通过ROS通信的frame;

rosrun tf tf_echo [reference_frame] [target_frame]
2 键盘控制小乌龟demo

首先确保roscore已经运行, 打开一个新的终端:

$ roscore

在一个新的终端中运行:

$ rosrun turtlesim turtlesim_node

通过键盘远程控制turtle

$ rosrun turtlesim turtle_teleop_key

turtlesim_node 和 turtle_teleop_key节点之间通过一个ROS话题来相互通信。
turtle_teleop_key:发布按键输入消息
turtlesim_node:订阅该话题
使用rqt_graph显示当前运行的节点和话题(rqt_graph能够创建一个显示当前系统运行情况的动态图形)

rosrun rqt_graph rqt_graph

在这里插入图片描述
turtlesim_node和turtle_teleop_key节点正通过一个名为 /turtle1/cmd_vel的话题来互相通信。

2.1 rostopic介绍

rostopic命令工具能让你获取有关ROS话题的信息。可以使用帮助选项查看rostopic的子命令:

$ rostopic -h
	Commands:
		rostopic bw	display bandwidth used by topic
		rostopic delay	display delay of topic from timestamp in header
		rostopic echo	print messages to screen #显示在某个话题上发布的数据
		rostopic find	find topics by type
		rostopic hz	display publishing rate of topic    
		rostopic info	print information about active topic
		rostopic list	list active topics  #列出所有当前订阅和发布的话题
		rostopic pub	publish data to topic  #发布消息到某个给定的话题
		rostopic type	print topic or field type #查看所发布话题的消息类型
	
$ rostopic echo /turtle1/cmd_vel  #查看发布到该话题( /turtle1/cmd_vel)的数据
  • 刷新rqt_graph可以看到rostopic也订阅了 /turtle1/cmd_vel 话题:
    在这里插入图片描述
  • rostopic list 能够列出所有当前订阅和发布的话题:
rostopic list -h #查看帮助
rostopic list -v #显示所有发布和订阅的话题及其类型的详细信息
	Published topics:
	 * /turtle1/color_sensor [turtlesim/Color] 1 publisher
	 * /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher
	 * /rosout [rosgraph_msgs/Log] 4 publishers
	 * /rosout_agg [rosgraph_msgs/Log] 1 publisher
	 * /turtle1/pose [turtlesim/Pose] 1 publisher
	
	Subscribed topics:
	 * /turtle1/cmd_vel [geometry_msgs/Twist] 2 subscribers
	 * /rosout [rosgraph_msgs/Log] 1 subscriber
	 * /statistics [rosgraph_msgs/TopicStatistics] 1 subscriber
  • rostopic type [topic] 可以查看发布话题的消息类型
  • rosmsg show [topic] 可以查看消息的详细情况
    在这里插入图片描述
2.2 现在我们已经知道了turtlesim节点所期望的消息类型,接下来我们就可以给turtle发布命令了:
$ rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

以上命令会发送一条消息给turtlesim,告诉它以2.0大小的线速度和1.8大小的角速度开始移动。
-1: (单个破折号)这个参数选项使rostopic发布一条消息后马上退出;
/turtle1/cmd_vel : 消息所发布到的话题名称;
geometry_msgs/Twist : 所发布消息的类型;
– :(双破折号)这会告诉命令选项解析器接下来的参数部分都不是命令选项。这在参数里面包含有破折号-(比如负号)时是必须要添加的。
2.0是linear的值,1.8是angular的值。这些参数其实是按照YAML语法格式编写的。
在这里插入图片描述
执行完pub之后,turtle已经停止了。因为turtle需要一个稳定的频率为1Hz的命令流来保持移动状态。可以使用rostopic pub -r命令来发布一个稳定的命令流。

$ rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

这条命令以1Hz的频率发布速度命令到速度话题上。

  • rqt_plot命令可以实时显示一个发布到某个话题上的数据变化图形。这里我们将使用rqt_plot命令来绘制正在发布到/turtle1/pose话题上的数据变化图形。

Mavros
catkin_make
rosrun demo demo_node

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你关于ROS历程中乌龟跟随实验的问题。 在ROS中,乌龟跟随实验是一个很经典的例子。该实验使用了ROS中的turtlesim软件包和ROS的Python API进行编程。 在该实验中,我们需要让一只乌龟跟随另一只乌龟。具体步骤如下: 1. 首先,我们需要启动turtlesim节点。在终端中输入以下命令: ``` $ rosrun turtlesim turtlesim_node ``` 2. 接着,我们需要创建两只乌龟。在终端中输入以下命令: ``` $ rosrun turtlesim turtle_teleop_key ``` 然后,按下Ctrl+C,关闭该节点。 接着,输入以下命令: ``` $ rosrun turtlesim turtle_teleop_key turtle1 ``` 这样就创建了两只乌龟,一只是默认的turtle,另一只是命名为turtle1的乌龟。 3. 然后,我们需要编写一个Python脚本来实现乌龟跟随。在终端中输入以下命令: ``` $ cd ~/catkin_ws/src $ mkdir turtlesim_follow $ cd turtlesim_follow $ touch follow.py $ chmod +x follow.py ``` 然后,将以下代码复制粘贴到follow.py文件中: ``` #!/usr/bin/env python import rospy from geometry_msgs.msg import Twist from turtlesim.msg import Pose def pose_callback(pose): global x, y, theta x = pose.x y = pose.y theta = pose.theta rospy.init_node('turtle_follow') rospy.Subscriber('/turtle1/pose', Pose, pose_callback) pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) rate = rospy.Rate(10) while not rospy.is_shutdown(): vel_msg = Twist() vel_msg.linear.x = 1.5 * ((x_turtle - x) ** 2 + (y_turtle - y) ** 2) ** 0.5 vel_msg.angular.z = 4 * (math.atan2(y_turtle - y, x_turtle - x) - theta) pub.publish(vel_msg) rate.sleep() ``` 这段代码通过订阅/turtle1/pose主题来获取turtle1的位置信息,然后计算出turtle1和默认乌龟之间的距离和方向,最后发布/cmd_vel主题来控制默认乌龟的移动。 4. 最后,我们需要运行follow.py脚本。在终端中输入以下命令: ``` $ rosrun turtlesim_follow follow.py ``` 这样,你就可以看到默认乌龟开始跟随turtle1移动了。 希望这个简单的乌龟跟随实验能够帮助你更好地理解ROS的基本概念和编程方法。如果你还有其他问题,可以继续问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值