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.

162 lines
5.7 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
  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. public:
  83. TMC5130(/* args */);
  84. // static void createDeafultTMC5130Config(TMC5130Config_t *config, TMC5130Port *m_port);
  85. void initialize(cfg_t *cfg);
  86. void enableIC(bool enable);
  87. virtual void enable(bool enable) { enableIC(enable); }
  88. uint8_t reset();
  89. /*******************************************************************************
  90. * üĴд *
  91. *******************************************************************************/
  92. uint32_t readICVersion(); // 5130:0x11 5160:0x30
  93. virtual int32_t getXACTUAL(); // ��ȡ������ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  94. virtual void setXACTUAL(int32_t value); // ���õ�����ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  95. virtual int32_t getVACTUAL(); // ��ȡ������ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  96. uint32_t readXTARGET(); // ��ȡ������Ŀ��λ��
  97. virtual int32_t getENC_POS() { return 0; } // TODO impl it
  98. virtual void setENC_POS(int32_t value) {} // TODO impl it
  99. virtual void setScale(int32_t scale);
  100. virtual void setScaleDenominator(int32_t scale);
  101. virtual void setAcceleration(float accelerationpps2);
  102. virtual void setDeceleration(float accelerationpps2);
  103. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  104. void setMotorShaft(bool reverse); // ���õ�����ת����
  105. uint32_t getTMC5130_RAMPSTAT(); // �����Ĵ�����ȡ����TMC5130��״̬�Ĵ���
  106. Tmc5130RampStat getTMC5130_RAMPSTAT2();
  107. virtual void rotate(int32_t velocity);
  108. virtual void right(int32_t velocity);
  109. virtual void left(int32_t velocity);
  110. virtual void moveTo(int32_t position, uint32_t velocityMax);
  111. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  112. virtual void stop();
  113. virtual bool isReachTarget(); // �Ƿ񵽴�Ŀ��λ��
  114. virtual bool isStoped() { return isReachTarget(); }
  115. /*******************************************************************************
  116. * Ĵд *
  117. *******************************************************************************/
  118. // void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  119. void readWriteArray(uint8_t *data, size_t length);
  120. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  121. void writeInt(uint8_t address, int32_t value);
  122. int32_t readInt(uint8_t address);
  123. private:
  124. uint32_t haspassedms(uint32_t now, uint32_t last);
  125. int32_t to_motor_acc(int32_t acc); // rpm/s^2
  126. int32_t to_motor_vel(int32_t vel); // rpm
  127. int32_t to_motor_pos(int32_t pos); //
  128. int32_t to_user_pos(int32_t pos); //
  129. int32_t to_user_vel(int32_t vel);
  130. };
  131. } // namespace iflytop
  132. #endif