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.

125 lines
5.0 KiB

12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "stm32basic/zbasic.h"
  8. #include "stm32basic/zgpio.hpp"
  9. //
  10. #include "../tmc/tmc_type.h"
  11. #include "reg/TMC5130_Type.h"
  12. #include "stm32basic/mutex.hpp"
  13. namespace iflytop {
  14. /**
  15. * @brief
  16. *
  17. *
  18. * 1. 6
  19. * 2. AMAX有效
  20. *
  21. */
  22. class TMC51X0Cfg {
  23. public:
  24. SPI_HandleTypeDef *hspi = NULL;
  25. Pin_t csnpin;
  26. Pin_t ennpin;
  27. TMC51X0Cfg(SPI_HandleTypeDef *hspi, Pin_t csnpin, Pin_t ennpin) : hspi(hspi), csnpin(csnpin), ennpin(ennpin) {}
  28. TMC51X0Cfg() {}
  29. };
  30. class TMC51X0 {
  31. protected:
  32. TMC51X0Cfg m_cfg;
  33. SPI_HandleTypeDef *m_hspi = NULL;
  34. ZGPIO m_csnpin;
  35. ZGPIO m_ennpin;
  36. int32_t m_scale = 10000;
  37. int32_t m_scale_deceleration = 1;
  38. mres_type_t m_MRES = kmres_256;
  39. double m_onecirclepulse = 51200;
  40. int32_t m_enc_resolution = 0; // 编码器分辨率,默认只有在256细分的情况下有效
  41. zmutex m_mutex;
  42. bool m_driErr = false;
  43. public:
  44. void initialize(TMC51X0Cfg cfg);
  45. private:
  46. /*******************************************************************************
  47. * *
  48. *******************************************************************************/
  49. void readWriteArray(uint8_t *data, size_t length);
  50. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  51. void writeInt(uint8_t address, int32_t value);
  52. void writeField(uint8_t add, uint32_t mask, uint32_t shift, uint32_t value);
  53. int32_t readInt(uint8_t address);
  54. uint32_t readUInt(uint8_t address);
  55. public:
  56. bool ping();
  57. void enable(bool enable);
  58. void setdriErr(bool val) { m_driErr = val; }
  59. bool getdriErr() { return m_driErr; }
  60. /***********************************************************************************************************************
  61. * ctrl *
  62. ***********************************************************************************************************************/
  63. void rotate(int32_t velocity);
  64. void moveTo(int32_t position, uint32_t velocityMax);
  65. void moveToEnd(int32_t direction, uint32_t velocityMax);
  66. void moveBy(int32_t relativePosition, uint32_t velocityMax);
  67. void stop();
  68. /***********************************************************************************************************************
  69. * get state *
  70. ***********************************************************************************************************************/
  71. int32_t getXACTUAL(); // 当前位置
  72. int32_t getVACTUAL(); // 当前速度
  73. int32_t getEncVal(); // ENCVAL
  74. TMC5130RampStat getRampStat();
  75. TMC5130DevStatusReg_t getDevStatus(); // R 读后不清
  76. TMC5130GState_t getGState(); // R+C 读后自动清除
  77. int32_t readICVersion();
  78. bool isReachTarget(TMC5130RampStat *state); // 是否到达目标位置
  79. bool isTMC5130();
  80. /***********************************************************************************************************************
  81. * set state *
  82. ***********************************************************************************************************************/
  83. void setXACTUAL(int32_t value); // 设置当前位置
  84. void setMotorShaft(bool reverse);
  85. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  86. void setGlobalScale(uint8_t globalscale); // ONLY for 5160
  87. void setScale(int32_t scale);
  88. void setScaleDenominator(int32_t scale);
  89. void setVstart(int32_t val);
  90. void setA1(int32_t val);
  91. void setAmax(int32_t val);
  92. void setV1(int32_t val);
  93. void setDmax(int32_t val);
  94. void setD1(int32_t val);
  95. void setVstop(int32_t val);
  96. void setTzerowait(int32_t val);
  97. bool setEncResolution(int32_t enc_resolution);
  98. void setEncVal(int32_t enc_val);
  99. /***********************************************************************************************************************
  100. * reg operation *
  101. ***********************************************************************************************************************/
  102. void writeIntExt(uint8_t address, int32_t value);
  103. int32_t readIntExt(uint8_t address);
  104. private:
  105. int32_t to_motor_acc(int32_t acc); // rpm/s^2
  106. int32_t to_motor_vel(int32_t vel); // rpm
  107. int32_t to_motor_pos(int32_t pos); //
  108. int32_t to_user_pos(int32_t pos); //
  109. int32_t to_user_vel(int32_t vel);
  110. };
  111. } // namespace iflytop