Ask Your Question
0

send and receive service message using rosbridge

asked 2014-05-22 20:49:35 -0500

ROSCMBOT gravatar image

updated 2014-05-22 21:04:16 -0500

Hello,

I'm using rosbridge 2.0 to connect to a remote computer and call a ROS service on the remote machine. I'm using the sample client below (from the the rosbride website) to make the connection and call the service:

  from json import dumps
  from ws4py.client.threadedclient import WebSocketClient

  class GetLoggersClient(WebSocketClient):

       def get_loggers(self):
           msg = {'op': 'call_service', 'service': '/rosout/get_loggers'}
           self.send(dumps(msg))

       def opened(self):
           print "Connection opened..."
           self.get_loggers()

       def closed(self, code, reason=None):
           print code, reason

       def received_message(self, m):
           print "Received:", m

  if __name__=="__main__":
       try:
           ws = GetLoggersClient('ws://127.0.0.1:9090/')
           ws.connect()
       except KeyboardInterrupt:
           ws.close()

My question is how I can receive the output message of the service call. The output message is printed in the console by received_message(self,m), but I intend to assign this output to a variable.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2014-05-22 22:10:51 -0500

alex.filgueira gravatar image

updated 2014-05-22 23:41:34 -0500

m is your message. I search in m (string) the interesting data. Example, search "level":

def received_message(self, m):
    #print type(m)
     print("recived => %s \n" %str(m))
     difference_topic(m)

def difference_topic(m):
  pos_level = m.find('level')

or

  if (m[n] == 'l'):
     if (m[n+1] == 'e):
           ....
edit flag offensive delete link more

Comments

but m is not a global variable. How can I get the output message in other methods other than received_message(). In particular, I'd like to receive the output in the method I'm calling the service (for example inside get_loggers() in the client above). Shouldn't there be a receive() or recv() function to get the output (similar to send() )?

ROSCMBOT gravatar image ROSCMBOT  ( 2014-05-22 22:41:45 -0500 )edit

I edited my answer. ;-)

alex.filgueira gravatar image alex.filgueira  ( 2014-05-22 23:42:42 -0500 )edit

Thanks Alex, but is there a more elegant way to receive the message in another method? If I have multiple methods, I don't want to call them all in the received_message method. and I don't want to add another parameter (m) to all the methods as your example suggests

ROSCMBOT gravatar image ROSCMBOT  ( 2014-05-27 07:48:02 -0500 )edit

Also your suggested way is not going to work for methods having multiple arguments. methods similar to closed(self, code, reason=None). You can't give a value to 'code' when calling the method from within received_message()

ROSCMBOT gravatar image ROSCMBOT  ( 2014-05-27 07:58:13 -0500 )edit

I use "difference_topic(m)" with a argument, after I tell to other functions. I´m sure that others methods are better, but this is enough for me in this moment. ;-)

alex.filgueira gravatar image alex.filgueira  ( 2014-05-27 22:07:13 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-05-22 20:49:35 -0500

Seen: 1,356 times

Last updated: May 22 '14