Making a rosservice call (python)
Hi, I am pretty new to ROS and already had a look on how to write a Server and Client for a Service. Unfortunately, I couldn't find a solution for my problem. Additionally to ROS and Python, I am using Flask to program a web GUI.
Via the console, I can use:
rosservice call /set_vacuum_cmd "vacuum1:
val: 0
vacuum2:
val: 0
vacuum3:
val: 0"
to turn on vacuum grippers of my robot. I am trying to achieve the same with python. I tried to write a server like this:
def turn_on_vacuum_callback(self, vac_id):
print "I am inside callback-function (RUMR) XXX!"
if vac_id == 1:
self.vacuum1.val = 1
self.vacuum1.val = 0
self.vacuum1.val = 0
elif vac_id == 2:
self.vacuum1.val = 0
self.vacuum1.val = 1
self.vacuum1.val = 0
elif vac_id == 3:
self.vacuum1.val = 0
self.vacuum1.val = 0
self.vacuum1.val = 1
elif vac_id == 4:
self.vacuum1.val = 1
self.vacuum1.val = 1
self.vacuum1.val = 1
@api_vacuum.route('/turn_on_vacuum', methods=['POST'])
class TurnOnVacuum(Resource):
@api_vacuum.doc('turn on individual or all vacuum suction cups')
@roscore_required
def post(self):
vac_id = request.get_json(force=True)[0]
srv = rospy.Service('/set_vacuum_cmd', VacuumCmd, turn_on_vacuum_callback(self, vac_id))
I am well aware, that "self" is wrong in the callback. But I don't know what else I could use.
VacuumCmd.srv looks like this:
enums/VacuumStates vacuum1
enums/VacuumStates vacuum2
enums/VacuumStates vacuum3
---
and VacuumStates.msg:
uint8 OFF = 0
uint8 VACUUM_ON = 1
uint8 BLOW = 2
uint8 NONE = 3
uint8 val
I hope someone could help me or at least give me a hint. Thank you all in advance :)
this does not look like standard Python code. What are you using?
I am using flask because I am working on a web GUI
@roscore_required
Just makes sure a roscore is started and working in the background
It sounds like there's already a working service (you can do what you want from the command line), so to access it you need to use a client (
ServiceProxy
) in your Python code. Does that make sense, or am I misunderstanding?