-
Notifications
You must be signed in to change notification settings - Fork 139
Tutorial: Writing and Executing Your Own Controller
justinyim edited this page Jun 2, 2022
·
3 revisions
Users can now generate new controllers by inheriting the LegController
abstract class (see existing examples in robot_driver/src/controllers
).
To write your own controller and execute it on the robot, follow these steps:
- Write your source code for your controller class in the
robot_driver/sr/controllers
androbot_driver/include/robot_driver
folders (these instructions will assume you have created acontroller
class incontroller.cpp
with header filecontroller.h
). You can look atjoint_controller.cpp
as an example. You will need to implement acomputeLegCommandArray
method specified inrobot_driver/include/robot_driver/leg_controller.h
that populates aLegCommandArray
with the desired commands to the legs. - Update the
CMakeLists.txt
of robot_driver to include your source code in the project library (you can look for wherejoint_controller.cpp
appears):
add_library(robot_driver src/controller.cpp)
- Write a test for your code (at a bare minimum, call the constructor) and add it to the CMakeLists.txt:
catkin_add_gtest(controller_test test/test_controller.cpp)
target_link_libraries(controller_test example_package ${catkin_LIBRARIES})
- Build your code and run the tests:
(In separate terminal) roslaunch quad_utils load_global_params.launch load_robot_params:=true
(In original terminal) cd ~/catkin_ws
catkin run_tests
- Integrate your controller into robot_driver:
Include your controller in
robot_driver/include/robot_driver/robot_driver.h
:
#include "robot_driver/include/controllers/controller.h"
Add a case for your controller in the RobotDriver constructor in robot_driver/src/robot_driver.cpp
(look for where JointController appears):
...
} else if (controller_id_ == "controller") {
leg_controller_ = std::make_shared<controller>();
...
If required, insert any additional calls to your controller where appropriate in robot_driver.cpp
. Refer to JointController
and, InverseDynamicsController
for examples.
- Test your controller in simulation - refer to Gazebo Simulation for additional arguments:
roslaunch quad_utils quad_gazebo.launch controller:=<your-controller-arg>