1、单线程订阅
示例:
#include <ros/ros.h>
#include <std_msgs/Bool.h>
void tempCallback(const std_msgs::BoolConstPtr& msg)
{
int num = 10;
ros::Rate loop_rate(10);
while (ros::ok() && num)
{
ROS_INFO_STREAM("temp temp temp temp temp temp temp temp call back, num->"<<num);
num--;
loop_rate.sleep();
}
}
void longCallback(const std_msgs::BoolConstPtr& msg)
{
int num = 10;
ros::Rate loop_rate(10);
while (ros::ok() && num)
{
ROS_INFO_STREAM("long call back, num->"<<num);
num--;
loop_rate.sleep();
}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "test_node");
ros::NodeHandle nh;
ros::Subscriber tempSub = nh.subscribe<std_msgs::Bool>("temp", 1, tempCallback);
ros::Subscriber longSub = nh.subscribe<std_msgs::Bool>("long", 1, longCallback);
ros::spin();
return 0;
}
输入
rostopic pub /long std_msgs/Bool "data: false" -r 10
rostopic pub /temp std_msgs/Bool "data: false" -r 10
输出:
[ INFO] [1565918138.532995825]: long call back, num->10
[ INFO] [1565918138.633079367]: long call back, num->9
[ INFO] [1565918138.733064686]: long call back, num->8
[ INFO] [1565918138.832994117]: long call back, num->7
[ INFO] [1565918138.932998012]: long call back, num->6
[ INFO] [1565918139.032996832]: long call back, num->5
[ INFO] [1565918139.133071165]: long call back, num->4
[ INFO] [1565918139.232960431]: long call back, num->3
[ INFO] [1565918139.332995614]: long call back, num->2
[ INFO] [1565918139.432999905]: long call back, num->1
[ INFO] [1565918139.533033535]: temp temp temp temp temp temp temp temp call back, num->10
[ INFO] [1565918139.633149213]: temp temp temp temp temp temp temp temp call back, num->9
[ INFO] [1565918139.733148347]: temp temp temp temp temp temp temp temp call back, num->8
[ INFO] [1565918139.833150790]: temp temp temp temp temp temp temp temp call back, num->7
[ INFO] [1565918139.933050574]: temp temp temp temp temp temp temp temp call back, num->6
[ INFO] [1565918140.033141442]: temp temp temp temp temp temp temp temp call back, num->5
[ INFO] [1565918140.133159048]: temp temp temp temp temp temp temp temp call back, num->4
[ INFO] [1565918140.233151712]: temp temp temp temp temp temp temp temp call back, num->3
[ INFO] [1565918140.333148631]: temp temp temp temp temp temp temp temp call back, num->2
[ INFO] [1565918140.433147718]: temp temp temp temp temp temp temp temp call back, num->1
[ INFO] [1565918140.533266548]: long call back, num->10
[ INFO] [1565918140.633400237]: long call back, num->9
[ INFO] [1565918140.733407032]: long call back, num->8
[ INFO] [1565918140.833389468]: long call back, num->7
[ INFO] [1565918140.933386767]: long call back, num->6
[ INFO] [1565918141.033389566]: long call back, num->5
[ INFO] [1565918141.133389982]: long call back, num->4
[ INFO] [1565918141.233389902]: long call back, num->3
[ INFO] [1565918141.333395700]: long call back, num->2
[ INFO] [1565918141.433399861]: long call back, num->1
[ INFO] [1565918141.533413994]: temp temp temp temp temp temp temp temp call back, num->10
[ INFO] [1565918141.633477569]: temp temp temp temp temp temp temp temp call back, num->9
[ INFO] [1565918141.733526139]: temp temp temp temp temp temp temp temp call back, num->8
[ INFO] [1565918141.833527631]: temp temp temp temp temp temp temp temp call back, num->7
[ INFO] [1565918141.933526546]: temp temp temp temp temp temp temp temp call back, num->6
[ INFO] [1565918142.033560826]: temp temp temp temp temp temp temp temp call back, num->5
[ INFO] [1565918142.133522903]: temp temp temp temp temp temp temp temp call back, num->4
[ INFO] [1565918142.233525957]: temp temp temp temp temp temp temp temp call back, num->3
[ INFO] [1565918142.333525708]: temp temp temp temp temp temp temp temp call back, num->2
[ INFO] [1565918142.433521566]: temp temp temp temp temp temp temp temp call back, num->1
2、多线程订阅
示例
#include <ros/ros.h>
#include <std_msgs/Bool.h>
void tempCallback(const std_msgs::BoolConstPtr& msg)
{
int num = 1;
ros::Rate loop_rate(10);
while (ros::ok() && num)
{
ROS_INFO_STREAM("temp temp temp temp temp temp temp temp call back, num->");
num--;
loop_rate.sleep();
}
}
void longCallback(const std_msgs::BoolConstPtr& msg)
{
int num = 10;
ros::Rate loop_rate(10);
while (ros::ok() && num)
{
ROS_INFO_STREAM("long call back, num->"<<num);
num--;
loop_rate.sleep();
}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "test_node");
ros::NodeHandle nh;
ros::Subscriber tempSub = nh.subscribe<std_msgs::Bool>("temp", 1, tempCallback);
ros::Subscriber longSub = nh.subscribe<std_msgs::Bool>("long", 1, longCallback);
ros::MultiThreadedSpinner spinner(3);//三个线程订阅
spinner.spin();
return 0;
}
输入
rostopic pub /long std_msgs/Bool "data: false" -r 10
rostopic pub /temp std_msgs/Bool "data: false" -r 10
输出
[ INFO] [1565917840.270586066]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917840.332730168]: long call back, num->10
[ INFO] [1565917840.370680016]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917840.432812052]: long call back, num->9
[ INFO] [1565917840.470733027]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917840.532746151]: long call back, num->8
[ INFO] [1565917840.570822519]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917840.632748312]: long call back, num->7
[ INFO] [1565917840.670920845]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917840.732748731]: long call back, num->6
[ INFO] [1565917840.771020449]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917840.832749593]: long call back, num->5
[ INFO] [1565917840.871116535]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917840.932749685]: long call back, num->4
[ INFO] [1565917840.971215785]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917841.032749710]: long call back, num->3
[ INFO] [1565917841.071313507]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917841.132749230]: long call back, num->2
[ INFO] [1565917841.171407053]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917841.232743868]: long call back, num->1
[ INFO] [1565917841.271526256]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917841.332755181]: long call back, num->10
[ INFO] [1565917841.371625145]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917841.432905582]: long call back, num->9
[ INFO] [1565917841.471753894]: temp temp temp temp temp temp temp temp call back, num->
[ INFO] [1565917841.532846109]: long call back, num->8