Flask application dies after first POST request
I am using Flask application to accept user goals for my robot. The problem occurs when I call the service. Here is the code:
from flask import Flask, jsonify, request
from flask_pymongo import PyMongo
import json
import rospy
from path_caller import PathCaller
from tf import TransformListener
from geometry_msgs.msg import PoseStamped
from nav_msgs.srv import GetPlan
app = Flask(__name__)
#app.config['MONGO_DBNAME'] = 'prge'
app.config["MONGO_URI"] = "mongodb://user:pass@ds04505.mlab.com:29345/prge"
mongo = PyMongo(app)
@App.route('/goal', methods=['POST'])
def add_goal():
goal = mongo.db.goal
position = request.json['position']
orientation = request.json['orientation']
goal_id = goal.insert({'position' : position, 'orientation' : orientation})
new_goal = goal.find_one({'_id' : goal_id})
output = {'position' : new_goal['position'], 'orientation' : new_goal['orientation']}
position_x = json.loads(position['x'])
position_y = json.loads(position['y'])
#call_service(position_x, position_y)
return jsonify(output)
def create_pose_stamped(x,y,th_x,th_y):
pose = PoseStamped()
pose.header.stamp = rospy.Time.now()
pose.header.frame_id = "map"
pose.pose.position.x = x
pose.pose.position.y = y
pose.pose.position.z = 0.0
pose.pose.orientation.x = 0
pose.pose.orientation.y = 0
pose.pose.orientation.z = th_x
pose.pose.orientation.w = th_y
return pose
def get_start_position():
tf = TransformListener()
if tf.frameExists("base_footprint") and tf.frameExists("odom"):
print "Frames exist"
t = tf.getLatestCommonTime("/base_link", "/map")
p,q = tf.lookupTransform("/odom", "/base_footprint", t)
print p
x = 1
y = 5.9
z = 0
w = 1
return [x,y,z,w]
def call_service(x,y):
start_vars = get_start_position()
start = create_pose_stamped(start_vars[0], start_vars[1], start_vars[2], start_vars[3])
end = create_pose_stamped(x,y,0,1.0)
rospy.loginfo("The start x: %.2f, y: %.2f, the end x: %.2f, y: %.2f", start.pose.position.x, start.pose.position.y, end.pose.position.x, end.pose.position.y)
rospy.wait_for_service('/move_base/make_plan')
gp_service = rospy.ServiceProxy('/move_base/make_plan', GetPlan)
try:
response = gp_service(start, end, 1.0)
rospy.loginfo("The path is %s", response.plan.poses)
except rospy.ServiceException as e:
print "Service did not process request " + str(e)
if __name__ == '__main__':
rospy.init_node("path_planner")
app.run(debug=True)
After I POST a goal first time, everything works as expected. But when I POST the goal second time I get this error:
Exception happened during processing of request from ('127.0.0.1', 53198)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 596, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
self.handle()
File "/home/ikhsanov/.local/lib/python2.7/site-packages/werkzeug/serving.py", line 293, in handle
rv = BaseHTTPRequestHandler.handle(self)
File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/home/ikhsanov/.local/lib/python2.7/site-packages/werkzeug/serving.py", line 324, in handle_one_request
self.raw_requestline = self.rfile.readline()
IOError: [Errno 11] Resource temporarily unavailable
This is definately due to the service call as if I comment out the service call, everything works as expected. Also, when I call it for ...
When I have added time.sleep(1) right after the tf declaration, the problem has been solved. No idea why.
I don't know anything about flask I'm afraid. Although looking at your get_start_position function, it will return the default value (which is an invalid rotation quaternion) always since there is not return inside the if statement. Don't know if that's related.