Ask Your Question
0

Get service message data

asked 2018-09-18 06:05:57 -0500

mneumann gravatar image

updated 2018-09-18 06:34:36 -0500

I have a service node server that handles a simple request by a client. The client sends an id (as a string), which the server then should read out. I am using rospy.

If I print the message in the server, it says "request: 1". I want to only extract the "1". How do I do that?

Rephrasing a bit: what "member" of the message do I have to call to get the raw data? Something like message.data.

Here's the handler:

def handle_cac(request):
    # receive goal id, return goal information
    print("Received this message: ")
    print request.data # this does not work! I'd like to just get the raw id, e.g. "1"
    for goals in root.getchildren():
        for goal in goals.getchildren():
            print goal.tag
            print goal.attrib
            if goal.attrib.text == request.data:
                print "Found goal: " + goal.name
                # print((root.findall("goals"))[0][0].attribute)
               return "nothing"

And here's the client:

def cac_client(request_message):
    print "Running request"
    rospy.wait_for_service('CAC')
    try:
        GetGoal = rospy.ServiceProxy('CAC', cac)
        resp1 = GetGoal(request_message)
        print resp1
        return resp1
    except rospy.ServiceException, e:
        print "Service call failed: %s" %e

if __name__ == "__main__":
    cac_client("1")
edit retag flag offensive close merge delete

Comments

what type of service are you using? What are you writing in your client? Could it be that you are actually writing request: 1 in it?

mgruhler gravatar imagemgruhler ( 2018-09-18 06:12:00 -0500 )edit

I added the client's code to the question.

mneumann gravatar imagemneumann ( 2018-09-18 06:15:21 -0500 )edit

What service type is that? I see cac, but I don't know what this is supposed to be. You can actually have a look at the cac definition yourself. This is the data member...

E.g. the AddTwoInts.srv of the beginner_tutorials has the members a and b.

mgruhler gravatar imagemgruhler ( 2018-09-18 06:45:13 -0500 )edit

Sorry, I was not sure what you meant. This is what it looks like: string request \--- string response. Ignore the \, didn't know how to escape the - in this comment. Also, the type seems to be "cacRequest", which is normal for services, right?

mneumann gravatar imagemneumann ( 2018-09-18 06:47:01 -0500 )edit

yes, basically from the srv definition you posted, two "messages" are created, which are called cacRequest and cacResponse, in this case...

mgruhler gravatar imagemgruhler ( 2018-09-18 07:06:35 -0500 )edit

How do I access the raw string I sent via these messages?

mneumann gravatar imagemneumann ( 2018-09-18 07:10:36 -0500 )edit
1

some patience, man ;-D

mgruhler gravatar imagemgruhler ( 2018-09-18 07:11:30 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-09-18 07:11:14 -0500

mgruhler gravatar image

Given your service definition,

string request
---
string response

you can access the data using request.request in your case.

I'd strongly encourage you to choose something else as the "request name" in your definition, as everything above the three dashes is actually the request, so if you add another field, this is also in the request, but is called something else. Ideally, it should be clear from the name what this is supposed to be. I'd change it to id in your case, then.

So :

string id
---
string whateveryourresponseactuallyis

and access using request.id.

In general, I'd try to avoid names in message/service/action definitions that appear often as the names in the callback signatures, such as msg, srv, request, response, data, etc., as this usually leads to quite some misunderstandings...

edit flag offensive delete link more

Comments

thanks! this is exactly what i meant!

mneumann gravatar imagemneumann ( 2018-09-18 07:14:29 -0500 )edit

happy to help!

mgruhler gravatar imagemgruhler ( 2018-09-18 07:24:33 -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: 2018-09-18 06:05:57 -0500

Seen: 177 times

Last updated: Sep 18 '18