This example introduces the TurtleBot® platform and the ways in which MATLAB® users can interact with it. Specifically, the code in this example demonstrates how to publish messages to the TurtleBot (such as velocities) and how to subscribe to topics that the TurtleBot publishes (such as odometry).
The TurtleBot must be running for this example to work.
This example gives an overview of working with a TurtleBot using its native ROS interface. The Robotics System Toolbox™ Support Package for TurtleBot®-Based Robots provides a more streamlined interface to TurtleBot. It allows you to:
Acquire sensor data and send control commands without explicitly calling ROS commands
Communicate transparently with a simulated robot in Gazebo or with a physical TurtleBot
To install the support package, open Add-Ons > Get Hardware Support Packages on the MATLAB Home tab and select "TurtleBot-Based Robots". Alternatively, use the
The TurtleBot must be running. If you are using a real TurtleBot and followed the hardware setup steps in Get Started with a Real TurtleBot, the robot is running. If you are using a TurtleBot in simulation and followed the setup steps in Get Started with Gazebo and a Simulated TurtleBot, launch one of the Gazebo® worlds from the desktop (
Gazebo TurtleBot World, for instance).
In your MATLAB instance on the host computer, run the following command. Replace
ipaddress with the IP address of the TurtleBot. This line initializes ROS and connects to the TurtleBot.
ipaddress = '192.168.203.129'; rosinit(ipaddress)
Initializing global node /matlab_global_node_43053 with NodeURI http://192.168.203.1:55724/
If the network you are using to connect to the TurtleBot is not your default network adapter, you can manually specify the IP address of the adapter that is used to connect to the robot. This might happen if you use a Wireless network, but also have an active Ethernet connection. Replace IP_OF_TURTLEBOT with the IP address of the TurtleBot and IP_OF_HOST_COMPUTER with the IP address of the host adapter that is used to connect to the robot:
Display all the available ROS topics by entering the following command:
If you do not see any topics, then the network has not been set up properly. Refer to the beginning of this document for network setup steps.
You can control the movement of the TurtleBot by publishing a message to the
/mobile_base/commands/velocity topic. The message has to be of type
geometry_msgs/Twist and contains data specifying desired linear and angular velocities. The TurtleBot's movements can be controlled through two different values: the linear velocity along the X-axis controls forward and backward motion and the angular velocity around the Z-axis controls the rotation speed of the robot base.
Set a variable
velocity to use for a brief TurtleBot movement.
velocity = 0.1; % meters per second
Create a publisher for the
/mobile_base/commands/velocity topic and the corresponding message containing the velocity values.
robot = rospublisher('/mobile_base/commands/velocity') ; velmsg = rosmessage(robot);
Set the forward velocity (along the X-axis) of the robot based on the
velocity variable and publish the command to the robot. The TurtleBot will move forward a small distance and then come to a stop.
velmsg.Linear.X = velocity; send(robot,velmsg);
For safety reasons, the TurtleBot will only keep moving, if it continuously receives velocity data on the
To view the type of the message published by the velocity topic, execute the following:
rostopic type /mobile_base/commands/velocity
The topic expects messages of type
geometry_msgs/Twist, which is exactly the type of the
velmsg that you created above.
To view which nodes are publishing and subscribing to a given topic, use the command:
info TOPICNAME. The following command lists the publishers and subscribers for the velocity topic. MATLAB is listed as one of the publishers.
rostopic info /mobile_base/commands/velocity
Type: geometry_msgs/Twist Publishers: * /matlab_global_node_43053 (http://192.168.203.1:55724/) * /mobile_base_nodelet_manager (http://192.168.203.129:45425/) Subscribers: * /gazebo (http://192.168.203.129:40018/)
OPTIONAL: If you are using the real TurtleBot, you can send sound commands to it.
soundpub = rospublisher('/mobile_base/commands/sound', 'kobuki_msgs/Sound') soundmsg = rosmessage('kobuki_msgs/Sound'); soundmsg.Value = 6; % Any number 0-6 send(soundpub,soundmsg);
The TurtleBot uses the
/odom topic to publish its current position and orientation (collectively denoted as pose). Since the TurtleBot is not equipped with a GPS system, the pose will be relative to the pose that the robot had when it was first turned on.
Create a subscriber for the odometry messages
odom = rossubscriber('/odom');
Wait for the subscriber to return data, then extract the data and assign it to variables x, y, and z:
odomdata = receive(odom,3); pose = odomdata.Pose.Pose; x = pose.Position.X; y = pose.Position.Y; z = pose.Position.Z;
Note: If you see an error, then it is likely that the
receive command timed out. Make sure that odometry is being published and that your network is set up properly.
Display the x, y, and z values
ans = 1×3 1.8626 -0.0309 0
The orientation of the TurtleBot is stored as a quaternion in the
pose.Orientation variable. Use
quat2eul to convert into the more convenient representation of Euler angles. To display the current orientation,
theta, of the robot in degrees, execute the following lines.
quat = pose.Orientation; angles = quat2eul([quat.W quat.X quat.Y quat.Z]); theta = rad2deg(angles(1))
theta = 4.4540
Make sure that your Kinect® camera is running. If you list the topics again with
list, you can see that many topics beginning with
/camera are listed. With real TurtleBot hardware, you can find the following topic:
Subscribe to the compressed image topic.
if ismember('/camera/rgb/image_color/compressed', rostopic('list')) imsub = rossubscriber('/camera/rgb/image_color/compressed'); end
If you are using Gazebo, the topic list is different. Use the following topic instead:
Subscribe to the image raw topic.
if ismember('/camera/rgb/image_raw', rostopic('list')) imsub = rossubscriber('/camera/rgb/image_raw'); end
After subscribing to an image topic, wait for the data and then display it with
img = receive(imsub); figure imshow(readImage(img));
An example of a real world image from the Kinect camera looks like this:
To display a continuously updating image from the Kinect camera, use the following while loop:
tic; while toc < 20 img = receive(imsub); imshow(readImage(img)) end
Clear the workspace of publishers, subscribers, and other ROS-related objects when you are finished with them:
rosshutdown once you are done working with the ROS network. Shut down the global node and disconnect from the TurtleBot.
Shutting down global node /matlab_global_node_43053 with NodeURI http://192.168.203.1:55724/
Refer to the next example: Explore Basic Behavior of the TurtleBot