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.
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.
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.
Sending a command to a robot motor, following its movement, and making sure it matches the desired movement is called control.
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.
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.
The desired state. Here: one radian per second.
The current state of the system. A stopped motor has an output of zero. The input is compared with this.
The difference between the input and the output. It indicates how far the current state of the motor is from the desired state.
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.
The loop repeats. Here it runs only when the button is pressed, so each time around can be read.
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 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.
| time around | output before | error | command | output after |
|---|
The same four variables, now continuous.
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.
It can happen that the motor is already rotating at one radian per second.
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.
Manipulator, mobile base or drone.
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.
Since this is a common problem for all robots, there is already a framework in ROS 2 for it, called ros2_control.
The whole stack on one diagram. Click a layer to open it, then the sections that follow take each one in turn.
At the core of the framework there are components called hardware resources. Everything else is arranged around them.
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.
To interact with the hardware resources the library provides exactly two interfaces. Try to use the wrong one and the demo will refuse.
The command interface carries the command outward. The state interface carries the output back. Both sit between the resource manager and the hardware.
Managing those interfaces with the hardware resources is a component called the resource manager.
The resource manager stands in for the actual hardware component of the robot and also for its drivers.
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.
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.
An additional level of abstraction, sitting above the resource manager and below the controllers.
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.
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.
The components loaded by the controller manager, and the place where the control logic is actually implemented.
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.
In addition to developing new controllers, it is also possible to use many controllers that are already available in the ros2_controllers library.
ros2_control against ros2_controllersTwo libraries whose names differ by one letter and whose contents do not overlap at all. This one catches people.
| library | contains |
|---|
Shorter name holds the infrastructure. Longer name holds the control logic. Reading them the other way round is the usual mistake.
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.
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.
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.
Everything from the previous sections working together, following one request from the outside down to the motor and back.