Overview
A ROS2 package is the fundamental unit of software organization in ROS2. Packages contain nodes, launch files, configuration files, and other resources needed for specific robot functionality. For the mecanum robot, you’ll create multiple packages to organize different subsystems.Modular Design
Separate packages for description, control, navigation, etc.
Reusability
Share packages across projects and with the community
Package Structure
Typical ROS2 Python Package
Typical ROS2 C++ Package
Creating a Package
Using ros2 pkg create
1
Navigate to Workspace
2
Create Python Package
3
Add Dependencies (Optional)
4
Build Package
5
Verify Package
Package Metadata (package.xml)
Thepackage.xml file defines package information and dependencies:
package.xml
<buildtool_depend>: Build tools (ament_python, ament_cmake)<build_depend>: Needed to build package (compile-time)<exec_depend>: Needed to run package (runtime)<test_depend>: Needed for testing only<depend>: Needed for both build and runtime (shorthand)
Setup Files
setup.py (Python Packages)
setup.py
packages: Python modules to installdata_files: Non-Python files (launch, config, URDF)entry_points: Executable nodes that can be run withros2 run
setup.cfg (Python Packages)
setup.cfg
Mecanum Robot Package Organization
Recommended Package Structure
- mecanum_description: Robot physical model (URDF, meshes)
- mecanum_control: Low-level motor control
- mecanum_localization: Sensor fusion and odometry
- mecanum_navigation: Autonomous navigation configuration
- mecanum_bringup: Master launch files to start entire system
Creating a Custom Node
Example: Motor Controller Node
1
Create Python File
2
Write Node Code
motor_controller.py
3
Register Node in setup.py
setup.py
4
Build and Run
Building Packages
colcon Build System
After Building
Package Dependencies
Adding Dependencies
- Runtime Dependencies
- Python Import Dependencies
- System Dependencies
Edit No rebuild needed for pure runtime dependencies
package.xml:Checking Dependencies
Package Best Practices
Naming Conventions
- Use lowercase with underscores:
mecanum_control - Descriptive names:
mecanum_navigationnotnav - Suffix purpose:
_description,_control,_bringup - Avoid generic names:
robot,control,utils
Package Scope
- One package = one logical subsystem
- Keep packages focused and cohesive
- Separate configuration from code
- Group related launch files together
Documentation
- Add README.md to each package
- Document dependencies and usage
- Include example launch commands
- Explain configuration parameters
Version Control
.gitignorebuild artifacts:- Commit package.xml and setup.py
- Tag releases (v1.0.0, v2.0.0)
Workspace Overlays
ROS2 supports workspace overlays for extending functionality:Troubleshooting
Package not found after building
Package not found after building
Symptoms:
Package 'mecanum_control' not foundSolutions:-
Re-source workspace:
-
Verify package built successfully:
-
Check package name matches directory:
Node executable not found
Node executable not found
Symptoms:
Executable 'motor_controller' not foundSolutions:-
Verify
entry_pointsinsetup.py: -
Rebuild package:
-
Check Python file has
main()function:
Import errors in Python nodes
Import errors in Python nodes
Symptoms:
ModuleNotFoundError: No module named 'numpy'Solutions:-
Add dependency to
package.xml: -
Install system dependency:
-
Use rosdep to install all dependencies:
Changes not reflected after rebuild
Changes not reflected after rebuild
Solutions:
-
Always re-source after building:
-
Use
--symlink-installfor faster iteration: -
For C++ or setup.py changes, clean build:
Next Steps
Launch Files
Start multiple nodes with a single command
Parameters
Configure nodes with YAML parameter files
Custom Messages
Create custom ROS2 message types (advanced)
Testing
Write unit tests for ROS2 nodes (pytest)