The ROS Robot Package Structure

This article explains a good layout for a ROS-based robot’s code. This includes packages, meta-packages, launch and config.

Packages

Packages are the main unit of ROS code. They can contain nodes, launch files, and configuration files. A package can depend on other packages. A package is a collection of directories that define a specific type of functionality. The package is the unit of organization and distribution in ROS. These are the main types of packages:

  • Library packages contain code that can be used by other packages.
  • Node packages contain executable code that runs in the ROS ecosystem.
  • Meta-packages contain only a package.xml file. They are used to group other packages together.

Package Structure

A package must have a specific structure. This is the basic structure of a ROS package:
my_package/
  CMakeLists.txt
  package.xml
  launch/
    my_node.launch
  src/
    my_node.cpp
  include/
    my_package/
      my_node.h
  config/
    my_node.yaml

Meta-packages

Meta-packages are used to group several packages together. They are useful for organizing code in a large project. For example, you might have a meta-package for a robot that contains packages for the robot’s sensors, actuators, and controllers. The directory structure of a meta-package should look something like this:

meta_package/
  meta_package/
    CMakeLists.txt
    package.xml
  package1/
  package2/
  package3/

Robot Package Structure

A robot package should contain all the code needed to run the robot. This includes the robot’s sensors, actuators, controllers, and any other code needed to control the robot. A good way to manage all this code is separate it into different packages. The robot application is just the collection of all these packages and tracked using a meta-package. Here is an example of a robot package structure:

my_robot/
  my_robot/
    CMakeLists.txt
    package.xml
  my_robot_drivers/
    sensor1/
    sensor2/
  my_robot_control/
  my_robot_controllers/
  my_robot_description/
  my_robot_navigation/

Common robot specific packages

Most robots need a few common packages that are unique to the robot. These are some common robot-specific packages:
descriptionThis package contains the URDF and XACRO files that describe the robot’s physical structure.
controlThis package contains the code that controls the robot using the ros-control framework.
controllersThis package contains custom controllers and launch files for controllers using the ros-control framework.
navigationThis is a common package for robots that need to navigate in a 2D or 3D space. It contains the code for the robot’s navigation stack.
moveit_configThis package contains the configuration files for the MoveIt! motion planning framework. This is used in robot manipulation applications.
driversThis package contains the code for the robot’s sensors and actuators. This is often a meta-package that contains several other packages, or references other packages as dependencies.
gazeboThis package contains the Gazebo simulation files for the robot. This is useful for testing the robot in a simulated environment.