202 lines
4.9 KiB
C++
202 lines
4.9 KiB
C++
// mechanism_simulation.h
|
|
#ifndef MECHANISM_SIMULATION_H
|
|
#define MECHANISM_SIMULATION_H
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <cmath>
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include "SharedGeometry/SharedGeometry.h" // 包含共享的几何类
|
|
// 定义常数
|
|
#ifndef M_PI
|
|
#define M_PI 3.14159265358979323846
|
|
#endif
|
|
|
|
// // 前向声明
|
|
// class Vector2D;
|
|
// class Pose7;
|
|
// class PoseCalculator;
|
|
struct MechanismParameters;
|
|
struct MechanismState;
|
|
struct ValidationResult;
|
|
class SimpleTimer;
|
|
class CrankSliderMechanism;
|
|
|
|
// 机构类型枚举
|
|
enum class MechanismType
|
|
{
|
|
Unknown = 0,
|
|
CrankSlider = 1, // 曲柄滑块机构
|
|
FourBar = 2, // 四杆机构
|
|
CrankRockingBlock = 3, // 曲柄摇块机构(逆解)
|
|
CrankRockingBlock_Forward = 4, // 曲柄摇块机构(正解)
|
|
};
|
|
|
|
// 机制参数结构体
|
|
struct MechanismParameters
|
|
{
|
|
double CrankLength2;
|
|
double ConnectingRodLength2;
|
|
double SliderOffset2;
|
|
double InputValue;
|
|
double AngularVelocity;
|
|
|
|
// 四杆机构参数
|
|
double L1; // 曲柄
|
|
double L2; // 连杆
|
|
double L3; // 摇杆
|
|
double L4; // 机架
|
|
|
|
MechanismParameters();
|
|
};
|
|
|
|
// 机制状态结构体
|
|
struct MechanismState
|
|
{
|
|
std::map<std::string, Vector2D> Points;
|
|
std::map<std::string, Pose7> Poses;
|
|
std::map<std::string, double> Angles;
|
|
double InputValue;
|
|
std::string ErrorMessage;
|
|
std::string WarningMessage;
|
|
|
|
MechanismState();
|
|
bool hasError() const;
|
|
bool hasWarning() const;
|
|
};
|
|
|
|
// 验证结果结构体
|
|
struct ValidationResult
|
|
{
|
|
std::vector<std::string> Errors;
|
|
std::vector<std::string> Warnings;
|
|
|
|
bool isValid() const;
|
|
std::string getCombinedMessage() const;
|
|
};
|
|
|
|
// 姿态计算器
|
|
// class PoseCalculator
|
|
// {
|
|
// public:
|
|
// static Pose7 CalculatePoseAndQuaternion(const Vector2D &start, const Vector2D &end);
|
|
// };
|
|
|
|
// 简单的时间封装
|
|
class SimpleTimer
|
|
{
|
|
private:
|
|
std::chrono::steady_clock::time_point start;
|
|
|
|
public:
|
|
SimpleTimer();
|
|
void reset();
|
|
double elapsedSeconds() const;
|
|
};
|
|
|
|
// 曲柄滑块机构类
|
|
class CrankSliderMechanism
|
|
{
|
|
private:
|
|
std::string name;
|
|
MechanismParameters parameters;
|
|
MechanismType type;
|
|
|
|
// 轨迹数据
|
|
std::vector<Vector2D> trajectoryB;
|
|
std::vector<Vector2D> sliderTrajectory;
|
|
std::vector<Vector2D> crankCirclePoints;
|
|
|
|
// 性能统计
|
|
int frameCount;
|
|
SimpleTimer timer;
|
|
|
|
// 配置参数
|
|
std::string codeBody;
|
|
std::string l1ABModelName;
|
|
std::string l2BSModelName;
|
|
std::string l3SModelName;
|
|
double thetaADelta;
|
|
double thetaAValueFactor;
|
|
int thetaAFlip;
|
|
|
|
// 辅助方法
|
|
double degreesToRadians(double degrees) const;
|
|
double radiansToDegrees(double radians) const;
|
|
std::string formatDouble(double value, int precision = 3) const;
|
|
|
|
public:
|
|
CrankSliderMechanism();
|
|
// json calculate(const json ¶m)
|
|
// 基本属性访问
|
|
std::string getName() const;
|
|
MechanismType getType() const;
|
|
MechanismParameters getParameters() const;
|
|
|
|
void setName(const std::string &newName);
|
|
void setParameters(const MechanismParameters ¶ms);
|
|
|
|
// 设置连杆长度
|
|
void setLink(double crankLength, double rodLength, double sliderOffset);
|
|
|
|
// 配置方法
|
|
void setThetaADelta(double delta);
|
|
void setThetaAValueFactor(double factor);
|
|
void setThetaAFlip(int flip);
|
|
void setL_AB(double length);
|
|
void setL_BS(double length);
|
|
void setS_OFS(double offset);
|
|
void setCodeBody(const std::string &code);
|
|
void setL1ABModelName(const std::string &modelName);
|
|
void setL2BSModelName(const std::string &modelName);
|
|
void setL3SModelName(const std::string &modelName);
|
|
|
|
// 主要计算函数
|
|
MechanismState calculate(double _angleDeg);
|
|
|
|
// 参数验证
|
|
ValidationResult validateParameters();
|
|
|
|
// 获取输入范围
|
|
std::pair<double, double> getInputRange();
|
|
|
|
// 获取轨迹点
|
|
std::vector<Vector2D> getTrajectoryPoints();
|
|
|
|
// 获取滑块轨迹
|
|
std::vector<Vector2D> getSliderTrajectory() const;
|
|
|
|
// 获取曲柄圆轨迹点
|
|
std::vector<Vector2D> getCrankCirclePoints() const;
|
|
|
|
// 清除轨迹
|
|
void clearTrajectory();
|
|
|
|
// 获取状态文本
|
|
std::string getStatusText();
|
|
|
|
// 获取配置参数
|
|
double getThetaADelta() const;
|
|
double getThetaAValueFactor() const;
|
|
int getThetaAFlip() const;
|
|
double getL_AB() const;
|
|
double getL_BS() const;
|
|
double getS_OFS() const;
|
|
std::string getCodeBody() const;
|
|
std::string getL1ABModelName() const;
|
|
std::string getL2BSModelName() const;
|
|
std::string getL3SModelName() const;
|
|
};
|
|
|
|
// 工厂函数
|
|
CrankSliderMechanism *createCrankSliderMechanism();
|
|
void deleteCrankSliderMechanism(CrankSliderMechanism *mechanism);
|
|
|
|
// 智能指针版本(可选)
|
|
using CrankSliderMechanismPtr = std::shared_ptr<CrankSliderMechanism>;
|
|
CrankSliderMechanismPtr createCrankSliderMechanismSmart();
|
|
|
|
#endif // MECHANISM_SIMULATION_H
|