Turtlebotrobot Ug
Turtlebotrobot Ug
R2023b
How to Contact MathWorks
Phone: 508-647-7000
Robot Applications
2
Get Image Data from TurtleBot at a Fixed Rate . . . . . . . . . . . . . . . . . . . . . 2-2
iii
1
This example demonstrates how to interface with the TurtleBot® robot. You can acquire sensor data
from the robot's sensors and send commands to control the robot's motion.
All functionality works either with a simulated robot in Gazebo or with a physical TurtleBot.
• If you are using a real TurtleBot, follow the hardware setup steps in “Get Started with a Real
TurtleBot”.
• Alternatively, you can download a virtual machine image that already has ROS and Gazebo
installed. This virtual machine is based on Ubuntu® Linux® and is pre-configured to support this
example. Follow the setup steps in “Get Started with Gazebo and Simulated TurtleBot”, and
launch the Gazebo Office world to access a simulated TurtleBot.
Connect to TurtleBot
Connect to the robot by replacing the sample IP address with the IP address of the TurtleBot. This
creates a turtlebot object that can be used to retrieve sensor data to control the robot's motion.
ipaddress = '192.168.192.132';
tbot = turtlebot(ipaddress)
tbot =
turtlebot with properties:
tbot.Velocity.TopicName = '/cmd_vel';
Get the position and orientation of the robot from its odometry sensor.
pose = getOdometry(tbot)
Every TurtleBot sensor or control command is represented by a capability property in the turtlebot
class. For example, the Odometry property contains information about the robot's odometry sensor.
1-2
Get Started with the TurtleBot Support Package
tbot.Odometry
Each capability property has two fields. The TopicName field shows the ROS topic used to receive
sensor data or send control commands. The Active field allows you to explicitly enable or disable the
data stream.
Explicitly enable the color image subscriber. Note that for convenience, the color image subscriber is
enabled automatically when you first call getColorImage.
tbot.ColorImage.Active = true;
The same behavior applies to all functions starting with get. Each capability property has an
associated getPROP or setPROP function, where PROP refers to the property name.
Get a color image from the TurtleBot camera and display it. The getColorImage function waits for
up to 5 seconds for a new image.
[colorImg] = getColorImage(tbot);
figure
imshow(colorImg)
1-3
1 ROS Toolbox Support Package for TurtleBot-Based Robots Examples
You can change the maximum time that the function should wait for a color image by specifying an
optional timeout value (in seconds). Wait for up to 10 seconds for a new image.
colorImg = getColorImage(tbot, 20);
Disable the color image subscriber. This can be useful if you want to save network bandwidth or
process sensor data offline.
tbot.ColorImage.Active = false;
You can also send commands to control the robot's motion. Move the robot forward at a speed of 0.3
meters per second.
setVelocity(tbot, 0.3);
Now wait until the robot finishes moving and then get a new odometry reading.
pause(1)
pose = getOdometry(tbot)
1-4
Get Started with the TurtleBot Support Package
You can reset the odometry and then try the movement again. resetOdometry sets the robot
position and robot orientation to [0, 0, 0] respectively.
When you move the robot forward, you can expect only the X position value to change, whereas the Y
and Z values should be close to 0.
resetOdometry(tbot)
setVelocity(tbot, 0.3);
pause(1)
pose = getOdometry(tbot)
Notice that setVelocity only sends a single command to the robot and it stops moving fairly
quickly. To command the TurtleBot to continue a motion for a specified time period, you can use an
alternative syntax.
At the end of the elapsed time, a velocity of 0 is sent to the robot. At the end of the elapsed time, a
velocity of 0 is sent to the robot. Get a point cloud from the robot's depth sensor to verify where the
robot is.
1-5
1 ROS Toolbox Support Package for TurtleBot-Based Robots Examples
The second output argument pcloudmsg is optional and gives you access to the received ROS
message. See “Work with Specialized ROS Messages” for more information. All other functions
starting with get also return the ROS message as a second, optional output.
Disconnect from the robot by clearing the tbot variable from the workspace.
clear tbot
1-6
2
Robot Applications
This example shows how to get image data from the TurtleBot and display it at a fixed rate. ROS
Toolbox Support Package for TurtleBot-Based Robots enables you to connect to TurtleBot
hardware and get the color, grayscale, and depth images from it. In this example, use the
getColorImage function to get color images off the TurtleBot to display them. To collect images at a
fixed rate, use rateControl.
Connect to TurtleBot
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™. Specify the command velocity
topic name.
Get the latest image from the TurtleBot and show it in a figure. This TurtleBot is simulated in Gazebo.
For more information on the Gazebo simulator, see “Get Started with Gazebo and Simulated
TurtleBot”.
img = getColorImage(tbot);
imshow(img)
2-2
Get Image Data from TurtleBot at a Fixed Rate
Create a rateControl object to control the execution rate of code. The desired rate is 2 Hz.
desiredRate = 2;
rate = rateControl(desiredRate);
Reset the rateControl object and start a loop to drive the robot in a circle for 10 seconds and
capture images. In the loop, use setVelocity to command a linear and angular velocity of 0.1 m/s
and 0.5 rad/s respectively. Then capture the latest image and display it. Wait for the desired rate at
the end of each loop. The image shown is the final image after the 10 turns.
reset(rate)
for i = 1:20
setVelocity(tbot,0.01,0.5)
img = getColorImage(tbot,0);
imshow(img)
waitfor(rate);
end
2-3
2 Robot Applications
To save bandwidth on the network, disable the image subscriber now that it is no longer in use. On
the ColorImage property of the robot, set Active to false. You can also disconnect from the robot
by calling clear tbot.
tbot.ColorImage.Active = false;
See Also
turtlebot | getColorImage | setVelocity | rateControl
Related Examples
• “Plot Turtlebot Odometry” on page 2-5
• “Track and Follow an Object Using a TurtleBot” on page 2-11
• “Execute Code at a Fixed-Rate” (Robotics System Toolbox)
2-4
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 TurtleBot
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(tbot)
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(tbot)
setVelocity(tbot,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(tbot)
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(tbot)
for i = 1:20
odom = getOdometry(tbot);
2-5
2 Robot Applications
if i < 10
setVelocity(tbot,0.25,0.15)
else
setVelocity(tbot,0.25,-0.15)
end
pause(1);
end
plot(odomList(:,1),odomList(:,2))
See Also
turtlebot | getOdometry | setVelocity
Related Examples
• “Get Image Data from TurtleBot at a Fixed Rate” on page 2-2
• “Create a UI for TurtleBot” on page 2-7
2-6
Create a UI for TurtleBot
This example shows how to create a UI for collecting sensor data and controlling a TurtleBot® robot.
You can use the UI to get and display odometry data and laser scan readings and to teleoperate the
robot by sending it velocity commands.
For this example, specify all code and dependent callback functions in a single MATLAB® function
file.
Create a function that sets up the UI for controlling the robot. Specify the dimensions of the buttons
and the callbacks associated with each button.
function turtlebot_UI
f = figure('Visible','off','Position',[50,50,900,570]);
Add the components for the different buttons and for the axes used to plot data. It is important to
specify the 'Callback' name-value pair with the correct function handles. These functions are
specified at the end.
hforward = uicontrol('Style','pushbutton',...
'String','Forward','Position',[770,440,70,25],...
'Callback',@forwardCB);
hleft = uicontrol('Style','pushbutton',...
'String','Left','Position',[730,400,70,25],...
'Callback',@leftCB);
hright = uicontrol('Style','pushbutton',...
'String','Right','Position',[815,400,70,25],...
'Callback',@rightCB);
hback = uicontrol('Style','pushbutton',...
'String','Back','Position',[770,360,70,25],...
'Callback',@backCB);
hgetOdom = uicontrol('Style','pushbutton',...
'String','Get Odom','Position',[730,320,70,25],...
'Callback',@getOdomCB);
hreset = uicontrol('Style','pushbutton',...
'String','Reset','Position',[815,320,70,25],...
'Callback',@resetCB);
hgetScan = uicontrol('Style','pushbutton',...
'String','Get Scan','Position',[730,280,70,25],...
'Callback',@getScanCB);
hclear = uicontrol('Style','pushbutton',...
'String','Clear','Position',[815,280,70,25],...
2-7
2 Robot Applications
'Callback',@clearCB);
ha = axes('Units','pixels','Position',[50,60,600,400]);
Show the UI after adding all the components by setting Visible to 'on'.
% Make UI visible
f.Visible = 'on';
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.
Initialize the variables needed for sensor data. Reset the odometry of the TurtleBot.
odomList = [];
scanPts = [];
resetOdometry(tbot);
Each UI button has a callback function that activates when the button is pressed. You specify these
functions inside your turtlebot_UI function file.
function forwardCB(source,eventdata)
setVelocity(tbot,0.25,0)
end
function leftCB(source,eventdata)
setVelocity(tbot,0,0.25)
end
function rightCB(source,eventdata)
setVelocity(tbot,0,-0.25)
end
function backCB(source,eventdata)
setVelocity(tbot,-0.25,0)
end
The inputs to setVelocity are the interface object, turtlebot, and the linear and angular
velocities.
Create callbacks to get and clear odometry and laser scan data. plotOdom and plotScan are
convenience functions for plotting odometry and laser scan to the current axes.
function getOdomCB(source,eventdata)
plotOdom
end
function resetCB(source,eventdata)
resetOdometry(tbot);
odomList = [];
2-8
Create a UI for TurtleBot
plotOdom
end
function getScanCB(source,eventdata)
plotOdom
hold on
plotScan
hold off
end
function clearCB(source,eventdata)
scanPts = [];
plotOdom
end
Specify the plotOdom and plotScan functions, which plot the sensor data on the current axes in the
UI.
function plotOdom
% Gets and plots current and saved odometry
odom = getOdometry(tbot);
odomList = [odomList; odom.Position(1) odom.Position(2)];
plot(odomList(:,1),odomList(:,2),'b')
hold on
quiver(odom.Position(1),odom.Position(2),...
cos(odom.Orientation(1)),sin(odom.Orientation(1)),'r',...
'LineWidth',2)
hold off
xlim([-5,5])
ylim([-5,5])
grid on
end
function plotScan
% Plots laser scan data on top of current odometry
[scan,scanMsg] = getLaserScan(tbot);
odom = getOdometry(tbot);
cart = readCartesian(scanMsg);
scanPts = [cart(:,1)+odom.Position(1) cart(:,2)+odom.Position(2)];
yaw = odom.Orientation(1);
R = [cos(yaw) -sin(yaw); sin(yaw) cos(yaw)];
scanPts = (R*scanPts')';
plot(scanPts(:,1),scanPts(:,2),'k')
xlim([-5,5])
ylim([-5,5])
grid on
end
End the turtlebot_UI function. Make sure that all the example code is between this end and the
function header.
end
Call turtlebot_UI to run the function. The UI is created and connects to the robot. Set up your
robot before running the UI.
turtlebot_UI
2-9
2 Robot Applications
Use the buttons to move the robot. Click Get Odom to plot a new odometry point (blue) or Get Scan
to plot the current laser scan data (black).
See Also
turtlebot | getOdometry | getLaserScan | setVelocity
Related Examples
• “Get Image Data from TurtleBot at a Fixed Rate” on page 2-2
• “Track and Follow an Object”
2-10
Track and Follow an Object Using a TurtleBot
This example shows how to track an object based on color using a TurtleBot® robot connected via a
ROS network. ROS Toolbox Support Package for TurtleBot-Based Robots enables you to capture
images to find an object in the environment and send velocity commands to navigate toward the
object. To follow the object, you use the getColorImage and setVelocity functions along with
image processing techniques.
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®.
Track Object
To detect the example object, a blue ball, you must specify some of its properties. You use these
properties to process the images you capture. This example uses Image Processing Toolbox™ to run
color and blob detection algorithms on each image to find the ball.
Specify the properties of the ball shown, substituting in values that are tuned for your environment.
The values given are optimal for the Gazebo simulation detailed in “Get Started with Gazebo and
Simulated TurtleBot”.
latestImg = getColorImage(tbot);
Use these two example helper functions to get and plot the location of the ball in the image. These
functions use regionprops from Image Processing Toolbox to get the location and size of the ball.
The first image is the output from the TurtleBot with the object location overlaid on it. The second
image is the binary image, ballImage, for detecting blue in the image based on blueBallParams.
If the ball does not show up as white with limited background in this binary image, try tuning the
blueBallParams values.
[position,~,ballImage] = exampleHelperTurtleBotFindBlueBall(latestImg,blueBallParams,false);
exampleHelperTurtleBotPlotObject(latestImg,ballImage,position);
2-11
2 Robot Applications
2-12
Track and Follow an Object Using a TurtleBot
After you tune the tracking of the object in the image, you can set up a basic control algorithm for
sending commands to the TurtleBot. This example uses basic logic with singular linear and angular
velocity commands to get the robot to follow the ball. For a more advanced control scheme, see
“Track and Follow an Object”.
Get a new image and store the image size. Recalculate the position and size of the object.
latestImg = getColorImage(tbot);
[height,width] = size(latestImg);
[position,~] = exampleHelperTurtleBotFindBlueBall(latestImg,blueBallParams,false);
Generate angular and linear velocities based on the position and size of the ball. If no object is found,
spin and search for the ball (positive angular velocity only).
• If the ball is on the far left side of the screen, the robot needs to turn left (positive angular
velocity). The same is true for the right side (negative angular velocity).
• If the ball is too small in the image, the robot must move closer (positive linear velocity). If the ball
is too large, the robot must move farther away (negative linear velocity).
Finally, send the linear and angular velocities using setVelocity and pause to wait for the
command to send. Iterate for 100 cycles to test tracking.
% Parameters for ball position and size
horizontalTolerance = 20;
sizeGoal = 70;
sizeTolerance = 5;
for i = 1:100
% Get latest image, ball postion, and ball size.
latestImg = getColorImage(tbot);
[height,width] = size(latestImg);
[position,ballSize] = exampleHelperTurtleBotFindBlueBall(latestImg,blueBallParams,false);
2-13
2 Robot Applications
To verify the algorithm is successful, capture an image and use the help functions to track and plot
the position once more. The TurtleBot focuses on the ball, assuming it found the ball during its
search. Based on the Gazebo simulated world in this example, a final image capture shows the ball
right in front of the robot.
latestImg = getColorImage(tbot);
[c,ballSize,ballImage] = exampleHelperTurtleBotFindBlueBall(latestImg,blueBallParams,false);
exampleHelperTurtleBotPlotObject(latestImg,ballImage,c);
2-14
Track and Follow an Object Using a TurtleBot
Improvement Suggestions
This example uses simple sensor feedback and control parameters for tracking and following an
object. It works well in a simulated environment due to its low level of risk to hardware and real
environments. However, it does not factor in the bump or clip sensors, which can help with collisions
or terrain changes. Dynamically adjusting the velocity commands and factoring in previous states can
improve the movements of the TurtleBot.
To improve this example for real-world applications, consider using more advanced methods of
sensing and control for real robots that have more robust handling of sensor data. See “Track and
Follow an Object” for a more advanced example that uses a PID controller and other robot sensors.
See Also
turtlebot | getColorImage | setVelocity
Related Examples
• “Get Image Data from TurtleBot at a Fixed Rate” on page 2-2
• “Track and Follow an Object”
2-15