ros2_control, from the error variable to the controller manager

The robot is built. Now it has to move.

A model that can be seen in RViz and dropped into Gazebo is still a statue. This page covers the one function that turns it into a machine: taking a command, driving a motor, and checking the motor actually did what it was told.

It starts with four numbers and one subtraction, then builds up the full ros2_control stack on top of that, one layer at a time.

part one of three

The idea: control, before any ROS appears

None of this is ROS specific. It is four numbers, one subtraction and a loop. Everything in part two is machinery built to run that loop on real hardware.

01

Where we are: a robot that cannot move

There is now a model of the robot. It can be visualized in RViz, simulated in Gazebo, and used to develop and test new functionalities.

what exists so far

So far the robot is static, and it cannot yet be moved in Gazebo. The mobile robot is not yet mobile.

This part covers the first, and perhaps the most elementary, function a robot must implement: actuating the joint motors in response to a received command.

Although it is the most basic function, it is also fundamental. It is used by all the other modules that perform advanced operations, so that they can move the robot according to their own logic.

02

What control means

Sending a command to a robot motor, following its movement, and making sure it matches the desired movement is called control.

follow the signal round the loop

Control is an entire field of study. It deals with the mathematics for creating a component called a controller.

The goal is simple and practical: implement the logic that actuates a system, a motor in this case, as required by an input variable.

The input variable holds the desired value of the motion. For example: rotate at a speed of one radian per second.

What "1 rad/s" means. Radians per second is a way of saying how fast something spins. 1 rad/s is about 10 rpm, which is one full turn every 6.3 seconds. Every speed on this page shows the rpm beside it.
Reading the circle. The circle is where the input is compared with the current state of the motor, which comes back round as the output. The input goes in with a +, the output with a -. What comes out is the error.
03

Input, output, error and command

Four variables. Two are set by hand here, and the other two are worked out from them. The wheel really turns at the output speed.

set the input and the output
1. input variable

The desired state. Here: one radian per second.

2. output variable

The current state of the system. A stopped motor has an output of zero. The input is compared with this.

3. error variable

The difference between the input and the output. It indicates how far the current state of the motor is from the desired state.

a first exampleoutput = 0 input = 1 error = 1 - 0 = 1
4. command

The controller receives the error variable and converts it into a command for the system, which here means a command for the motor. It does this with the aim of making the error closer and closer to zero, and as quickly as possible.

In simple words. The input says what is wanted. The command is what the controller actually sends to the motor to get there. Nobody types the command in: the controller works it out from the error, and sends it.
About the demo. Turning the error into a command is the mathematics of control. So the demo uses the simplest rule so the numbers can be followed: the command is half the error.
04

Going around the loop, one time at a time

The loop repeats. Here it runs only when the button is pressed, so each time around can be read.

go around the loop
what one time around does

Upon receiving the command, the motor starts to rotate and gradually increases its speed. The control system is then executed again. The speed is no longer zero, so the error is recalculated as the new difference between the current speed and the desired speed, and another command is sent.

The closer the motor gets, the smaller the error becomes, and consequently the smaller the command sent to the motor.

when it ends

When the motor finally reaches the desired velocity, the output of the motor and the input of the control system are equal. The motor is rotating at the speed that was requested by the input variable.

every time around, written out. newest at the top
time aroundoutput beforeerrorcommandoutput after
05

The loop running on its own

The same four variables, now continuous.

change the input

The blue line is the input and the green line is the output. The red line is the error and the orange line is the command.

As the output rises to meet the input, the error falls, and the command falls with it. When the output equals the input, both reach zero together.

Press "change the input suddenly" and the error comes straight back, and so does the command. The loop does the same work again.

06

When the control system has nothing to do

It can happen that the motor is already rotating at one radian per second.

set the input and the output

If the motor is already rotating at one radian per second, and one radian per second is requested, the control system does not have to do anything.

The input is compared with the current state. They match, so the error is zero, and there is no command to send.

Set the two sliders to different values to bring the command back.

07

The same problem in every robot

Manipulator, mobile base or drone.

pick a robot

The control problem is common to all robots. Whether the aim is a manipulator, a mobile base or even a drone, sooner or later it has to be dealt with.

