Main Content

Sign Following Robot with ROS 2 in MATLAB

This example shows you how to use MATLAB® to control a simulated robot running on a separate ROS-based simulator over ROS 2 network.

In this example, you run a MATLAB script 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 is designed to turn left when robot encounters a blue sign and turn right when robot encounters a green sign. Finally the robot stops when it encounters a red sign.

To see this example using ROS 1 or Simulink®, see Sign Following Robot with ROS in MATLAB.

Connect to a Robot Simulator

Start a ROS-based simulator for a differential-drive robot and configure the MATLAB® connection with the robot simulator.

To follow along with this example, download a virtual machine using instructions in Get Started with Gazebo and a Simulated TurtleBot.

  • Start the Ubuntu® virtual machine desktop.

  • In the Ubuntu desktop, click the Gazebo ROS2 Maze icon to start the Gazebo world built for this example.

  • In MATLAB Command Window, set the ROS_DOMAIN_ID environment variable to 25 to match the robot simulator ROS bridge settings and run ros2 topic list to verify that the topics from the robot simulator are visible in MATLAB.


Setup ROS 2 Communication

Create a ROS 2 node using the specified domain ID.

domainID = 25;
n = ros2node("matlab_example_robot",domainID);

Create publishers and subscribers to relay messages to and from the robot simulator over the ROS 2 network. You need subscribers for the image and odometry data. To control the robot, set up a publisher to send velocity commands using the /cmd_vel topic.

imgSub = ros2subscriber(n, "/camera/image_raw","sensor_msgs/Image");

odomSub = ros2subscriber(n, "/odom","nav_msgs/Odometry");

[velPub, velMsg] = ros2publisher(n, "/cmd_vel", "geometry_msgs/Twist");

Define the image processing color threshold parameters. Each row defines the threshold values for the different colors.

colorThresholds = [100 255 0 55 0 50; ... % Red
                   0 50 50 255 0 50; ...  % Green
                   0 40 0 55 50 255]';    % Blue

Create Sign Following Controller Using Stateflow® Chart

This example provides an example helper MATLAB Stateflow® chart that takes in the image size, coordinates from processed image, and the robot odometry poses. The chart provides linear and angular velocity to drive the robot based on these inputs.

controller = ExampleHelperSignFollowingControllerChart;

Run Control Loop

This section runs the controller to receive images and move the robot to follow the sign. The controller does the following steps:

  • Gets the latest image and odometry message from the ROS network.

  • Runs the algoritm for detecting image features (ExampleHelperSignFollowingProcessImg).

  • Generates control commands from the Stateflow® chart using step.

  • Publishes the velocity control commands to the ROS network.

To visualize the masked image the robot sees, change the value of doVisualization variable to true.


% Control the visualization of the mask
doVisualization = false;

r = rateControl(10);
receive(imgSub); % Wait to receive an image message before starting the loop
    % Get latest sensor messages and process them
    imgMsg = imgSub.LatestMessage;
    odomMsg = odomSub.LatestMessage;
    [img,pose] = ExampleHelperSignFollowingProcessMsg(imgMsg, odomMsg);
    % Run vision and control functions
    [mask,blobSize,blobX] = ExampleHelperSignFollowingProcessImg(img, colorThresholds);
    v = controller.v;
    w = controller.w;
    % Publish velocity commands
    velMsg.linear.x = v;
    velMsg.angular.z = w;
    % Optionally visualize
    % NOTE: Visualizing data will slow down the execution loop.
    % If you have Computer Vision Toolbox, we recommend using
    % vision.DeployableVideoPlayer instead of imshow.
    if doVisualization
        title(['Linear Vel: ' num2str(v) ' Angular Vel: ' num2str(w)]);
    % Pace the execution loop.

You should see the robot moving in the ROS-based robot simulator as shown below.