MATLAB Examples

Test Robot Autonomy in Simulation

Contents

Introduction

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 docid:robotics_examples.example-TurtleBotCommunicationExample 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: docid:robotics_examples.example-GettingStartedWithGazeboExample, docid:robotics_examples.example-GazeboModelAndSimulationPropertiesExample, docid:robotics_examples.example-GazeboAddBuildAndRemoveObjectsExample, docid:robotics_examples.example-GazeboApplyForcesExample

Connect to Gazebo

  • On your Linux® machine, start Gazebo. If you are using the virtual machine from docid:robotics_examples.example-GettingStartedWithGazeboExample, use the Gazebo Empty world.
  • Initialize ROS by replacing the sample IP address (192.168.1.1) with the IP address of the virtual machine. Create an instance of the ExampleHelperGazeboCommunicator class.
ipaddress = '192.168.1.1'
 rosinit(ipaddress)
 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 (pi/2 radians) so that it is directly facing the wall.
 kobuki = ExampleHelperGazeboSpawnedModel('mobile_base',gazebo)
 setState(kobuki,'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('/mobile_base/commands/velocity')
 velmsg = rosmessage(robot)

The output from the publishers will look like this:

  • Subscribe to the laser scan topic.
 timerHandles.sub = rossubscriber('/scan')
  • Create a timer to control the main control loop of the TurtleBot.
 t = timer('TimerFcn',{@exampleHelperGazeboAvoidanceTimer,timerHandles},'Period',0.1,'ExecutionMode','fixedSpacing');

In the callback function for the timer, exampleHelperGazeboAvoidanceTimer defines the laser scan callback function and executes a basic algorithm to allow the TurtleBot to avoid hitting objects as it moves. The file is located in the +gazebo directory.

  • Start the timer with the following line:
 start(t);

Add Objects

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.

  • 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

You see something like this graphic:

Remove Models and Shut Down

  • Stop the timer to halt the robot algorithm.
stop(t)
  • Find all objects in the world and remove the ones added manually
list = getSpawnedModels(gazebo)

  • Remove the two walls, using the following commands:
removeModel(gazebo,'grey_wall');
removeModel(gazebo,'grey_wall_0');

It is good practice to clear the workspace of publishers, subscribers, and other ROS-related objects when you are finished with them

clear
  • It is recommended to use rosshutdown once you are done working with the ROS network. Shut down the global node and disconnect from Gazebo.
rosshutdown
  • When finished, close the Gazebo window on your virtual machine