延時啟動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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM