Plot TurtleBot Odometry

This example shows how to get, store, and display odometry data from a TurtleBot® as it drives in an environment.

Connect to Robot

Connect to your TurtleBot robot using its specific IP address. Your simulated or real TurtleBot must be on the same ROS network as the computer running MATLAB®.

ipaddress = '192.168.1.10'; % IP address of your robot
tb = turtlebot(ipaddress);

Get Odometry Data

Use getOdometry to get a single odometry point off the TurtleBot. The function returns the position and orientation as [x y z] coordinates and [yaw pitch roll] angles.

odom = getOdometry(tb)
       Position: [0.2951 -6.7229e-04 0]
    Orientation: [-0.2145 0 0]

Send velocity commands to change the position of the robot. To note the change in position, reset the odometry first. Use setVelocity to drive the robot forward for 2 seconds and wait for the robot to execute this command.

resetOdometry(tb)
setVelocity(tb,0.5,'Time',2)

Get the odometry after the velocity command is sent. The x-coordinate of odom.Position reflects the change in position. A velocity of 0.5 m/s for 2 seconds yields a displacement of 1m in the x direction as expected.

odom = getOdometry(tb)
odom = 

       Position: [1.0002 -0.0058 0]
    Orientation: [-0.0192 0 0]

Plot Odometry Data Points

You can store odometry data to get a full trajectory of a robot path as it navigates through its environment.

Start a loop to store the odometry position and then send a velocity command. This loop runs 20 times to create a basic two-turn trajectory. Plot the trajectory.

odomList = zeros(20,2);
resetOdometry(tb)

for i = 1:20
    odom = getOdometry(tb);
    odomList(i,:) = [odom.Position(1) odom.Position(2)];
    
    if i < 10
        setVelocity(tb,0.25,0.15)
    else
        setVelocity(tb,0.25,-0.15)
    end
    pause(1);
end

plot(odomList(:,1),odomList(:,2))

See Also

| |

Related Topics