ros进二阶学习笔记-programmaticwaytostart/stoparoslaunch(代码片段)

Sonictl Sonictl     2022-12-09     709

关键词:

ROS进二阶学习笔记(3) - programmatic way to start/stop a roslaunch

Sometimes, we need to start/stop a ros .launch file/ ros node in a programmatic way, especially when we bring in the SMACH method to handle our applications.
Here are some threads in answers.ros.org web, and I summarised.

Just share what I've done. to be Generous. Specify this source if you need to repost/quote. thx~

First of first:

You have two requirments: Starting a node or a launch file:
-----------------------------------------------------------

To Start a Node:

   1. roslaunch api:
   http://wiki.ros.org/roslaunch/API%20Usage

==========================

To Start a Launch file:

=== 1. subprocess ===

  subprocess module may be the first choose to start/stop a roslaunch process in programmatic way. 
  The subprocess module allows you to spawn new processes, connect to theirinput/output/error pipes, and obtain their return codes. This module intends toreplace several older modules and functions:

os.system
os.spawn*
os.popen*
popen2.*
commands.*
  ref: https://docs.python.org/2/library/subprocess.html
         http://www.bogotobogo.com/python/python_subprocess_module.php
         http://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html (CN)
         http://hackerxu.com/2014/10/09/subprocess.html (CN)
 
----------------------

   rather than the `execfile` runs a python file, the `subprocess.call` execute roslaunch and give it either absolute path to


