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

Revision history [back]

click to hide/show revision 1
initial version

The ros::spinOnce(), where the callback is actually called, is not in your main while loop! Should be like:

int main(int argc, char** argv){
  ros::init(argc, argv, "turtle_tf_listener");

ros::NodeHandle n;

  ros::Rate rate(10.0);
  while (node.ok()){

 ros::Subscriber sub = n.subscribe ("/chatter", 10, cb);

 ROS_INFO_STREAM(message);
    ros::spinOnce(); // this is where the magic happens!!
    rate.sleep();
  }

  return 0;

};

The ros::spinOnce(), where the callback is actually called, is not in your main while loop! Further the subscriber should be set up only once before the while loop. Should be like:

int main(int argc, char** argv){
  ros::init(argc, argv, "turtle_tf_listener");

ros::NodeHandle n;

 ros::Subscriber sub = n.subscribe ("/chatter", 10, cb);

  ros::Rate rate(10.0);
  while (node.ok()){

 ros::Subscriber sub = n.subscribe ("/chatter", 10, cb);

 ROS_INFO_STREAM(message);
    ros::spinOnce(); // this is where the magic happens!!
    rate.sleep();
  }

  return 0;

};