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.

175 lines
6.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "../basic/tmc_ic_interface.hpp"
  7. #include "sdk/os/zos.hpp"
  8. extern "C" {
  9. #include "TMC5130\TMC5130.h"
  10. }
  11. #ifdef HAL_SPI_MODULE_ENABLED
  12. namespace iflytop {
  13. #define TMC5130_LISTENER_MAX 5
  14. /**
  15. * @brief
  16. *
  17. * ע
  18. * 1. ٶȵλ
  19. * velocityĵλ:p/t
  20. * t = 2^24/fin (finΪʱӣʱƵΪ16MHzʱt = 1.048576s)
  21. * ʱ pptԼpps
  22. *
  23. * οTMC5130A_datasheet_rev1.20%20(1)%20(3).pdf 38ҳ е
  24. *
  25. * 2. оƬλ
  26. * reset(); λоƬͬʱоƬļĴΪĬֵһ²Ҫûø÷ΪڳʼʱԶø÷
  27. *
  28. */
  29. class Tmc5130RampStat {
  30. public:
  31. uint32_t m_state;
  32. // �ο�TMC5130A_datasheet_rev1.20%20(1).pdf 40ҳ
  33. typedef enum {
  34. /**
  35. * @brief
  36. * R ֻ
  37. * R+CֻԶ
  38. */
  39. ktmc5130_rs_stopl = TMC5130_RS_STOPL, // (R )
  40. ktmc5130_rs_stopr = TMC5130_RS_STOPR, // (R )
  41. ktmc5130_rs_latchl = TMC5130_RS_LATCHL, // (R+C)
  42. ktmc5130_rs_latchr = TMC5130_RS_LATCHR, // (R+C)
  43. ktmc5130_rs_ev_stopl = TMC5130_RS_EV_STOPL, // (R )
  44. ktmc5130_rs_ev_stopr = TMC5130_RS_EV_STOPR, // (R )
  45. ktmc5130_rs_ev_stop_sg = TMC5130_RS_EV_STOP_SG, // (R+C)
  46. ktmc5130_rs_ev_posreached = TMC5130_RS_EV_POSREACHED, // (R+C)
  47. ktmc5130_rs_velreached = TMC5130_RS_VELREACHED, // (R )
  48. ktmc5130_rs_posreached = TMC5130_RS_POSREACHED, // (R ) �Ƿ񵽴�Ŀ��λ��
  49. ktmc5130_rs_vzero = TMC5130_RS_VZERO, // (R )
  50. ktmc5130_rs_zerowait = TMC5130_RS_ZEROWAIT, // (R )
  51. ktmc5130_rs_secondmove = TMC5130_RS_SECONDMOVE, // (R+C)
  52. ktmc5130_rs_sg = TMC5130_RS_SG, // (R )
  53. } ramp_stat_bit_t;
  54. Tmc5130RampStat(uint32_t state) : m_state(state) {}
  55. bool isSetted(ramp_stat_bit_t bit) { return (m_state & bit) != 0; }
  56. };
  57. class TMC5130 : public IStepperMotor {
  58. public:
  59. typedef struct {
  60. SPI_HandleTypeDef *spi;
  61. Pin_t csgpio = PinNull; //
  62. Pin_t resetPin = PinNull; //
  63. Pin_t fREEZEPin = PinNull; //
  64. Pin_t ennPin = PinNull; //
  65. Pin_t spi_mode_select = PinNull; //
  66. } cfg_t;
  67. protected:
  68. // TMC5130Port *m_port;
  69. // TMC5130Config_t *m_config;
  70. cfg_t m_cfg;
  71. ZGPIO *m_csnpin = NULL;
  72. ZGPIO *m_ennpin = NULL;
  73. ZGPIO *m_spi_mode_select_gpio = NULL;
  74. SPI_HandleTypeDef *m_hspi = NULL;
  75. // uint8_t m_channel;
  76. uint32_t m_lastCallPeriodicJobTick;
  77. int32_t m_shadowRegister[TMC_REGISTER_COUNT];
  78. const uint8_t *m_registerAccessTable;
  79. const int32_t *m_defaultRegisterResetState;
  80. int32_t m_scale = 10000;
  81. int32_t m_scale_deceleration = 1;
  82. mres_type_t m_MRES = kmres_256;
  83. double m_onecirclepulse = 51200;
  84. public:
  85. TMC5130(/* args */);
  86. // static void createDeafultTMC5130Config(TMC5130Config_t *config, TMC5130Port *m_port);
  87. void initialize(cfg_t *cfg);
  88. void enableIC(bool enable);
  89. virtual void enable(bool enable) { enableIC(enable); }
  90. uint8_t reset();
  91. /*******************************************************************************
  92. * üĴд *
  93. *******************************************************************************/
  94. uint32_t readICVersion(); // 5130:0x11 5160:0x30
  95. virtual int32_t getXACTUAL(); // ��ȡ������ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  96. virtual void setXACTUAL(int32_t value); // ���õ�����ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  97. virtual int32_t getVACTUAL(); // ��ȡ������ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  98. uint32_t readXTARGET(); // ��ȡ������Ŀ��λ��
  99. virtual int32_t getENC_POS() { return 0; } // TODO impl it
  100. virtual void setENC_POS(int32_t value) {} // TODO impl it
  101. virtual void setScale(int32_t scale);
  102. virtual void setScaleDenominator(int32_t scale);
  103. virtual void setAcceleration(float accelerationpps2);
  104. virtual void setDeceleration(float accelerationpps2);
  105. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  106. // void setSubdivision(uint8_t subdivision);
  107. void setMotorShaft(bool reverse); // ���õ�����ת����
  108. uint32_t getTMC5130_RAMPSTAT(); // �����Ĵ�����ȡ����TMC5130��״̬�Ĵ���
  109. Tmc5130RampStat getTMC5130_RAMPSTAT2();
  110. virtual void rotate(int32_t velocity);
  111. virtual void right(int32_t velocity);
  112. virtual void left(int32_t velocity);
  113. virtual void moveTo(int32_t position, uint32_t velocityMax);
  114. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  115. virtual void stop();
  116. virtual bool isReachTarget(); // �Ƿ񵽴�Ŀ��λ��
  117. virtual bool isStoped() { return isReachTarget(); }
  118. /*******************************************************************************
  119. * Ĵд *
  120. *******************************************************************************/
  121. // void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  122. void readWriteArray(uint8_t *data, size_t length);
  123. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  124. void writeInt(uint8_t address, int32_t value);
  125. int32_t readInt(uint8_t address);
  126. void setNoAccLimit(bool enable) override;
  127. /**
  128. * @brief õϸ
  129. *
  130. * @param value
  131. */
  132. void setMRES(mres_type_t value);
  133. private:
  134. uint32_t haspassedms(uint32_t now, uint32_t last);
  135. int32_t to_motor_acc(int32_t acc); // rpm/s^2
  136. int32_t to_motor_vel(int32_t vel); // rpm
  137. int32_t to_motor_pos(int32_t pos); //
  138. int32_t to_user_pos(int32_t pos); //
  139. int32_t to_user_vel(int32_t vel);
  140. };
  141. } // namespace iflytop
  142. #endif