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

Revision history [back]

For the callback to be called, ros::spin() or ros::spinOnce() have to be called (for the simple single threaded case). In your code, this is missing from your while(ros::ok()) loop. The spin() call only gets called when you abort your program (as soon as ros::ok() returns false).

See Callbacks and Spinning. You're basically looking at the second option and should call execute ros::spinOnce inside your loop, for instance like this:

while(ros::ok()) {
    ros::spinOnce();

    //ROS_INFO("state: %d  ",state);

    ss << "State is: " << state << " y: " << y;// << " Battery is: " << battery;
    state_s.data = ss.str();

    pub_s.publish(state_s);

    if(state==2) {
        pub_t.publish(emp);
    }
    else
        pub_f.publish(msg);

    loop_rate.sleep();
}