Start launchfile from service
I'm looking into using the roslaunch
python API to start/stop a given launch file based on service calls. The roslaunch
API is fairly straight forward to work with, but I've run into a threading issue. When calling roslaunch.parent.ROSLaunchParent
from my Service Handler function, I get the following error:
Error processing request: signal only works in main thread
.
This makes sense, as the documentation clearly states that ROSLaunchParent
must be called from the main thread, while the service handler function is clearly running in its own thread.
So my question is: is there an easy way to switch context/execute the ROSLaunchParent
call in the main thread of the node hosting the service?
The node looks like this:
from rosbag_logging.srv import *
import rospy
import roslaunch
class LoggingNode:
def __init__(self):
self.launch = None
self.start_service = rospy.Service('rosbag_logging/start', StartLogging, self._srv_start_logging)
self.stop_service = rospy.Service('rosbag_logging/stop', StopLogging, self._srv_stop_logging)
self.status_service = rospy.Service('rosbag_logging/status', GetLoggingStatus, self._srv_status_logging)
def _srv_start_logging(self, req):
if self.launch == None:
uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)
self.launch = roslaunch.parent.ROSLaunchParent(uuid, ['rosbag_logging.launch'])
self.launch.start()
return EmptyResponse()
def _srv_stop_logging(self, req):
if self.launch != None:
self.launch.shutdown()
self.launch = None
return EmptyResponse()
def _srv_status_logging(self, req):
is_running = self.launch != None
return GetLoggingStatusResponse(is_running)
if __name__ == "__main__":
rospy.init_node('rosbag_logging')
logging_node = LoggingNode()
rospy.spin()
The ROS capabilities provides a set of services for discovering, starting and stopping launch files using ROS services. Maybe you can use that instead of writing your own version?
@follesoe Did you figure out a bespoke solution or a solution using capabilities?
I would like to know it too
Any developments on this topic? I face the same problem - I cannot launch a node from the ros server.