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.

123 lines
5.0 KiB

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