UP | HOME

The ROS API and Python

Client Libraries

ROS client libraries are how generic ROS concepts such as Nodes, Topics, Services etc. are actually implemented and used in a given programming language.

There are three primarily supported client libraries:

  • rclcpp is the C++ client library.
  • rclpy is the Python client library.
  • rcl is the C client library, upon which the other client libraries are built.

Volunteers also maintain client libraries for many other programming languages, but these are not officially "supported" by ROS.

rclpy

rclpy is a python library that allows a python program to become a ROS 2 node.

  1. import rclpy to use the client library.
  2. rclpy.node defines the Node class
    • One way to make nodes in ROS 2 is to sub-class rclpy.node
    • We focus on this method because it is the most flexible and makes it easier to combine nodes within the same python process.
    • It is also possible to instantiate an rclpy.node.Node object and call it's methods directly.
  3. Each python ROS 2 node has an entry_point function that is called when the node is started.
    • The entrypoint function can have any name, and it is registered in the setup.py.
    • The def entry_point_name(args=None) function is passed command line arguments.
    • Call rclpy.init(args=args) to have the node parse its ROS command line arguments
  4. The Node class has methods that enable
  5. There are many other ROS functions and classes available in rclpy, look at the documentation.
  6. An example of some ROS API functions is available at https://github.com/m-elwin/me495_demo

Using Interfaces

To use interfaces from a ROS node you must import the corresponding package. This will expose an object that contains the elements in that interface.

# import for a package/msg/MessageType
from package.msg import MessageType

# import for a package/srv/ServiceType
from package.srv import  ServiceType

# The request type:
ServiceType.Request()

# The response Type:
ServiceType.Response()

# Each object has the same field names as set in the IDL file
# These types can also be constructed by providing keyword arguments to the constructor
# Which in turn contains the field names of the type

Author: Matthew Elwin.