Generate and Deploy ROS 2 Component Nodes for Sign Following Robot Using Simulink
This example shows how to generate ROS 2 component nodes from Simulink for handling perception and tracking logic of a sign-following robot. It also shows how to load and run the generated ROS 2 components in a docker container running the simulated robot in Gazebo.
The sign-following algorithm receives the location and camera information from the simulated robot. Based on the detected sign, the algorithm sends velocity commands to the robot.
Prerequisites
This example illustrates steps to use the docker container in a host Windows Subsystem for Linux 2 (WSL 2) environment with Xserver as the GUI application. The steps are similar for other host linux environments but with slightly different commands. For more information on setting up a docker container, see Install and Set Up Docker for ROS, ROS 2, and Gazebo.
Build ROS 2 Docker Image
You can download the ROS 2 Dockerfile in your WSL environment by cloning ROS Toolbox Docker Files. Then, build a Docker image that has ROS 2 and Gazebo installed by following these steps:
Open a WSL terminal and navigate to the
support-and-docker-files-for-ros-toolbox
folder.Build the Dockerfile by executing this command where
jazzy_docker_image
is the docker image name.
$ docker build -t jazzy_docker_image ros_jazzy/Ubuntu/gazebo
Start Docker Container and Robot Simulation in Gazebo
To start the docker container, open a WSL terminal and enter this command, where ros2_sign_follower_robot
is the name of the container.
$ docker run -it --env="DISPLAY=$DISPLAY" --env="QT_X11_NO_MITSHM=1" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" --net=host --name=ros2_sign_follower_robot jazzy_docker_image
Open a new terminal and connect to the docker container using this command.
$ docker exec -it ros2_sign_follower_robot /bin/bash
In the docker container, run these commands to launch Turtlebot in Gazebo.
$ source /home/user/ros2_ws/install/setup.bash $ ros2 launch turtlebot3_gazebo maze_world.launch.py
Generate and Deploy ROS 2 Component Node for Perception Algorithm
Open the signFollowingPerceptionComponent
model.
open_system("signFollowingPerceptionComponent")
The signFollowingPerceptionComponent
model subscribes to the camera images from the robot and performs color thresholding to determine which sign is currently being seen by the robot. Based on the color thresholding values, the model segments a blob or mask on the image containing the sign. The model then publishes the size and location of the segmented blob on the image as two separate ROS 2 topics.
To generate a ROS 2 component node from the model, in the ROS tab, from the Deploy section dropdown, click Build Model.
Note that the Deploy Type option is selected as Component Node. Ensure that you configure the correct remote device IP address for your docker container in the Deploy to > Manage Remote Device drop down.
The model deploys the generated ROS 2 package to the docker container and builds the colcon workspace.
Generate and Deploy ROS 2 Component Node for Tracking Algorithm
Open the signFollowingTrackingComponent
model.
open_system("signFollowingTrackingComponent")
The signFollowingTrackingComponent
model subscribes to the segmented blob size and location topics published by the signFollowingTrackingComponent
. Using the current pose of the robot, the model then tracks the location and proximity to the sign based on the segmented blob size and location. Based on the sign color and proximity, the model publishes velocity command to the robot whether to turn right or left.
To generate a ROS 2 component node from the model, in the ROS tab, from the Deploy section dropdown, click Build Model.
Note that the Deploy Type option is selected as Component Node. Ensure that you configure the correct remote device IP address for your docker container in the Deploy to > Manage Remote Device drop down.
The model deploys the generated ROS 2 package to the docker container and builds the colcon workspace.
Run Deployed ROS 2 Components for Sign Following Perception and Tracking
Open a WSL terminal and connect to the docker container.
$ docker exec -it ros2_sign_follower_robot /bin/bash
From a docker container terminal, start the ROS 2 component container. This enables run-time composition of components where you can dynamically load or unload the components.
$ source ros2_ws/install/setup.bash $ ros2 run rclcpp_components component_container
In a new docker container terminal, load the deployed perception and tracking components.
$ source ros2_ws/install/setup.bash $ ros2 component load /ComponentManager signfollowingperceptioncomponent SLROS2::SLROSComponent $ ros2 component load /ComponentManager signfollowingtrackingcomponent SLROS2::SLROSComponent
The robot starts moving in Gazebo and follows the signs.