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.

186 lines
6.3 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
  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; // 10000һȦ
  65. public:
  66. TMC4361A(/* args */);
  67. /*******************************************************************************
  68. * ʼط *
  69. *******************************************************************************/
  70. /**
  71. * @brief ̬ĬϵTMC4361Aò,ʹʱֻ޸ԼҪIJ
  72. *
  73. * ע:
  74. * 1. ÷ڲʹõһ̬ÿεø÷ʱصĶͬһĵַ
  75. * 2. ÷صֵҪͷ
  76. * @param config
  77. */
  78. void initialize(cfg_t *cfg);
  79. void enableIC(bool enable);
  80. virtual void enable(bool enable) { enableIC(enable); }
  81. /*******************************************************************************
  82. * IStepperMotor impl *
  83. *******************************************************************************/
  84. // virtual void registerListener(MotorEventListener *listener);
  85. virtual void rotate(int32_t velocity);
  86. virtual void moveTo(int32_t position, uint32_t velocityMax);
  87. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  88. virtual void stop();
  89. virtual int32_t getXACTUAL();
  90. virtual int32_t getXTARGET();
  91. virtual void setXACTUAL(int32_t value);
  92. virtual int32_t getVACTUAL();
  93. virtual int32_t getENC_POS();
  94. virtual void setENC_POS(int32_t value);
  95. virtual void setAcceleration(float accelerationpps2); // �����������ٶ�,����ֵ4000000
  96. virtual void setDeceleration(float accelerationpps2); // �����������ٶ�,����ֵ4000000
  97. virtual void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) { driverIC_setIHOLD_IRUN(ihold, irun, iholddelay); }
  98. virtual void setMotorShaft(bool reverse) { driverIC_setMotorShaft(reverse); }
  99. virtual void setScale(int32_t scale);
  100. // Left Virtual Limit Switch XACTUAL �� VIRT_STOP_LEFT ʱ����
  101. void setLeftVirtualLimitSwitch(bool enable, int32_t position);
  102. // Right Virtual Limit Switch XACTUAL �� VIRT_STOP_RIGHT ʱ����
  103. void setRightVirtualLimitSwitch(bool enable, int32_t position);
  104. /*******************************************************************************
  105. * ʼ *
  106. *******************************************************************************/
  107. /**
  108. * @brief ʼ
  109. *
  110. * @param channel SPIͨ
  111. * @param driver_ic_type оƬ
  112. *
  113. * TMC4361A:0x2 DriverIC:0x30
  114. */
  115. int32_t readICVersion();
  116. uint8_t reset();
  117. uint8_t restore();
  118. /*******************************************************************************
  119. * Ĵд *
  120. *******************************************************************************/
  121. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  122. void writeInt(uint8_t address, int32_t value);
  123. int32_t readInt(uint8_t address);
  124. void readWriteCover(uint8_t *data, size_t length);
  125. void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  126. int32_t getENC_POS_DEV(); // ENC_POS��XACTUAL��ƫ��
  127. uint32_t readEVENTS(); // ��ȡ�¼��Ĵ�
  128. /*******************************************************
  129. * driverIc function *
  130. *******************************************************/
  131. int32_t readSubICVersion();
  132. virtual bool isReachTarget();
  133. virtual bool isStoped() { return isReachTarget(); }
  134. public:
  135. // only used in tmc4361A.cpp
  136. void tmc4361AConfigCallback(ConfigState state);
  137. void readWriteArray(uint8_t *data, size_t length);
  138. private:
  139. uint32_t haspassedms(uint32_t now, uint32_t last);
  140. void driverIC_reset();
  141. void driverIC_enableIC(bool enable);
  142. void driverIC_setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  143. void driverIC_writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  144. void driverIC_writeInt(uint8_t address, int32_t value);
  145. int32_t driverIC_readInt(uint8_t address);
  146. void driverIC_setMotorShaft(bool reverse);
  147. uint32_t driverIC_readICVersion();
  148. private:
  149. int32_t to_motor_acc(int32_t acc); // rpm/s^2
  150. int32_t to_motor_vel(int32_t vel); // rpm
  151. int32_t to_motor_pos(int32_t pos); //
  152. int32_t to_user_pos(int32_t pos); //
  153. int32_t to_user_vel(int32_t vel);
  154. };
  155. } // namespace iflytop
  156. #endif
  157. #endif