-
Notifications
You must be signed in to change notification settings - Fork 162
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/controllersandrobot_driver/include/robot_driverfolders (these instructions will assume you have created acontrollerclass incontroller.cppwith header filecontroller.h). You can look atjoint_controller.cppas an example. You will need to implement acomputeLegCommandArraymethod specified inrobot_driver/include/robot_driver/leg_controller.hthat populates aLegCommandArraywith the desired commands to the legs. - Update the
CMakeLists.txtof robot_driver to include your source code in the project library (you can look for wherejoint_controller.cppappears):
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>