Writing a new controller
In this framework controllers are libraries, dynamically loaded by the controller manager using the pluginlib interface. The following is a step-by-step guide to create source files, basic tests, and compile rules for a new controller.
Preparing package
If the package for the controller does not exist, then create it first. The package should have
ament_cmakeas a build type. The easiest way is to search online for the most recent manual. A helpful command to support this process isros2 pkg create. Use the--helpflag for more information on how to use it. There is also an option to create library source files and compile rules to help you in the following steps.Preparing source files
After creating the package, you should have at least
CMakeLists.txtandpackage.xmlfiles in it. Create alsoinclude/<PACKAGE_NAME>/andsrcfolders if they do not already exist. Ininclude/<PACKAGE_NAME>/folder add<controller_name>.hppand<controller_name>.cppin thesrcfolder. Optionally addvisibility_control.hwith the definition of export rules for Windows. You can copy this file from an existing controller package and change the name prefix to the<PACKAGE_NAME>.Adding declarations into header file (.hpp)
Take care that you use header guards. ROS2-style is using
#ifndefand#definepreprocessor directives. (For more information on this, a search engine is your friend :) ).include
"controller_interface/controller_interface.hpp"andvisibility_control.hif you are using one.Define a unique namespace for your controller. This is usually a package name written in
snake_case.Define the class of the controller, extending
ControllerInterface, e.g., .. code:: c++ class ControllerName : public controller_interface::ControllerInterfaceAdd a constructor without parameters and the following public methods overriding the
ControllerInterfacedefinition:init,command_interface_configuration,state_interface_configuration,on_configure,on_activate,on_deactivate,update. For exact definitions check thecontroller_interface/controller_interface.hppheader or one of the controllers from ros2_controllers.(optional) Often, controllers accept lists of joint names and interface names as parameters. If so, you can add two protected string vectors to store those values.
Adding definitions into source file (.cpp)
Include the header file of your controller and add a namespace definition to simplify further development.
(optional) Implement a constructor if needed. There, you could initialize member variables. This could also be done in the
initmethod.Implement the
initmethod. The first line usually calls the parentinitmethod. Here is the best place to initialize the variables, reserve memory, and most importantly, declare node parameters used by the controller. If everything works fine returncontroller_interface::return_type::OKorcontroller_interface::return_type::ERRORotherwise.Write the
on_configuremethod. Parameters are usually read here, and everything is prepared so that the controller can be started.Implement
command_interface_configurationandstate_interface_configurationwhere required interfaces are defined. There are three options of the interface configurationALL,INDIVIDUAL, andNONEdefined incontroller_interface/controller_interface.hpp".ALLandNONEoption will ask for access to all available interfaces or none of them. TheINDIVIDUALconfiguration needs a detailed list of required interface names. Those are usually provided as parameters. The full interface names have structure<joint_name>/<interface_type>.Implement the
on_activatemethod with checking, and potentially sorting, the interfaces and assigning members’ initial values. This method is part of the real-time loop, therefore avoid any reservation of memory and, in general, keep it as short as possible.Implement the
on_deactivatemethod, which does the opposite ofon_activate. In many cases, this method is empty. This method should also be real-time safe as much as possible.Implement the
updatemethod as the main entry point. The method should be implemented with real-time constraints in mind. When this method is called, the state interfaces have the most recent values from the hardware, and new commands for the hardware should be written into command interfaces.IMPORTANT: At the end of your file after the namespace is closed, add the
PLUGINLIB_EXPORT_CLASSmacro. For this you will need to include the"pluginlib/class_list_macros.hpp"header. As first parameters you should provide exact controller class, e.g.,<controller_name_namespace>::<ControllerName>, and as second the base class, i.e.,controller_interface::ControllerInterface.
Writing export definition for pluginlib
Create the
<controller_name>.xmlfile in the package and add a definition of the library and controller’s class which has to be visible for the pluginlib. The easiest way to do that is to check other controllers in the ros2_controllers package.Usually, the plugin name is defined by the package (namespace) and the class name, e.g.,
<controller_name_package>/<ControllerName>. This name defines the controller’s type when the controller manager searches for it. The other two files have to correspond to the definition done in macro at the bottom of the<controller_name>.cppfile.
Writing simple test to check if the controller can be found and loaded
Create the folder
testin your package, if it does not exist already, and add a file namedtest_load_<controller_name>.cpp.You can safely copy the file’s content for any controller defined in the ros2_controllers package.
Change the name of the copied test and in the last line, where controller type is specified put the name defined in
<controller_name>.xmlfile, e.g.,<controller_name_package>/<ControllerName>.
Add compile directives into ``CMakeLists.txt`` file
Under the line
find_package(ament_cmake REQUIRED)add further dependencies. Those are at least:controller_interface,hardware_interface,pluginlib,rclcppandrclcpp_lifecycle.Add a compile directive for a shared library providing the
<controller_name>.cppfile as the source.Add targeted include directories for the library. This is usually only
include.Add ament dependencies needed by the library. You should add at least those listed under 1.
Export for pluginlib description file using the following command: .. code:: cmake
pluginlib_export_plugin_description_file(controller_interface <controller_name>.xml)
Add install directives for targets and include directory.
In the test section add the following dependencies:
ament_cmake_gmock,controller_manager,hardware_interface,ros2_control_test_assets.Add compile definitions for the tests using the
ament_add_gmockdirective. For details, see how it is done for controllers in the ros2_controllers package.(optional) Add your controller`s library into
ament_export_librariesbeforeament_package().
Add dependencies into ``package.xml`` file
Add at least the following packages into
<depend>tag:controller_interface,hardware_interface,pluginlib,rclcppandrclcpp_lifecycle.Add at least the following package into
<test_depend>tag:ament_add_gmock,controller_manager,hardware_interface, andros2_control_test_assets.
Compiling and testing the controller
Now everything is ready to compile the controller using the
colcon build <controller_name_package>command. Remember to go into the root of your workspace before executing this command.If compilation was successful, source the
setup.bashfile from the install folder and executecolcon test <controller_name_package>to check if the new controller can be found throughpluginliblibrary and be loaded by the controller manager.
That’s it! Enjoy writing great controllers!