Naming and Organizing Packages in Large ROS 2 Projects

In this tutorial, I discuss the best practices for naming and organizing packages in ROS 2 projects. 

You can find the official best practices on this page. I will elaborate on the most important best practices below, as well as add some best practices that the official guide left out.

Use a Prefix for Robot-Specific Functionality

Add the prefix “my_robot_” for packages that are specifically designed for your robot (credit to RoboticsBackend.com for this one).

Use a Suffix to Describe the Purpose of a Package

To the end of a package name, add a word that is specific enough to describe what the package does (e.g. my_robot_navigation).

Package Names Should Be In Lowercase

Package names should follow common C variable naming conventions: lower case, start with a letter, use underscore separators, e.g. my_robot_navigation.

Make It Easy to Maintain

You should be able to make changes in one package without breaking other packages.

One Package, One Purpose

Each package should have only one purpose.

Minimize Dependencies

Each package should only use the dependencies that it needs.

Example ROS 2 Project Package Architecture

For example, consider the following packages that might reside in a folder named ~/ros2_ws/src/my_robot :

  • my_robot
  • my_robot_base
  • my_robot_bringup 
  • my_robot_description
  • my_robot_gazebo
  • my_robot_kinematics
  • my_robot_localization
  • my_robot_manipulation
  • my_robot_moveit_config
  • my_robot_msgs 
  • my_robot_navigation
  • my_robot_teleop
  • my_robot_tests
  • my_robot_rviz_plugins

Let’s write the purpose for each package below:

  • my_robot (This package is a metapackage. A metapackage doesn’t contain anything except a list of dependencies to other packages. You can use a metapackage to make it easier to install multiple related packages at once. Package installation tools like apt-get can automatically install all the ROS2 packages on which a metapackage directly or indirectly depends. Example.).
  • my_robot_base (This package is used to control the motors of your robot. Example.)
  • my_robot_bringup (Put the ROS 2 launch files that bring up the robot inside this folder. Example.)
  • my_robot_description (Contains the URDF and mesh files of your robot. Example.)
  • my_robot_gazebo (Configuration and launch files for spawning the robot in Gazebo. Example)
  • my_robot_kinematics (Your forward and inverse kinematics algorithms go here. Example.)
  • my_robot_localization (Files for localizing in an environment. Example.)
  • my_robot_manipulation (Contains algorithms for manipulating objects in the environment. Example.)
  • my_robot_moveit_config (Configuration files using the MoveIt framework. Example.)
  • my_robot_msgs (Contains custom messages, services, and actions. Example.)
  • my_robot_navigation (Contains configuration and launch files for the ROS 2 Navigation Stack. Example.)
  • my_robot_teleop (A node for manually tele-operating a robot using a keyboard, joystick, game console controller, etc. Example.)
  • my_robot_tests (A package used for system tests. Example.)
  • my_robot_rviz_plugins (RViz specific plugins go here. Example.)

That’s it. Keep building!

Attributes of High-Quality ROS2-Based Systems

Below are the attributes of high-quality ROS2-based systems.

Compatibility

Use standard ROS 2 message formats as much as possible to ensure the project is compatible with other ROS2-based systems.

Avoid using non-standard tools or libraries that may not be accessible to everyone.

Independence

Design ROS 2 nodes to be as hardware-independent as possible.

Extendibility

Make it easy to add new features and functionality.

Maintainability

Write and document code so that it is easy to make changes in the future.

Modularity

Divide nodes into small, independent, loosely coupled, highly cohesive, functional modules to avoid high technical debt. Each node should have a clear responsibility, and make sure nodes have minimal dependence on each other.

The easiest way to ensure modularity is to isolate what changes. Code that is likely to change in the future should be isolated from the rest of the system.

Performance

Software should be responsive and stable when run under its expected workload.

One technique for improving performance and reducing overhead at runtime is to not persist raw data if only a piece of it will be used. 

For example, if you have a robotic arm on a conveyor belt with an overhead camera, save only the pixels of the camera image that change. Do not save all pixels from all video frames. This technique was used in this ROS system.

Readability

Make code easy to understand and follow. Someone else should be able to look at your code and figure out what is going on.

Reliability

The system should behave as it is expected to behave. You can achieve good reliability by running many tests on the software to minimize the probability of future failure.

Resiliency

ROS 2 nodes should be able to handle gaps in sensor data as well as times when there is an overload of sensor data. Using an Extended Kalman Filter to estimate the robot’s state via a motion model is a good design choice.

Reusability

Write code so that it could easily be reused in an entirely different project.

Safety

Think about different scenarios that could cause the robot to do harm to a person or object, and write code to handle these scenarios (e.g. run into a wall at full speed).

Security

Add safeguards to the system to make it robust to tampering. 

Testability

Make it easy to debug your code and ensure the system is working correctly.

“Anything that can go wrong, will go wrong.”

