MATLAB Examples

Connect to a ROS-enabled Robot from Simulink®

This example shows you how to configure a Simulink model to send and receive information from a separate ROS-based simulator such as Gazebo®.

Contents

Introduction

You can use Simulink to connect to a ROS-enabled physical robot or to a ROS-enabled robot simulator such as Gazebo. In this example, you will learn how to:

  • Configure Simulink to connect to a separate robot simulator using ROS
  • Send velocity commands to the simulated robot
  • Receive position information from the simulated robot

Here is the model you will be creating in this example.

Task 1 - Start a Robot Simulator

In this task, you will start a ROS-based simulator for a differential-drive robot. The simulator receives and sends messages on the following topics:

  • It receives velocity commands, as messages of type "geometry_msgs/Twist", on the "/mobile_base/commands/velocity" topic
  • It sends odometry information, as messages of type "nav_msgs/Odometry", to the "/odom" topic

You can choose one of two options for setting up the ROS-based simulator.

Option A : Simulator in MATLAB®

This option uses a simple MATLAB-based simulator that plots the current location of the robot in a separate figure window.

  • Type rosinit at the MATLAB command line. This creates a local ROS master with network address (URI) of http://localhost:11311.
  • Type ExampleHelperSimulinkRobotROS to start the Robot Simulator. This will open a figure window:

Option B : Gazebo Simulator

This option uses a simulated TurtleBot® in Gazebo.

  • See docid:robotics_examples.example-GettingStartedWithGazeboExample for instructions on setting up the Gazebo environment. In the Ubuntu® desktop in the virtual machine, click on the "Gazebo Empty" icon.
  • Note the network address (URI) of the ROS master. It will look like http://192.168.84.128:11311.
  • Verify that the Gazebo environment is properly set up by typing the rostopic list in the Ubuntu terminal window. You should see a list of topics, including "/mobile_base/commands/velocity" and "/odom".

Task 2 - Configure Simulink to Connect to the ROS Network

1. From the model menu, choose Tools > Robot Operating System > Configure Network Addresses.

2. In the "ROS Master" section, select "Custom" from the Network Address dropdown.

  • If you chose Option A (MATLAB Simulator), ensure that the Hostname/IP Address is set to "localhost", and Port is set to "11311".
  • If you chose Option B (Gazebo Simulator), specify the IP address and port number of the ROS master in Gazebo. For example, if it is http://192.168.60.165:11311, then enter 192.168.60.165 in the "Hostname/IP address" field and 11311 in the Port Number field.

Task 3 - Send Velocity Commands To the Robot

In this task, you will create a publisher that sends control commands (linear and angular velocities) to the simulator. You will make these velocities adjustable by using Slider Gain blocks.

ROS uses a right-handed coordinate system, so X is forward, Y is left and Z is up. Control commands are sent using a "geometry_msgs/Twist" message, where Linear.X indicates linear forward velocity (in meters/sec), and Angular.Z indicates angular velocity around Z-axis (in radians/sec).

  • Open a new Simulink model.
  • From the Robotics System Toolbox tab in the Library Browser, drag and drop a Publish block to the model. Double-click on the block.
  • Set Topic source field to Select From ROS network. Click on Select... next to the Topic field, select "/mobile_base/commands/velocity", and click OK. Note that the message type ("geometry_msgs/Twist") is set automatically.
  • From the Robotics System Toolbox tab in the Library Browser, drop and drop a Blank Message block to the model. Double-click on the block.
  • Click on Select... next to the Message type field and select "geometry_msgs/Twist".
  • Set Sample time to 0.01 and click OK.
  • From the Simulink > Signal Routing tab in the Library Browser, drag and drop a Bus Assignment block to the model.
  • Connect the Blank Message, Bus Assignment, and Publish blocks as follows. Click on Simulation > Update Diagram to ensure that the bus information is correctly propagated. You may get an error, "Selected signal 'signal1' in the Bus Assignment block 'untitled/Bus Assignment' cannot be found in the input bus signal". This error is expected, and will be resolved by the next step.

  • Double-click on the Bus Assignment block. Select "??? signal1" in the right listbox and click Remove. In the left listbox, expand both Linear and Angular properties. Select Linear > X and Angular > Z and click Select>>. Click OK to close the block mask.

  • Add a Constant block, a Gain block, and two Slider Gain blocks. Connect them together as shown in picture below, and set the Gain value to -1

  • Set the limits and current value of each Slider Gain block as shown in the picture below.

Task 4 - Receive Location Information From the Robot

In this task, you will create a subscriber to receive messages sent to the "/odom" topic. You will also extract (x,y) location of the robot and plot the path in the XY-plane.

  • From the Robotics System Toolbox tab in the Library Browser, drag and drop a Subscribe block to the model. Double-click on the block.
  • Set Topic source field to Select From ROS network, and click on Select... next to the Topic field. Select "/odom" for the topic and click OK. Note that the message type ("nav_msgs/Odometry") is set automatically.
  • From the Simulink > Signal Routing tab in the Library Browser, drag and drop a Bus Selector block to the model.
  • Connect the output port of the Subscribe block to the input port of the Bus Selector block. Click on Simulation > Update Diagram to ensure that the bus information is correctly propagated.
  • Double-click on the Bus Selector block. Select "??? signal1" and "??? signal2" in the right listbox and click Remove. In the left listbox, expand Pose > Pose > Position and select X and Y. Click Select>> and then OK.

  • From the Simulink > Sinks tab in the Library Browser, drag and drop an XY Graph block to the model. Connect the output ports of the Bus Selector block to the input ports of the XY Graph block.

The entire model should look like this. A pre-configured model is included for your convenience.

Task 5 - Configure and Run the Model

In this task, you will configure and run your model.

  • Click on Simulation > Model Configuration Parameters. In the Solver pane, set Type to Fixed-step and Fixed-step size to 0.01.
  • Set simulation Stop time to inf.
  • Click the Play button to start simulation. An XY plot will appear. In both the simulator and XY plot, you should see the robot moving in a circle.
  • While the simulation is running, change the values of Slider Gain blocks to control the robot. If the robot moves out of range in the XY plot, double-click on the XY Graph block and change the X and Y axis limits (you can do this while the simulation is running).
  • To stop the simulation, click the Stop button.

Summary

This example showed you how to communicate with external simulator by subscribing and publishing to existing topics in the ROS network. The example also showed you how to extract information from subscribed topics.