2024-09-24 19:48:14 +08:00
|
|
|
//
|
|
|
|
// Created by tlab-uav on 24-9-24.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef OCS2QUADRUPEDCONTROLLER_H
|
|
|
|
#define OCS2QUADRUPEDCONTROLLER_H
|
2024-09-25 21:01:50 +08:00
|
|
|
|
2024-09-24 19:48:14 +08:00
|
|
|
#include <controller_interface/controller_interface.hpp>
|
2024-09-25 21:01:50 +08:00
|
|
|
#include <control_input_msgs/msg/inputs.hpp>
|
2024-09-24 21:50:46 +08:00
|
|
|
#include <ocs2_centroidal_model/CentroidalModelRbdConversions.h>
|
2024-09-25 21:01:50 +08:00
|
|
|
#include <ocs2_mpc/MPC_MRT_Interface.h>
|
2024-09-24 21:50:46 +08:00
|
|
|
#include <ocs2_quadruped_controller/estimator/StateEstimateBase.h>
|
2024-09-25 21:01:50 +08:00
|
|
|
#include <ocs2_quadruped_controller/interface/LeggedInterface.h>
|
2024-09-24 21:50:46 +08:00
|
|
|
#include <ocs2_quadruped_controller/wbc/WbcBase.h>
|
2024-09-27 17:40:22 +08:00
|
|
|
#include <ocs2_msgs/msg/mpc_observation.hpp>
|
2024-09-25 21:01:50 +08:00
|
|
|
#include "SafetyChecker.h"
|
|
|
|
#include "ocs2_quadruped_controller/control/CtrlComponent.h"
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
namespace ocs2::legged_robot {
|
|
|
|
class Ocs2QuadrupedController final : public controller_interface::ControllerInterface {
|
|
|
|
public:
|
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
Ocs2QuadrupedController() = default;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::InterfaceConfiguration command_interface_configuration() const override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::InterfaceConfiguration state_interface_configuration() const override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::return_type update(
|
|
|
|
const rclcpp::Time &time, const rclcpp::Duration &period) override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::CallbackReturn on_init() override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::CallbackReturn on_configure(
|
|
|
|
const rclcpp_lifecycle::State &previous_state) override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::CallbackReturn on_activate(
|
|
|
|
const rclcpp_lifecycle::State &previous_state) override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::CallbackReturn on_deactivate(
|
|
|
|
const rclcpp_lifecycle::State &previous_state) override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::CallbackReturn on_cleanup(
|
|
|
|
const rclcpp_lifecycle::State &previous_state) override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::CallbackReturn on_shutdown(
|
|
|
|
const rclcpp_lifecycle::State &previous_state) override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
CONTROLLER_INTERFACE_PUBLIC
|
|
|
|
controller_interface::CallbackReturn on_error(
|
|
|
|
const rclcpp_lifecycle::State &previous_state) override;
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
protected:
|
2024-09-25 21:01:50 +08:00
|
|
|
void setupLeggedInterface();
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
void setupMpc();
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
void setupMrt();
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
void setupStateEstimate();
|
2024-09-24 19:48:14 +08:00
|
|
|
|
2024-09-26 17:28:18 +08:00
|
|
|
void updateStateEstimation(const rclcpp::Time &time,
|
|
|
|
const rclcpp::Duration &period);
|
|
|
|
|
2024-09-25 21:01:50 +08:00
|
|
|
CtrlComponent ctrl_comp_;
|
|
|
|
std::vector<std::string> joint_names_;
|
2024-10-02 13:45:21 +08:00
|
|
|
std::vector<std::string> feet_names_;
|
2024-09-25 21:01:50 +08:00
|
|
|
std::vector<std::string> command_interface_types_;
|
|
|
|
std::vector<std::string> state_interface_types_;
|
|
|
|
|
|
|
|
std::unordered_map<
|
|
|
|
std::string, std::vector<std::reference_wrapper<hardware_interface::LoanedCommandInterface> > *>
|
|
|
|
command_interface_map_ = {
|
|
|
|
{"effort", &ctrl_comp_.joint_torque_command_interface_},
|
|
|
|
{"position", &ctrl_comp_.joint_position_command_interface_},
|
|
|
|
{"velocity", &ctrl_comp_.joint_velocity_command_interface_},
|
|
|
|
{"kp", &ctrl_comp_.joint_kp_command_interface_},
|
|
|
|
{"kd", &ctrl_comp_.joint_kd_command_interface_}
|
|
|
|
};
|
|
|
|
std::unordered_map<
|
|
|
|
std::string, std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface> > *>
|
|
|
|
state_interface_map_ = {
|
|
|
|
{"position", &ctrl_comp_.joint_position_state_interface_},
|
|
|
|
{"effort", &ctrl_comp_.joint_effort_state_interface_},
|
|
|
|
{"velocity", &ctrl_comp_.joint_velocity_state_interface_}
|
|
|
|
};
|
|
|
|
|
|
|
|
// IMU Sensor
|
|
|
|
std::string imu_name_;
|
|
|
|
std::vector<std::string> imu_interface_types_;
|
|
|
|
// Foot Force Sensor
|
|
|
|
std::string foot_force_name_;
|
|
|
|
std::vector<std::string> foot_force_interface_types_;
|
2024-10-02 13:45:21 +08:00
|
|
|
|
2024-09-30 17:48:59 +08:00
|
|
|
double default_kp_ = 0;
|
|
|
|
double default_kd_ = 6;
|
2024-09-25 21:01:50 +08:00
|
|
|
|
|
|
|
rclcpp::Subscription<control_input_msgs::msg::Inputs>::SharedPtr control_input_subscription_;
|
2024-09-27 17:40:22 +08:00
|
|
|
rclcpp::Publisher<ocs2_msgs::msg::MpcObservation>::SharedPtr observation_publisher_;
|
2024-09-25 21:01:50 +08:00
|
|
|
|
2024-09-24 21:50:46 +08:00
|
|
|
std::string task_file_;
|
|
|
|
std::string urdf_file_;
|
|
|
|
std::string reference_file_;
|
2024-09-26 17:28:18 +08:00
|
|
|
std::string gait_file_;
|
2024-09-24 21:50:46 +08:00
|
|
|
|
|
|
|
bool verbose_;
|
|
|
|
|
|
|
|
std::shared_ptr<LeggedInterface> legged_interface_;
|
|
|
|
std::shared_ptr<PinocchioEndEffectorKinematics> eeKinematicsPtr_;
|
|
|
|
|
|
|
|
// Whole Body Control
|
|
|
|
std::shared_ptr<WbcBase> wbc_;
|
2024-09-26 22:18:11 +08:00
|
|
|
std::shared_ptr<SafetyChecker> safety_checker_;
|
2024-09-24 21:50:46 +08:00
|
|
|
|
|
|
|
// Nonlinear MPC
|
|
|
|
std::shared_ptr<MPC_BASE> mpc_;
|
2024-09-26 22:18:11 +08:00
|
|
|
std::shared_ptr<MPC_MRT_Interface> mpc_mrt_interface_;
|
2024-09-24 21:50:46 +08:00
|
|
|
|
2024-09-26 22:18:11 +08:00
|
|
|
std::shared_ptr<CentroidalModelRbdConversions> rbd_conversions_;
|
2024-09-24 21:50:46 +08:00
|
|
|
|
|
|
|
private:
|
2024-09-29 17:09:06 +08:00
|
|
|
vector_t measured_rbd_state_;
|
2024-09-26 22:18:11 +08:00
|
|
|
std::thread mpc_thread_;
|
|
|
|
std::atomic_bool controller_running_{}, mpc_running_{};
|
|
|
|
benchmark::RepeatedTimer mpc_timer_;
|
|
|
|
benchmark::RepeatedTimer wbc_timer_;
|
2024-09-24 21:50:46 +08:00
|
|
|
};
|
|
|
|
}
|
2024-09-24 19:48:14 +08:00
|
|
|
|
|
|
|
#endif //OCS2QUADRUPEDCONTROLLER_H
|