Skip to content

Change controllers

This tutorial relies on the usage of the ros2_control cli.

Controller manager services

The controller_manager is the main component managing lifecycle of controllers. To change controller through the command line, one has to do a call to the corresponding controller manager service. The ros2_control framework offers also a command line interface (cli) for it

The controller manager has 4 main services

List controllers

To load all the loaded controllers, one has to call the service list_controllers:

ros2 service call /controller_manager/list_controllers controller_manager_msgs/srv/ListControllers

or just

ros2 control list_controllers

using the ros2_control cli.

Load controller

To add a controller in the list of available loaded controllers one has to make a call to the /controller_manager/load_controller service. The parameter file for the specific controller must be previously spawned within the controller manager node, otherwise the loading will fail. Do:

ros2 service call /controller_manager/load_controller controller_manager_msgs/srv/LoadController "name:'controller_name'" 

or just

ros2 control load_controller controller_name

using the ros2_control cli.

It returns true if the controller has been loaded correctly, and false otherwise. After being loaded, the controller is unconfigured. This is one of the so-called states of a controller in the ros2_control framework. The 3 states are unconfigured, inactive and active. The controller state can be optionally set during the loading using the following ros2_control cli command:

ros2 control load_controller controller_name --set-state active

That would set the controller to the active state.

Configure controller

If one wants to set the state to inactive, one has to make a call to the /controller_manager/configure_controller

ros2 service call /controller_manager/configure_controller controller_manager_msgs/srv/ConfigureController
     "name: controller_name"

or using the ros2_control cli

ros2 control set_controller_state controller_name active

Switch controllers

The /controller_manager/switch_controllers service starts and/or stops a set of controllers. A controller should be active in order to be stopped. A controller must be loaded before starting it. And a controller can only be activated if their required resources are not used by another controller. The service can be called as follows:

ros2 service call /controller_manager/switch_controller controller_manager_msgs/srv/SwitchController
     "{ activate_controllers: ["whole_body_kinematic_controller"],
     deactivate_controllers:["head_controller", "arm_controller", "torso_controller"]}"

or using the ros2_control cli

ros2 control switch_controllers --activate whole_body_kinematic_controller
     --deactivate_controllers head_controller, arm_controller, torso_controller

It is recommended to start and stop the desired controllers in one service call. Stopping the controller stops sending any current to the motor and hence the arm of the robot would fall. For example, to activate the gravity compensation controller one has to first deactivate the default arm_<side>_controller.

The service returns true if the switch has been successfully executed, false otherwise.

Unload controller

The service /controller_manager/unload_controller unloads a controller. The controller must be stopped before being unloaded.

The service can be called in the following way

ros2 service call /controller_manager/unload_controller controller_manager_msgs/srv/UnloadController
     "name: controller_name"

or through the ros2_control cli doing

ros2 unload_controller controller_name

It returns true if the controller has been unloaded correctly, and false otherwise.

Once a controller has been unloaded, it will be necessary to load it again in order to restart it.

Change controllers action

The /change_controllers action uses the ros2 service API of the controller manager, but allows the change of controllers in a simple and intuitive way with a single call. The user doesn’t need to know which controllers are active and which resources are being used at any time. The action automatically stops the active controllers that share resources with those who want to be loaded. The goal message has different components:

  • start_controllers: a list containing the names of the controllers to be started
  • stop_controllers: a list containing the names of the controllers to be stopped
  • switch_controllers: If true it stops and starts the controllers in a single switch service call. Otherwise, first it calls stop and then it calls start. By default this should be always True, specially when gravity compensation controller needs to be stopped.
  • load: If true it loads the controllers in start_controllers.
  • unload: If true it unloads the requested stop_controllers. This doesn’t affect the controllers that are automatically stopped because they share resources with the controllers that are going to be started.
  • configure: If true it configures the controllers in start_controllers. The parameter file of the requested controller must be previously uploaded in the controller manager node.

This action is also optimized to avoid issues with PAL controllers, as for example the whole body kinematic controller that needs to be unloaded and loaded every time.

The following is an example of use

ros2 action send_goal /change_controllers change_controllers_interfaces/action/Switch
    "{start_controllers: [whole_body_kinematic_controller] ,
    stop_controllers: [head_controller, arm_controller, torso_controller],
    switch_controllers: true,
    load: true,
    unload: true,
    configure: true}"

This server is launched at startup in the robot. In simulation one has to start it as follows

ros2 run change_controllers change_controllers