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.
import rclpyto use the client library.rclpy.nodedefines theNodeclass- 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.Nodeobject and call it's methods directly.
- One way to make nodes in ROS 2 is to sub-class
- Each python ROS 2 node has an
entry_pointfunction 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
- The entrypoint function can have any name, and it is registered in the
- The
Nodeclass has methods that enable- Creating service servers: create_service
- Creating service clients: create_client
- Creating publishers: create_publisher
- Creating subscribers: create_subscription
- Creating timers: create_timer
- Declaring and using parameters: declare_parameters, get_parameter
- The rcl_interfaces package defines some types that are useful for describing and interacting with parameters.
- For example, there are a few basic data types for parameters, outlined in the ParameterType message
- The ParameterDescriptor message allows setting metadata for parameters
- Logging:
- Obtain the logger associated with a node using Node.get_logger
- The Logger API
- There are many other ROS functions and classes available in
rclpy, look at the documentation. - 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
- It is also possible to make Custom Interfaces