Learn ROS in 5 days, rosject part 3 odometry

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.

Let me ask you a basic question first: are you moving the robot while doing the measurements?

Second, I assume you are populating self.position_x and the others inside the self.callback_odom?

Third, it is not a good idea to create a subscriber inside a callback. You should create any subscribers you will need in the constructor of the class (the __init__ function).

Hi!

Thanks for your fast reply.

  1. Yes, I am moving the robot while taking the measurements. I’m running my wall following node at the same time.
  2. Yes, I have a the following callback function:
def callback_odom(self, msg):
        #since robot moves in 2D we only care about linear x, linear y and angular z
        self.position_x = msg.pose.pose.position.x
        self.position_y = msg.pose.pose.position.y
        self.orientation_theta = msg.pose.pose.orientation.z
  1. And thanks for that suggestion! I’ll make that change.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.