Table of Contents
Introduction
ROS2 is an open-source framework on which a huge number of people are working. To make all the work from these contributors useful, development standards are introduced. People who are just starting out have to follow procedures that make things confusing and hard . For example
- Every change needs to be build before running
- A lot of files are auto generated and you find them usless and confusing
- Colcon build has to be in workspace root; otherwise, other wise things do not work.
Starting Point
- No understanding of fies in a ROS2 package
- Confused by auto generated structure in a package
- Cannot decide between Python and C++ package
Learning Outcomes
- When to use CPP Package and when to use Python Package
- Comfortable with CmakeLists and setup.py
- Clear about structure generated by Colcon build
What are the requirements of installing ROS2
Ubuntu Specific Compatibility
ROS2 versions have specific documentation pages,like this one is for ros2 humble . ROS2 distributions are closely tied to specific Ubuntu versions. This ensures that developers have a stable and consistent environment, as the underlying libraries and tools provided by Ubuntu are version-specific and tested for compatibility with ROS2.The tight coupling between ROS2 versions and Ubuntu ensures that the dependencies and libraries used by ROS2 are stable and tested. This minimises compatibility issues and provides a consistent development environment.
Although this can be taken care of through Docker or virtual machines,. Let’s discuss the architecture of ROS2 development.
What are ROS2 Workspaces and packages?
Workspace : System-level isolation
A workspace in ROS2 is a directory where you can create, build, and manage your ROS2 projects. It serves as an isolated environment where all the necessary files and dependencies for your projects are stored. Workspaces provide a structured way to organise your projects. They help manage different versions of packages, dependencies, and configurations, ensuring that your development environment remains clean and manageable.
Workspace is simply a directory on your computer before a package and colcon building. You create it with below commands
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws
colcon build
This colcon build in ros2_ws creates the following directories ( independent of location; starters make this mistake a lot and every where they have below mentioned directors):
ros2_ws
├── build # Stores intermediate build files and compilation artifacts.
├── log # Stores log files generated during the build and run processes.
├── install # Contains the final built executables, scripts, and resources ready for deployment.
└── src # Contains the source code of your ROS2 packages.
Package : Development level isolation
Packages are the fundamental units of software in ROS2. They contain nodes, libraries, datasets, configuration files, and more, all bundled together to perform specific tasks.Packages modularize the code, making it easier to develop, share, and maintain. They encapsulate functionality and dependencies, allowing developers to reuse and build upon existing work.
They are created inside of src directory of workspace hrough below mentioned commands respectively
ros2_ws
├── build
├── log
├── install
└── src
├── ros2_cpp_pkg
├── ros2_py_pkg
C++ Package
- Creation command
ros2 pkg create --build-type ament_cmake ros2_cpp_pkg
- File Structure Generated
ros2_cpp_pkg
├── CMakeLists.txt
├── include
│ └── ros2_cpp_pkg
├── package.xml
└── src
Python Package
- Creation command
ros2 pkg create --build-type ament_python ros2_py_pkg
- File Structure Generated
ros2_py_pkg
├── package.xml
├── resource
│ └── ros2_py_pkg
├── ros2_py_pkg
│ └── __init__.py
├── setup.cfg
├── setup.py
└── test
├── test_copyright.py
├── test_flake8.py
└── test_pep257.py
Important files and thier purpose
- CMakeLists.txt : This file tells ROS2 how to build your package.
- package.xml : This file contains metadata about your package like dependencies and version.
- src/main.cpp : This is where you write the main logic for your node.
- include/package_name/header.hpp : This file holds function declarations and class definitions.
- setup.py : This file is used to configure the installation of your package.
- package.xml : This file contains metadata about your package like dependencies and version.
- node_name.py : This script contains the main logic for your ROS2 node.
Why we have these auto generated files ?
Auto-generating files reduce boilerplate code, minimise errors, and save development time, allowing developers to focus on core functionality. The most important thing is that it ensures consistency, and that is a need in an open-source framework. It allows developers to focus on the core functionality rather than repetitive setup tasks. While many files are auto-generated, they can often be customised to suit specific needs.
When you are starting out , only few files will be going to make sense . Do not worry about other files .
How to decide between creating a C++ or Python package.
If you are just starting out and not comfortable with C++ and Cmakelists then just go with python.
C++ is a compiled language and generally offers better performance compared to Python.
- Real-time systems, high-frequency data processing, or computationally intensive tasks such as image processing and control loops.
- Direct interaction with sensors, actuators, or other hardware components.
- Embedded systems or applications with strict memory constraints
Python is an interpreted language, known for its simplicity and ease of use.
- Prototyping, scripting, and projects where development speed and ease of iteration are more critical than raw performance.
- Applications that require integration with machine learning models, data analysis, or web interfaces.
- Educational purposes, learning ROS2, or when leveraging existing Python-based ROS2 packages and tutorials is easier.
When to Have Both C++ and Python Nodes in One Package
Some projects benefit from the strengths of both languages.
- Applications that require high-performance nodes for critical tasks (C++) and flexible, easy-to-develop nodes for less critical or high-level logic (Python).
- Large projects with distinct components, such as a robotics system with high-performance motion control (C++) and a high-level decision-making layer (Python).
- Integrating a C++ library for sensor data processing with a Python-based machine learning model for decision making.
Why we perform colcon build ?
It is a recommended command-line tool used to build and test ROS2 python , cpp packages. You move into your root of workspace and run colcon build to perform following tasks.
- Identifies the build types (
ament_cmake
for C++ andament_python
for Python) and uses appropriate build tools (CMake for C++ and setuptools for Python) . Allows for building both C++ and Python packages in the same workspace seamlessly. - it handles dependencies for both
package.xml
andCMakeLists.txt
(for C++) orsetup.py
(for Python) - It compiles executables for C++ nodes and installed Python scripts for Python nodes which are placed in the
install
directory. Ensures that all nodes are available for execution regardless of the language they are written in.
Summary
Starting with ROS2 can seem daunting due to its structured development environment and the array of auto-generated files. However, understanding the purpose of workspaces and packages and knowing when to use C++ or Python will significantly ease your journey. Focus on the core files like and,setup.py
, and remember that thecolcon build
process is essential for integrating your code. With patience and practice, you’ll become comfortable with these concepts and be well-equipped to build robust robotics applications. Embrace the learning curve, and you’ll soon find ROS2 a powerful and flexible tool for your robotics projects.
FAQ Section
- What is a workspace in ROS2?
- A workspace is a directory structure where ROS2 projects are created, built, and managed, providing an isolated environment for development.
- Why are ROS2 versions specific to Ubuntu?
- ROS2 versions are tied to specific Ubuntu versions to ensure stability and compatibility with the underlying libraries and tools.
- What is colcon build?
- It is a command-line tool used to build, test, and package ROS2 projects, offering advanced features like parallel builds and sophisticated dependency management.
- Why are many files in ROS2 auto-generated?
- Auto-generating files reduces boilerplate code, minimizes errors, and saves development time, allowing developers to focus on core functionality.
- How do you decide between a C++ or Python package in ROS2?
- Choose C++ for performance-critical applications and Python for rapid development and flexibility. A hybrid approach can also be beneficial in some cases.
- What tools are available for ROS2 development?
- Key tools include RViz for visualization, Gazebo for simulation, and rqt for creating graphical interfaces.