Hi!
I’m doing the rosject part 3 about action server. There we are asked to get the distance (total amount of meters) that the robot has moved. For that I use the current odometry reading and the previous one. But I keep getting the same value in both readings. The only way in which I was able to get different readings was when I added the initialization of my Point32 variable inside my while loop. Just above the individual declarations. Can explain to me why this happens?
I leave a sample of my code below:
def goal_callback(self, goal):
self.sub_odom = rospy.Subscriber('/odom', Odometry, self.callback_odom)
#to allow the subscriber to get readings from the odometry topic
time.sleep(3)
# helper variables
r = rospy.Rate(1) #for 1 second loop
success = True
dis = 0
i = 0
#value measured
while dis < 5.5:
# check that preempt (cancelation) has not been requested by the action client
if self._as.is_preempt_requested():
rospy.loginfo('The goal has been cancelled/preempted')
# the following line, sets the client in preempted state (goal cancelled)
self._as.set_preempted()
success = False
# we end the calculation of the Fibonacci sequence
break
#If I leave the initialization odom_readings = Point32() outside the loop it always has the same values
#for odom_readings.x and self._result.list_of_odoms[i - 1].x
#it's like if they never update, why??????????
odom_readings = Point32()
odom_readings.x = self.position_x
odom_readings.y = self.position_y
odom_readings.z = self.orientation_theta
if(i >= 1):
distance = math.sqrt((math.pow(odom_readings.x - self._result.list_of_odoms[i - 1].x, 2)) + (math.pow(odom_readings.y - self._result.list_of_odoms[i - 1].y, 2)))
else:
distance = 0.0
dis += distance
self._feedback.current_total = dis
self._as.publish_feedback(self._feedback)
self._result.list_of_odoms.append(odom_readings)
i += 1
r.sleep()
if success:
rospy.loginfo('Odometry data for one lap recorded')
self._as.set_succeeded(self._result)
rospy.loginfo("finishing action server...")
Thanks in advance.