Pregunta

I have code to define behavior of a position model set in a world file for stageros. I want to keep track of it's current position in the world in the variables px py and ptheta by subscribing to the odom topic for Odometry messages sent by stageros. Like this:

ros::Subscriber RobotOdometry_sub = n.subscribe<nav_msgs::Odometry>("robot_0/odom",1000,&Robot::ReceiveOdometry,this);

Which is put in the constructor for a Robot object. Then the callback is as follows:

void Robot::ReceiveOdometry(nav_msgs::Odometry msg)
{
    //This is the call back function to process odometry messages coming from Stage.
    px = initialX + msg.pose.pose.position.x;
    py = initialY + msg.pose.pose.position.y;
    ptheta = angles::normalize_angle_positive(asin(msg.pose.pose.orientation.z) * 2);
    ROS_INFO("x odom %f y odom %f theta %f", px, py, ptheta);
}

This callback seems to get called no problem. The px, py and ptheta values printed by the callback are all correct as well and correspond with their current position in the world. The problem occurs in other functions:

void Robot::OtherFunction() {
    while (ros::ok())
    {
        ros::spinOnce();
        ROS_INFO("x %f y %f theta %f", px, py, ptheta);
    }
}

This is just an example, but for some reason the px, py and ptheta values printed from another function always seem to be stuck at the initial px, py and ptheta values. Even though the ReceiveOdometry callback is continuously printing the right values as well. The px, py, ptheta values are different as if there are two different values for each variable.

ROS_INFO from ReceiveOdometry prints the current position correctly.

ROS_INFO from OtherFunction prints the initial positions and doesn't change at all even though px, py and ptheta are continuously being set in ReceiveOdometry.

Does anyone know what is causing the changes to px, py and ptheta in the ReceiveOdometry callback not to carry over to the OtherFunction? Hopefully this question makes sense.

Thanks.

¿Fue útil?

Solución

Print and check this in both functions. You shall be using two different objects.

Otros consejos

Maybe there is some optimization going on and the variables are not read from memory but kept in cache since they are not modified inside the while loop. If that's the case declaring them as volatilewould help.

About the volatile keyword

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top