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
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.
ipaddress = '192.168.192.130'; % IP address of your robot tbot = turtlebot(ipaddress,11311); tbot.Velocity.TopicName = '/cmd_vel';
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 a Simulated TurtleBot.
img = getColorImage(tbot); imshow(img)
Create a rateControl object to control the execution rate of code. The desired rate is 2 Hz.
desiredRate = 2; rate = rateControl(desiredRate);
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
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
tbot.ColorImage.Active = false;