Test Robot Autonomy in Simulation

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 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.

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.

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.