Making a rosservice call (python)

asked 2021-03-02 03:25:00 -0600

ros_developer23 gravatar image

updated 2021-03-02 04:09:05 -0600

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
  val: 0
  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')
   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 :)

edit retag flag offensive close merge delete



this does not look like standard Python code. What are you using?

gvdhoorn gravatar image gvdhoorn  ( 2021-03-02 03:43:28 -0600 )edit

I am using flask because I am working on a web GUI

ros_developer23 gravatar image ros_developer23  ( 2021-03-02 04:07:36 -0600 )edit


Just makes sure a roscore is started and working in the background

ros_developer23 gravatar image ros_developer23  ( 2021-03-02 04:09:55 -0600 )edit

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?

tryan gravatar image tryan  ( 2021-03-02 10:17:53 -0600 )edit