延时启动node或.launch的方法

>在某个catkin package下新建文件夹,并命名为“scripts”,并新建文件“/scripts/timed_roslaunch.sh”(名字不重要,这里只是示例)。并将如下内容复制粘贴到timed_roslaunch.sh文件中:

 1 function showHelp(){
 2     echo 
 3     echo "This script can delay the launch of a roslaunch file"
 4     echo "Place it in the 'scripts' folder of your catkin package"
 5     echo "and make sure that the file is executable (chmod +x timed_roslaunch.sh)"
 6     echo 
 7     echo "Run it from command line:"
 8     echo 
 9     echo "Use: ./timed_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]"
10     echo "Or: rosrun [yourpackage] time_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]"
11     echo "Example: ./timed_roslaunch.sh 2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=17.0"
12     echo 
13     echo "Or run it from another roslaunch file:"
14     echo 
15     echo '<launch>'
16     echo '  <arg name="initial_pose_y" default="17.0" />'
17     echo '  <node pkg="semantic_turtle_test" type="timed_roslaunch.sh"'
18     echo '    args="2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=$(arg initial_pose_y)"'
19     echo '    name="timed_roslaunch" output="screen">'
20     echo '  </node>'
21     echo '</launch>'
22 }
23 
24 if [ "$1" = "-h" ]; then    //如果第一个参数为“-h”,则显示帮助信息
25     showHelp
26 else 
27     echo "start wait for $1 seconds"
28     sleep $1               //延时时长,由执行.sh时给入的第一个参数指定
29     echo "end wait for $1 seconds"
30     shift
31         echo "now running 'roslaunch $@'"
32     roslaunch $@          //调用指定的roslaunch文件,参数由执行.sh时去除第一个给入参数后剩下的所有参数指定
33 fi

>新建完脚本文件timed_roslaunch.sh后要赋给.sh文件执行权限,方法:

1 [molmc-chy:~] $ cd SLAM-proj/src/path_planning/scripts/
2 [molmc-chy:~/SLAM-proj … scripts] master(+76/-1311)* ± chmod +x timed_roslaunch.sh

>.sh获取执行权限之后有以下几种执行方法:

This script can delay the launch of a roslaunch file
Place it in the 'scripts' folder of your catkin package
and make sure that the file is executable (chmod +x timed_roslaunch.sh)

Run it from command line:

Use: ./timed_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]   //直接执行.sh文件:cd 到../scripts/文件加下执行该指令,注意必须要有“./”符号才可执行
Or: rosrun [yourpackage] time_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]  //将.sh作为一个ROS节点执行
Example: ./timed_roslaunch.sh 2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=17.0

Or run it from another roslaunch file:  //因为.sh可以作为一个ros节点,所以可以将.sh文件在其他launch文件中启动,达到延时的目的

<launch>
<arg name="initial_pose_y" default="17.0" />
  <node pkg="semantic_turtle_test" type="timed_roslaunch.sh" 
    args="2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=$(arg initial_pose_y)"  //args中后四个参数是启动launch文件时需要的,即$roslaunch $@中“$@”所代表的部分
    name="timed_roslaunch" output="screen">
  </node>
</launch>

 >注1:这里给出的是一个延时启动.launch文件的示例,想延时启动node也很简单,只需做如下修改:

example:
>首先修改.launch文件,将启动另一个.launch文件修改为启动一个node:
<node pkg="semantic_turtle_test" type="timed_roslaunch.sh" args="10 [package_name] [node_name] [arguments(optional)]" name="timed_roslaunch" output="screen" />

>然后修改timed_roslaunch.sh文件中的"roslaunch $@" 为 “rosrun $@”

 >注2:在一个.launch文件中启动另外.launch文件,参考:

<launch>
<include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="world_name" value="$(find rrbot_gazebo)/worlds/rrbot.world"/> <arg name="debug" value="$(arg debug)" /> <arg name="gui" value="$(arg gui)" /> <arg name="paused" value="$(arg paused)"/> <arg name="use_sim_time" value="$(arg use_sim_time)"/> <arg name="headless" value="$(arg headless)"/> </include>
...
...
</launch>

 

posted on 2017-07-17 16:41  zhch_pan  阅读(4764)  评论(0编辑  收藏  举报

导航