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.

163 lines
6.0 KiB

2 years ago
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "../../../hal/zhal.hpp"
  7. #include "../basic/tmc_ic_interface.hpp"
  8. extern "C" {
  9. #include "TMC5130\TMC5130.h"
  10. }
  11. namespace iflytop {
  12. #define TMC5130_LISTENER_MAX 5
  13. /**
  14. * @brief
  15. *
  16. *
  17. * 1.
  18. * velocity的单位是:p/t
  19. * t = 2^24/fin (fin为外接时钟16MHz时t = 1.048576s)
  20. * ppt约等于pps
  21. *
  22. * TMC5130A_datasheet_rev1.20%20(1)%20(3).pdf 38
  23. *
  24. * 2.
  25. * reset();
  26. *
  27. */
  28. class Tmc5130RampStat {
  29. public:
  30. uint32_t m_state;
  31. // 参考TMC5130A_datasheet_rev1.20%20(1).pdf 40页
  32. typedef enum {
  33. /**
  34. * @brief
  35. * R
  36. * R+C
  37. */
  38. ktmc5130_rs_stopl = TMC5130_RS_STOPL, // (R )
  39. ktmc5130_rs_stopr = TMC5130_RS_STOPR, // (R )
  40. ktmc5130_rs_latchl = TMC5130_RS_LATCHL, // (R+C)
  41. ktmc5130_rs_latchr = TMC5130_RS_LATCHR, // (R+C)
  42. ktmc5130_rs_ev_stopl = TMC5130_RS_EV_STOPL, // (R )
  43. ktmc5130_rs_ev_stopr = TMC5130_RS_EV_STOPR, // (R )
  44. ktmc5130_rs_ev_stop_sg = TMC5130_RS_EV_STOP_SG, // (R+C)
  45. ktmc5130_rs_ev_posreached = TMC5130_RS_EV_POSREACHED, // (R+C)
  46. ktmc5130_rs_velreached = TMC5130_RS_VELREACHED, // (R )
  47. ktmc5130_rs_posreached = TMC5130_RS_POSREACHED, // (R ) 是否到达目标位置
  48. ktmc5130_rs_vzero = TMC5130_RS_VZERO, // (R )
  49. ktmc5130_rs_zerowait = TMC5130_RS_ZEROWAIT, // (R )
  50. ktmc5130_rs_secondmove = TMC5130_RS_SECONDMOVE, // (R+C)
  51. ktmc5130_rs_sg = TMC5130_RS_SG, // (R )
  52. } ramp_stat_bit_t;
  53. Tmc5130RampStat(uint32_t state) : m_state(state) {}
  54. bool isSetted(ramp_stat_bit_t bit) { return (m_state & bit) != 0; }
  55. };
  56. class TMC5130Port {
  57. public:
  58. /**
  59. * @brief 使
  60. *
  61. * @param state
  62. */
  63. virtual void TMC5130Port_setENNPinState(uint16_t channel, bool state) = 0;
  64. /**
  65. * @brief TMC5130没有复位引脚
  66. *
  67. * @param channel
  68. * @param state
  69. */
  70. virtual void TMC5130Port_setResetNPinState(uint16_t channel, bool state) = 0;
  71. /**
  72. * @brief us级别延时函数
  73. *
  74. * @param us
  75. */
  76. virtual void TMC5130Port_sleepus(int32_t us) = 0;
  77. /**
  78. * @brief SPI读写接口
  79. *
  80. * @param data
  81. * @param length
  82. */
  83. virtual void TMC5130Port_readWriteArray(uint16_t channel, uint8_t *data, size_t length) = 0;
  84. };
  85. class TMC5130 : public IStepperMotor {
  86. public:
  87. typedef struct {
  88. bool autoReadEventRegister; // 是否自动读取事件寄存器
  89. uint32_t autoReadEventRegisterInterval; // 自动读取事件寄存器的时间间隔,单位为ms
  90. TMC5130Port *m_port;
  91. } TMC5130Config_t;
  92. protected:
  93. TMC5130Port *m_port;
  94. TMC5130Config_t *m_config;
  95. uint8_t m_channel;
  96. uint32_t m_lastCallPeriodicJobTick;
  97. int32_t m_shadowRegister[TMC_REGISTER_COUNT];
  98. const uint8_t *m_registerAccessTable;
  99. const int32_t *m_defaultRegisterResetState;
  100. public:
  101. TMC5130(/* args */);
  102. static void createDeafultTMC5130Config(TMC5130Config_t *config, TMC5130Port *m_port);
  103. void initialize(uint8_t channel, TMC5130Config_t *config);
  104. void periodicJob(uint32_t ticket);
  105. void enableIC(bool enable);
  106. uint8_t reset();
  107. /*******************************************************************************
  108. * *
  109. *******************************************************************************/
  110. uint32_t readChipVERSION(); // 5130:0x11
  111. virtual int32_t getXACTUAL(); // 读取电机当前位置,与编码器的位置值不同,该值是电机在不丢步的情况下的位置
  112. virtual void setXACTUAL(int32_t value); // 设置电机当前位置,与编码器的位置值不同,该值是电机在不丢步的情况下的位置
  113. virtual int32_t getVACTUAL(); // 读取电机当前位置,与编码器的位置值不同,该值是电机在不丢步的情况下的位置
  114. uint32_t readXTARGET(); // 读取驱动器目标位置
  115. virtual int32_t getENC_POS() { return 0; } // TODO impl it
  116. virtual void setENC_POS(int32_t value) {} // TODO impl it
  117. virtual void setAcceleration(float accelerationpps2);
  118. virtual void setDeceleration(float accelerationpps2);
  119. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  120. void setMotorShaft(bool reverse); // 设置电机旋转方向
  121. uint32_t getTMC5130_RAMPSTAT(); // 这个寄存器读取的是TMC5130的状态寄存器
  122. Tmc5130RampStat getTMC5130_RAMPSTAT2();
  123. virtual void rotate(int32_t velocity);
  124. virtual void right(int32_t velocity);
  125. virtual void left(int32_t velocity);
  126. virtual void moveTo(int32_t position, uint32_t velocityMax);
  127. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  128. virtual void stop();
  129. virtual bool isReachTarget(); // 是否到达目标位置
  130. /*******************************************************************************
  131. * *
  132. *******************************************************************************/
  133. void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  134. void readWriteArray(uint8_t *data, size_t length);
  135. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  136. void writeInt(uint8_t address, int32_t value);
  137. int32_t readInt(uint8_t address);
  138. private:
  139. uint32_t haspassedms(uint32_t now, uint32_t last);
  140. };
  141. } // namespace iflytop