2024-09-11 20:41:12 +08:00
|
|
|
//
|
|
|
|
// Created by tlab-uav on 24-9-11.
|
|
|
|
//
|
|
|
|
|
2024-09-14 17:54:39 +08:00
|
|
|
#include "unitree_guide_controller/FSM/StateFixedDown.h"
|
|
|
|
|
2024-09-11 20:41:12 +08:00
|
|
|
#include <cmath>
|
|
|
|
|
2024-10-21 12:43:17 +08:00
|
|
|
StateFixedDown::StateFixedDown(CtrlComponent &ctrlComp,
|
|
|
|
const std::vector<double> &target_pos,
|
|
|
|
const double kp,
|
|
|
|
const double kd)
|
|
|
|
: FSMState(FSMStateName::FIXEDDOWN, "fixed down", ctrlComp),
|
|
|
|
kp_(kp), kd_(kd) {
|
2024-09-17 14:03:08 +08:00
|
|
|
duration_ = ctrl_comp_.frequency_ * 1.2;
|
2024-10-21 12:43:17 +08:00
|
|
|
for (int i = 0; i < 12; i++) {
|
|
|
|
target_pos_[i] = target_pos[i];
|
|
|
|
}
|
2024-09-11 20:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StateFixedDown::enter() {
|
|
|
|
for (int i = 0; i < 12; i++) {
|
2024-09-17 14:03:08 +08:00
|
|
|
start_pos_[i] = ctrl_comp_.joint_position_state_interface_[i].get().get_value();
|
2024-09-11 20:41:12 +08:00
|
|
|
}
|
2024-09-29 20:27:02 +08:00
|
|
|
ctrl_comp_.control_inputs_.command = 0;
|
2024-10-21 12:43:17 +08:00
|
|
|
for (int i = 0; i < 12; i++) {
|
|
|
|
ctrl_comp_.joint_velocity_command_interface_[i].get().set_value(0);
|
|
|
|
ctrl_comp_.joint_torque_command_interface_[i].get().set_value(0);
|
|
|
|
ctrl_comp_.joint_kp_command_interface_[i].get().set_value(kp_);
|
|
|
|
ctrl_comp_.joint_kd_command_interface_[i].get().set_value(kd_);
|
|
|
|
}
|
2024-09-11 20:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StateFixedDown::run() {
|
|
|
|
percent_ += 1 / duration_;
|
|
|
|
phase = std::tanh(percent_);
|
|
|
|
for (int i = 0; i < 12; i++) {
|
2024-09-17 14:03:08 +08:00
|
|
|
ctrl_comp_.joint_position_command_interface_[i].get().set_value(
|
2024-09-11 20:41:12 +08:00
|
|
|
phase * target_pos_[i] + (1 - phase) * start_pos_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateFixedDown::exit() {
|
|
|
|
percent_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSMStateName StateFixedDown::checkChange() {
|
2024-09-14 17:54:39 +08:00
|
|
|
if (percent_ < 1.5) {
|
2024-09-11 20:41:12 +08:00
|
|
|
return FSMStateName::FIXEDDOWN;
|
|
|
|
}
|
2024-09-29 20:27:02 +08:00
|
|
|
switch (ctrl_comp_.control_inputs_.command) {
|
2024-09-11 20:41:12 +08:00
|
|
|
case 1:
|
|
|
|
return FSMStateName::PASSIVE;
|
|
|
|
case 2:
|
|
|
|
return FSMStateName::FIXEDSTAND;
|
|
|
|
default:
|
|
|
|
return FSMStateName::FIXEDDOWN;
|
|
|
|
}
|
|
|
|
}
|