sending commandsto the actuators.
receiving feedbackfrom the sensors, on how the actuators that were commanded have actually moved.

Since this is a common problem for all robots, there is already a framework in ROS 2 for it, called ros2_control.

part two of three

The framework: ros2_control, one layer at a time

Built from the hardware upward. Each layer exists to solve a problem created by the layer below it.

08

ros2_control, and the five pieces it is made of

The whole stack on one diagram. Click a layer to open it, then the sections that follow take each one in turn.

click a layer
09

Hardware resources, the thing at the core

At the core of the framework there are components called hardware resources. Everything else is arranged around them.

what kind of resource, and is it real

A hardware resource can be composed of actuators, to which commands can be sent, or sensors, from which information about the state of an actuator can be retrieved, such as its velocity or its position.

Additionally a hardware resource can represent an entire complex system composed of several actuators and sensors together.

Real or simulated. All these components can be real hardware components, or simulated components inside a physics engine such as Gazebo.
10

The two interfaces: one writes, one reads

To interact with the hardware resources the library provides exactly two interfaces. Try to use the wrong one and the demo will refuse.

attempt an operation
command interfaceSends commands to the robot's hardware. Commands can actually be written through it. It is the way out.
state interfaceOnly retrieves information. It can only read the current state of the hardware. It is the way in.
In short. With the command interface it is possible to write commands to the hardware. With the state interface it is possible to read from the hardware. That split is the whole design.
which half of the loop each one carries

The command interface carries the command outward. The state interface carries the output back. Both sit between the resource manager and the hardware.

11

The resource manager

Managing those interfaces with the hardware resources is a component called the resource manager.

load hardware components
An abstraction of the hardware and its drivers

The resource manager stands in for the actual hardware component of the robot and also for its drivers.

Reuse

It allows already implemented hardware components to be reused without the need to implement new ones. A component someone else wrote for a motor driver can be loaded as it stands.

One or many

The resource manager can load one or more hardware components at the same time. Having loaded them it makes them available and interactable with the other components of the ros2_control library, and so with the controller manager that sits above it.

12

The controller manager

An additional level of abstraction, sitting above the resource manager and below the controllers.

load controllers onto the robot
A middleware, and a connection point

The controller manager acts as a middleware: a connection point between the resource manager and the actual controller that implements the control logic of a specific system or actuator.

Several controllers at once

It can load multiple controllers simultaneously, and those controllers may implement entirely different control logics. It connects them to the hardware, or more precisely it connects them to the resource manager that manages the hardware.

Exactly that. The controllers are connected to the resource manager, which manages the hardware.
13

The controllers themselves

The components loaded by the controller manager, and the place where the control logic is actually implemented.

which controller is driving

Controllers are the components where the control logic is actually implemented. They are the ones that ensure, for example, that the system output is equal to the desired input: exactly the loop from part one of this page.

writing one is not the only option

In addition to developing new controllers, it is also possible to use many controllers that are already available in the ros2_controllers library.

14

The naming trap: ros2_control against ros2_controllers

Two libraries whose names differ by one letter and whose contents do not overlap at all. This one catches people.

where does this component live?
Do not be misled by the names of these libraries.
librarycontains

Shorter name holds the infrastructure. Longer name holds the control logic. Reading them the other way round is the usual mistake.

15

Talking to it, from a terminal and from other nodes

The controller manager does not only connect controllers to hardware. It also faces outward, toward the rest of the system and toward a person at a keyboard.

run a command
For a person: the command line interface

The controller manager provides an interface to other ROS 2 applications and to the user. From a terminal it can be asked for the list of controllers or the list of hardware components, and it can also be told to load new controllers and in general to interact with the ros2_control library.

For other nodes: services and topics

Other ROS 2 nodes and applications that want to actuate the hardware, or activate the robot, or simply obtain information about its state, can do so directly through the services and topics provided by the controller manager.

16

The whole stack, with a command flowing through it

Everything from the previous sections working together, following one request from the outside down to the motor and back.

run it
every step
part three of three

Checking it stuck

A quiz, then a map of every point this page set out to cover.

17

Checkpoint