Main Content

Test Robot Autonomy in Simulation

This example explores MATLAB® control of the Gazebo® Simulator.

When using robot simulators, it is important to test autonomous algorithms and dynamically alter the surroundings in the world while the simulation is running. This example shows how to create basic robot autonomy with Gazebo and how to interact with it. In this example the robot is the TurtleBot® platform. For specific examples involving the TurtleBot, see the Communicate with the TurtleBot example.

In this example, you use a timer to control the autonomous aspects of TurtleBot movement. Timers allow processes to run in the background in regular execution intervals without blocking the MATLAB® command line. While you can use loops and other methods to examine basic autonomy, the scheduled execution and non-blocking nature of timers make them the best choice for achieving autonomous behavior.

Prerequisites: Get Started with Gazebo and Simulated TurtleBot, Add, Build, and Remove Objects in Gazebo, Apply Forces and Torques in Gazebo

Connect to Gazebo

On your Linux® machine, start Gazebo. If you are using the virtual machine from Get Started with Gazebo and Simulated TurtleBot, use the Gazebo Empty world.

Initialize ROS by replacing the sample IP address with the IP address of the virtual machine. Create an instance of the ExampleHelperGazeboCommunicator class.

rosinit("http://192.168.178.133:11311")
Initializing global node /matlab_global_node_07643 with NodeURI http://192.168.178.1:57635/
gazebo = ExampleHelperGazeboCommunicator;

Build a wall in the world.

 wall = ExampleHelperGazeboModel("grey_wall","gazeboDB");
 spawnModel(gazebo,wall,[-2 4 0]);

All units in Gazebo are specified using SI convention.

Create a ExampleHelperGazeboSpawnedModel object for the mobile base and change its orientation state. Manually rotate the TurtleBot by 90 degrees (π/2 radians) so that it is directly facing the wall.

 turtleBot = ExampleHelperGazeboSpawnedModel("turtlebot3_burger",gazebo);
 setState(turtleBot,"orientation",[0 0 pi/2]);

Start TurtleBot Obstacle Avoidance

This section describes a simple way to create autonomous behavior on a TurtleBot in Gazebo. Use a basic obstacle avoidance behavior for the TurtleBot. The behavior is to drive forward and turn when the robot gets very close to an obstacle detected by the laser scanner.

Create global variables for the publisher and publisher message so they can be accessed by the control algorithm.

 global robot
 global velmsg

Create the publisher for velocity and the ROS message to carry the information.

 robot = rospublisher("/cmd_vel","DataFormat","struct");
 velmsg = rosmessage(robot);

Subscribe to the laser scan topic.

 timerHandles.sub = rossubscriber("/scan","DataFormat","struct");

Create a timer to control the main control loop of the TurtleBot.

 t = timer("TimerFcn",{@exampleHelperGazeboAvoidanceTimer,timerHandles},"Period",0.1,"ExecutionMode","fixedSpacing");

The timer callback function, exampleHelperGazeboAvoidanceTimer defines the laser scan callback function and executes a basic algorithm to allow the TurtleBot to avoid hitting objects as it moves.

Start the timer.

start(t)

The TurtleBot drives toward the wall. Once it gets very close to the wall, it must turn left to avoid running into it.

Note: If the TurtleBot crashes into the wall, the laser scan is probably not being published through Gazebo. Restart your Gazebo session and try again.

Add Objects

You can still make changes to the world while the TurtleBot is moving. Add a new wall to the world. If you add it soon enough, it can block the TurtleBot so that it avoids hitting the wall.

spawnModel(gazebo,wall,[-5.85 0.15 0],[0, 0, pi/2]);
pause(20)     % TurtleBot avoids walls for 20 seconds

Remove Models and Shut Down

Stop the timer to halt the robot algorithm.

stop(t)
delete(t)

Find all objects in the world and remove the ones added manually.

list = getSpawnedModels(gazebo)
list = 4×1 cell
    {'ground_plane'     }
    {'turtlebot3_burger'}
    {'grey_wall'        }
    {'grey_wall_0'      }

Remove the two walls, using the following commands:

removeModel(gazebo,"grey_wall");
removeModel(gazebo,"grey_wall_0");

Clear the workspace of publishers, subscribers, and other ROS-related objects when you are finished with them.

clear

Use rosshutdown once you are done working with the ROS network. Shut down the global node and disconnect from Gazebo.

rosshutdown
Shutting down global node /matlab_global_node_07643 with NodeURI http://192.168.178.1:57635/

When finished, close the Gazebo window on your virtual machine.