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

ROS1 Subscriber callback

asked 2020-05-21 11:34:35 -0600

fiji3119 gravatar image

I have a simple class as follows:

class SimpleSub {
public:
    SimpleSub(ros::NodeHandle *nh) {
           nh->subscribe("/imu", 1000, &SimpleSub::imuSubsCallback, this);
   }
  void imuSubsCallback(const sensor_msgs::ImuConstPtr& msg)
  {
      ROS_INFO("Orientation: X=%f, Y=%f, Z=%f\n", msg->orientation.x, msg->orientation.y, msg->orientation.z);
  }
private:
       ros::Subscriber imuSub_;
};

When I run IMU publisher, my callback does not get called. However, it works when I use just stand-alone (not class member function) like this:

void imuSubsCallback(const sensor_msgs::ImuConstPtr& msg);

There is no compile error. Am I doing something wrong when registering callback (nh->subscriber)? Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-05-21 18:31:59 -0600

Thomas D gravatar image

Try to use imuSub_ to subscribe, otherwise the subscription goes out of scope when you leave your constructor.

imuSub_= nh->subscribe("/imu", 1000, &SimpleSub::imuSubsCallback, this);
edit flag offensive delete link more

Comments

Duh! Yeah. Thank you for pointing that out. Works!

fiji3119 gravatar image fiji3119  ( 2020-05-22 16:20:33 -0600 )edit
0

answered 2020-05-21 17:04:52 -0600

billy gravatar image

updated 2020-05-21 18:23:18 -0600

To make a callback run, you to need to spin. Are you spinning? http://wiki.ros.org/roscpp/Overview/C...

Of course then the trigger for the callback also has to occur so you'll have to make sure the publisher is publishing.

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2020-05-21 11:34:35 -0600

Seen: 559 times

Last updated: May 21 '20