quadruped_ros2_control/controllers/ocs2_quadruped_controller/src/control/TargetManager.cpp

60 lines
2.6 KiB
C++
Raw Normal View History

2024-09-30 11:59:35 +08:00
//
// Created by tlab-uav on 24-9-30.
//
#include "ocs2_quadruped_controller/control/TargetManager.h"
#include <ocs2_core/misc/LoadData.h>
#include <ocs2_robotic_tools/common/RotationTransforms.h>
2025-02-23 22:07:46 +08:00
namespace ocs2::legged_robot
{
2025-02-27 21:43:10 +08:00
TargetManager::TargetManager(CtrlInterfaces& ctrl_component,
2025-02-23 22:07:46 +08:00
const std::shared_ptr<ReferenceManagerInterface>& referenceManagerPtr,
const std::string& task_file,
const std::string& reference_file)
2024-09-30 11:59:35 +08:00
: ctrl_component_(ctrl_component),
2025-02-23 22:07:46 +08:00
referenceManagerPtr_(referenceManagerPtr)
{
2024-09-30 11:59:35 +08:00
default_joint_state_ = vector_t::Zero(12);
loadData::loadCppDataType(reference_file, "comHeight", command_height_);
loadData::loadEigenMatrix(reference_file, "defaultJointState", default_joint_state_);
loadData::loadCppDataType(task_file, "mpc.timeHorizon", time_to_target_);
loadData::loadCppDataType(reference_file, "targetRotationVelocity", target_rotation_velocity_);
loadData::loadCppDataType(reference_file, "targetDisplacementVelocity", target_displacement_velocity_);
2024-09-30 11:59:35 +08:00
}
void TargetManager::update(SystemObservation& observation)
2025-02-23 22:07:46 +08:00
{
2024-09-30 11:59:35 +08:00
vector_t cmdGoal = vector_t::Zero(6);
cmdGoal[0] = ctrl_component_.control_inputs_.ly * target_displacement_velocity_;
cmdGoal[1] = -ctrl_component_.control_inputs_.lx * target_displacement_velocity_;
2024-09-30 11:59:35 +08:00
cmdGoal[2] = ctrl_component_.control_inputs_.ry;
cmdGoal[3] = -ctrl_component_.control_inputs_.rx * target_rotation_velocity_;
2024-09-30 11:59:35 +08:00
2025-02-27 21:43:10 +08:00
const vector_t currentPose = observation.state.segment<6>(6);
2024-09-30 11:59:35 +08:00
const Eigen::Matrix<scalar_t, 3, 1> zyx = currentPose.tail(3);
2024-10-16 22:59:11 +08:00
vector_t cmd_vel_rot = getRotationMatrixFromZyxEulerAngles(zyx) * cmdGoal.head(3);
2024-09-30 11:59:35 +08:00
2025-02-23 22:07:46 +08:00
const vector_t targetPose = [&]()
{
2024-09-30 11:59:35 +08:00
vector_t target(6);
2024-10-16 22:59:11 +08:00
target(0) = currentPose(0) + cmd_vel_rot(0) * time_to_target_;
target(1) = currentPose(1) + cmd_vel_rot(1) * time_to_target_;
2024-09-30 11:59:35 +08:00
target(2) = command_height_;
2024-10-16 22:59:11 +08:00
target(3) = currentPose(3) + cmdGoal(3) * time_to_target_;
2024-09-30 11:59:35 +08:00
target(4) = 0;
target(5) = 0;
return target;
}();
2025-02-27 21:43:10 +08:00
const scalar_t targetReachingTime = observation.time + time_to_target_;
2024-09-30 11:59:35 +08:00
auto trajectories =
2025-02-27 21:43:10 +08:00
targetPoseToTargetTrajectories(targetPose, observation, targetReachingTime);
2024-10-16 22:59:11 +08:00
trajectories.stateTrajectory[0].head(3) = cmd_vel_rot;
trajectories.stateTrajectory[1].head(3) = cmd_vel_rot;
2024-09-30 11:59:35 +08:00
referenceManagerPtr_->setTargetTrajectories(std::move(trajectories));
}
}