Easiest way to implement HTTP server that can send ROS messages
I have a web interface that sends http requests to a server. When this server receives a request it needs to send messages through a ROS topic. Since this server will be very simple I thought that the easiest way would be to use Flask to make a REST service, but ROS doesn't seem to play nice with Flask. This is the code I am trying to execute:
#!/usr/bin/env python
## RESTful server that controls robot arm
import rospy
from std_msgs.msg import String
from flask import Flask, jsonify, request, abort
app = Flask(__name__)
topic='mytopic'
def talker():
rospy.loginfo('Starting talker')
pub = rospy.Publisher(topic, String, queue_size=10)
rospy.init_node('talker', anonymous=True)
rospy.loginfo('Talker started')
@App.route('/actions/execute', methods=['GET'])
def get_execute():
action = request.args.get('action')
if not action:
abort(400)
return action
if __name__ == '__main__':
talker()
rospy.loginfo("Starting Flask server")
app.run(debug=True)
If I comment out the line that calls talker() in main, the server starts correctly. If I execute it as shown however, I get the following:
[INFO] [WallTime: 1463411613.101553] Talker started
[INFO] [WallTime: 1463411613.102112] Starting Flask server
[INFO] [WallTime: 1463411613.576470] Talker started
[INFO] [WallTime: 1463411613.576941] Starting Flask server
And the Flask server never starts. I also can't kill the process with ctrl-c. Is there a way to make ROS work with Flask? And if there isn't, what is the easiest way to accomplish what I'm trying?
I am currently working on the same concept, however am also having to success. I am not super familiar with Flask, however it seems that both Flask and Ros require threads for their nodes. Have you had any success after this post?
@iarc_team_erau as far as I know there is no way to use ROS in a python server. Look into rosbridge/roslibjs which let you do the same (it's what I ended up using https://github.com/biellls/arm_contro... )