基质喷涂
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.

130 lines
3.4 KiB

3 weeks ago
3 weeks ago
  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_move_finished_; // 是否已经移动结束
  52. osTimerId_t readTimer;
  53. osMutexId_t mutex_;
  54. bool isRunHomingTask { true }; // 是否运行回原点任务
  55. osThreadAttr_t home_task_attr = {0};
  56. osThreadId_t homingStateMachineThreadId; // 回原点线程
  57. HomeEvent home_event_;
  58. osMessageQueueId_t homeEventQueue; // 回原点事件队列
  59. HomeState home_state_{STATE_INIT}; // home 状态机
  60. bool is_home_init_{ false }; // 是否已经第一次(初始化回过原点)回过原点
  61. bool is_home_suc_ { false }; // 是否已经成功回原点
  62. uint8_t seq_id_home_ {0}; // 回 HOME 点 指令 SEQ ID
  63. uint8_t seq_id_move_ {0}; // 移动到指定到位置 SEQ ID
  64. static void readTimerCallback(void* arg);
  65. public:
  66. ECLMotor();
  67. ~ECLMotor();
  68. void setMotorIndex(int32_t index);
  69. // 是否正在回原点的逻辑
  70. bool isHomeSuc();
  71. void startMoveHome();
  72. void setHomeSeqId(uint8_t seq_id);
  73. void setMoveFinishSeqId(uint8_t seq_id);
  74. int32_t getPosition();
  75. void setTargetPosition(int32_t target);
  76. void setShiftPosition(int32_t shift_target);
  77. void moveToWithSpeed(int32_t target, int32_t speed);
  78. void setSpeed(int32_t speed);
  79. bool isMoveFinished();
  80. int32_t getEncoderPosition();
  81. void moveToHome();
  82. void runZeroLimit(bool isEnter); // 停止运动
  83. void runEndLimit(bool isEnter); // 停止运动
  84. void runE_Stop();
  85. void runPause();
  86. void runStop();
  87. void ECL_Rotate(int32_t speed, const bool is_flip = true);
  88. void readMotorPosition();
  89. void startReadTimer();
  90. void setEncoderPosition(int32_t encoderPos);
  91. int32_t getRTSpeed();
  92. private:
  93. void runhomeSuc();
  94. // 回原点任务
  95. static void homingTask(void *arg);
  96. };
  97. #endif //MOTOR_STATUS_H