your launch file or two parameters where the first is the package of your launch file and the second is the name of the file.
(http://answers.ros.org/question/42849/how-to-launch-a-launch-file-from-python-code/)
   
   the subprocess module can also be used to terminated processes, as described in this stackoverflow question.
(https://stackoverflow.com/questions/12321077/killing-a-script-launched-in-a-process-via-os-system)

   User tfoote said:"pass the SIGINT to whatever subprocess handle you started it with in code."
   ref: http://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/

=== 2. os.system('roslaunch pkg *.launch') ===

   #process gets stucked when this line run
   You can pack a list of code into a process which can be launched by os.system
   You may need to usd subprocess to terminate it:
(https://stackoverflow.com/questions/12321077/killing-a-script-launched-in-a-process-via-os-system)


===*3. "roslaunch.parent.ROSLaunchParent" object ===

>>  import roslaunch
>>
>>  uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
>>  roslaunch.configure_logging(uuid)
>>  launch = roslaunch.parent.ROSLaunchParent(uuid, [file_path])
>>  launch.start()
>>  launch.shutdown()
(http://answers.ros.org/question/42849/how-to-launch-a-launch-file-from-python-code/)

=== 4. roslaunch.main ===

User lucasw said:
>>  import roslaunch;
>>  args = ['roslaunch', 'my_package', 'foo.launch'];
>>  roslaunch.main(args)

    - but it blocks and doesn't have the stop/is_alive methods.
ref: (http://answers.ros.org/question/12260/terminate-launch-other-node-from-code/)
   
also, in this thread:
User lucasw said:
Unless the scriptapi actually can run launch files, here is my (somewhat confusing) solution.
1-Create a node that uses scriptapi to start/stop a single node specified by params. (the scriptapi approach doesn't look like it can run launch files, just nodes- it is just a programmatic rosrun, not roslaunch?)

2-Create a node that uses roslaunch directly to start a param defined launch file, with a hack to get around the way roslaunch uses sys.argv. The main method blocks so doesn't have a nice start/stop/is_alive interface like scriptapi.
    >> sys.agv=['roslaunch', 'pkg_name', 'foo.launch', 'arg:=bar']
    >> import roslaunch
    >> roslaunch.main(args)
3-Start the second roslaunch raw node within the first scriptapi node, and then if the stop method is used it uses the scriptapi stop method which interrupts the roslaunch, kills all the child nodes as desired.
ref(http://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/)


==============================

Stop the launch file:


1. Python:

>> import os
>> os.system('roslaunch pkg launch.launch')  #The code get stuck(blocked) when this line launched.

   in the launch file make one node has the tag of the requied=true, so all the other nodes started by that launch file will

halt when that node is killed.
   e.g. <node pkg="rosbag" type="play" name="rosbagplay" args="test.bag" required="true"/>
---
   bash = rosnode kill rplidarNode
   Equivalent python code:
>> import os
>> os.system('rosnode kill rplidarNode')

   About the block problem, the user tanghz posted this:"remember to new a thread, or the code would block and you cannot stop the node." I do not understand the "new a thread". ref:(http://answers.ros.org/question/223090/start-or-stop-the-ros-node-in-another-node/)

2. Python/C++ :

   User tfoote said:"pass the SIGINT to whatever subprocess handle you started it with in code."
   ref: http://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/

----------------------------------------------------------

Some other ways that I didn't try to test or look into it:


X. C++:
   >> system("roslaunch rplidar_ros rplidar.launch");  //start
   >>
   >> system("rosnode kill rplidarNode");              //stop

   By the way, remember to new a thread, or the code would block and you cannot stop the node.
   ref: http://answers.ros.org/question/223090/start-or-stop-the-ros-node-in-another-node/

User supoerjax said:" use system("pkill roslaunch") in your node to kill the roslaunch from the system level."
ref:(http://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/)

XI. C++/Bash:
    ref:http://answers.ros.org/question/12260/terminate-launch-other-node-from-code/
    ref:

XII. `ros::kill(__pid_t __pid, int __sig)` and `getpid()` s
in that thread they also discussed about the SIGINT/Ctrl-C methods to stop the roslaunch
ref:(http://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/)


references

* http://answers.ros.org/question/10493/programmatic-way-to-stop-roslaunch/
* http://answers.ros.org/question/223090/start-or-stop-the-ros-node-in-another-node/
http://answers.ros.org/question/203154/is-it-possible-to-run-and-quit-launch-files-from-code-inside-a-node/
http://answers.ros.org/question/12260/terminate-launch-other-node-from-code/
http://answers.ros.org/question/42849/how-to-launch-a-launch-file-from-python-code/

ros进二阶学习笔记-programmaticwaytostart/stoparoslaunch(代码片段)

ROS进二阶学习笔记(3)-programmaticwaytostart/stoparoslaunchSometimes,weneedtostart/stoparos.launchfile/rosnodeinaprogrammaticway,especiallywhenwebringintheSMACHmethodtohandleourapplications.Here 查看详情

ros进二阶学习笔记--metapackage

ROS进阶学习笔记(24)--MetapackageMetapackage是ROSFileSystem概念层中的一个概念:2.CreateandConfigureaMetapackage:url:http://wiki.ros.org/catkin/package.xml#MetapackagesUsuallytheparentfolder,namedlikethemetapackageorjustthereponame,containsnopackage.xmlbutallpackage... 查看详情

ros进二阶学习笔记--rosbag

ROSBag是ROS计算图级的一个概念:Bags:ref:http://wiki.ros.org/Bags在计算图里在线使用  工具:rosbag  创建bags,收听topic,记录数据。可以回放或者remap到别的topic。  rosbag还能处理具有时间戳的数据,publish一个simula... 查看详情

ros进二阶学习笔记(10)--rospy.publisher()之queue_size

ROS进二阶学习笔记(10)--rospy.Publisher()之queue_sizeref link===============queue_size:publish()behaviorandqueuingpublish()inrospyissyn 查看详情

ros进二阶学习笔记-命名与命名空间

ref:http://wiki.ros.org/Names命名空间(wikipedia)ref:https://zh.wikipedia.org/wiki/命名空间命名空间(英语:Namespace),也称名字空间、名称空间等,它表示着一个标识符(identifier)的可 查看详情

ros进二阶学习笔记(10)--rospy.publisher()之queue_size

ROS进二阶学习笔记(10)--rospy.Publisher()之queue_sizeref link===============queue_size:publish()behaviorandqueuingpublish()inrospyissynchronousbydefault(forbackwardcompatibilityreasons)whichmeansthattheinvocationisbloc... 查看详情

ros进二阶学习笔记--关于overlay:重名package在不同catkinworkspace中,(代码片段)

要把ROS玩转,必须把catkin玩转。http://wiki.ros.org/catkin/Tutorials其中,Overlay问题是重名package在不同catkinworkspace中时,如何处理他们的关系。一个检查的命令:echo$ROS_PACKAGE_PATH可检查overlay用来设置path的命令们:$sou 查看详情

ros进二阶学习笔记--关于rospy和parameters(代码片段)

ref:http://wiki.ros.org/ParameterServer -- 总领阐述parameter的一些概念。比如namespacehttp://wiki.ros.org/rospy/Overview/ParameterServer -- 如何使用Python操作paramshttp://wiki.ros.org/rospy_tutorials/Tutorials/Parameters -- 如何使用Python操作paramshttp://wiki.ros.org/ros... 查看详情

ros进二阶学习笔记--关于rospy和parameters(代码片段)

ref:http://wiki.ros.org/ParameterServer -- 总领阐述parameter的一些概念。比如namespacehttp://wiki.ros.org/rospy/Overview/ParameterServer -- 如何使用Python操作paramshttp://wiki.ros.org/rospy_tutorials/Tutorials/Parameters -- 如何使用Python操作paramshttp://wiki.ros.org/ros... 查看详情

ros进二阶学习笔记--rosbag

ROSBag是ROS计算图级的一个概念:Bags:ref:http://wiki.ros.org/Bags在计算图里在线使用  工具:rosbag  创建bags,收听topic,记录数据。可以回放或者remap到别的topic。  rosbag还能处理具有时间戳的数据,publish一个simula... 查看详情

ros进二阶学习笔记--关于overlay:重名package在不同catkinworkspace中,(代码片段)

要把ROS玩转,必须把catkin玩转。http://wiki.ros.org/catkin/Tutorials其中,Overlay问题是重名package在不同catkinworkspace中时,如何处理他们的关系。一个检查的命令:echo$ROS_PACKAGE_PATH可检查overlay用来设置path的命令们:$sou... 查看详情

ros学习笔记02:ros基础

文章目录一、ROS架构一、ROS架构ROS架构分为三个层次:OS层⟹\\Longrightarrow⟹中间层⟹\\Longrightarrow⟹应用层 查看详情

ros学习笔记三(理解ros节点)

要求已经在Linux系统中安装一个学习用的ros软件包例子:sudoapt-getinstallros-indigo-ros-tutorialsROS图形概念概述nodes:节点,一个节点即为一个可执行文件,可以通过ROS和其他节点进行通信;messages:消息,当订阅或者发布一个topic时使... 查看详情

ros学习笔记——debug

NEW1$roscoreNEW2$rosrunrqt_consolerqt_console  $rosrunrqt_logger_levelrqt_logger_levelNEW3$rosrunturtlesimturtlesim_node 查看详情

ros学习笔记(十六)——初级教程学习结束

ROS系统查错的功能:NEW1$roscd$roswtf  #看起来很简单,但是具体怎么用?没搞懂.这部分的内容太少了...来个有用的指令roslocateuri<package_name> 查看详情

ros学习笔记——软件版本的选择

下面是Google的SLAM系统Cartographer对系统的要求:Cartographer对ROS版本要求: ROSIndigo对Ubantu的版本要求: 所以,综上所述:Ubantu版本:Trusty(14.04)ROS版本:ROSIndigoIgloo  PS:ROS网站:http://wiki.ros.org/   Cartog 查看详情

ros学习笔记

创建ros工作环境:mkdir-p~/catkin_ws/src//建立项目目录,同时生成src文件夹cd~/catkin_ws///进入项目目录catkin_make//编译项目,即使什么文件也没有也可以编译sourcedevel/setup.bash//执行编译生成的脚本文件,这会使当前项目目录加入环境变... 查看详情

ros学习笔记——rostopic

NEW1$roscoreNEW2$rosrunturtlesimturtlesim_nodeNEW3$rosrunturtlesimturtle_teleop_key NEW4$sudoapt-getinstallros-indigo-rqt  #安装rqt,这个貌似是一个节点管理器$sudosudoapt-getinstallros-indigo-rqt-common-plugins$ 查看详情