Ask Your Question
1

ROS1 Subscriber callback

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

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 -0500

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 -0500 )edit
0

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

billy gravatar image

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

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

Seen: 177 times

Last updated: May 21 '20