rm_controllers
Loading...
Searching...
No Matches
controller.h
Go to the documentation of this file.
1//
2// Created by guanlin on 25-8-28.
3//
4
5#pragma once
6
7#include <rm_common/lqr.h>
8#include <rm_msgs/LegCmd.h>
9#include <rm_msgs/LeggedChassisStatus.h>
10#include <rm_msgs/LeggedLQRStatus.h>
11#include <rm_msgs/LQRkMatrix.h>
12#include <rm_msgs/LeggedChassisMode.h>
13#include <rm_msgs/LeggedUpstairStatus.h>
14#include <rm_common/filters/kalman_filter.h>
15#include <rm_common/filters/lp_filter.h>
16#include <rm_common/DebugDataPublisher.h>
17#include <rm_common/linear_interpolation.h>
18#include <control_toolbox/pid.h>
19#include <controller_interface/multi_interface_controller.h>
20#include <geometry_msgs/TwistStamped.h>
21#include <geometry_msgs/Vector3.h>
22#include <hardware_interface/imu_sensor_interface.h>
23#include <hardware_interface/joint_command_interface.h>
24#include <std_msgs/Float64.h>
25#include <std_msgs/Bool.h>
26#include <std_srvs/Trigger.h>
27#include <atomic>
28#include <string>
30#include <dynamic_reconfigure/server.h>
31#include <rm_chassis_controllers/LQRWeightConfig.h>
32
38
40{
41using Eigen::Matrix;
42
44{
45 double Q_theta{}, Q_d_theta{}, Q_x{}, Q_dx{}, Q_phi{}, Q_d_phi{};
46 double R_T{}, R_Tp{};
47};
48
49class BipedalController : public ChassisBase<rm_control::RobotStateInterface, hardware_interface::ImuSensorInterface,
50 hardware_interface::EffortJointInterface>,
52{
53public:
54 BipedalController() = default;
55 bool init(hardware_interface::RobotHW* robot_hw, ros::NodeHandle& root_nh, ros::NodeHandle& controller_nh) override;
56 void moveJoint(const ros::Time& time, const ros::Duration& period) override;
57 void stopping(const ros::Time& time) override;
58 // clang-format off
59 // BipedalControllerInterface implementations
60 bool getOverturn() const override { return overturn_; }
61 bool getStateChange() const override { return balance_state_changed_; }
62 bool getCompleteStand() const override { return complete_stand_; }
63 Eigen::Matrix<double, 4, CONTROL_DIM * STATE_DIM> getCoeffs() override { return coeffs_; }
64 const std::shared_ptr<ModelParams>& getModelParams() const override { return model_params_; }
65 const std::shared_ptr<ControlParams>& getControlParams() const override { return control_params_; }
66 const std::shared_ptr<BiasParams>& getBiasParams() const override { return bias_params_; }
67 const std::shared_ptr<LegStateThresholdParams>& getLegThresholdParams() const override { return leg_threshold_params_; }
68 const std::shared_ptr<ChassisGeometryParams>& getChassisGeometryParams() const override { return chassis_geometry_params_; }
69 double getLegCmd() const override { return legCmd_; }
70 double getJumpCmd() const override { return jumpCmd_; }
71 int getBaseState() const override { return state_; }
72 inline double getDefaultLegLength() const override { return default_leg_length_;}
73 geometry_msgs::Vector3 getVelCmd() override { return vel_cmd_; }
74 inline bool getMoveFlag() const override { return move_flag_; }
75 inline void setMoveFlag(const bool& move_flag) override { move_flag_ = move_flag; }
76 inline const ChassisState& getChassisState() override { return chassis_state_; };
77 inline LegState& getLegState(Side side) override { return leg_state_[side]; };
78 inline bool getDown5cmStairFlag() const override { return down_5cm_stair_flag_.load(std::memory_order_acquire); }
79 inline void setDown5cmStairFlag(bool flag) override { down_5cm_stair_flag_.store(flag, std::memory_order_release); }
80 inline void setStateChange(bool state) override { balance_state_changed_ = state; }
81 inline void setCompleteStand(bool state) override { complete_stand_ = state; }
82 void setJumpCmd(bool cmd) override { jumpCmd_ = cmd; }
83 void setMode(int mode) override { balance_mode_ = mode; }
84 inline void clearRecoveryFlag() override { overturn_ = false; }
85 double f_spring_force(double L0) override;
86 void pubState() override;
87 void pubLQRStatus(Eigen::Matrix<double, STATE_DIM, 1> left_error, Eigen::Matrix<double, STATE_DIM, 1> right_error,
88 Eigen::Matrix<double, STATE_DIM, 1> left_ref, Eigen::Matrix<double, STATE_DIM, 1> right_ref,
89 Eigen::Matrix<double, CONTROL_DIM, 1> u_left, Eigen::Matrix<double, CONTROL_DIM, 1> u_right,
90 Eigen::Matrix<double, CONTROL_DIM, 1> F_leg_, const bool unstick[2]) const override;
91 void pubLegLenStatus(const bool& upstair_flag) override;
92 void clearStatus() override;
93 void pubDebugData(const std::string& name, double value) override{ debugPub_->add(name, value); };
94 void setRecoveryLegSpdTurnback(bool recovery_leg_spd_turnback) override { recovery_leg_spd_turnback_ = recovery_leg_spd_turnback; }
95 bool getRecoveryLegSpdTurnback() const override { return recovery_leg_spd_turnback_;}
96 double getLegThetaOffset(const double& leg_len) override { return leg_theta_offset_interp_.output(leg_len); }
97 // clang-format on
98private:
99 void updateEstimation(const ros::Time& time, const ros::Duration& period);
100
101 bool setupLQR(ros::NodeHandle& controller_nh);
102 bool setupParams(ros::NodeHandle& controller_nh);
103 bool setupModelParams(ros::NodeHandle& controller_nh);
104 bool setupControlParams(ros::NodeHandle& controller_nh);
105 bool setupBiasParams(ros::NodeHandle& controller_nh);
106 bool setupThresholdParams(ros::NodeHandle& controller_nh);
107 bool setupSpringParams(ros::NodeHandle& controller_nh);
108 bool setupChassisGeometryParams(ros::NodeHandle& controller_nh);
109 void polyfit(const std::vector<Eigen::Matrix<double, 2, 6>>& Ks, const std::vector<double>& L0s,
110 Eigen::Matrix<double, 4, 12>& coeffs);
111 void triggerDown5cmStairAction()
112 {
113 down_5cm_stair_flag_.store(true, std::memory_order_release);
114 }
115 bool down5cmStairSrvCallback(std_srvs::Trigger::Request& req, std_srvs::Trigger::Response& res);
116 geometry_msgs::Twist odometry() override;
117
118 void reconfigCB(rm_chassis_controllers::LQRWeightConfig& config, uint32_t level);
119
120 Eigen::Matrix<double, 4, CONTROL_DIM * STATE_DIM> coeffs_;
121 Eigen::Matrix<double, STATE_DIM, STATE_DIM> q_{};
122 Eigen::Matrix<double, CONTROL_DIM, CONTROL_DIM> r_{};
123
124 std::shared_ptr<ModelParams> model_params_;
125 std::shared_ptr<ControlParams> control_params_;
126 std::shared_ptr<BiasParams> bias_params_;
127 std::shared_ptr<SpringParams> spring_params_;
128 std::shared_ptr<ChassisGeometryParams> chassis_geometry_params_;
129 std::shared_ptr<LegStateThresholdParams> leg_threshold_params_;
130
131 int balance_mode_ = BalanceMode::SIT_DOWN;
132 bool balance_state_changed_ = false;
133 std::shared_ptr<ModeManager> mode_manager_;
134
135 // Slippage_detection
136 double leftWheelVel{}, rightWheelVel{}, leftWheelVelAbsolute{}, rightWheelVelAbsolute{}, slip_alpha_{ 2.0 },
137 slip_R_wheel_{}, R_wheel_{};
138 int itor = 0, sample_times_ = 0;
139 bool slip_flag_{ false };
140 Eigen::Matrix<double, 2, 2> A_, B_, H_, Q_, R_;
141 Eigen::Matrix<double, 2, 1> X_, U_;
142 std::shared_ptr<KalmanFilter<double>> kalmanFilterPtr_;
143
144 ChassisState chassis_state_;
145 LegState leg_state_[2];
146 // leg_theta_offset
147 rm_common::LinearInterp leg_theta_offset_interp_;
148 double default_leg_length_{ 0.12 };
149 bool move_flag_{ false };
150 std::atomic_bool down_5cm_stair_flag_{ false };
151 // stand up
152 bool complete_stand_ = false, overturn_ = false;
153 // recovery
154 bool recovery_leg_spd_turnback_{ false };
155
156 // handles
157 hardware_interface::ImuSensorHandle imu_handle_, gimbal_imu_handle_;
158 hardware_interface::JointHandle left_wheel_joint_handle_, right_wheel_joint_handle_;
159 hardware_interface::JointHandle left_hip_joint_handle_, left_knee_joint_handle_, right_hip_joint_handle_,
160 right_knee_joint_handle_;
161 std::vector<hardware_interface::JointHandle*> joint_handles_;
162
163 // Leg Cmd
164 double legCmd_{ 0.2 };
165 bool jumpCmd_{ false };
166
167 // ROS Interface
168 dynamic_reconfigure::Server<rm_chassis_controllers::LQRWeightConfig>* d_srv_;
169 realtime_tools::RealtimeBuffer<LQRConfig> config_rt_buffer_;
170 LQRConfig config_{};
171 bool dynamic_reconfig_initialized_{ false };
172 ros::Subscriber leg_cmd_sub_, recovery_leg_spd_turnback_sub_;
173 ros::Publisher unstick_pub_, upstair_status_pub_;
174 std::shared_ptr<realtime_tools::RealtimePublisher<rm_msgs::LeggedChassisStatus>> legged_chassis_status_pub_;
175 std::shared_ptr<realtime_tools::RealtimePublisher<rm_msgs::LeggedChassisMode>> legged_chassis_mode_pub_;
176 std::shared_ptr<realtime_tools::RealtimePublisher<rm_msgs::LeggedLQRStatus>> lqr_status_pub_;
177 ros::Time cmd_update_time_;
178 std::shared_ptr<DebugDataPublisher> debugPub_;
179 ros::ServiceServer down_5cm_stair_srv_;
180};
181} // namespace rm_chassis_controllers
void clearRecoveryFlag() override
Definition controller.h:84
bool getDown5cmStairFlag() const override
Definition controller.h:78
void setDown5cmStairFlag(bool flag) override
Definition controller.h:79
const std::shared_ptr< ControlParams > & getControlParams() const override
Definition controller.h:65
const std::shared_ptr< ModelParams > & getModelParams() const override
Definition controller.h:64
int getBaseState() const override
Definition controller.h:71
void pubLegLenStatus(const bool &upstair_flag) override
Definition controller.cpp:622
bool init(hardware_interface::RobotHW *robot_hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh) override
Definition controller.cpp:18
bool getStateChange() const override
Definition controller.h:61
void setStateChange(bool state) override
Definition controller.h:80
double getDefaultLegLength() const override
Definition controller.h:72
double getLegCmd() const override
Definition controller.h:69
void pubLQRStatus(Eigen::Matrix< double, STATE_DIM, 1 > left_error, Eigen::Matrix< double, STATE_DIM, 1 > right_error, Eigen::Matrix< double, STATE_DIM, 1 > left_ref, Eigen::Matrix< double, STATE_DIM, 1 > right_ref, Eigen::Matrix< double, CONTROL_DIM, 1 > u_left, Eigen::Matrix< double, CONTROL_DIM, 1 > u_right, Eigen::Matrix< double, CONTROL_DIM, 1 > F_leg_, const bool unstick[2]) const override
Definition controller.cpp:585
void moveJoint(const ros::Time &time, const ros::Duration &period) override
Definition controller.cpp:102
void clearStatus() override
Definition controller.cpp:122
Eigen::Matrix< double, 4, CONTROL_DIM *STATE_DIM > getCoeffs() override
Definition controller.h:63
bool getOverturn() const override
Definition controller.h:60
double getLegThetaOffset(const double &leg_len) override
Definition controller.h:96
const std::shared_ptr< BiasParams > & getBiasParams() const override
Definition controller.h:66
void setCompleteStand(bool state) override
Definition controller.h:81
void pubDebugData(const std::string &name, double value) override
Definition controller.h:93
geometry_msgs::Vector3 getVelCmd() override
Definition controller.h:73
bool getCompleteStand() const override
Definition controller.h:62
void stopping(const ros::Time &time) override
Definition controller.cpp:306
void setMoveFlag(const bool &move_flag) override
Definition controller.h:75
const std::shared_ptr< ChassisGeometryParams > & getChassisGeometryParams() const override
Definition controller.h:68
void pubState() override
Definition controller.cpp:299
LegState & getLegState(Side side) override
Definition controller.h:77
const std::shared_ptr< LegStateThresholdParams > & getLegThresholdParams() const override
Definition controller.h:67
void setJumpCmd(bool cmd) override
Definition controller.h:82
const ChassisState & getChassisState() override
Definition controller.h:76
double f_spring_force(double L0) override
Definition controller.cpp:698
double getJumpCmd() const override
Definition controller.h:70
void setMode(int mode) override
Definition controller.h:83
bool getMoveFlag() const override
Definition controller.h:74
bool getRecoveryLegSpdTurnback() const override
Definition controller.h:95
void setRecoveryLegSpdTurnback(bool recovery_leg_spd_turnback) override
Definition controller.h:94
Definition controller_interface.h:13
Definition chassis_base.h:95
Definition active_suspension.h:16
Side
Definition definitions.h:116
@ SIT_DOWN
Definition definitions.h:109
Definition definitions.h:146
Definition controller.h:44
double Q_dx
Definition controller.h:45
double Q_d_theta
Definition controller.h:45
double R_Tp
Definition controller.h:46
double Q_d_phi
Definition controller.h:45
double Q_phi
Definition controller.h:45
double Q_theta
Definition controller.h:45
double Q_x
Definition controller.h:45
double R_T
Definition controller.h:46
Definition definitions.h:138