Main Content

Sign-Following Robot with ROS 2 in Simulink

This example shows how to control a simulated robot running on a separate ROS-based simulator over ROS 2 network using Simulink® software. You can run this example using Docker container or virtual machine.

In this example, you run a model that implements a sign-following algorithm and controls the simulated robot to follow a path based on signs in the environment. The algorithm receives the location information and camera information from the simulated robot, which is running in a separate ROS-based simulator. The algorithm detects the color of the sign and sends the velocity commands to turn the robot based on the color. In this example, the algorithm turns the robot to the left when it encounters a blue sign and to the right when it encounters a green sign. The robot stops when it encounters a red sign.

For an example that shows how to control a sign-following robot using MATLAB®, see Sign Following Robot with ROS in MATLAB.

Prerequisites

To run this example, you are require to set up Docker in your host environment (Windows Subsystem for Linux (WSL), Linux or Mac) and create a Docker image for Gazebo. For more information on how to configure Docker, see Install and Set Up Docker for ROS, ROS 2, and Gazebo.

Start Docker Container

With the prerequisites set up and the Docker image created, the first step is to launch an instance of Docker container.

To start a Docker container, run the following command in WSL/Linux/Mac terminal:

$ docker run -it --net=host -v /dev/shm:/dev/shm --name sl_sign_follower <image-name>

Here, 'sl_sign_follower' is the name of the Docker container. Replace the <image-name> with the name of the Docker image created in the prerequisite section.

Get IP Address of Docker Container

While the Docker container is running, obtain the IP address of the Docker container running on the remote device. This IP address is necessary for establishing an SSH connection and accessing the Gazebo simulator. Note that the IP address of the Docker container is the same as the IP of the host machine.

To retrieve this IP address, run the following command in the WSL/Linux/Mac terminal:

$ ifconfig

The network connection type can vary depending on how host is connected to Docker container. In this case use the Ethernet (eth0), however, in many cases the wireless (wlan0) is the appropriate connection.

Start Gazebo Robot Simulator

Once the container is running, start the Gazebo robot simulator inside the Docker container.

Open a terminal within the Docker container by executing the following command in the WSL/Linux/Mac terminal:

$ docker exec -it sl_sign_follower /bin/bash

Here, 'sl_sign_follower' is the name of Docker container.

In the Docker container terminal, launch the TurtleBot in a Gazebo world by running the following command:

$ source start-gazebo-maze-world.sh

To view the Gazebo world, open a web browser on your host machine and connect to the URL <docker-ip-address>:8080. If Docker is running on your host machine, you can simply use localhost:8080. You can now visualize and interact with the Gazebo world directly in your web browser.

Configure MATLAB for ROS 2 Network

Launch MATLAB on your host machine and set the ROS_DOMAIN_ID environment variable to 25 and configure the ROS Middleware in MATLAB to rmw_fastrtps_cpp to match the robot simulator's ROS settings. For detailed steps, refer to Switching Between ROS Middleware Implementations.

Once the domain ID is set, run the following command in MATLAB to verify that the topics from the robot simulator are visible:

setenv('ROS_DOMAIN_ID','25')
ros2('topic','list')
/camera/camera_info
/camera/image_raw
/camera/image_raw/compressed
/camera/image_raw/compressedDepth
/camera/image_raw/theora
/camera/image_raw/zstd
/clock
/cmd_vel
/imu
/joint_states
/odom
/parameter_events
/robot_description
/rosout
/scan
/tf
/tf_static

Note: If you are unable to view topic published by Gazebo (/camera/image_raw), or facing frequent message drop, run the following command in MATLAB Command Window: "setenv('FASTRTPS_DEFAULT_PROFILES_FILE', <path-to-fastrtps-xml-file>)". Replace <path-to-fastrtps-xml-file> with the path of 'fastrtps.xml' provided with the example.

Open Model and Configure Simulink

Setup the Simulink ROS preferences to communicate with the robot simulator.

Open the example model.

open_system("signFollowingRobotROS2.slx");

To configure the network settings for ROS 2.

  1. From the Prepare section of the Simulation tab, select ROS Toolbox > ROS Network.

  2. In Configure ROS Network Addresses, set the ROS 2 Domain ID value to 25.

  3. Click OK to apply changes and close the dialog.

At each time step, the algorithm detects a sign from the camera feed, decides which way to turn and drives the robot. The Image Processing subsystem of the model detects the signs.

open_system("signFollowingRobotROS2/Image Processing");

The Sign Tracking Logic subsystem implements a Stateflow® chart that takes in the detected image size and coordinates from Image Processing and provides linear and angular velocity to drive the robot.

open_system("signFollowingRobotROS2/Sign Tracking Logic");

Run Model

Run the model and observe the behavior of the robot in the robot simulator.

  • The video viewers show the actual camera feed and the detected sign image.

  • In the simulator, the robot follows the sign and turns based on the color.

  • Simulation stops when the robot reaches the red sign at the end.