Murphy’s Law

Think of all the ways that your code can break under abuse from users, and write test cases for those scenarios to see how your system holds up.

What Are the Most Important Attributes?

This paper mentions that maintainability, performance, and reliability (in order from most important to least important) are the most important attributes for a ROS-based system.

References

I. Malavolta, G. Lewis, B. Schmerl, P. Lago and D. Garlan, “How do you Architect your Robots? State of the Practice and Guidelines for ROS-based Systems,” 2020 IEEE/ACM 42nd International Conference on Software Engineering: Software Engineering in Practice (ICSE-SEIP), 2020, pp. 31-40.

How to Run an Inspection With a Robot – ROS 2 Navigation

In this tutorial, I will show you how to command a simulated autonomous mobile robot to carry out an inspection task using the ROS 2 Navigation Stack (also known as Nav2). Here is the final output you will be able to achieve after going through this tutorial:

mobile-inspection-robot

Real-World Applications

The application that we will develop in this tutorial can be used in a number of real-world robotic applications: 

  • Hospitals and Medical Centers
  • Hotels (e.g. Room Service)
  • House
  • Offices
  • Restaurants
  • Warehouses
  • And more…

We will focus on creating an application that will enable a robot to perform an inspection inside a house.

Prerequisites

You can find the files for this post here on my Google Drive. Credit to this GitHub repository for the code.

Create a World

Open a terminal window, and move to your package.

cd ~/dev_ws/src/two_wheeled_robot/worlds

Make sure this world is inside this folder. The name of the file is house.world.

Create a Map

Open a terminal window, and move to your package.

cd ~/dev_ws/src/two_wheeled_robot/maps/house_world

Make sure the pgm and yaml map files are inside this folder.

My world map is made up of two files:

  • house_world.pgm
  • house_world.yaml

Create the Parameters File

Let’s create the parameters file.

Open a terminal window, and move to your package.

cd ~/dev_ws/src/two_wheeled_robot/params/house_world

Add this file. The name of the file is nav2_params.yaml.

Create the RViz Configuration File

Let’s create the RViz configuration file.

Open a terminal window, and move to your package.

cd ~/dev_ws/src/two_wheeled_robot/rviz/house_world

Add this file. The name of the file is nav2_config.rviz.

Create a Python Script to Convert Euler Angles to Quaternions

Let’s create a Python script to convert Euler angles to quaternions. We will need to use this script later.

Open a terminal window, and move to your package.

cd ~/dev_ws/src/two_wheeled_robot/two_wheeled_robot

Open a new Python program called euler_to_quaternion.py.

gedit euler_to_quaternion.py

Add this code.

Save the code, and close the file.

Change the access permissions on the file.

chmod +x euler_to_quaternion.py 

Since our script depends on NumPy, the scientific computing library for Python, we need to add it as a dependency to the package.xml file.

cd ~/dev_ws/src/two_wheeled_robot/
gedit package.xml
<exec_depend>python3-numpy</exec_depend>

Here is the package.xml file. Add that code, and save the file.

To make sure you have NumPy, return to the terminal window, and install it.

sudo apt-get update
sudo apt-get upgrade
sudo apt install python3-numpy

Add the Python Script

Open a terminal window, and move to your package.

cd ~/dev_ws/src/two_wheeled_robot/scripts

Open a new Python program called run_inspection.py.

gedit run_inspection.py

Add this code.

Save the code, and close the file.

Change the access permissions on the file.

chmod +x run_inspection.py

Open a new Python program called robot_navigator.py.

gedit robot_navigator.py 

Add this code.

Save the code and close the file.

Change the access permissions on the file.

chmod +x robot_navigator.py 

Open CMakeLists.txt.

cd ~/dev_ws/src/two_wheeled_robot
gedit CMakeLists.txt

Add the Python executables.

scripts/run_inspection.py
scripts/robot_navigator.py

Create a Launch File

Add the launch file.

cd ~/dev_ws/src/two_wheeled_robot/launch/house_world
gedit house_world_inspection.launch.py

Save and close.

Build the Package

Now we build the package.

cd ~/dev_ws/
colcon build

Open a new terminal and launch the robot.

ros2 launch two_wheeled_robot house_world_inspection.launch.py

Now command the robot to perform the house inspection by opening a new terminal window, and typing:

ros2 run two_wheeled_robot run_inspection.py

The robot will perform the inspection.

1-launch-inspection-robot-1
2-done

To modify the coordinates of the waypoints located in the run_inspection.py file, you can use the Publish Point button in RViz and look at the output in the terminal window by observing the clicked_point topic.

ros2 topic echo /clicked_point

Each time you click on an area, the coordinate will publish to the terminal window.

Also, if you want to run a node that runs in a loop (e.g. a security patrol demo), you can use this code.

To run that node, you would type:

ros2 run two_wheeled_robot security_demo.py

That’s it! Keep building!