2012-08-16 62 views
0

我有代码来定义舞台的世界文件中设置的位置模型的行为。我想通过订阅odom话题由stageros发送里程计的邮件跟踪世界它的当前位置的变量pxpyptheta。像这样:回调中未更新的变量

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

这仅仅是一个例子,但由于某些原因的像素,从另一个功能印刷PY和ptheta值似乎总是在初始像素,PY和ptheta值被卡住。即使ReceiveOdometry回调也在不断打印正确的值。 px,py,ptheta值不同,就好像每个变量有两个不同的值。

来自ReceiveOdometry的ROS_INFO正确输出当前位置。

ROS_INFO从OtherFunction打印初始位置,不会发生任何变化,即使PX,PY和ptheta不断地被在ReceiveOdometry设置。

有谁知道是什么原因造成的ReceiveOdometry回调不结转到OtherFunction更改到PX,PY和ptheta?希望这个问题是有道理的。

谢谢。

+0

你确定它是'Robot'的同一个实例吗?确保输出'this'的值。 – hmjd 2012-08-16 10:05:46

+0

@hmjd是的,它是机器人的同一个实例。 – Jigglypuff 2012-08-16 10:13:32

+0

这可能是一个线程可见性问题。大概回调是从'OtherFunction()'的另一个线程上调用的? – hmjd 2012-08-16 10:17:16

回答

1

打印和检查在两种功能。你应该使用两个不同的对象。

0

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

About the volatile keyword