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

How to use data from a topic inside action server?

asked 2013-06-22 09:49:34 -0500

Fei Liu gravatar image

Hi,

I'm using action server following the tutorial. In my application, I want to implement the action as run the motor and stop when the torque is greater than some value or the limit the reached. So I need to check the torque and limit switch frequently (published as two topics \torque, \limit) while the action is executing. I checked the tutorial http://www.ros.org/wiki/actionlib_tutorials/Tutorials/SimpleActionServer%28GoalCallbackMethod%29 (SimpleActionServer(GoalCallbackMethod)), but this tutorial only have one topic to subscribe and that's why it can use the topic callback function to implement the function of the action. A more general question would be how can I use topic data inside action server. Is there any example I could check? Thanks for any hint.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2013-06-24 04:51:40 -0500

thebyohazard gravatar image

updated 2013-06-24 10:43:52 -0500

The goal callback method is probably still the skeleton of what you want. I'd make the following changes.

  • Instead of spin() ing, use a spinOnce() inside of a loop.
  • Instead of doing the computation inside the topic callbacks, just make each callback store the message/data you get. So you'd have class members of, say, current_torque and limit_reached. These would get updated every time spinOnce is called and there's a message available.
  • Then, do your computation in the main loop. You can use both current_torque and limit_reached since they're class members. Be careful about initialization, though.

EDIT with additional questions:

Q1. Do you mean the spin() in the main function?

A1: Yes. Have a look at this page.

Q2. You mention about the main loop, is the main loop inside the main function or the callback function in the action server class?

A2: I meant inside the main function. That really depends more on your needs, though. It sounded like you wanted your action server to start executing a goal, checking two quantities as it goes along, perhaps aborting, succeeding, or continuing execution based on their values. In that case, I'd have a callback for each topic you're interested in and callbacks for goal messages and cancel messages. Like I said above, the callbacks for topics just store the value of the messages. Similarly, the goal callback just stores the goal. Then, in the main function, you check to see if you have a goal or not, what type it is, and whether you should abort, succeed or continue based on the inputs you saved when they came in before the current loop iteration.

If you only need to check your values once when you get a goal or a cancel request, then it should be fine to put the logic in the callback. The values should be there if something is publishing them.

Q3. Will the action server callback function block the topic callback?

A3: Yes, callbacks will block. You need to spin to get messages on topics you're subscribed to. That's why I'd make simple callbacks and a more complicated main function. But again, that depends on your goals.

edit flag offensive delete link more

Comments

Thanks for your answer. I have a few questions. 1. Do you mean the spin() in the main function? 2. You mention about the main loop, is the main loop inside the main function or the callback function in the action server class? 3. Will the action server callback function block the topic callback?

Fei Liu gravatar image Fei Liu  ( 2013-06-24 09:00:34 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2013-06-22 09:49:34 -0500

Seen: 1,590 times

Last updated: Jun 24 '13