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

Revision history [back]

Quoting @ahendrix's comment: "You've got the right idea. Integrating the velocity command is the correct way to simulate motion over time and produce odometry messages." Please, note that I have emphasised the word time.

To properly integrate your fake odometry you need to keep track of time. I'll try to explain clearly as possible. In your code, you store the velocities that you receive on the /cmd_vel topic (which are in meters per second for linear velocities and radians per second for angular velocities) and then add them to your position variables.

Now, lets consider a simplified example case for a robot that only moves along the x axis : Imagine that you start your node and you receive a value of vel_x = 3 (this means that the robot should travel at 3 meters per second on the x direction). For every iteration of your main loop you will be adding a value of 3 to the variable pos_x. So, here you are assuming that the robot moved 3 meters since the last time that you updated pos_x. Which would be true if the main loop took exactly one second to execute. Do you see where I am going?

Usually your main loop would take a very short time to execute, lets say for example 0.01 seconds. So, having a robot traveling at 3 m/s... how many meters will it move in 0.01 seconds? Easy! 3 m/s * 0.01s = 0.03m!! Therefore, in this example, we would have to add 0.03 to pos_x to have the proper odometry value.

Also, as @ahendrix pointed out, you are forgetting to deal with the rotation in your code (which requires a bit of math, but it is really simple). Here you can find some example code of how to calculate properly a fake odometry, mix that with your code and you will be calculating a fake odometry like a pro!