// KinematicsReverse.h #ifndef KINEMATICS_REVERSE_H #define KINEMATICS_REVERSE_H #include #include #include #include #include #include #include #include #include "utils.h" #include "QuadrupedRobotSimulation/BaseClass.h" #include "QuadrupedRobotSimulation/OPERATION.h" #include "SharedGeometry/SharedGeometry.h" class ReverseKinematicsCalculator { private: // 统一配置 QuadrupedRobotConfiguration _config; std::shared_ptr _manager; // 基本输入参数 double adjusted_shank_angles_factory; std::vector A_point; double L10; double L20; double L30; double L21; double L22; double L23; double beta1; double CC1; double L31; double C2C3_length; double lead1; double D1_L_offset; double D2_L_offset; double C4_D2_offset; // 初始C3C4距离 double initial_C3C4_LF; double initial_C3C4_LH; double initial_C3C4_RF; double initial_C3C4_RH; // 计算得到的中间变量 double _BB2_BC_angle_rad; double _C1_B_length; double _CC4_distance; // 数学常量 static constexpr double PI = 3.14159265358979323846; static constexpr double DegToRad = PI / 180.0; static constexpr double RadToDeg = 180.0 / PI; // 静态变量(模拟C#的静态字段) std::map> old_initial_points_dictLF; std::map> old_initial_points_dictLH; static bool IsSupporLF; static bool IsSupporLH; static double BodyPosition_x; static double BodyPosition_y; static double BodyPosition_z; static double BodyPosition_qx; static double BodyPosition_qy; static double BodyPosition_qz; static double BodyPosition_qw; static const std::string BodyCode; public: // 构造函数 ReverseKinematicsCalculator(std::shared_ptr manager = nullptr); void debugPrintParamDict(const std::map ¶mDict) const; void debugPrintLegCalculation(const std::string &legName, const LegParam &legParam, const std::map> &points_dict, const ModelID &modelID) const; void debugPrintFinalJson(const std::vector &objStates) const; // 主计算函数 std::string CalculateAllPointsFromMotorAnglesJsonStr(); std::string CalculateAllPointsOnlyOneLegFromMotorAnglesJsonStr( double thigh_angle_deg, double shank_angle_deg, double ankle_angle_deg, const std::string &leg_type = "LF", bool IsReset = false); // 逆向计算主函数 std::map> CalculateAllPointsFromMotorAnglesReverse( double thigh_angle_deg, double shank_angle_deg, double ankle_angle_deg, const std::string &leg_type = "LF", bool IsReset = false); // 判断是否为支撑点 bool IsSupportPoint(const std::map> &pointsDict, double groundHeight = 0.0, double tolerance = 1.0); // 验证计算结果 std::pair> ValidateReverseCalculation( const std::map> &points_dict, double thigh_angle, double shank_angle, double ankle_angle); // 批量计算 std::vector BatchReverseCalculation( const std::map> &motor_data, const std::string &leg_type = "LF", std::optional max_frames = std::nullopt); // 获取配置 QuadrupedRobotConfiguration GetConfig() const { return _config; } // 更新配置 void UpdateConfiguration(const QuadrupedRobotConfiguration &newConfig); private: // 从配置加载参数 void LoadParametersFromConfig(); // 计算派生参数 void CalculateDerivedParameters(); // 验证配置 void ValidateConfiguration(); // 计算点C(四杆机构解析解) std::optional CalculatePointC(const Vector2D &A, const Vector2D &D, const Vector2D &B, double l2, double l3); // 选择C点(确保C和B在AD同侧) Vector2D SelectCPointByADSide(const Vector2D &A, const Vector2D &D, const Vector2D &B, const Vector2D &C1, const Vector2D &C2); // 根据大腿角度计算B点 std::vector CalculateBFromThighAngleReverse(double thigh_angle_deg); // 根据小腿角度计算B1点 std::vector CalculateB1FromShankAngleReverse(const std::vector &B_p, double shank_angle_deg); // 根据四杆机构计算B2点 std::pair, std::string> CalculateB2FromFourbarReverse( const std::vector &A_p, const std::vector &B_p, const std::vector &B1_p, double L22, double L23); // 根据B和B2计算C点 std::vector CalculateCFromBAndB2Reverse(const std::vector &B1, const std::vector &B2, double L, double theta_deg); // 反向计算C1点 std::vector CalculateC1PointReverse(const std::vector &B_p, const std::vector &C_p); // 根据C1点计算C2点 std::vector CalculateC2PointFromC1Reverse(const std::vector &C1_p, const std::vector &B_p, const std::vector &C_point); // 根据脚踝角度计算C3C4距离 double CalculateC3C4DistanceFromAnkleAngleReverse(double ankle_angle_deg, const std::string &leg_type = "LF"); // 已知三角形三边和两个顶点,求第三个顶点 std::vector FindThirdVertex(const std::vector &C, const std::vector &C2, double a, double b, double c); // 根据C和C2点计算C4点 std::vector CalculateC4PointFromCAndC2Reverse(const std::vector &C_p, const std::vector &C2_p, double ankle_angle_deg, const std::string &leg_type = "LF"); // 根据C和C4点计算L点 std::vector CalculateLPointsFromCAndC4Reverse(const std::vector &C, const std::vector &C4); // 求直角三角形直角顶点 std::vector FindRightAngleThirdVertex(const std::vector &C, const std::vector &L, double a, double b); // 根据C和L点计算D1点 std::vector CalculateD1PointsFromCAndLReverse(const std::vector &C_p, const std::vector &L_p); // 求直角三角形直角顶点D2 std::vector FindRightAngleVertexD2(const std::vector &L, const std::vector &C4, double a, double b); // 根据L和C4点计算D2点 std::vector CalculateD2PointsFromCAndLReverse(const std::vector &L, const std::vector &C4); // 求直角三角形直角顶点C3 std::vector FindRightAngleVertex(const std::vector &C2, const std::vector &C4, double L); // 根据C2和C4点计算C3点 std::vector CalculateC3PointFromC2AndC4Reverse(const std::vector &C2_point, const std::vector &C4_point); // 从字典安全获取点坐标 std::vector GetPointSafe(const std::map> &pointsDict, const std::string &key); // 获取模型ID // 新版本: // std::string GetValueModelID( // const ModelID &modelID, // 改为 ModelID 类型 // const std::string &legName, const std::string &key); const LegModelIDs &GetLegModelIDs(const ModelID &modelID, const std::string &legName); // 辅助函数:旋转向量 std::vector RotateVector(double angle, const std::vector &vector); }; #endif // KINEMATICS_REVERSE_H