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

55 lines
1.6 KiB
C++
Raw Normal View History

//
// Created by biao on 24-9-10.
//
2024-09-14 17:54:39 +08:00
#include "unitree_guide_controller/FSM/StateFixedStand.h"
2024-09-11 20:41:12 +08:00
#include <cmath>
2024-09-18 13:18:02 +08:00
StateFixedStand::StateFixedStand(CtrlComponent &ctrlComp): FSMState(
FSMStateName::FIXEDSTAND, "fixed stand", ctrlComp) {
2024-09-17 14:03:08 +08:00
duration_ = ctrl_comp_.frequency_ * 1.2;
}
void StateFixedStand::enter() {
2024-09-11 14:01:07 +08:00
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 14:01:07 +08:00
}
}
void StateFixedStand::run() {
2024-09-11 20:41:12 +08:00
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]);
2024-09-17 14:03:08 +08:00
ctrl_comp_.joint_velocity_command_interface_[i].get().set_value(0);
ctrl_comp_.joint_effort_command_interface_[i].get().set_value(0);
ctrl_comp_.joint_kp_command_interface_[i].get().set_value(
2024-09-12 20:04:20 +08:00
phase * 60.0 + (1 - phase) * 20.0);
2024-09-17 14:03:08 +08:00
ctrl_comp_.joint_kd_command_interface_[i].get().set_value(3.5);
2024-09-11 20:41:12 +08:00
}
}
void StateFixedStand::exit() {
2024-09-11 20:41:12 +08:00
percent_ = 0;
}
FSMStateName StateFixedStand::checkChange() {
2024-09-14 17:54:39 +08:00
if (percent_ < 1.5) {
2024-09-11 20:41:12 +08:00
return FSMStateName::FIXEDSTAND;
}
2024-09-17 14:03:08 +08:00
switch (ctrl_comp_.control_inputs_.get().command) {
2024-09-12 13:56:51 +08:00
case 1:
return FSMStateName::FIXEDDOWN;
2024-09-13 15:00:20 +08:00
case 3:
return FSMStateName::FREESTAND;
case 9:
2024-09-12 13:56:51 +08:00
return FSMStateName::SWINGTEST;
2024-09-16 21:15:34 +08:00
case 10:
return FSMStateName::BALANCETEST;
2024-09-12 13:56:51 +08:00
default:
return FSMStateName::FIXEDSTAND;
2024-09-11 14:01:07 +08:00
}
}