延时启动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>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
· 在外漂泊的这几年总结和感悟,展望未来
· 博客园 & 1Panel 联合终身会员上线
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· https证书一键自动续期,帮你解放90天限制
· 在 ASP.NET Core WebAPI如何实现版本控制?