0
$\begingroup$

Rosanswers logo

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")

Originally posted by mneumann on ROS Answers with karma: 13 on 2018-09-18

Post score: 0


Original comments

Comment by mgruhler on 2018-09-18:
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?

Comment by mneumann on 2018-09-18:
I added the client's code to the question.

Comment by mgruhler on 2018-09-18:
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.

Comment by mneumann on 2018-09-18:
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?

Comment by mgruhler on 2018-09-18:
yes, basically from the srv definition you posted, two "messages" are created, which are called cacRequest and cacResponse, in this case...

Comment by mneumann on 2018-09-18:
How do I access the raw string I sent via these messages?

Comment by mgruhler on 2018-09-18:
some patience, man ;-D

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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...


Originally posted by mgruhler with karma: 12390 on 2018-09-18

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by mneumann on 2018-09-18:
thanks! this is exactly what i meant!

Comment by mgruhler on 2018-09-18:
happy to help!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.