How to start an HTTP server from within a ROS node?

asked 2015-08-19 03:41:21 -0500

I've written a ROS node that acts as an HTTP server, receiving control commands from an Android app. This way I can use my phone as a handy robot controller.

But I'm struggling with the two main loops waiting for the node and the server to be terminated.

This is how I start the server:

httpd = ThreadingHttpServer(("", port), url, ControlRequestHandler)
assassin = threading.Thread(target=httpd.shutdown)
assassin.daemon = True

Then I start the ROS node:

rospy.init_node('server')
powerPub = rospy.Publisher('motorPower', MotorPower, queue_size=1)

Finally, I ask the server to "run forever" - at least until it gets keyboard-interrupted or shut down via assassin.start() (will be called after a '/kill' request):

try:
    httpd.serve_forever()
except KeyboardInterrupt:
    pass
httpd.server_close()

Now, how can I handle rospy.is_shutdown() in this context? Both, the termination ofroscore as well as receiving a /kill request should be able to stop the node.

edit retag flag offensive close merge delete