ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How do I publish gazebo position of a robot model on odometry topic.

asked 2015-12-06 01:21:42 -0500

cybodroid gravatar image

updated 2018-06-11 17:25:35 -0500

Hello, I have a robot model that runs using teleop key, and I want to publish its position and velocity on odometry topic. How should I achieve this?


edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2017-10-09 03:56:25 -0500

You can use the gazebo_ros_p3d plugin in your robot's URDF to directly publish (ground truth) odometry for your robot. Here's a usage example: tracker_chassis.gazebo.xacro.xml

For your use case (publishing directly to odom) it would probably look like this:

<plugin name="p3d_base_controller" filename="">
  <xyzOffsets>0 0 0</xyzOffsets>
  <rpyOffsets>0 0 0</rpyOffsets>
edit flag offensive delete link more


Thanks, it worked for me. Now, I am able to get position data of my robot from the gazebo. Thanks a lot.

Ankita gravatar image Ankita  ( 2021-07-24 14:37:59 -0500 )edit

answered 2017-07-01 20:16:05 -0500

R. Tellez gravatar image

updated 2017-10-09 03:29:01 -0500

You can do that by doing two options:

  1. You compute the odometry based on the movement of the robot and odometry equations
  2. You get the odometry directly asking Gazebo how much your robot moved

Once you have that data, you create a topic with Odometry msg type and publish the data obtained from the call.

Since you are not providing any information about your robot, we cannot explain here option 1. Let's explain option 2

In order to get how much the robot moved you should call the /gazebo/get_model_state service provided by Gazebo. That service will provide you with the current position of the robot in the world. In order to call it, do the following:

rosservice call /gazebo/get_model_state "model_name: 'the_name_of_your_robot' relative_entity_name: ' ' "

Just calling that service you will get the current position of the robot in Gazebo. However, you must do that call inside a node to get the position data and publish it later in an odometry topic. You can do all that with the following code:

#! /usr/bin/env python
from nav_msgs.msg import Odometry
from std_msgs.msg import Header
from gazebo_msgs.srv import GetModelState, GetModelStateRequest


odom_pub=rospy.Publisher ('/my_odom', odometry)

rospy.wait_for_service ('/gazebo/get_model_state')
get_model_srv = rospy.ServiceProxy('/gazebo/get_model_state', GetModelstate)

header = Header()

model = GetModelStateRequest()

r = rospy.Rate(2)

while not rospy.is_shutdown():
    result = get_model_srv(model)

    odom.pose.pose = result.pose
    odom.twist.twist = result.twist

    header.stamp =
    odom.header = header

    odom_pub.publish (odom)


That's it! That Python code will do exactly that: get the robot position from the simulator, and then publish it in an odometry topic.

Here you can find a video ( ) that shows how to do all the explained above with a full example for a kobuki robot.

edit flag offensive delete link more



This answer would be greatly improved by adding the instructions given in the video. Links can and do change/go down, and if this were to happen then this accepted answer would no longer useful.

jayess gravatar image jayess  ( 2017-07-26 00:13:26 -0500 )edit

Answer edited and corrected as requested. Cheers

R. Tellez gravatar image R. Tellez  ( 2017-10-09 03:30:10 -0500 )edit

answered 2021-07-05 18:41:12 -0500

ahmedelhaddad2012 gravatar image

Hi sir just a correction for the above code its missing import rospy and in 8th line its Odometry

Now my question is how can i create a subscriber for this because i tried following the ros tutorials and didnt work for me.

edit flag offensive delete link more

Question Tools



Asked: 2015-12-06 01:21:42 -0500

Seen: 13,585 times

Last updated: Jul 05 '21