Is this related / a follow-up / a repost of your #q372471?
As to your question: I don't believe there is anything special to publishers or subscribers here.
Publishers broadcast messages, subscribers receive them.
Publishers and subscribers do not contain messages, nor do they contain values (or at least: not the kind of values I believe you are interested in).
Messages are kept in a queue by subscribers, and they get passed to your callback function when it gets invoked (people typically give the name msg
to that argument of the callback function/method).
In that callback function you then access the information you are interested in inside the message object. You can only do that inside the callback: the message object is "thrown away" after the callback completes and is not retained by the subscriber (by default). Nor does the data inside the message get stored in the subscriber directly.
To make it extra clear: publishers are not comparable to simple variables which contain data, nor are subscribers. They are active objects which you can use to transmit and receive data (called messages in ROS).
I would like to enquire there are any examples for conditional statement for publisher and subscriber.
I hope it's somewhat clearer now that this particular question isn't really something you can ask.
The direct answer would be: no, there are no comparison operators defined for ros::Publisher
and ros::Subscriber
. No direct (specialised) comparisons between publishers and subscribers are available.
I also don't believe it would make sense either: what would be compared (remember: there is no data directly stored in either of these which relates to message content)?
For instance, if the battery level falls below the specified battery level, there would be a warning message. Else, the machine does not publish anything.
This is certainly possible, but would be essentially the same as comparing any two number-like values in any programming language. Only in this case, you'd make that comparison in the callback.
To give an example (in C++, roscpp
, pseudo-code):
void callback(const sensor_msgs::BatteryState::ConstPtr& msg)
{
// minimum acceptable battery level
const float MIN_PERCENTAGE = 0.1;
// compare with level reported in the message
if (msg->percentage < MIN_PERCENTAGE)
{
// do something when battery level drops below the minimum
..
}
else
{
// still sufficient battery charge available, do something else
..
}
}
note how this code uses the percentage
field which is part of the sensor_msgs/BatteryState message which was received.
It does not check anything in a subscriber or a publisher (and how could it: those objects do not contain any of this data).
note also this is exactly how you'd do something without ROS: comparing number-like variables is done using the regular <
operator.