Skip to content

Commit 57b6ef6

Browse files
authored
Update writing_new_controller tutorial (#1832)
1 parent 42e7ba4 commit 57b6ef6

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

doc/writing_new_controller.rst

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ The following is a step-by-step guide to create source files, basic tests, and c
1212

1313
If the package for the controller does not exist, then create it first.
1414
The package should have ``ament_cmake`` as a build type.
15-
The easiest way is to search online for the most recent manual.
16-
A helpful command to support this process is ``ros2 pkg create``.
17-
Use the ``--help`` flag for more information on how to use it.
15+
Generally, you can use ``ros2 pkg create <controller_name_package> --build-type ament_cmake``.
16+
Use the ``--help`` flag for more information on how to use this command.
1817
There is also an option to create library source files and compile rules to help you in the following steps.
1918

2019
2. **Preparing source files**
@@ -25,17 +24,32 @@ The following is a step-by-step guide to create source files, basic tests, and c
2524

2625
3. **Adding declarations into header file (.hpp)**
2726

28-
1. Take care that you use header guards. ROS 2-style is using ``#ifndef`` and ``#define`` preprocessor directives. (For more information on this, a search engine is your friend :) ).
27+
1. Take care that you use header guards. ROS 2-style is using ``#ifndef`` and ``#define`` preprocessor directives.
2928

3029
2. include ``"controller_interface/controller_interface.hpp"``.
3130

3231
3. Define a unique namespace for your controller. This is usually a package name written in ``snake_case``.
3332

34-
4. Define the class of the controller, extending ``ControllerInterface``, e.g.,
33+
4. Define the class of the controller, extending ``ControllerInterface``. Now, your code should look similar to this:
3534

3635
.. code:: c++
3736

37+
#ifndef <CONTROLLER_NAME>_HPP_
38+
#define <CONTROLLER_NAME>_HPP_
39+
40+
#include "controller_interface/controller_interface.hpp"
41+
42+
namespace <controller_name>
43+
{
44+
3845
class ControllerName : public controller_interface::ControllerInterface
46+
{
47+
// ...
48+
};
49+
50+
} // namespace <controller_name>
51+
52+
#endif // <CONTROLLER_NAME>_HPP_
3953

4054
5. Add a constructor without parameters and the following public methods overriding the ``ControllerInterface`` definition: ``on_init``, ``command_interface_configuration``, ``state_interface_configuration``, ``on_configure``, ``on_activate``, ``on_deactivate``, ``update``.
4155
For exact definitions check the ``controller_interface/controller_interface.hpp`` header or one of the controllers from `ros2_controllers <https://github.com/ros-controls/ros2_controllers>`_.
@@ -53,7 +67,7 @@ The following is a step-by-step guide to create source files, basic tests, and c
5367
This could also be done in the ``on_init`` method.
5468

5569
3. Implement the ``on_init`` method. The first line usually calls the parent ``on_init`` method.
56-
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 return ``controller_interface::return_type::OK`` or ``controller_interface::return_type::ERROR`` otherwise.
70+
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 return ``controller_interface::CallbackReturn::SUCCESS`` or ``controller_interface::CallbackReturn::ERROR`` otherwise.
5771

5872
4. Write the ``on_configure`` method. Parameters are usually read here, and everything is prepared so that the controller can be started.
5973

@@ -65,7 +79,6 @@ The following is a step-by-step guide to create source files, basic tests, and c
6579
6. Implement the ``on_activate`` method with checking, and potentially sorting, the interfaces and assigning members' initial values.
6680
This method is part of the real-time loop, therefore avoid any reservation of memory and, in general, keep it as short as possible.
6781

68-
6982
7. Implement the ``on_deactivate`` method, which does the opposite of ``on_activate``.
7083
In many cases, this method is empty.
7184
This method should also be real-time safe as much as possible.
@@ -129,10 +142,10 @@ The following is a step-by-step guide to create source files, basic tests, and c
129142

130143
9. **Compiling and testing the controller**
131144

132-
1. Now everything is ready to compile the controller using the ``colcon build <controller_name_package>`` command.
145+
1. Now everything is ready to compile the controller using the ``colcon build --packages-select <controller_name_package>`` command.
133146
Remember to go into the root of your workspace before executing this command.
134147

135-
2. If compilation was successful, source the ``setup.bash`` file from the install folder and execute ``colcon test <controller_name_package>`` to check if the new controller can be found through ``pluginlib`` library and be loaded by the controller manager.
148+
2. If compilation was successful, source the ``setup.bash`` file from the install folder and execute ``colcon test --packages-select <controller_name_package>`` to check if the new controller can be found through ``pluginlib`` library and be loaded by the controller manager.
136149

137150

138151
That's it! Enjoy writing great controllers!

0 commit comments

Comments
 (0)