Robot Operating System for Autonomous Vehicles Part 1: ROS2 Setup
Jong Hang Siong
I founded OTONOCO in Singapore to design and build SaaS and Mobile Apps that are AI-enabled to address complex problems and unmet needs in the industry.
This is Part 1 of a series of articles discussing the use of Robot Operating System 2 (ROS2) to implement Autonomous Land Vehicles.?This Part emphasizes the procedures of setting up software stack to develop ROS2 packages for Autonomous Vehicles.
The road map for this ROS series for autonomous vehicles is as follow:
Autonomous Vehicles
Development of autonomous vehicles has been a long arduous journey.?The idea of self-driving cars is almost as old as the automobile. This inspiration came from the reality of driving in busy traffic. ?As early as 1925, Francis Udina demonstrated a remote control car called the American wonder, which drove through the streets of Manhattan frightening pedestrians with an empty driver's seat. This youtube video shows GM’s vision of a self driving car during the 1950s (https://www.youtube.com/watch?v=Rx6keHpeYak). ?
?One of the most dangerous things we do on a regular basis is driving our car. ?Majority of driving accidents today are caused by human errors. What if we could minimize or completely eliminate driving deaths by taking human error out of the picture through automation?
?There are also many other profound benefits that could arise from a driverless future. Instead of focusing on lanes, turns, and traffic, you can reply to work emails. Eat or relax and make the morning so much more productive.
?In 2004 and Defense Advanced Research Project Agency (DARPA) offered the winners a one million dollar prize if they could build an autonomous vehicle. ?The vehicle must be able to navigate 142 miles through the Mojave Desert. Although the first event saw only a few teams get off the start line and Carnegie Mellon's red team took back the first-place. It was clear that the task of driving 140 miles through the desert without any human aid was indeed possible.
?Lessons learned from these and many other initiatives have led to the development of autonomous vehicle architecture such as the A-model at illustrated below conceived at Institute of Automotive Engineering (IKA), Rheinisch-Westf?lische Technische Hochschule (RWTH) at Aachen, Germany:
Autonomous vehicle development is a highly complex.?Significant amount of mathematical, optimization, statistics and machine learning are essential to develop architecture of automated vehicles. ?Sensors, perception, environment modeling, prediction, behaviour planning, trajectory planning and optimization as well as vehicle control are some of the major components in this development.
Current State-of-the-Art for Autonomous Driving
Functional Architecture of Autonomous Vehicle
There is a vast number of technical challenges that must be overcome before deploying autonomous vehicles on the road so that the vehicle can perform the following tasks based on IKA’s A-model:
Robot Operating System
The Robot Operating System (ROS) is a set of software libraries and tools that help you build robot applications. From drivers to state-of-the-art algorithms, and with powerful developer tools, ROS has what developers for autonomous vehicles need.?It's all open source.
?The following sections demonstrate how to setup ROS 2 development environment in Ubuntu on AWS EC2.
EC2 Ubuntu 20.04 AMI
Install Ubuntu Desktop
Installation scripts for Ubuntu Cinnamon Desktop are as follow.?It might take a while to complete:
$ sudo apt update
$ sudo apt install cinnamon-desktop-environment
Install XRDP for Remote Access
$ sudo apt update
$ sudo apt install xrdp
$ sudo systemctl enable xrdp
$ sudo systemctl start xrdp
Create a password for ubuntu user for remote access using RDP
$ sudo passwd ubuntu
Download and install Microsoft Remote Desktop in your local computer in order to access the EC2 instance containing your ROS2.
In EC2 dashboard, select the EC2 instance and reboot:
Launch Microsoft RDP App and connect to the EC2 instance:
Optional VS Code IDE
Go to https://code.visualstudio.com/download to download Visual Studio Code for Ubuntu for x86 system.?Install vscode by running the following script:
$ sudo apt install ./code_x.xx.x-xxxxxxx_amd64.deb
Upon completion, run the following script to ensure VS Code can be launched:
$ code
Install Robot Operation System 2
Update Ubuntu repositories before installing:
$ sudo apt update
The following steps on installing ROS2 have been modified from ROS documentation
SETUP LOCALE
$ sudo apt update && sudo apt install locale
$ sudo locale-gen en_US en_US.UTF-8
$ sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
$ export LANG=en_US.UTF-8
$ locales
Update Ubuntu Repository for ROS2 by running the following scripts in sequence:
$ sudo apt updat
$ sudo apt install software-properties-common
$ sudo add-apt-repository universe
$ sudo apt update && sudo apt install curl gnupg2 lsb-release
$ sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key? -o /usr/share/keyrings/ros-archive-keyring.gpg
?
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] https://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/nulle
Visit ROS2 documentation for latest updates.
Update and Upgrade Ubuntu repository:
$ sudo apt update
$ sudo apt upgradee
Install ROS2:
$ sudo apt install ros-foxy-desktop python3-argcomplete
Test ROS2 Installation by running the following command to determine if ROS2 has been successfully installed:
$ ros
ROS2 installed files are located in /opt/ros/foxy.?This directory contains a file called setup.bash that must be included in the Linux classpath for ROS2 to work properly when creating packages.?Use a preferred editor to open the .bashrc file and add the following line at the bottom of the .bashrc file:
$ source /opt/ros/foxy/setup.bash
Create ROS2 Workspace
A ROS2 workspace is simply a directory in your computer.?Create a new directory in your home directory /home/ubuntu.?This workspace is required to store codes and artefacts for your ROS2 packages for the autonomous vehicles.
$ mkdir ros2_workspace
Update Ubuntu repository and Install Colcon extension:
$ sudo apt update
$ sudo apt install python3-colcon-common-extensionse
The extensions are installed in /usr/share/ directory.?Use sudo privilege to open the .bashrc file and add the following line to source the colcon variables into the Ubuntu environment.
$ source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash
Navigate to the workspace directory and create a /src subdirectory:
$ cd ros2_workspace
$ mkdir src
Staying at ros2_workspace directory, run the following command to start building a workspace in this directory:
$ colcon build
Upon successful build, 3 new directories will be created:
$ ls -l
Change to install directory and ensure the following files are created:
$ cd install
$ ls -
Use sudo privilege to edit .bashrc file:
$ sudo vim ~/.bashrc
Add this line to the bottom of .bashrc file:
source $HOME/ros2_workspace/install/setup.bash
In the home directory, load all variables into the Ubuntu environment:
$ source ~/.bashrc
We are now ready to test drive ROS2 by creating some custom nodes.
Create ROS2 Package and Node
Install Python 3
If your Ubuntu is newly installed, chances are default python has not been installed.?One way to check is to run python --version command.?The following shows default python is not implemented.
Check python 3 installation bundle that come with Ubuntu by running this command:
$ whereis python
The above illustration shows pre-loaded python 2.7, 3.8 and 3.9 in /usr/bin/ directory.?To set python 3.8 as the default python implementation, do the following to install python 3.8 in /usr/bin/python link and make it as priority number 1:
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
Run the following script to ensure python 3.8 is installed as default:
Create Package
Continue from the previous section, navigate to the ros2_workspace/src directory:
$ cd ~/ros2_workspace/src
Run the following command to create a package called my_first_robot and at the same time create a ROS node called my_first_node:
$ ros2 pkg create --build-type ament_python –node-name my_first_node my_first_robot
You can also choose C++ as base by specifying ament_cmake instead of ament_python whichever you are comfortable writing codes with.
The ros2_workspace/src now has a new my_first_robot directory.?Navigate into my_first_robot and you will see another my_first_robot directory.?The second my_first_robot directory has 2 ?autogenerated __init__.py and my_first_node.py python files.
my_first_node.py just prints a simple message to the console.
Navigate back to the /ros2_workspace, build the newly created package and node:
$ colcon build
Load the environment variables:
$ source ~/.bashrc
Test run the package and node:
$ ros2 run my_first_robot my_first_node
Modify the my_first_node.py using object-oriented approach to recreate the node:
Go back to the /ros2_workspace, rebuild the package:
$ colcon build
Reload the environment variables:
$ source ~/.bashrc
$ ros2 run my_first_robot my_first_node
Alternatively, use colcon build --symlink-install to avoid having to rebuild the workspace.
The node can be visualized using rqt_graph:
$ rqt_graph
The GUI shows only one node which is the first_node.
Communications Between Nodes
PUB-SUB Demonstration using Turtlesim ROS Package
ROS2 installation comes with several read-to-run bundled packages.?You can check these available packages by running the following command:
$ ros2 pkg list
The turtlesim package will be used to demonstrate how pub-sub mechanism works in ROS2.
First, launch several terminals in your linux.?Select one of the terminals to launch the turtlesim_node from turtlesim package:
$ ros2 run turtlesim turtlesim_node
A GUI appears with a turtle in the middle
Select another terminal and run the turtle_teleop_key node:
$ ros2 run turtlesim turtle_teleop_key
In the GUI, press the arrow keys on your computer to move the turtle that will also leave a trail behind:
Open a new terminal, run rqt_graph:
The graph GUI appears and shows turtlesim and teleop_turtle nodes. There is also a node to receive input from the keyboard.?The turtlesim and teleop_turtle take the role of publisher as well as subscriber to each other.
Pub-Sub in the Context of Autonomous Vehicles
This is a simplified system architecture on the interactions of various nodes in an autonomous vehicle illustrating how different nodes communicating with each other via pub-sub approach.
Heads-Up to Part 2
In Part 2, we will look at various perception models developed for autonomous driving and deploy some of these models to ROS.