quadruped_ros2_control/controllers/unitree_guide_controller/src/FSM/StateBalanceTest.cpp

97 lines
3.5 KiB
C++
Raw Normal View History

2024-09-16 21:15:34 +08:00
//
// Created by tlab-uav on 24-9-16.
//
#include "unitree_guide_controller/FSM/StateBalanceTest.h"
2024-09-18 13:52:37 +08:00
#include "unitree_guide_controller/common/mathTools.h"
2024-09-16 21:15:34 +08:00
2024-09-18 13:18:02 +08:00
StateBalanceTest::StateBalanceTest(CtrlComponent &ctrlComp) : FSMState(FSMStateName::BALANCETEST, "balance test",
2024-09-18 13:52:37 +08:00
ctrlComp),
estimator_(ctrlComp.estimator_),
robot_model_(ctrlComp.robot_model_),
2024-09-18 19:37:01 +08:00
balance_ctrl_(ctrlComp.balance_ctrl_),
wave_generator_(ctrl_comp_.wave_generator_) {
2024-09-16 21:15:34 +08:00
_xMax = 0.05;
_xMin = -_xMax;
_yMax = 0.05;
_yMin = -_yMax;
_zMax = 0.04;
_zMin = -_zMax;
_yawMax = 20 * M_PI / 180;
_yawMin = -_yawMax;
Kp_p_ = Vec3(150, 150, 150).asDiagonal();
Kd_p_ = Vec3(25, 25, 25).asDiagonal();
kp_w_ = 200;
Kd_w_ = Vec3(30, 30, 30).asDiagonal();
}
void StateBalanceTest::enter() {
2024-09-18 12:29:26 +08:00
pcd_init_ = estimator_.getPosition();
pcd_ = pcd_init_;
init_rotation_ = estimator_.getRotation();
2024-09-18 19:37:01 +08:00
wave_generator_.status_ = WaveStatus::STANCE_ALL;
2024-09-16 21:15:34 +08:00
}
void StateBalanceTest::run() {
2024-09-18 12:29:26 +08:00
pcd_(0) = pcd_init_(0) + invNormalize(ctrl_comp_.control_inputs_.get().ly, _xMin, _xMax);
pcd_(1) = pcd_init_(1) - invNormalize(ctrl_comp_.control_inputs_.get().lx, _yMin, _yMax);
pcd_(2) = pcd_init_(2) + invNormalize(ctrl_comp_.control_inputs_.get().ry, _zMin, _zMax);
2024-09-16 21:15:34 +08:00
2024-09-19 14:00:43 +08:00
const float yaw = - invNormalize(ctrl_comp_.control_inputs_.get().rx, _yawMin, _yawMax);
2024-09-18 21:48:14 +08:00
Rd_ = rotz(yaw) * init_rotation_;
2024-09-16 21:15:34 +08:00
for (int i = 0; i < 12; i++) {
2024-09-17 14:03:08 +08:00
ctrl_comp_.joint_kp_command_interface_[i].get().set_value(0.8);
ctrl_comp_.joint_kd_command_interface_[i].get().set_value(0.8);
2024-09-16 21:15:34 +08:00
}
2024-09-17 14:03:08 +08:00
calcTorque();
2024-09-16 21:15:34 +08:00
}
void StateBalanceTest::exit() {
}
FSMStateName StateBalanceTest::checkChange() {
2024-09-17 14:03:08 +08:00
switch (ctrl_comp_.control_inputs_.get().command) {
2024-09-16 21:15:34 +08:00
case 1:
return FSMStateName::FIXEDDOWN;
case 2:
return FSMStateName::FIXEDSTAND;
default:
return FSMStateName::BALANCETEST;
}
}
void StateBalanceTest::calcTorque() {
2024-09-18 21:48:14 +08:00
const auto B2G_Rotation = estimator_.getRotation();
2024-09-17 18:21:04 +08:00
const RotMat G2B_Rotation = B2G_Rotation.transpose();
2024-09-18 21:48:14 +08:00
const Vec3 pose_body = estimator_.getPosition();
const Vec3 vel_body = estimator_.getVelocity();
2024-09-16 21:15:34 +08:00
// expected body acceleration
2024-09-18 21:48:14 +08:00
dd_pcd_ = Kp_p_ * (pcd_ - pose_body) + Kd_p_ * (Vec3(0, 0, 0) - vel_body);
2024-09-16 21:15:34 +08:00
// expected body angular acceleration
2024-09-18 21:48:14 +08:00
d_wbd_ = kp_w_ * rotMatToExp(Rd_ * G2B_Rotation) +
2024-09-19 17:16:17 +08:00
Kd_w_ * (Vec3(0, 0, 0) - estimator_.getGyroGlobal());
2024-09-16 21:15:34 +08:00
// calculate foot force
2024-09-18 22:19:37 +08:00
const Vec34 pos_feet_2_body_global = estimator_.getFeetPos2Body();
const Vec34 force_feet_global = -balance_ctrl_.calF(dd_pcd_, d_wbd_, B2G_Rotation,
pos_feet_2_body_global, wave_generator_.contact_);
const Vec34 force_feet_body = G2B_Rotation * force_feet_global;
2024-09-16 21:15:34 +08:00
2024-09-18 12:29:26 +08:00
std::vector<KDL::JntArray> current_joints = robot_model_.current_joint_pos_;
2024-09-16 21:15:34 +08:00
for (int i = 0; i < 4; i++) {
2024-09-18 22:19:37 +08:00
KDL::JntArray torque = robot_model_.getTorque(force_feet_body.col(i), i);
2024-09-16 21:15:34 +08:00
for (int j = 0; j < 3; j++) {
2024-09-17 14:03:08 +08:00
ctrl_comp_.joint_effort_command_interface_[i * 3 + j].get().set_value(torque(j));
ctrl_comp_.joint_position_command_interface_[i * 3 + j].get().set_value(current_joints[i](j));
2024-09-16 21:15:34 +08:00
}
}
}