rm_controllers
Loading...
Searching...
No Matches
helper_functions.h
Go to the documentation of this file.
1//
2// Created by guanlin on 25-8-27.
3//
4
5#pragma once
6
7#include <angles/angles.h>
8#include <cstddef>
9#include <memory>
10#include <vector>
11#include <Eigen/Dense>
12#include <control_toolbox/pid.h>
13#include <geometry_msgs/Quaternion.h>
14#include <hardware_interface/joint_command_interface.h>
15
19
21{
22static inline double get_LM(const double& l)
23{
24 return 0.218f * l + 0.075f;
25};
26
27static inline double get_i_p(const double& l)
28{
29 return 0.4f * l + 0.07f;
30};
31
32static inline double get_theta_leg_offset(const double& l)
33{
34 return M_PI_4 / 2;
35}
36
44inline void generateAB(const std::shared_ptr<ModelParams>& model_params, Eigen::Matrix<double, STATE_DIM, STATE_DIM>& a,
45 Eigen::Matrix<double, STATE_DIM, CONTROL_DIM>& b, double leg_length)
46{
47 double A[36] = { 0. }, B[12]{ 0. };
48 // double L = leg_length * model_params->L_weight;
49 // double Lm = leg_length * model_params->Lm_weight;
50 double Lm = get_LM(leg_length);
51 double L = leg_length - Lm;
52 double i_p = get_i_p(leg_length);
53
54 // auto theta_from_length = [](double L) -> double {
55 // return -23.36693691 * L * L * L + 24.76241959 * L * L - 11.65313741 * L + 2.49258628;
56 // };
57 // double theta_L = theta_from_length(leg_length);
58 // gen_A_leg_offset(model_params->i_m, model_params->i_p, model_params->i_w, L, Lm, model_params->M, model_params->r,
59 // model_params->g, model_params->l, model_params->m_p, model_params->m_w, theta_L, A);
60 // gen_B_leg_offset(model_params->i_m, model_params->i_p, model_params->i_w, L, Lm, model_params->M, model_params->r,
61 // model_params->g, model_params->l, model_params->m_p, model_params->m_w, theta_L, B);
62
63 // gen_A(model_params->i_m, model_params->i_p, model_params->i_w, L, Lm, model_params->M, model_params->r,
64 // model_params->g, model_params->l, model_params->m_p, model_params->m_w, A);
65 // gen_B(model_params->i_m, model_params->i_p, model_params->i_w, L, Lm, model_params->M, model_params->r,
66 // model_params->l, model_params->m_p, model_params->m_w, B);
67
68 gen_A(model_params->i_m, i_p, model_params->i_w, L, Lm, model_params->M, model_params->r, model_params->g,
69 model_params->l, model_params->m_p, model_params->m_w, A);
70 gen_B(model_params->i_m, i_p, model_params->i_w, L, Lm, model_params->M, model_params->r, model_params->l,
71 model_params->m_p, model_params->m_w, B);
72
73 // clang-format off
74 a<< 0. ,1.,0.,0.,0. ,0.,
75 A[1],0.,0.,0.,A[25],0.,
76 0. ,0.,0.,1.,0. ,0.,
77 A[3],0.,0.,0.,A[27],0.,
78 0. ,0.,0.,0.,0. ,1.,
79 A[5],0.,0.,0.,A[29],0.;
80 b<< 0. ,0. ,
81 B[1],B[7],
82 0. ,0. ,
83 B[3],B[9],
84 0. ,0. ,
85 B[5],B[11];
86 // clang-format on
87}
88
97inline void setJointCommands(std::vector<hardware_interface::JointHandle*>& joints, const LegCommand& left_cmd,
98 const LegCommand& right_cmd, double wheel_left = 0., double wheel_right = 0.)
99{
100 if (joints.size() != 6)
101 throw std::runtime_error("Joint handle vector size must be 6!");
102
103 joints[0]->setCommand(left_cmd.input[0]);
104 joints[1]->setCommand(left_cmd.input[1]);
105 joints[2]->setCommand(right_cmd.input[0]);
106 joints[3]->setCommand(right_cmd.input[1]);
107 joints[4]->setCommand(wheel_left);
108 joints[5]->setCommand(wheel_right);
109}
110
118inline void quatToRPY(const geometry_msgs::Quaternion& q, double& roll, double& pitch, double& yaw)
119{
120 double as = std::min(-2. * (q.x * q.z - q.w * q.y), .99999);
121 yaw = std::atan2(2 * (q.x * q.y + q.w * q.z), q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z);
122 pitch = std::asin(as);
123 roll = std::atan2(2 * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z);
124}
125
126inline void clamp(double& val, const double& minVal, const double& maxVal)
127{
128 if (val < minVal)
129 val = minVal;
130 if (val > maxVal)
131 val = maxVal;
132}
133
134} // namespace rm_chassis_controllers
void gen_A(double Im, double Ip, double Iw, double L, double Lm, double M, double R, double g, double l, double mp, double mw, double A[36])
Definition gen_A.c:32
void gen_B(double Im, double Ip, double Iw, double L, double Lm, double M, double R, double l, double mp, double mw, double B[12])
Definition gen_B.c:29
Definition active_suspension.h:16
void setJointCommands(std::vector< hardware_interface::JointHandle * > &joints, const LegCommand &left_cmd, const LegCommand &right_cmd, double wheel_left=0., double wheel_right=0.)
Definition helper_functions.h:97
void quatToRPY(const geometry_msgs::Quaternion &q, double &roll, double &pitch, double &yaw)
Definition helper_functions.h:118
void generateAB(const std::shared_ptr< ModelParams > &model_params, Eigen::Matrix< double, STATE_DIM, STATE_DIM > &a, Eigen::Matrix< double, STATE_DIM, CONTROL_DIM > &b, double leg_length)
Definition helper_functions.h:44
void clamp(double &val, const double &minVal, const double &maxVal)
Definition helper_functions.h:126
Definition definitions.h:84
double input[2]
Definition definitions.h:87