ROS2 Overview for Autonomous Robots
ROS2 Overview for Autonomous Robots
Report
Introduction to ROS2
Dinh-Son Vu
Table of Contents
1. Introduction .............................................................................................................................. 4
Page 2 of 51
RMIT Classification: Trusted
10.2. Topics............................................................................................................................ 36
Page 3 of 51
RMIT Classification: Trusted
1. Introduction
This is an introduction to ROS2, which is a middleware running on a development computer or an
on-board computer (Raspberry Pi, Jetson). ROS2 comes with a steep learning curve, but it is useful
to build robots with intelligence, contrary to robots that are teleoperated only.
- ROS2 stands for Robotic Operating System, and it is the second version. Operating system
is quite confusing because it does not include a graphical interface just like Windows or
MacOS, but it mainly uses terminal command.
- A middleware is a software that stands in between the hardware and the software. ROS2 is
similar to a driver for peripherals: when you plug a mouse to a computer, it just works
instantaneously. The drivers are installed automatically as soon as the device is plugged in,
and you do not write any code to convert the optical flow sensor (mouse sensor) to movement
in the x-y direction. Moreover, your computer understands the information provided by the
mouse directly thanks to the standardization of the communication protocol via USB.
A robot has several peripherals (generic names for sensors and actuators, similar to peripherals for
a computer, e.g., mouse, keyboard, screen, speakers, microphone, etc.), with different algorithms
that are intense and quite consuming to develop, such as:
- Code interfacing between the hardware to the computer (i.e., a driver)
o Sensors: Lidar, radar, sonar, camera, GPS, IMU, encoder
Page 4 of 51
RMIT Classification: Trusted
Page 5 of 51
RMIT Classification: Trusted
Figure 2: Autonomous vehicles must handle several tasks: mapping, navigation, computer vision, etc. Code sharing
become even more important as the robot is becoming more autonomous and intelligent.
Figure 3: ROS2 aims at creating a standard for all robotic platforms: they thus can be summarized as black box receiving
inputs and transmitting outputs.
Figure 4: Digital Twin (Rviz and Gazebo) - ROS2 offers tools for visualization of data coming from the hardware, which is
time consuming if one needs to develop it entirely.
Page 6 of 51
RMIT Classification: Trusted
Figure 6: URDF is essential for describing a robot in ROS2 and especially for RVIZ
Page 7 of 51
RMIT Classification: Trusted
Figure 8: Launch file: a bit similar to MATLAB script: you launch the nodes
Page 8 of 51
RMIT Classification: Trusted
Name Description
1. rmitbot_description - contains urdf and gazebo configuration
- uses gazebo and rviz (package for visualization)
- The virtual robot cannot move yet.
2. rmitbot_controller - uses ros2_control and ros2_controller (package for controlling robot)
- interface the joystick/keyboard controller to the joints of the robot
- the virtual robot can move, its motion can be measured with odometry.
Adding an IMU would attenuate the error due to slippage.
3. rmitbot_localization - uses robot_localization (package for ekf)
- performs sensor fusion (ekf) between odometry and imu
- the virtual robot can move, and its pose can be displayed
4. rmitbot_firmware - hardware interface from the ros2_control package
- transition between the virtual environment to the real hardware.
5. rmitbot_mapping - Create a map of the environment with slam_toolbox
6. rmitbot_navigation - path planning (a_star): path to get to the target
- motion planning: path following with PID
- navigation: decision tree relative to known and unknown obstacles
Page 9 of 51
RMIT Classification: Trusted
Page 10 of 51
RMIT Classification: Trusted
Page 11 of 51
RMIT Classification: Trusted
Figure 10: Structure of the lesson, including simulation and hardware manipulation.
Page 12 of 51
RMIT Classification: Trusted
Page 13 of 51
RMIT Classification: Trusted
- For the Wi-Fi to work, add an Adapter 1 and Adapter 2 as shown below:
Page 14 of 51
RMIT Classification: Trusted
- If your computer has USB3, use it, otherwise ubuntu freeze while disconnecting the esp32.
For flashing purposes, USB must be configured as follows.
Page 15 of 51
RMIT Classification: Trusted
Figure 15: Please verify that the PlatformIO (PIO) core is 6.1.18 or above
Page 16 of 51
RMIT Classification: Trusted
- ros2_controller: to perform the inverse kinematics (from joystick input to wheel speed output)
sudo-get install ros-jazzy-ros2-controller
- xacro: xml macro for describing the robot architecture
sudo-get install ros-jazzy-xacro
- Gazebo: simulator (quite heavy installation): physic engine
sudo-get install ros-jazzy-ros-gz*
- Additional plugins for the control of the robot in Gazebo simulator
sudo-get install ros-jazzy-*-ros2-control
- Joint state publisher
sudo-get install ros-jazzy-joint-state-publisher-gui
- For the tutorial simulation: turtlesim
sudo-get install ros-jazzy-turtlesim
- Robot localization
sudo-get install ros-jazzy-robot-localization
- Joystick
sudo-get install ros-jazzy-joy
sudo-get install ros-jazzy-joy-teleop
- TF Transformation
sudo-get install ros-jazzy-tf-transformations
- Python pip
sudo apt-get install python3-pip
- python 3D package
pip install transforms3d (sometimes it may not work)
sudo apt install python3-transforms3d
- serial lib
sudo apt install libserial-dev
Page 17 of 51
RMIT Classification: Trusted
A workspace is just a folder that will contains packages. Make a directory called lesson1_ws, with a
folder called src:
mkdir -p lesson1_ws/src
Packages are stored in the src folder. The nodes of a package can be code in python or cpp. For
the moment, we will focus on python code. Change directory to the src folder and build a new
package called my_package:
cd lesson1_ws/src
ros2 pkg create --build-type ament_python my_package → for python nodes
ros2 pkg create --build-type ament_cmake my_package → for cpp nodes (for reference)
A folder called “my_package” has been created, with the following components: include, src,
[Link], [Link]. Verify that your package is available
ros2 pkg list
For the moment, the newly package created does nothing (there is no node/code insde).
Change directory back to the workspace folder and build the workspace It will create three folders:
build, install, and log. Then source the workspace, to let the computer know where you are starting
to work on your computer.
cd ~/lesson1_ws
colcon build
source install/[Link] → source the workspace
. install/[Link] → this command line can be used as well.
Page 18 of 51
RMIT Classification: Trusted
Optional: At some point, you may be tired of sourcing your workspace every time you are opening a
terminal. In this case, you can add definitively the workspace to the main bashrc.
echo "source /opt/ros/jazzy/[Link]" >> ~/.bashrc → Add the global ros2
echo "source ~/ros2_ws/install/[Link]" >> ~/.bashrc → Add this particular directory
source ~/.bashrc → Apply change
Bw careful, do not do this if you have several package of the same name in different workspace.
Page 19 of 51
RMIT Classification: Trusted
Page 20 of 51
RMIT Classification: Trusted
At the end of this lesson, the robot can be visuazlised in rviz (i.e., the tf) and gazebo, but the robot
cannot move. This is covered by rmibot_controller package in the next section.
Instead of cloning the repository directly, you may want to do the process manually to get familiar
with command line interface (CLI).
Create a workspace:
mkdir -p lesson2_ws/src
Change directory to src
cd src
Create a package called rmitbot_description in the src directory
ros2 pkg create --build-type ament_cmake rmitbot_description
Page 21 of 51
RMIT Classification: Trusted
Explanation:
- The folder include is created by default. It is empty since no node is developed.
- The folder launch will contain the launch file, to launch several nodes at the same time:
o [Link]: visualization in rviz
o [Link]: simulation in gazebo
- The folder meshes contains stl file for the rendering of the robot.
- The folder rviz contains a configuration file for rviz
- The folder src is created by default. It is empty since no node is developed
- The folder urdf will contain the description of the robot in a xml format
- The [Link] must be modified to add the path of the folder
o launch, urdf, rviz, meshes must be added to the path
- The [Link] must be modifed to add the executable dependencies.
o robot_state_publisher, joint_state_publisher_gui, rviz, ros2launch for rviz
o ros_gz_sim for gazebo
There are packages that will be used (natively installed with ros2):
- robot_state_publisher: Requires an urdf file as an input. It performs the transform (tf) between
robot’s links. These tf datatype are broadcasted and used by rviz2.
- joint_state_publisher_gui: provides a real-time joint state data, which is used by the
robot_state_publisher to calculate the tf and broadcast them.
- rviz : rviz can be used to analyze topic, especially graphical one (lidar, TF2, etc. )
takes the tf broadcasted by the robot_state_publisher and the joint state from the
joint_state_publisher_gui to render the robot
Gazebo is a useful tool for simulation of physics and digital twin (similar to unity, but gz is more suited
fo ros2). It can replace the real robot and publish topics on Rivz. It requires the urdf file to have
visual, collision, and inertia (the latter can be quite tough to obtain manually, Solidworks and Fusion
can handle it. SolidWorks→URDF export or Fusion→URDF). Gazebo also requires a file for dynamic
coefficient: friction, stiffness, damping.
Optional: at some point, you may want to convert from Fusion360 to urdf:
[Link]
Page 22 of 51
RMIT Classification: Trusted
Page 23 of 51
RMIT Classification: Trusted
Now, We can use ros2_control to move our robot in the simulated environment. Later, we will see
how to go from simulation to real hardware. One should pay attention that the parameters
use_sim_time has been set to true for all nodes. When real hardware will be used, then
use_sime_time will not be used and be set to false.
Note: the package publishes a topic called /rmitbot_controller/odom, and it also broadcasts a tf called
/odom. Publishing a topic and broadcasting a tf is similar, but differs in the following aspects:
- Publishing a topic in the general method for communication in ros2.
- Broadcasting a tf is specialized for pose of the robot
Page 24 of 51
RMIT Classification: Trusted
Important note:
- When using the robot_localization/ekf package, in [Link], make sure that the
parameter enable_odom_tf is set to FALSE, otherwise there will be a conflict on the tf
publication and the tf odom → base_footprint will “jerk” in rviz. The diff_drive_controller still
publishes the TOPIC /rmitbot_controller/odom, but the ekf node should be the only one to
broadcast the tf odom→base_footprint. The ekf will publish the topic /odometry/filtered, which
is the fused odometry from different sources.
Later, you will need to visualize the tf tree by running the following cli:
ros2 run tf2_tools view_frames
evince <[Link]>
Page 25 of 51
RMIT Classification: Trusted
The localization of a robot is one of the hardest challenge in mobile robot. The encoder of the robot
give the pose (position and orientation) of a robot, but it can drift due to slippage of the wheel,
inaccuracy of the encoders, error of the wheel and base dimensions, etc. Usually, the pose
estimate is given with:
- encoders: also called odometry or dead reckoning
- imu: inertial measurement unit
- gps (outdoor): absolute positioning of the robot
- lidar odometry: LiDAR odometry estimates motion by aligning successive LiDAR scans
- visual odometry: works better with depth camera and is similar to lidar odometry.
- Visual apriltag: tag are used to give absolute positioning of the robot
Page 26 of 51
RMIT Classification: Trusted
Page 27 of 51
RMIT Classification: Trusted
Create a directory/workspace. The terminal with mkdir or the file explorer can be used.
If you are using the file explorer, with a right-click the option Open in Terminal is available.
Where the terminal is open, typing “code .” open VSCode and open the directory inside VSCode.
Figure 18: git clone the online repository and launch vscode with “code .”
The terminal can be open in VSCode as well. From here, you may clone the code from GitHub:
Page 28 of 51
RMIT Classification: Trusted
Figure 19: Pick the folder that has just been cloned. You may now build and compile the project
Page 29 of 51
RMIT Classification: Trusted
A code should be written in order to read the serial communication and the speed command from
the ros2 hardware interface.
Page 30 of 51
RMIT Classification: Trusted
It would be possible to write a node that reads and write on the serial communication. However,
hardware interface and ros2_control uses the concept of lifecycle node, which works as a state
machine. This lifecyle node is useful when the node is initializing, waiting for data, or deconstructed.
The effort to write a hardware interface is medium, but can be alleviate, as only the read/write
function is important. The rest can be copied/pasted.
Package modification:
- Description:
o [Link]
<xacro:arg name="is_sim" default="true"/>
This variable is added to select the real robot or the gazebo simulated robot.
o rmitbot_ros2_control.xacro
using simulation or real robot for the hardware interface
- Controller: rmitbot_controller.yaml: use_sim_time: true/false depending on the
real/simulated hardware
- Localization: [Link]: use_sim_time: true/false depending on the real/simulated hardware
- Firmware: new package
o Arduino_firmware: the sketch to be flashed on the Arduino
o Include/rmitbot_firmware/rmitbot_interface.hpp: header file for the cpp code.
Function and variable declaration
Page 31 of 51
RMIT Classification: Trusted
gazebo
controller
teleopkeyboard
optional: launch robot_localization as well
This bringup pkg allows to launch several launching file.
topic explanation
/joint_states: wheel velocity and position
/rmitbot_controller/cmd_vel : twist command (Cartesian velocity command)
Motor velocity command are usually not published.
The hardware connection works well, but there is a lot of inertia (?)
Page 32 of 51
RMIT Classification: Trusted
Page 33 of 51
RMIT Classification: Trusted
If the robot is similar to a turtle bot (i.e., diff drive controtroller), then launch
ros2 launch nav2_bringup navigation_launch.py use_sim_time:=true
Add a new map (on top of the map created by mapping), with the topic global coastmap.
Select the two goals (on top of rviz) and select where you want to go.
/goal_pose can be published, whether the point is located inside or outside the map
Apparently, /cmd_vel from nav2 stack only generates /cmd_vel when the /goal_pose is within the
map created.
The robot AND the goal_pose must be inside the coastmap area. (you cannot send the robot in the
fog of war).
Make sure to modify the yaml file with base_footprint instead of base_link (which is by default in the
yaml file )
Page 34 of 51
RMIT Classification: Trusted
10.1. Nodes
Each node in ROS should be responsible for a single, modular purpose, e.g. controlling the wheel
motors or publishing the sensor data from a laser rangefinder. Each node can send and receive data
from other nodes via topics, services, actions, or parameters.
Page 35 of 51
RMIT Classification: Trusted
10.2. Topics
Page 36 of 51
RMIT Classification: Trusted
10.3. Services
Services are based on a call-and-response model versus the publisher-subscriber model of topics.
While topics allow nodes to subscribe to data streams and get continual updates, services only
provide data when they are specifically called by a client.
Page 37 of 51
RMIT Classification: Trusted
10.4. Parameters
A parameter is a configuration value of a node. You can think of parameters as node settings.
Page 38 of 51
RMIT Classification: Trusted
10.5. Actions
They consist of three parts: a goal, feedback, and a result. Their functionality is similar to services,
except actions can be canceled. They also provide steady feedback, as opposed to services which
return a single response.
Page 39 of 51
RMIT Classification: Trusted
Example:
- In a first terminal, launch the teleoperation keyboard node:
ros2 run teleop_twist_keyboard teleop_twist_keyboard
- In a second terminal, launch plotjuggler:
ros2 run plotjuggler plotjuggler
- In the third terminal, type “ros2 node list”. You should see:
/plotjuggler
/teleop_twist_keyboard
- In the third terminal, type “ros2 topic list”. You should see:
/cmd_vel → This is the topic from the node /teleop_twist_keyboard
/parameter_events → This is by default used by ROS2
/rosout → This is by default used by ROS2
- In plotjuggler, chose the correct topic to stream and to visualize the data.
Page 40 of 51
RMIT Classification: Trusted
12. Appendix
Page 41 of 51
RMIT Classification: Trusted
OOP is better for abstraction and code reuse. However, the code might be longer, but the main code
would be much clearer. It is defined as follows:
- Class: concatenation of several functions. For example, a motor has the following function:
o Init: defining the pin
o Run: running the motor with the correct pins
- A class is defined with:
o Private Attributes (inside the class only), also known as encapsulation
▪ Example: pin used for pwm
o Public attributes (inside and outside the class)
▪ Constructor (name of the class)
▪ this->pin : uses the variable defined in the private attribute
▪ method (funcitonality)
• void in
• void off
• void init
- Object: calling the class. For example, for 4 motors, we will have one class, but four objects.
Page 42 of 51
RMIT Classification: Trusted
Page 43 of 51
RMIT Classification: Trusted
Hardware ESP32
ENC1_A 23
ENC1_B 22
ENC2_A 21
ENC2_B 19
MOT1_A 16
MOT1_B 4
MOT2_A 18
MOT2_B 17
Page 44 of 51
RMIT Classification: Trusted
MicroROS is a library that must be imported in the esp32 in the [Link]. It prepares the
communication between the microcontroller and the on-board computer. It allows the esp32 to
communicates with the on-board computer via nodes, topics, services, actions.
On the on-board computer, one must run an Agent, which is the communication interface between
the MicroROS microcontroller and the ROS2 system. More information in:
[Link]
Page 45 of 51
RMIT Classification: Trusted
After flashing the code (publisher - subscriber) on the esp32, you may want to see if the
communication is enabled.
- How to listen to a node:
o Launch the docker agent
docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm --privileged --net=host microros/micro-ros-
agent:jazzy serial --dev /dev/ttyUSB0 -v6
o Press and release enable button on esp32 (if necessary)
o List all the topics available: ros2 topic list -t
o Listen to the topic: ros2 topic echo /micro_ros_arduino_node_publisher
o Publish on the topic:
ros2 topic pub /int32_subscriber std_msgs/Int32 "{data: 123}"
Page 46 of 51
RMIT Classification: Trusted
[Link]
How to use keyboard twist (for input command from the keyboard)
- If you need to install it (normally it comes directly with ROS2)
sudo apt-get install ros2-teleop-twist-keyboard
- You will probably need to remap it to publish to the correct topic
ros2 run teleop_twist_keyboard teleop_twist_keyboard –ros-args –remap cmd_vel:=<topic_name>
Page 47 of 51
RMIT Classification: Trusted
- In a terminal type:
ssh id@ipaddr
ssh v120506-pi@[Link]
then type the password that you have selected while creating the pi
You are know controlling the rpi4 from your computer via ssh
You may want to update and upgrade your packages
sudo apt update
sudo apt upgrade
sudo reboot → reboot the rpi4 (it closes the ssh connection as well)
exit → end ssh connection
sudo poweroff → turn off rpi4
If you are using a VM machine, make sure that you are using a bridge connection (network
option), otherwise you can ping, but DDS multicasr will not work (no publish/subscribe
between different computers).
Page 48 of 51
RMIT Classification: Trusted
Page 49 of 51
RMIT Classification: Trusted
Instal twist_stamper: joystick publishes a twist without a stamp. A stamp is required while using a
real hardware, with mapping and all.
sudo apt install ros-jazzy-twist-stamper
Out of the box, it should publish a topic called /key_vel. You can verify with
ros2 topic list
ros2 topic echo /key_vel
Page 50 of 51
RMIT Classification: Trusted
Page 51 of 51