ROS2与launch入门教程-创建ROS2的launch(启动)文件
ROS2与launch入门教程-创建ROS2的launch(启动)文件
说明:
- 介绍创建launch文件以运行复杂的ROS2系统
- 测试环境: ubuntu 20.04 galactic
- 创建一个launch文件来启动两个小海龟
准备:
- 更新软件源
sudo apt update
- 安装turtlesim
sudo apt install ros-galactic-turtlesim
- 安装rqt
sudo apt install ~nros-galactic-rqt*
独立launch文件测试:
- 创建luanch目录
mkdir -p ~/launch_ws
- 新建launch文件turtlesim_mimic.launch.py
cd ~/launch_ws
vim turtlesim_mimic.launch.py
- 内容如下
#引用launch相关包
from launch import LaunchDescription
from launch_ros.actions import Node
#定义launch描述
def generate_launch_description():
return LaunchDescription([
#调用节点turtlesim_node,生成第一个小海龟
Node(
package='turtlesim',
namespace='turtlesim1',
executable='turtlesim_node',
name='sim'
),
#调用节点turtlesim_node,生成第二个小海龟
Node(
package='turtlesim',
namespace='turtlesim2',
executable='turtlesim_node',
name='sim'
),
#调用节点mimic,实现跟随
Node(
package='turtlesim',
executable='mimic',
name='mimic',
remappings=[
('/input/pose', '/turtlesim1/turtle1/pose'),
('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
]
)
])
- mimic的/input/pose话题重映射到/turtlesim1/turtle1/pose
- /output/cmd_vel的话题/turtlesim2/turtle1/cmd_vel
- 根据海龟1的位置,评估海龟2的速度,并发布给海龟2执行,从而实现海龟2跟随海龟1走同样的路径
- 启动launch文件
cd ~/launch_ws
ros2 launch turtlesim_mimic.launch.py
- 控制海龟1运行
ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"
- 效果图:
- 查看节点
rqt_graph
- 效果图:
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号