2024-09-12 13:56:51 +08:00
|
|
|
//
|
|
|
|
// Created by biao on 24-9-12.
|
|
|
|
//
|
|
|
|
|
2024-09-12 20:04:20 +08:00
|
|
|
#include <iostream>
|
2024-09-14 17:54:39 +08:00
|
|
|
#include "unitree_guide_controller/FSM/StateSwingTest.h"
|
|
|
|
#include "unitree_guide_controller/common/mathTools.h"
|
2024-09-12 13:56:51 +08:00
|
|
|
|
2024-09-18 13:52:37 +08:00
|
|
|
StateSwingTest::StateSwingTest(CtrlComponent &ctrl_component): FSMState(
|
|
|
|
FSMStateName::SWINGTEST, "swing test", ctrl_component), robot_model_(ctrl_component.robot_model_) {
|
2024-09-12 13:56:51 +08:00
|
|
|
_xMin = -0.15;
|
|
|
|
_xMax = 0.10;
|
|
|
|
_yMin = -0.15;
|
|
|
|
_yMax = 0.15;
|
|
|
|
_zMin = -0.05;
|
|
|
|
_zMax = 0.20;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateSwingTest::enter() {
|
2024-09-12 20:04:20 +08:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2024-09-17 14:03:08 +08:00
|
|
|
ctrl_comp_.joint_kp_command_interface_[i].get().set_value(3);
|
|
|
|
ctrl_comp_.joint_kd_command_interface_[i].get().set_value(2);
|
2024-09-12 20:04:20 +08:00
|
|
|
}
|
|
|
|
for (int i = 3; i < 12; i++) {
|
2024-09-17 14:03:08 +08:00
|
|
|
ctrl_comp_.joint_kp_command_interface_[i].get().set_value(180);
|
|
|
|
ctrl_comp_.joint_kd_command_interface_[i].get().set_value(5);
|
2024-09-12 20:04:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Kp = KDL::Vector(20, 20, 50);
|
|
|
|
Kd = KDL::Vector(5, 5, 20);
|
|
|
|
|
2024-09-18 13:52:37 +08:00
|
|
|
init_joint_pos_ = robot_model_.current_joint_pos_;
|
|
|
|
init_foot_pos_ = robot_model_.getFeet2BPositions();
|
2024-09-12 20:04:20 +08:00
|
|
|
|
|
|
|
target_foot_pos_ = init_foot_pos_;
|
|
|
|
fr_init_pos_ = init_foot_pos_[0];
|
|
|
|
fr_goal_pos_ = fr_init_pos_;
|
2024-09-12 13:56:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StateSwingTest::run() {
|
2024-09-29 20:27:02 +08:00
|
|
|
if (ctrl_comp_.control_inputs_.ly > 0) {
|
|
|
|
fr_goal_pos_.p.x(invNormalize(ctrl_comp_.control_inputs_.ly, fr_init_pos_.p.x(),
|
2024-09-12 20:04:20 +08:00
|
|
|
fr_init_pos_.p.x() + _xMax, 0, 1));
|
|
|
|
} else {
|
2024-09-29 20:27:02 +08:00
|
|
|
fr_goal_pos_.p.x(invNormalize(ctrl_comp_.control_inputs_.ly, fr_init_pos_.p.x() + _xMin,
|
2024-09-12 20:04:20 +08:00
|
|
|
fr_init_pos_.p.x(), -1, 0));
|
|
|
|
}
|
2024-09-29 20:27:02 +08:00
|
|
|
if (ctrl_comp_.control_inputs_.lx > 0) {
|
|
|
|
fr_goal_pos_.p.y(invNormalize(ctrl_comp_.control_inputs_.lx, fr_init_pos_.p.y(),
|
2024-09-12 20:04:20 +08:00
|
|
|
fr_init_pos_.p.y() + _yMax, 0, 1));
|
|
|
|
} else {
|
2024-09-29 20:27:02 +08:00
|
|
|
fr_goal_pos_.p.y(invNormalize(ctrl_comp_.control_inputs_.lx, fr_init_pos_.p.y() + _yMin,
|
2024-09-12 20:04:20 +08:00
|
|
|
fr_init_pos_.p.y(), -1, 0));
|
|
|
|
}
|
2024-09-29 20:27:02 +08:00
|
|
|
if (ctrl_comp_.control_inputs_.ry > 0) {
|
|
|
|
fr_goal_pos_.p.z(invNormalize(ctrl_comp_.control_inputs_.ry, fr_init_pos_.p.z(),
|
2024-09-12 20:04:20 +08:00
|
|
|
fr_init_pos_.p.z() + _zMax, 0, 1));
|
|
|
|
} else {
|
2024-09-29 20:27:02 +08:00
|
|
|
fr_goal_pos_.p.z(invNormalize(ctrl_comp_.control_inputs_.ry, fr_init_pos_.p.z() + _zMin,
|
2024-09-12 20:04:20 +08:00
|
|
|
fr_init_pos_.p.z(), -1, 0));
|
|
|
|
}
|
2024-09-13 15:00:20 +08:00
|
|
|
|
2024-09-12 20:04:20 +08:00
|
|
|
positionCtrl();
|
|
|
|
torqueCtrl();
|
2024-09-12 13:56:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StateSwingTest::exit() {
|
|
|
|
}
|
|
|
|
|
|
|
|
FSMStateName StateSwingTest::checkChange() {
|
2024-09-29 20:27:02 +08:00
|
|
|
switch (ctrl_comp_.control_inputs_.command) {
|
2024-09-12 13:56:51 +08:00
|
|
|
case 1:
|
2024-09-29 20:27:02 +08:00
|
|
|
return FSMStateName::PASSIVE;
|
2024-09-12 13:56:51 +08:00
|
|
|
case 2:
|
|
|
|
return FSMStateName::FIXEDSTAND;
|
|
|
|
default:
|
|
|
|
return FSMStateName::SWINGTEST;
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 20:04:20 +08:00
|
|
|
|
|
|
|
void StateSwingTest::positionCtrl() {
|
|
|
|
target_foot_pos_[0] = fr_goal_pos_;
|
2024-09-18 13:52:37 +08:00
|
|
|
target_joint_pos_ = robot_model_.getQ(target_foot_pos_);
|
2024-09-12 20:04:20 +08:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2024-09-17 14:03:08 +08:00
|
|
|
ctrl_comp_.joint_position_command_interface_[i * 3].get().set_value(target_joint_pos_[i](0));
|
|
|
|
ctrl_comp_.joint_position_command_interface_[i * 3 + 1].get().set_value(target_joint_pos_[i](1));
|
|
|
|
ctrl_comp_.joint_position_command_interface_[i * 3 + 2].get().set_value(target_joint_pos_[i](2));
|
2024-09-12 20:04:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 15:00:20 +08:00
|
|
|
void StateSwingTest::torqueCtrl() const {
|
2024-09-18 13:52:37 +08:00
|
|
|
const KDL::Frame fr_current_pos = robot_model_.getFeet2BPositions(0);
|
2024-09-12 20:04:20 +08:00
|
|
|
|
|
|
|
const KDL::Vector pos_goal = fr_goal_pos_.p;
|
|
|
|
const KDL::Vector pos0 = fr_current_pos.p;
|
2024-09-18 13:52:37 +08:00
|
|
|
const KDL::Vector vel0 = robot_model_.getFeet2BVelocities(0);
|
2024-09-12 20:04:20 +08:00
|
|
|
|
2024-09-17 14:03:08 +08:00
|
|
|
const KDL::Vector force0 = Kp * (pos_goal - pos0) + Kd * -vel0;
|
2024-09-18 13:52:37 +08:00
|
|
|
KDL::JntArray torque0 = robot_model_.getTorque(force0, 0);
|
2024-09-12 20:04:20 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2024-09-19 22:45:08 +08:00
|
|
|
ctrl_comp_.joint_torque_command_interface_[i].get().set_value(torque0(i));
|
2024-09-12 20:04:20 +08:00
|
|
|
}
|
|
|
|
}
|