Issue with ROS subscriber reading old message [closed]

asked 2016-11-11 15:02:10 -0500

215 gravatar image

updated 2016-11-12 13:59:09 -0500

I am currently communicating between two ros nodes via a rostopic.

The publisher is a python ros node, and the subscriber is a cpp ros node.

The problem is that when I send a message from the ros node (python), is the callback function activated on the cpp side, but the message it process is the message that was send previously. The subscriber on the cpp side seem to be one message behind all the time? I don't know why this is the case in the first place. I tried changing the queue_size with the no effect. I am sure that the message being sent from the publisher is the correct one, as I've confirmed it by echoing the topic. So something must be wrong on the cpp, but i've no idea what could be wrong?..

The only thing the callback does is printing the incoming message.

Does anyone have any idea on what could be wrong?

After some further investigations it seems that the error could be in the python code. (eventhoug debug shows should not be)

The publisher publishes the message inside a callback function. the python code has two callback function, which publishes to the same topic, but both would not be activated at the same time.

Could publishing a message to topic from different callback function affect the outcome?..

The overall situation is rather a bit more complicated than a simple main loop. I am running a multi threaded system, in which a message is being published when a certain thread is running. I tried making a MWE.

MWE:

ros::NodeHandle nodehandle;
ros::Publisher control_cpp_to_python;
ros::Publisher control_front_to_python;
ros::Subscriber status_manual;
ros::Subscriber status_auto;
ros::Rate loop_rate;
void callback_manual(const std_msgs::String& command)
{
    ROS_ERROR_STREAM("RECEIVED a callback");
    std::cout <<"Received unconverted message - manuak: "<< command.data << std::endl;

    if(checked == true)
    {
        std::cout << "Message received from correct callback - Manual" << std::endl;
        checked = false;
        thread_running=false;
    }
    else
    {
        checked = true;
    }
}
 void callback_auto(const std_msgs::String& command)
    {
        ROS_ERROR_STREAM("RECEIVED a callback");
        std::cout <<"Received unconverted message - auto: "<< command.data << std::endl;

        if(checked == true)
        {
            std::cout << "Message received from correct callback - Manual" << std::endl;
            checked = false;
            thread_running=false;
        }
        else
        {
            checked = true;
        }
    }

while(thread_running)
{
    loop_rate.sleep();
    if(!thread_running) break;
    if(mode == "manual")
    {   
        std_msgs::Int8 msg;
        std::bitset<4> msg_bit;
        msg_bit.set(3,1);
        msg_bit.set(2,1);
        msg_bit.set(1,1); 
        msg_bit.set(0,0);
        std::cout << "Sending message in manual mode!" << std::endl;
        std::cout << "Message being: "<< msg_bit << std::endl;
        msg.data = int(msg_bit.to_ulong());
        control_front_to_python.publish(msg);
    }
    if(mode == "auto")
    {
        std::cout << "Publishing message in auto" << std::endl;
        std_msgs::Int8 msg;
        msg.data = 8;
        control_cpp_to_python.publish(msg);
    }
    ros::spinOnce();
}

I guess this is as scraped as it can be. I guess the issue must be due to me setting the thread_running to false inside the callback. I've currently fixed it by the checked bool as a temporary fix.

edit retag flag offensive reopen merge delete

Closed for the following reason Question does not follow our guidelines for questions. Please see: http://wiki.ros.org/Support for more details. by tfoote
close date 2018-04-29 22:10:01.822445

Comments

1

As almost always with these kinds of questions, please include either a MWE (minimal working example) that shows this behaviour, or provide access to your code. Otherwise we can only guess what is going on.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-12 07:01:12 -0500 )edit