基质喷涂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.6 KiB

  1. //
  2. // Created by iflyt on 2025/3/3.
  3. //
  4. #ifndef MOTOR_STATUS_H
  5. #define MOTOR_STATUS_H
  6. #include "cmsis_os2.h"
  7. #include <stdint.h>
  8. #include "elc_motor_helper.h"
  9. // 读取速率档位
  10. typedef enum {
  11. READ_RATE_LOW,
  12. READ_RATE_MEDIUM,
  13. READ_RATE_HIGH
  14. } ReadRate;
  15. typedef struct {
  16. uint8_t event_type; // 事件类型
  17. int32_t event_data; // 事件相关数据,如编码器位置
  18. } HomeEvent;
  19. typedef enum {
  20. EVENT_ORIGIN_LEAVE, // 触发原点离开事件
  21. EVENT_IN_ORIGIN, // 当前已经在原点
  22. EVENT_ORIGIN_ENTER, // 触发原点进入事件
  23. EVENT_ENCODER_BIG_DIFF, // 编码器位置与实际位置偏差过大
  24. EVENT_ENCODER_FAR_ORIGIN, // 编码器位置距离原点较远
  25. EVENT_ENCODER_MODERATE_ORIGIN, // 编码器位置合适的距离
  26. EVENT_ENCODER_NEAR_ORIGN, // 编码器位置距离原点较近
  27. EVENT_MOTOR_MOVE_FINISHED, // 电机移动结束
  28. EVENT_START_HOMING // 开始回原点事件
  29. } HomeEventType;
  30. // 定义回原点状态机枚举
  31. typedef enum {
  32. STATE_INIT,
  33. STATE_BACK_TO_ORIGIN_HIGH, // 高速回原点
  34. STATE_BACK_TO_ORIGIN_MIDDLE, // 中速回原点
  35. STATE_BACK_TO_ORIGIN_LOW, // 低速回到原点
  36. STATE_LEAVE_ORIGIN_MIDDLE, // 低速离开原点
  37. STATE_FORWARD_TO_ORIGIN_MIDDLE, // 中速向前 (向前5mm)
  38. STATE_HOMED // 成功回到原点
  39. } HomeState;
  40. /**
  41. * Encoder Close Loop Motor Control
  42. */
  43. class ECLMotor {
  44. private:
  45. int32_t index_; // 电机索引
  46. int32_t x_actual_;
  47. int32_t enc_val_;
  48. int32_t rt_speed_; // 实时速度
  49. int32_t target_pos_; // 目标位置
  50. int32_t target_speed_; // 目标速度
  51. bool is_moving_;
  52. bool isFoward_; // 是否正向运动
  53. ReadRate readRate;
  54. osTimerId_t readTimer;
  55. osMutexId_t mutex_;
  56. bool isHomed_{ false }; // 是否已经回过原点
  57. HomeState homeState_{STATE_INIT}; // home 状态机
  58. HomeEvent home_event_;
  59. int32_t home_count_ { 0 }; // 回 home 点计数 超时清零
  60. int32_t max_home_count_ { 600 }; // 60S == 100ms * 600
  61. DistanceLevel level_ { DistanceLow };
  62. bool isRunHomingTask { true }; // 是否运行回原点任务
  63. osMessageQueueId_t homeEventQueue; // 回原点事件队列
  64. osThreadAttr_t home_task_attr = {0};
  65. osThreadId_t homingStateMachineThreadId; // 回原点线程
  66. uint8_t seq_id_home_ {0}; // 回 HOME 点 SEQ ID
  67. uint8_t seq_id_move_finished_ {0}; // 运动到指定位置 点 SEQ ID
  68. static void readTimerCallback(void* arg);
  69. public:
  70. ECLMotor();
  71. ~ECLMotor();
  72. void setMotorIndex(int32_t index);
  73. // 是否已经回归到原点
  74. bool isHomed();
  75. // 是否正在回原点的逻辑
  76. bool isHoming();
  77. void runhomeSuc();
  78. void startMoveHome();
  79. void setHomeSeqId(uint8_t seq_id);
  80. void setMoveFinishSeqId(uint8_t seq_id);
  81. int32_t getPosition();
  82. void setTargetPosition(int32_t target);
  83. void setShiftPosition(int32_t shift_target);
  84. void moveToWithSpeed(int32_t target, int32_t speed);
  85. void setSpeed(int32_t speed);
  86. bool isMoving();
  87. int32_t getEncoderPosition();
  88. void setMoving(bool moving); // 新增设置是否运动的接口
  89. void moveToHome();
  90. void runZeroLimit(bool isEnter); // 停止运动
  91. void runEndLimit(bool isEnter); // 停止运动
  92. void runE_Stop();
  93. void runPause();
  94. void runStop();
  95. void ECL_Rotate(int32_t speed, const bool is_flip = true);
  96. void readMotorPosition();
  97. void setReadRate(ReadRate rate);
  98. void setEncoderPosition(int32_t encoderPos);
  99. int32_t getRTSpeed();
  100. // 回原点任务
  101. static void homingTask(void *arg);
  102. };
  103. #endif //MOTOR_STATUS_H