0

我有代码来定义在 stageros 的世界文件中设置的位置模型的行为。我想跟踪它在变量中的当前位置,px pyptheta通过订阅odomstageros 发送的 Odometry 消息的主题。像这样:

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

它放在 Robot 对象的构造函数中。然后回调如下:

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);
}

这个回调似乎被调用没问题。回调打印的 px、py 和 ptheta 值也都是正确的,并且与它们在世界中的当前位置相对应。问题出现在其他函数中:

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

这只是一个示例,但由于某种原因,从另一个函数打印的 px、py 和 ptheta 值似乎总是停留在初始 px、py 和 ptheta 值上。即使 ReceiveOdometry 回调也在不断地打印正确的值。px、py、ptheta 值是不同的,就好像每个变量有两个不同的值一样。

来自 ReceiveOdometry 的 ROS_INFO 正确打印当前位置。

来自 OtherFunction 的 ROS_INFO 打印初始位置,并且即使在 ReceiveOdometry 中连续设置 px、py 和 ptheta 也不会改变。

有谁知道是什么导致 ReceiveOdometry 回调中对 px、py 和 ptheta 的更改没有延续到 OtherFunction?希望这个问题是有道理的。

谢谢。

4

2 回答 2

2

在这两个函数中打印并检查它。您将使用两个不同的对象。

于 2012-08-16T10:44:25.457 回答
0

也许正在进行一些优化,变量不是从内存中读取而是保存在缓存中,因为它们没有在while循环内被修改。如果是这种情况,声明它们volatile会有所帮助。

关于volatile关键字

于 2012-08-20T19:46:41.823 回答