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 a Simulated TurtleBot, and launch the
Gazebo TurtleBot World to access a simulated TurtleBot.
Connect to the robot by replacing the sample IP address (192.168.1.1) with the IP address of the TurtleBot.
ipaddress = '192.168.1.1'
tbot = turtlebot(ipaddress)
This call creates a
turtlebot object that can be used to retrieve sensor data and for controlling the robot's motion.
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.
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
tbot.ColorImage.Active = true;
The same behavior applies to all functions starting with
get. Each capability property has an associated
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);
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.
Now wait until the robot finishes moving and then get a new odometry reading.
pause(1) pose = getOdometry(tbot)
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)
You will 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.
Move the robot in a circular motion for 5 seconds.
setVelocity(tbot, 0.3, 0.6, 'Time', 10);
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.
[pcloud, pcloudmsg] = getPointCloud(tbot); figure; scatter3(pcloudmsg)
The second return argument of the
getPointCloud function 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.