Hi everyone,
I found some code which is using 4 arguments to define a subscriber, such as in the following code:
#include "ros/ros.h"
#include <stdio.h>
#include <stdlib.h>
#include <sensor_msgs/Imu.h>
#include <vector>
#include <string>
using namespace std;
class ClassFoo
{
public:
ClassFoo(string rname){
guest_name = rname;
imu_sub = nm.subscribe("/imu", 1, &ClassFoo::imuCallback, this);
}
void imuCallback(const sensor_msgs::Imu & msg)
{
lowState.imu.quaternion[0] = msg.orientation.w;
lowState.imu.quaternion[1] = msg.orientation.x;
lowState.imu.quaternion[2] = msg.orientation.y;
lowState.imu.quaternion[3] = msg.orientation.z;
lowState.imu.gyroscope[0] = msg.angular_velocity.x;
lowState.imu.gyroscope[1] = msg.angular_velocity.y;
lowState.imu.gyroscope[2] = msg.angular_velocity.z;
lowState.imu.accelerometer[0] = msg.linear_acceleration.x;
lowState.imu.accelerometer[1] = msg.linear_acceleration.y;
lowState.imu.accelerometer[2] = msg.linear_acceleration.z;
}
private:
ros::NodeHandle nm;
ros::Subscriber imu_sub;
string guest_name;
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "node_test");
string guest_name;
ros::param::get("/guest_name", guest_name);
ClassFoo listen_publish_obj(guest_name);
ros::AsyncSpinner spinner(1); // one threads
spinner.start();
usleep(1000000); // must wait 1s
ros::NodeHandle n;
while (ros::ok()){
// Do something
cout << "I did nothing!" << endl;
}
return 0;
}
The part I do not understand is the line: imu_sub = nm.subscribe("/imu", 1, &ClassFoo::imuCallback, this);
: what is the fourth argument for? I know it's a pointer to the class ClassFoo, but I do not get what the purpose of this argument is. When I go to the documentation , it says:
\param tracked_object
A shared pointer to an object to track for these callbacks. If set, the aweak_ptr
will be created to this object, and if the reference count goes to 0 the subscriber callbacks will not get called. Note that setting this will cause a new reference to be added to the object before the callback, and for it to go out of scope (and potentially be deleted) in the code path (and therefore thread) that the callback is invoked from.
So does it mean that the fourth argument is some sort of "insurance" that if the ClassFoo object dies for any reason, the callback is not called? But if the object dies, the callback would be dead anyway, no?
Thank you in advance!
Originally posted by robotguy on ROS Answers with karma: 13 on 2023-03-10
Post score: 1