ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I think a straightforward way is to use a Queue object that holds a boolean value. The service callback function can set or reset the bit in the queue and then the main thread can start or stop the launchfile using the roslaunch API. You could adapt the following pseudocode to your needs
from Queue import Queue
import rospy
from your_package.srv import StartPackage, StopPackage
def set_queue_item(queue):
queue.get()
queue.put(True)
def reset_queue_item(queue):
queue.get()
queue.put(False)
def read_from_queue(queue):
current_item = queue.get()
queue.put(current_item)
return current_item
def start_package(req, queue):
# callback method the StartPackage service
# the req parameter contains the message received from the client
set_queue_item(queue)
def stop_package(req, queue):
# callback method the StopPackage service
# the req parameter contains the message received from the client
reset_queue_item(queue)
if __name__ == '__main__':
queue = Queue(1) #create a queue of just 1 element
queue.put(False)
package_launched = False
rospy.init_node('your_node')
start_package_service = rospy.Service('start_package', StartPackage, lambda req: start_package(req, queue))
stop_package_service = rospy.Service('stop_package', StopPackage, lambda req: stop_package(req, queue))
while not rospy.is_shutdown():
if read_from_queue(queue) and not package_launched:
launch('yourfile.launch') # implement your custom launch method using the roslaunch API
package_launched = True
if not read_from_queue(queue) and package_launched:
stop('yourfile.launch') # implement your custom stop method using the roslaunch API
package_launched = False