使用命令行输入代码需要不断打开终端比较繁琐,而且容易输入错误,那么有没有什么方法可以快速启动所需节点呢?
Launch文件:通过XML文件实现多节点的配置和启动(可自启动ROS Master)
命令行语法:
roslaunch 功能包名 launch文件名
< launch > | launch文件中的根元素采用< launch >标签定义 |
---|---|
< node > | 启动节点 |
注:launch文件以< launch >开始,结束
pkg | 节点所在的功能包名称 |
---|---|
type | 节点的可执行文件名称(.cpp文件或.py文件) |
name | 节点运行时的名称:可以取代type中的节点名(即覆盖掉ros::init()定义的节点名) |
output | output=“screen” 终端输出转储在当前的控制台上,而不是在日志文件中 |
respawn | 是否要重启 |
required | 控制某个节点是否一定要启动 |
ns | 设置命名空间(避免命名冲突) |
args | 给节点输入参数 |
其中,ns是命名空间(name space)
cd ~/catkin_ws/src
catkin_create_pkg learning_launch
cd learning_launch
mkdir launch
注:launch功能包不需要依赖包
cd ~/catkin_ws/src/learning_launch/launch
touch simple.launch
gedit simple.launch
注:启动的节点所在功能包和可执行文件(.cpp文件)的创建请参考:ROS学习(四)–1.发布者与订阅者案例
编译:
cd ~/catkin_ws
catkin_make
运行:
source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch simple.launch
cd ~/catkin_ws/src/learning_launch/launch
touch turtlesim_parameter_config.launch
gedit turtlesim_parameter_config.launch
注:1.find learning_launch:自动寻找learning_launch路径
编译:
cd ~/catkin_ws
catkin_make
注:运行前需要先添加config/param.yaml文件
cd ~/catkin_ws/src/learning_launch
mkdir config
cd config
touch param.yaml
gedit param.yaml
A: 123
B: "hello"
group:
C: 456
D: "hello"
运行:
source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch turtlesim_parameter_config.launch
由图可知,launch文件启动海龟仿真器和海龟移动控制器成功。同时launch文件参数设置和加载param.yaml文件参数成功
cd ~/catkin_ws/src/learning_launch/launch
touch start_tf_demo_c++.launch
gedit start_tf_demo_c++.launch
注:启动的节点所在功能包和可执行文件(.cpp文件)的创建请参考:ROS学习(七)坐标系管理系统
编译:
cd ~/catkin_ws
catkin_make
运行:
source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch start_tf_demo_c++.launch
cd ~/catkin_ws/src/learning_launch/launch
touch turtlesim_remap.launch
gedit turtlesim_remap.launch
编译:
cd ~/catkin_ws
catkin_make
运行:
source ~/catkin_ws/devel/setup.bash
roslaunch learning_launch turtlesim_remap.launch
由图可知,simple.launch文件包含成功,同时remap重命名成功,成功将"/turtle1/cmd_vel"重命名为"/cmd_vel" ,向"/cmd_vel" 发布话题,海龟成功移动,说明重命名成功