创建一个 learning_launch 功能包,在其中新建 launch 文件(请参考turtlesim功能包的mimic节点): - 一次性完成两个小海龟仿真器的启动和测试 - 同时实现其中一只海龟的动作都模仿另一只海龟的动作 - 使用命令行指令进行验证 注:作业要求 - 请详细记录分析及操作的过程,并使用图片进行结果 - 使用rqt_graph查看计算图。请用ROS实现以上要求,给出具体实现代码
时间: 2024-01-24 14:19:04 浏览: 242
Java打包工具Launch4j.zip
创建功能包和launch文件
```
cd ~/catkin_ws/src
catkin_create_pkg learning_launch rospy
cd learning_launch
mkdir launch
touch launch/turtlesim.launch
```
编辑turtlesim.launch文件
```
<launch>
<node pkg="turtlesim" type="turtlesim_node" name="turtle1"/>
<node pkg="turtlesim" type="turtlesim_node" name="turtle2"/>
</launch>
```
启动launch文件
```
roslaunch learning_launch turtlesim.launch
```
启动结果如下图所示:
![turtlesim-launch](https://img-blog.csdnimg.cn/20211202144334334.png)
创建节点文件
```
cd ~/catkin_ws/src/learning_launch
touch mimic.py
chmod +x mimic.py
```
编辑mimic.py文件
```python
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
class Mimic:
def __init__(self):
rospy.init_node('mimic', anonymous=True)
self.rate = rospy.Rate(10)
self.turtle1_cmd_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
self.turtle2_cmd_vel_sub = rospy.Subscriber('/turtle2/cmd_vel', Twist, self.callback)
def callback(self, data):
self.turtle1_cmd_vel_pub.publish(data)
def run(self):
while not rospy.is_shutdown():
self.rate.sleep()
if __name__ == '__main__':
try:
mimic = Mimic()
mimic.run()
except rospy.ROSInterruptException:
pass
```
启动节点文件
```
rosrun learning_launch mimic.py
```
验证节点文件的功能
```
rosrun turtlesim turtle_teleop_key
```
使用键盘控制turtle2移动,turtle1会自动模仿turtle2的运动。
验证结果如下图所示:
![turtlesim-mimic](https://img-blog.csdnimg.cn/20211202144334385.png)
使用rqt_graph查看计算图
```
rosrun rqt_graph rqt_graph
```
计算图如下图所示:
![turtlesim-graph](https://img-blog.csdnimg.cn/20211202144334431.png)
完整代码如下:
turtlesim.launch
```xml
<launch>
<node pkg="turtlesim" type="turtlesim_node" name="turtle1"/>
<node pkg="turtlesim" type="turtlesim_node" name="turtle2"/>
</launch>
```
mimic.py
```python
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
class Mimic:
def __init__(self):
rospy.init_node('mimic', anonymous=True)
self.rate = rospy.Rate(10)
self.turtle1_cmd_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
self.turtle2_cmd_vel_sub = rospy.Subscriber('/turtle2/cmd_vel', Twist, self.callback)
def callback(self, data):
self.turtle1_cmd_vel_pub.publish(data)
def run(self):
while not rospy.is_shutdown():
self.rate.sleep()
if __name__ == '__main__':
try:
mimic = Mimic()
mimic.run()
except rospy.ROSInterruptException:
pass
```
阅读全文