ROS Threading, AsyncSpinners and Control
Hello everybody,
I am a relatively new ROS user (but I've spent years working with other middlewares), and up to now I am generally happy about it. I have a doubt related to my software framework I would like to clarify. To this end, I will describe the framework I have, so that some more experienced ROS user can tell me where I am doing wrong:
- I have a node that is composed of two controllers (one for each of the Baxter's arms). Each controller subscribes to a variety of topics and publishes to a variety of topics. They are implemented as classes, and they both use an AsyncSpinner.
This is a pseudo-code of my framework. In the main:
int main() { Controller c1("left"); Controller c2("right"); ros::spin(); }
in the constructors:
Controller::Controller(string limb): _limb(limb), spinner(4) { ... spinner.start(); }
Question 1: is this a correct initialization of an AsyncSpinner? Right now I have two AsyncSpinners that are initialized in the same process/node. Question 2: Do they share the same 4 threads? I am not using custom callback queues.
A part from subscribing to topics, both controllers have a main control loop that I implemented as a
pthread
, which runs in parallel with everything else. It is crucial for mypthread
s to have a constant control rate of at least 100Hz, but at the same time I would like to update the information coming from the subscribers while the thread is doing its job. What I did to this end was the following pseudo code related to therun()
of the thread:pthread_run() { while(ros::ok()) { ... ros::spinOnce(); ros::Rate(100).sleep(); } }
Question 3: with the presence of an AsyncSpinner, is the
ros::spinOnce()
necessary, or can I remove it? As far as I understood I should not need aros::spinOnce()
, because my callback queue is guaranteed to be emptied in the other three threads I fed the spinner with. Is it correct?- No matter what I try, some times I experience dropped frames in my
pthread
s, which negatively affect my control loop. The reasons for this might be multiple (delays in the network, etc), but here is my Question 4: is this issue related to how I implemented my code? As a matter of fact, this is the main reason for which created this question. I am sure it is not a CPU load problem. - I was thinking of dropping altogether the
pthread
s and implementing the control loops as callbacks that I would then add to theros::getGlobalCallbackQueue()
so that I do not have to take care of the threading at all. Question 5: does this make any sense, or is it going to solve my problem?
Thank you for your help. I know that some of this questions might seems stupid, but I didn't find anything useful online, and I would like to use ROS to the best of its capabilities.
Answer to kramer: thank you for ...