ROS MultiThreading example
Hello,
I'm currently working on a node that provides a service to other nodes but I noticed that being a server blocks the execution of the node until a Request. As my node needs to handle some callbacks, I read that a solution could be multithreading, and several good explanations on what's behind the hood, but I have trouble understanding how to use it.
My case is the following : the nodes receives sensory information and publish a motor command. In most of the case, this is enough, but in certain conditions, another node will send a request for stopping. I thought a service would fit better than a topic, as it allows the requesting node to wait for the response before continuing.
From my readings, I understand that I can declare a MultithreadSpinner or an AsyncSpinner, the second being non blocking. How does that changes my usual node organisation (C++ pseudo code) :
int main()
{
ros::init(...);
ros::NodeHandle nh;
MyClassController controller(nh, ...);
controller.run();
}
MyClassController::run()
{
while(nh_.oh())
{
// Process Callbacks
ros::spinOnce()
}
}
MyClassController::callback1(ros::Datatype msg){ myClassAttribute = msg }
MyClassController::myService(mypackage::mypackageserv req, mypackage::mypackageserv res){}
(...)
Would it be something like :
int main()
{
ros::init(...);
ros::NodeHandle nh;
MyClassController controller(nh, ...);
controller.run();
}
MyClassController::run()
{
// Process Callbacks
myclassAsyncSpinner.start();
myclassAsyncSpinner.waitForShutdown();
}
MyClassController::callback1(ros::Datatype msg){ myClassAttribute = msg }
MyClassController::myService(mypackage::mypackageserv req, mypackage::mypackageserv res){}
(...)
In this case, how can I be sure the service will be processed separately from other callbacks thus not block them? I feel that I may be mixing multi threading and "multi callback queuing", and the second is more relevant to my problem.
To sum things up : could you show me or point me to MWE (Minimal Working Example) of nodes that use Muilti-threading (let's say with AsyncSpinner) and (at least) two Callback queues, or more specifically, a MWE of a node that provide a service and process callback in parallel ?
Thanks for reading,
EDIT : From reading again the Different Queues explanations, I think I get it a bit better : should I declare 2 node handles in myClassController, with their queues, and then subscribe / advertise explicitly my service to one and my callbacks to the other ? Do I also need a multithread spinner and pass them the specific queue ?