ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Requesting different services with the same client node

asked 2012-09-20 16:59:57 -0600

Nishant gravatar image

updated 2012-09-20 17:07:59 -0600

Hi all!

So I have a ROS client like thus

ros::ServiceClient client

I also have two services:

1) "start_load_recording": This service basically tells a sensor to start logging force data into a text file. The type of message conveyed over this service is 'startMsg'

2) "stop_load_recording": This service tells that very same sensor to stop logging the force data, and close down the text file. The type of message conveyed over this service is 'stopMsg'

The thing is, I want to have a client which can request both these services. Thus I am using the client as a sort of 'on-off switch' for logging and un-logging the force data.

Can I do that? For example,

client = n.serviceClient<cell_node::startMsg>("start_load_recording");
//prepare my service message request variables. Lets say its called strt
client.call(strt);
.
.
.
//few lines of code
.
.
.
client = n.serviceClient<cell_node::stopMsg>("stop_load_recording");
//prepare my service message request variables. Lets say its called stop
client.call(stop);

Can I do the above?

Thanks and all help is greatly appreciated!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2012-09-20 22:23:14 -0600

Lorenz gravatar image

Sure. I don't see any problem with your code there. It's just important that the service implementation in the server does not block.

Btw. consider using ROS actionlib instead of services. You could send an action goal and cancel it in the client when you want to stop recording. This avoids synchronization problems when you have, for instance, two clients starting and stopping recording at different points in time. Have a look here for a discussion on when to use services.

edit flag offensive delete link more

Comments

Thanks for the heads up on the actionlib library! I went through it, and I am almost sure that the next version of my code will get rid of all services and have ActionServers and ActionClients. :)

Nishant gravatar image Nishant  ( 2012-09-20 22:36:02 -0600 )edit

I had a small question though. When you say the service implementation in the server does not block, what do you mean? Does that mean that the service server gets bound to the client in some way? I thought client-service communication was a quick one time request-response.

Nishant gravatar image Nishant  ( 2012-09-20 22:37:53 -0600 )edit

The call in the client blocks as long as the server service callback function is processing and returns only when it returned. That means the server should be quick. For recording, that means you have to have a separate recording thread that is triggered by the start call.

Lorenz gravatar image Lorenz  ( 2012-09-20 22:40:02 -0600 )edit

Question Tools

Stats

Asked: 2012-09-20 16:59:57 -0600

Seen: 450 times

Last updated: Sep 20 '12