基质喷涂
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.

126 lines
4.8 KiB

3 weeks 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_reg_cache.hpp"
  9. #include "../tmc/tmc_type.h"
  10. #include "reg/TMC5130_Type.h"
  11. #include "base/stm32basic.hpp"
  12. namespace iflytop {
  13. /**
  14. * @brief
  15. *
  16. */
  17. class TMC51X0Cfg {
  18. public:
  19. SPI_HandleTypeDef *hspi = NULL;
  20. Pin_t csnpin;
  21. Pin_t ennpin;
  22. TMC51X0Cfg(SPI_HandleTypeDef *hspi, Pin_t csnpin, Pin_t ennpin) : hspi(hspi), csnpin(csnpin), ennpin(ennpin) {}
  23. TMC51X0Cfg() {}
  24. };
  25. class TMC51X0 {
  26. protected:
  27. TMC51X0Cfg m_cfg;
  28. TMCRegCache m_cache;
  29. SPI_HandleTypeDef *m_hspi = NULL;
  30. ZGPIO m_csnpin;
  31. ZGPIO m_ennpin;
  32. int m_mid = 0;
  33. int32_t m_scale = 10000;
  34. int32_t m_scale_deceleration = 1;
  35. mres_type_t m_MRES = kmres_256;
  36. double m_onecirclepulse = 51200;
  37. int32_t m_enc_resolution = 0; //
  38. zmutex m_mutex = {"TMC51X0"};
  39. bool m_driErr = false;
  40. bool m_isMoveToEnd = false;
  41. public:
  42. void initialize(int mid, TMC51X0Cfg cfg);
  43. private:
  44. void readWriteArray(uint8_t *data, size_t length);
  45. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  46. void writeInt(uint8_t address, int32_t value);
  47. void writeField(uint8_t add, uint32_t mask, uint32_t shift, uint32_t value);
  48. int32_t readInt(uint8_t address);
  49. uint32_t readUInt(uint8_t address);
  50. public:
  51. bool ping();
  52. void enable(bool enable);
  53. void refreshcfg();
  54. void setdriErr(bool val) { m_driErr = val; }
  55. bool getdriErr() { return m_driErr; }
  56. /***********************************************************************************************************************
  57. * ctrl *
  58. ***********************************************************************************************************************/
  59. void setVMax(int32_t velocity);
  60. void rotate(int32_t velocity);
  61. void moveTo(int32_t position, uint32_t velocityMax);
  62. void moveToEnd(int32_t direction, uint32_t velocityMax);
  63. void refreshMoveToEnd();
  64. bool isMoveToEnd();
  65. void moveBy(int32_t relativePosition, uint32_t velocityMax);
  66. void stop();
  67. /***********************************************************************************************************************
  68. * get state *
  69. ***********************************************************************************************************************/
  70. int32_t getXACTUAL(); // now pos
  71. int32_t getVACTUAL(); // now vel
  72. int32_t getEncVal(); // ENCVAL
  73. TMC5130RampStat getRampStat();
  74. TMC5130DevStatusReg_t getDevStatus(); // R
  75. TMC5130GState_t getGState(); // R+C read and clear
  76. int32_t readICVersion();
  77. int32_t getXactualRAW(); // now pos 51200
  78. bool isReachTarget(TMC5130RampStat *state); // is reach target
  79. bool isTMC5130();
  80. /***********************************************************************************************************************
  81. * set state *
  82. ***********************************************************************************************************************/
  83. void setXACTUAL(int32_t value); // set pos
  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