Robotics StackExchange | Archived questions

How to get new messages while executing a service request?

I have written a class called "Driver" which advertises a service. Basically, this service will accept a point (x,y) and return a boolean. It rotates the turtlebot towards the point, and then drives until it reaches that point. The boolean it returns indicates whether or not the robot successfully reached that point.

The design of the service is relatively simple. It periodically sends velocity commands to the robot, as required.

The only time it will return false is when one of the robot bumpers is activated, which can be detected via messages.

My question is: what is the correct design for detecting this bumper change while executing the service request?

My way of doing this is to make another callback inside of the Driver class which specifically handles the bumper message. Then, I would call ros::spinOnce() inside my velocity command loop so that I can detect the message incoming. The bumper callback would set some state in the Driver class, which would be detected by my service request.

I think this would work, but is there a better way?

I was also curious what would happen if multiple service requests were sent to my node. Wouldn't the ros::spinOnce() inside my loop detect the second request and start executing it? This would screw everything up because I can't be handling two services at once. Or perhaps I misunderstand the ROS API behavior in that case.

Thank you for your help.

Asked by Sebastian on 2015-09-30 12:21:40 UTC

Comments

Answers

I implemented it like I said and no problems yet.

Asked by Sebastian on 2015-09-30 18:49:34 UTC

Comments

Calling spinOnce() from within a callback is really not recommended. For long running actions that need to be interruptible, want to change behaviour based on input or provide feedback while running, please use actions.

Asked by gvdhoorn on 2015-10-01 13:51:24 UTC

Also: for the general case of handling more than a single callback at the same time, look at multithreaded and asynchronous spinners.

Asked by gvdhoorn on 2015-10-01 13:52:33 UTC