当前位置:网站首页>Commonly used functions -- spineros:: and spineros::)

Commonly used functions -- spineros:: and spineros::)

2022-04-23 17:38:00 Cai Xukun, houchangcun Road

( One )ros::ok()

Used to check system status , Apply to while Judge the state in the loop
grammar :

while(ros::ok())
{
    
	//...
}

ros::ok() Returns... In the following cases false:

1、 Press down Ctrl-C when .
2、 We were kicked out of the network by a node with the same name .
3、ros::shutdown() Called by another part of the application .
4、 be-all ros::NodeHandles It's all destroyed .
5、 once ros::ok() return false, be-all ROS Calls fail 

( Two )ros::Rate

Used to set the cycle frequency , Apply to Publisher Program infinite loop
ros::Rate You can specify the frequency of the loop

ros::Rate loop_rate(10);//10Hz
//...
loop_rate.sleep();

( 3、 ... and )ros::spin() and ros::spinOnce()

Used for processing ROS Callback function for , Apply to Subscriber Processing callback data .
The difference is ros::spin() After calling, it will not return , and ros::spinOnce() After the call, you can continue to execute the subsequent program .
ros::spin() For acyclic scenes ,ros::spinOnce() For scenes with loops .

When to use ros::spin() and ros::spinOnce() Well , If it's just a response topic, Just use ros::spin(). When the program has other repetitive work besides the response callback function , Then do the work in the loop , And then call ros::spinOnce().

1、ros::spin()

int main(int argc, char **argv)
{
    
    ros::init(argc, argv, "listener");
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

    ros::spin();
    return 0;
}

2、ros::spinOnce()
This sentence means to monitor the feedback function (callback). Only listen for feedback , You can't cycle . So when you need to monitor , Just call this function .
This function is more flexible , Especially when I want to control the receiving speed . coordination ros::ok() Excellent results .

ros::Rate loop_rate(10);
while(ros::ok())
{
    
    ros::spinOnce();
    loop_rate.sleep();
}

版权声明
本文为[Cai Xukun, houchangcun Road]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231736284895.html