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

Revision history [back]

click to hide/show revision 1
initial version

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.

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.