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

ROS1 Subscriber callback

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

fiji3119 gravatar image

I have a simple class as follows:

class SimpleSub {
    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);
       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

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


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

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

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?

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



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

Seen: 510 times

Last updated: May 21 '20