Multi-threaded use of ROS subscription functions (c + +: External variable control jump out for loop)

Source: Internet
Author: User


Originally want to implement to receive a some_topic and then parse the content, if the content for the instruction Action,action function into a for loop, if again receive some_topic, inside the command is stop stop=true stop the action in progress, Jump out of the For loop (you want to control the end of the for loop with an external variable).

Similar:

Ros::nodehandle N;
Ros::subscriber sub = Nh.subscribe (. " Some_topic ", Actioncallback.);
.
.
.
void Actioncallback (..)
{
Ros_info ("recevied");
if (Command = "action")
{
Action (3);
....
}
else if (Command = "Stop")
{
Stop = true;
}
}

void action (int received)
{
for (int i=0; i<5;i++)
{
for (int j=0; j<4;j++)
{
std::cout<<received*i*j<<std::cout;
....

if (stop)
{

break;//jump out of the first layer loop
}
}
if (stop)
{
Ros_info ("Stop");
break;//jump out of the second layer of loops
}
Ros::D uration (0.2). Sleep ();
}
stop = false;
}
However, regardless of the stop why variable, it is always found that after entering the for loop, if the loop is not completed, the action has no return value. is not receiving the next message, from the time stamp of Ros_info can be seen.

The timestamp of the second print is always followed by the first timestamp, that is, if the first message is action, the second is stop, and the third is action, then the first action loop is executed completely, and the for loop of the third action is not executed again. So a thread is not able to complete the stop want function, so think of a method, specifically for stop to open a thread, with a some_stop_topic, then there are two threads.


Similar:
Ros::nodehandle N;
Ros::subscriber action_sub = N.subscribe (... "some_topic", actioncallback ...);
Ros::subscriber stop_action_sub =. Subscribe (... "Some_stop_topic", actionstopcallback ...);
Ros::spin ();
At this time the same test, found that still did not realize the desired function, and finally found that Ros::spin () just opened up a thread, I originally thought every callback is a thread, so far just dawned.
reference [1], found that this method can be solid line this function:

Ros::nodehandle N;
Ros::subscriber action_sub = N.subscribe (... "some_topic", actioncallback ...);
Ros::subscriber stop_action_sub =. Subscribe (... "Some_stop_topic", actionstopcallback ...);
Ros::multithreadedspinner spinner (2); Use 2 threads
Spinner.spin (); Spin () won't return until the node has been shutdown

Use Multithreadedspinner to indicate that this node has two threads, also available (Boost::thread), and that the desired functionality is finally implemented, but because the for loop is faster and no lock is used, the stop is sent every time, Exiting the For loop is not the same level, so you need to think about it.

[1] http://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning


If you have a good way to solve my problem, please do not hesitate to enlighten me. Thank you very much for communicating with each other.


Multi-threaded use of ROS subscription functions (c + +: External variable control jump out for loop)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.