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.

188 lines
6.4 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
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. #if 1
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "../basic/tmc_ic_interface.hpp"
  8. #include "sdk/os/zos.hpp"
  9. #include "tmc_driver_ic.hpp"
  10. extern "C" {
  11. #include "TMC2160\TMC2160.h"
  12. #include "TMC4361A\TMC4361A.h"
  13. }
  14. #ifdef HAL_SPI_MODULE_ENABLED
  15. namespace iflytop {
  16. #define TMC4361A_LISTENER_MAX 5
  17. class TMC4361A : public IStepperMotor {
  18. public:
  19. typedef enum {
  20. IC_TMC2130 = 0, //
  21. IC_TMC2160,
  22. IC_TMC2660,
  23. } driver_ic_type_t;
  24. #if 0
  25. typedef struct {
  26. SPI_HandleTypeDef *spi; //
  27. ZGPIO *csgpio; //
  28. ZGPIO *resetPin; //
  29. ZGPIO *fREEZEPin; //
  30. ZGPIO *ennPin; //
  31. ZGPIO *driverIC_ennPin; //
  32. ZGPIO *driverIC_resetPin; //
  33. } cfg_t;
  34. #endif
  35. class cfg_t {
  36. public:
  37. SPI_HandleTypeDef *spi = nullptr; //
  38. Pin_t csgpio = PinNull; //
  39. Pin_t resetPin = PinNull; //
  40. Pin_t fREEZEPin = PinNull; //
  41. Pin_t ennPin = PinNull; //
  42. Pin_t driverIC_ennPin = PinNull; //
  43. Pin_t driverIC_resetPin = PinNull; //
  44. };
  45. typedef enum {
  46. kvelmode,
  47. kposmode,
  48. } motor_mode_t;
  49. private:
  50. int32_t shadowRegister[TMC_REGISTER_COUNT];
  51. const uint8_t *m_registerAccessTable = nullptr;
  52. const int32_t *m_defaultRegisterResetState = nullptr;
  53. driver_ic_type_t m_driver_ic_type;
  54. uint32_t m_lastCallPeriodicJobTick;
  55. uint8_t m_status;
  56. SPI_HandleTypeDef *m_spi = nullptr;
  57. ZGPIO *m_csgpio = nullptr;
  58. ZGPIO *m_resetPin = nullptr;
  59. ZGPIO *m_fREEZEPin = nullptr;
  60. ZGPIO *m_ennPin = nullptr;
  61. ZGPIO *m_driverIC_ennPin = nullptr;
  62. ZGPIO *m_driverIC_resetPin = nullptr;
  63. motor_mode_t m_motor_mode = kvelmode;
  64. int32_t m_scale = 10000;
  65. int32_t m_scale_deceleration = 1;
  66. public:
  67. TMC4361A(/* args */);
  68. /*******************************************************************************
  69. * ʼط *
  70. *******************************************************************************/
  71. /**
  72. * @brief ̬ĬϵTMC4361Aò,ʹʱֻ޸ԼҪIJ
  73. *
  74. * ע:
  75. * 1. ÷ڲʹõһ̬ÿεø÷ʱصĶͬһĵַ
  76. * 2. ÷صֵҪͷ
  77. * @param config
  78. */
  79. void initialize(cfg_t *cfg);
  80. void enableIC(bool enable);
  81. virtual void enable(bool enable) { enableIC(enable); }
  82. /*******************************************************************************
  83. * IStepperMotor impl *
  84. *******************************************************************************/
  85. // virtual void registerListener(MotorEventListener *listener);
  86. virtual void rotate(int32_t velocity);
  87. virtual void moveTo(int32_t position, uint32_t velocityMax);
  88. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  89. virtual void stop();
  90. virtual int32_t getXACTUAL();
  91. virtual int32_t getXTARGET();
  92. virtual void setXACTUAL(int32_t value);
  93. virtual int32_t getVACTUAL();
  94. virtual int32_t getENC_POS();
  95. virtual void setENC_POS(int32_t value);
  96. virtual void setAcceleration(float accelerationpps2); // �����������ٶ�,����ֵ4000000
  97. virtual void setDeceleration(float accelerationpps2); // �����������ٶ�,����ֵ4000000
  98. virtual void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) { driverIC_setIHOLD_IRUN(ihold, irun, iholddelay); }
  99. virtual void setMotorShaft(bool reverse) { driverIC_setMotorShaft(reverse); }
  100. virtual void setScale(int32_t scale);
  101. virtual void setScaleDenominator(int32_t scale);
  102. // Left Virtual Limit Switch XACTUAL �� VIRT_STOP_LEFT ʱ����
  103. void setLeftVirtualLimitSwitch(bool enable, int32_t position);
  104. // Right Virtual Limit Switch XACTUAL �� VIRT_STOP_RIGHT ʱ����
  105. void setRightVirtualLimitSwitch(bool enable, int32_t position);
  106. /*******************************************************************************
  107. * ʼ *
  108. *******************************************************************************/
  109. /**
  110. * @brief ʼ
  111. *
  112. * @param channel SPIͨ
  113. * @param driver_ic_type оƬ
  114. *
  115. * TMC4361A:0x2 DriverIC:0x30
  116. */
  117. int32_t readICVersion();
  118. uint8_t reset();
  119. uint8_t restore();
  120. /*******************************************************************************
  121. * Ĵд *
  122. *******************************************************************************/
  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 readWriteCover(uint8_t *data, size_t length);
  127. void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  128. int32_t getENC_POS_DEV(); // ENC_POS��XACTUAL��ƫ��
  129. uint32_t readEVENTS(); // ��ȡ�¼��Ĵ�
  130. /*******************************************************
  131. * driverIc function *
  132. *******************************************************/
  133. int32_t readSubICVersion();
  134. virtual bool isReachTarget();
  135. virtual bool isStoped() { return isReachTarget(); }
  136. public:
  137. // only used in tmc4361A.cpp
  138. void tmc4361AConfigCallback(ConfigState state);
  139. void readWriteArray(uint8_t *data, size_t length);
  140. private:
  141. uint32_t haspassedms(uint32_t now, uint32_t last);
  142. void driverIC_reset();
  143. void driverIC_enableIC(bool enable);
  144. void driverIC_setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  145. void driverIC_writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  146. void driverIC_writeInt(uint8_t address, int32_t value);
  147. int32_t driverIC_readInt(uint8_t address);
  148. void driverIC_setMotorShaft(bool reverse);
  149. uint32_t driverIC_readICVersion();
  150. private:
  151. int32_t to_motor_acc(int32_t acc); // rpm/s^2
  152. int32_t to_motor_vel(int32_t vel); // rpm
  153. int32_t to_motor_pos(int32_t pos); //
  154. int32_t to_user_pos(int32_t pos); //
  155. int32_t to_user_vel(int32_t vel);
  156. };
  157. } // namespace iflytop
  158. #endif
  159. #endif