在ROS工程中经常需要启动好几个launch文件,比较不方便,有下面两种方法可以更高效些:
- 重写一个大型的launch文件,将所有的节点的启动配置信息都包含进去。
- 通过bash写一个xxx.sh文件,将命令行一起写入一个脚本。
launch文件中则如下进行设置:
pkg
对应文件的包名。
type
是CMakeList.txt中对应该文件add_executable(pcan_test src/pcan_test)
中可执行文件的名称,在python中则是文件名,因为Python的可执行文件就是文件本身(解释性语言,同Matlab),所以若用C++编程不要误写为文件名全称。
name
表示节点启动后的名称,该名称会覆盖ros::init
中初始化的名称。
output
后参数表示从屏幕输出打印信息,否则打印信息会存储到某个临时文件里。
1 2 3 4 5 6 7
| <launch> <node pkg="uav_dl" name="position_control" type="position_control.py" output="screen" /> <node pkg="uav_dl" name="action_control" type="action_control.py" output="screen" /> <node pkg="uav_dl" name="goto_position_server" type="goto_position_server.py" output="screen" /> <node pkg="uav_dl" name="detect_object_server" type="detect_object_server.py" output="screen" /> <node pkg="uav_dl" name="tensorflow_detection" type="tensorflow_detection.py" output="screen" /> </launch>
|
注:只需要在src下建立launch文件夹,然后在其中创建launch文件即可,不需要做其他工作。
参数里name
是ros::param::get()
中第一个字符串去掉“~
”后的名称,launch会在运行时进行查找匹配,type
是变量类型,value
是具体值。以下launch文件(包含私有变量和公有变量)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <launch> <arg name="fcu_url" default="serial:///dev/ttyACM0:921600" /> <arg name="gcs_url" default="udp://:14556@192.168.150.2:14550" /> <arg name="tgt_system" default="1" /> <arg name="tgt_component" default="50" /> <node name="mavros" pkg="mavros" type="mavros_node" output="screen"> <param name="fcu_url" value="$(arg fcu_url)" /> <param name="gcs_url" value="$(arg gcs_url)" /> <param name="target_system_id" value="$(arg tgt_system)" /> <param name="target_component_id" value="$(arg tgt_component)" /> <rosparam command="load" file="$(find mavros)/launch/px4_blacklist.yaml" /> <param name="conn_heartbeat" value="5.0" /> <param name="conn_timeout" value="5.0" /> <param name="startup_px4_usb_quirk" value="true" /> </node> <node name="camera" pkg="usb_cam" type="usb_cam_node"> <param name="video_device" value="/dev/video0" /> <param name="image_width" value="800" /> <param name="image_height" value="600" /> <param name="pixel_format" value="mjpeg" /> <param name="framerate" value="30" /> <param name="camera_frame_id" value="webcam" /> </node> <node name="viewer" pkg="image_view" type="image_view"> <remap from="image" to="/camera/image_raw" /> </node> </launch>
|
在ubuntu下进行节点启动顺序控制的简单策略就是通过shell实现
新建文件后命名为xxx.sh
1 2 3 4 5 6 7 8 9
| #!/bin/bash roslaunch bhand_controller bhand_controller.launch & sleep 5 echo "bhand controller starting success!" roslaunch beginner_tutorials bhand_force_control.launch & sleep 0.1 wait exit 0
|
代码解释:第一行表示用bash执行,sleep
表示演示,echo
用来输出一定内容,注意不要忘记句子后的”&
“符号。注:若ROS的关键词不能在终端识别,需先source下ROS环境。
节点启动顺序控制策略就是如果某个节点必须先执行,可以单独为其写一个launch文件,然后通过shell控制先行启动。
编写保存后,在终端要给xxx.sh执行权限,sudo chmod a+x xxx.sh
,之后可通过./xxx.sh
进行启动,xxx代表任意字符。有关Ubuntu shell的其他操作,可以自行查询相关资料。
Debugs
1 2 3 4
| # Bug: ERROR: cannot launch node of type [ros_tensorflow/imgTalker.py]: can't locate node [imgTalker.py] in package [ros_tensorflow] # Debug: 修改为可执行 sudo chmod +x imgTalker.py
|