ROS2 Core Concepts [Creating a Python Package]

ROS (Robot Operating System)

Python Package

Python Packages are a way to organize and structure your Python code into reusable components. Think of it like a folder that contains related Python files (modules) that work together to provide certain functionality. Packages help keep your code organized, make it easier to manage and maintain, and allow you to share your code with others. They're like a toolbox where you can store and organize your tools (functions and classes) for easy access and reuse in different projects.

~$ ros2 pkg -h
~$ ros2 pkg create -h
ROS2 pkg create
~/ros2_ws/src$ ros2 pkg create --build-type ament_python test_pkg

ROS2 pkg folder structure

ROS2 pkg folder structure

ROS2 pkg info

Executing Node as Python Script

~/ros2_ws/src $ cd ..
colcon build

Alternative Method:

~/ros2_ws $ colcon build --packages-select test_pkg
~/ros2_ws/src/test_pkg/test_pkg $ colcon build --packages-select test_pkg
~/ros2_ws/src/test_pkg/test_pkg $ touch test_node.py
~/ros2_ws/src/test_pkg/test_pkg $ chmod +x test_node.py
~/ros2_ws/src/test_pkg/test_pkg $ ./test_node.py

No error, but, nothing is displayed

Open src in vscode and open test_node.py. Write a small node to print "Hi from test_package"

Executing Node as Python Script
~/ros2_ws/src/test_pkg/test_pkg $ ./test_node.py

$ Hi from test_package

Executing the Script as Python Package

Open setup.py. Add "node_exe=test_pkg.test_node:main"

Executing the Script as Python Package
~/ros2_ws$ colcon build
~/ros2_ws $ ros2 run test_pkg node_exe

Open test_node.py.

Change to "Hi from test_package-1". Save

~/ros2_ws $ ros2 run test_pkg node_exe

No change observed. To see changes, build again

Creating Python Node

A typical ROS program consists of the following operations:

  1. Initialization
  2. Create one or more ROS nodes
  3. Process node callbacks
  4. Shutdown
https://docs.ros.org/en/foxy/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html CreatingPythonNode

rqt is a graphical user interface (GUI) tool for ROS 2. Everything done in rqt can be done on the command line, but rqt provides a more user-friendly way to manipulate ROS 2 elements.

ROS2 Commands : 'list' and 'info'

ros2 -h
ros2 run -h
ros2 node
ros2 topic
ros2 service
ros2 service -h
ros2 node list
ros2 topic list
ros2 service list
ros2 action list
ros2 node info
ros2 topic info
ros2 service info
ros2 action info

Use ros2 node list to discover active node names.

Use ros2 node info to introspect on a single node.

ros2 pkg list
ros2 pkg --help

The ros2 pkg list command is a command-line tool used in ROS 2 to list all the packages that are available in the system.

When this command is executed, ROS 2 searches the package paths defined in the AMENT_PREFIX_PATH environment variable and lists all the packages it finds. The output is a list of package names, one per line.

This command is useful when working with ROS 2 as it allows the user to quickly check which packages are available in the system, and can be used as a starting point for building, testing, or using ROS 2 packages. It can also be used to check if a specific package is installed in the system or to verify that a new package has been added to the AMENT_PREFIX_PATH correctly.

Using Domain ID and Localhost

~$ export ROS_DOMAIN_ID=user_defined_domain_id
~$ export ROS_LOCALHOST_ONLY=1

ROS2 Messages

Messages are the data structures that are used to exchange information between nodes.

ROS2Messages

ROS 2 uses a the interface definition language (IDL), to describe these interfaces.

This description makes it easy for ROS tools to automatically generate source code for the interface type in several target languages.

Messages are described and defined in .msg files in the msg/directory of a ROS package. .msg files are composed of two parts: fields and constants.

A data structure is a storage that is used to store and organize data.

It is a way of arranging data on a computer so that it can be accessed and updated efficiently.

fieldtype1 fieldname1

fieldtype2 fieldname2

fieldtype3 fieldname3

int32 my_int

string my_string

A single node can publish to multiple topics, subscribe to multiple topics, offer multiple services, and provide multiple actions. The specific implementation depends on the requirements of the system being built and the design choices made by the developer.

Thank You. End of ROS2 Core Concepts.