ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Callback in C++ class for topic subscription

asked 2018-05-17 04:38:40 -0500

saurabh gravatar image

updated 2018-05-17 07:49:47 -0500

gvdhoorn gravatar image

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.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2018-05-17 07:49:07 -0500

gvdhoorn gravatar image

Please see wiki/roscpp/Overview/Publishers and Subscribers - Subscribing to a Topic - Callback Types - Class Methods: for callbacks that are methods (ie: class members) you need to provide a pointer to the object that the method should be invoked on.

For your ctor initialisation of the Subscriber, that would be this.

Also: you don't register methods as callbacks with this->...

edit flag offensive delete link more

Comments

Thanks gvdhoorm, it is working.

saurabh gravatar image saurabh  ( 2018-05-19 04:45:52 -0500 )edit
0

answered 2021-07-21 01:23:09 -0500

Avery gravatar image

nh.subscribe("joint1_controller/state", 10, &joint_move::joint_state_cb, this);

edit flag offensive delete link more
0

answered 2018-05-17 05:24:56 -0500

Gil404 gravatar image

updated 2018-05-17 05:26:18 -0500

try changing: nh.subscribe("joint1_controller/state", 10, this->joint_state_cb);

to: nh.subscribe("joint1_controller/state", 10, &joint_move::joint_state_cb);

edit flag offensive delete link more

Comments

Thanks for the answer. Tried it, same error. Non-static member can't be called.

saurabh gravatar image saurabh  ( 2018-05-17 07:44:43 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-05-17 04:38:40 -0500

Seen: 1,968 times

Last updated: May 17 '18