In ROS Basics in 5 days, unit 4, the following program is used to subscribe to Laser data
C++ Program {4.2}: simple_topic_subscriber.cpp:
#include <ros/ros.h>
#include <std_msgs/Int32.h>
void counterCallback(const std_msgs::Int32::ConstPtr& msg)
{
ROS_INFO("%d", msg->data);
}
int main(int argc, char** argv) {
ros::init(argc, argv, "topic_subscriber");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("counter", 1000, counterCallback);
ros::spin();
return 0;
}
- Can someone tell me why
const std_msgs::Int32::ConstPtr&
is used? specifically the need for const and ConstPtr&? why have be opted for this?
- I am familiar with the pass by reference technique, where we use &variable to get the address of a variable,but what what is variable& as used in
ConstPtr&
?
- What is
boost
? I have seen this comeup now and then
Hi Joseph, let me answer this the best I can.
-
The subscriber callback uses const std_msgs::Int32::ConstPtr&
, to use the message received in a specific way, in a boost shared_ptr
, which means you can store this message if needed without it getting deleted underneath and without copying the underlying data.
-
::ConstPtr&
, according to this post,What is ConstPtr&? - ROS Answers: Open Source Q&A Forum, can be understood as a reference to a shared pointer to a constant message. It points to the allocated memory where the message is being stored.
-
boost
is a collection of C++ libraries. It includes the shared_ptr
class that is used in that callback variable.
Hi @roalgoal , thanks for replying. I think you answered most of my questions.
Feedback:
@staff It would have been great if ConstPtr&
and boost
was introduced in the C++ basics course provided by Construct. This way users could have more in-depth knowledge on the concepts in future courses.