ROS Q&A | How to know the Pose of a robot (Python) ?

Written by Alberto Ezquerro

09/10/2017

 

Here you have an example of how to read the Pose of a robot in Python, answering a question made in ROS Answers

Q: Hello ! I want to know the pose of turtlebot (x,y,z, z rotation) respect to the point from which it started. What is the best way to do that in python?

A:Hello! As you suggest in your last message, the best practice for doing that is to use the odom topic. For that, you can create a simple subscriber in Python that gets the data you need, which is, position and orientation respect to an starting point. The code for a simple subscriber could be like this:

#! /usr/bin/env python

import rospy
from nav_msgs.msg import Odometry

def callback(msg):
    print msg.pose.pose

rospy.init_node('check_odometry')
odom_sub = rospy.Subscriber('/odom', Odometry, callback)
rospy.spin()

This code basically subscribes to the odom topic, and prints the pose component of the message, which contains the position and orientation values.

 

The following steps take throught the development as shown in the video.

We can check the names of all the published topics with the following command (Enter in a Shell)

$ rostopic list

Check for /odom topic in the output of the command. We can now check the information being published on this topic using the following command

$ rostopic echo /odom -n1

The output of the above command should be as shown in the image below (x0x≈0, y0y≈0)

  • Next we will create a subscriber. Use the following commands to create a new package and a subscriber script file.

cd ~/catkin_ws/src catkin_create_pkg check_odometry cd check_odometry mkdir src launch

  • Now we will create a script named inside the check_odometry/src/ directory. Add the following code to the script file
#!/usr/bin/env python

import rospy
from nav_msgs.msg import Odometry

def callback(msg):
    print(msg.pose.pose)
    
rospy.init_node('check_odometry')
odom_sub = rospy.Subscriber('/odom', Odometry, callback)
rospy.spin()
  • We will now create a launch file with name inside the check_odometry/launch/ directory with the following content
<launch>
    <node pkg="check_odometry" type="check_odom.py" name="check_odometry" output="screen" />
</launch>
  • Lets launch this launch file with the following command in SHELL

$ roslaunch check_odometry check_odom.launch

The above command should start publishing a stream of Odometry messages (the pose part) as shown in this image

  • Now let us move the robot slightly using the keyboard_teleop and observe the /odom message

$ roslaunch turtlebot_teleop keyboard_teleop.launch

  • Now we can start the launch file again see the change in the location of the robot

This finishes the steps in the Video tutorial.

Topics:
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

2 Comments

  1. Kumar

    Greetings,

    fantastic tutorial for a newbie like me. I understood the script very well even though I’m new to Python and ROS. I do have a question though. What are those Linear and Angular values for twist output? Are they velocities? How can one get time also printed at the same time. That way one can plot the robot’s position with respect to time. Thanks in advance for your reply.

    Reply

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This