Callback in C++ class for topic subscription
Hello All,
I am trying to make a class for the joints in my robotic arm. And this class is as below:
class joint_move{
ros::Publisher joint_pub;
ros::Subscriber joint_status;
void move_joint_topic(double);
dynamixel_msgs::JointState joint_state;
void joint_state_cb(const dynamixel_msgs::JointState::ConstPtr& msg) { joint_state = *msg; }
public:
joint_move(ros::NodeHandle, int);
};
And the definition for constructor is as below:
joint_move::joint_move(ros::NodeHandle nh, int id)
{
joint_pub = nh.advertise<std_msgs::Float64>("joint1_controller/command", 1000);
joint_status = nh.subscribe("joint1_controller/state", 10, this->joint_state_cb);
}
But during the call of the nh.subscribe, there is a compilation error, that a non-static member function can't be called like this. I did some search myself, and came to know it is difficult to have a callback in c++.
So has somebody have made some class like this. Or is there any better implementation possible to achieve this kind of modularity. Or can somebody please let me know how to implement this.
Regards, Saurabh
Edit: I also tried to make this function as static in the class itself. But then I am not able to access the member variable i.e joint_state in the callback function. So that is useless for me.
I tried to make a wrapper also, to which I can pass this pointer. But if we make any function as static in a class, then we can't use this pointer also.
Upto now I didn't find any solution.