MOTOR_STEP_ATY.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @file MOTOR_STEP_ATY.h
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of SteppingMotor control
  20. *
  21. * @version
  22. * - 1_01_221231 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_230408 > ATY
  25. * -# Add State Machine and so
  26. * - 1_03_230426 > ATY
  27. * -# Change name "SteppingMotor_ATY" to "MOTOR_STEP_ATY"
  28. ********************************************************************************
  29. */
  30. #ifndef __MOTOR_STEP_ATY_H
  31. #define __MOTOR_STEP_ATY_H
  32. #include "INCLUDE_ATY.h"
  33. #include "HW_GPIO_ATY.h"
  34. #include "HW_PWM_ATY.h"
  35. /******************************* For user *************************************/
  36. #include "main.h"
  37. // #define __DEBUG_MOTOR_STEP_ATY
  38. /* set program for motor soft start */
  39. // #define S_PARAM_C 0.5f // 0-1
  40. // #define Motor_StartLine_2
  41. // #define Motor_StartLine_T
  42. // #define Motor_StartLine_S
  43. /* 3 max, if need more, add codes at every function */
  44. // #define MOTOR_COUNT 2
  45. /* pin defination must include all motors,
  46. diag pin is selectable, do not define if not use */
  47. // #define MOTOR_EN_PORT_0 TMC1_EN_GPIO_Port
  48. // #define MOTOR_EN_PIN_0 TMC1_EN_Pin
  49. // #define MOTOR_DIR_PORT_0 TMC1_DIR_GPIO_Port
  50. // #define MOTOR_DIR_PIN_0 TMC1_DIR_Pin
  51. // #define MOTOR_STEP_PORT_0 TMC1_STEP_GPIO_Port
  52. // #define MOTOR_STEP_PIN_0 TMC1_STEP_Pin
  53. // // #define MOTOR_DIAG_PORT_0 0
  54. // // #define MOTOR_DIAG_PIN_0 0
  55. // #define MOTOR_PWM_Channel_0 PWM_T3C2
  56. // #define MOTOR_EN_PORT_1 TMC1_EN_GPIO_Port
  57. // #define MOTOR_EN_PIN_1 TMC1_EN_Pin
  58. // #define MOTOR_DIR_PORT_1 TMC1_DIR_GPIO_Port
  59. // #define MOTOR_DIR_PIN_1 TMC1_DIR_Pin
  60. // #define MOTOR_STEP_PORT_1 TMC1_STEP_GPIO_Port
  61. // #define MOTOR_STEP_PIN_1 TMC1_STEP_Pin
  62. // // #define MOTOR_DIAG_PORT_1 0
  63. // // #define MOTOR_DIAG_PIN_1 0
  64. // #define MOTOR_PWM_Channel_1 PWM_T1C1
  65. /* set step pin vol level when motor is not enable */
  66. // #define STATIC_STEP_LEVEL 0
  67. /******************************************************************************/
  68. typedef struct{
  69. uint8_t motorEn;
  70. uint8_t motorDir;
  71. uint8_t motorStepLevel;
  72. uint8_t motorDiag;
  73. uint32_t motorSoftSpeed;
  74. uint32_t motorSpeed;
  75. uint32_t motorSpeedCounter;
  76. uint32_t motorSoftSteps;
  77. uint32_t motorSteps;
  78. uint32_t motorStepCounter;
  79. uint32_t motorStopCounter; // delay for stall detection or driver error(TMC2209)
  80. uint8_t motorSetState; // 0: stop, 1: start hold, 2: start soft, 3: run speed, 4: change dir, 5: stop state, soft set 0 after use, 10: scram motor
  81. float speedIncreaseStep;
  82. float S_paramA;
  83. #ifdef S_PARAM_C
  84. float S_paramC;
  85. #else
  86. float S_paramC;
  87. #endif
  88. }motorStep_t;
  89. extern motorStep_t motorStep_t_Group[MOTOR_COUNT];
  90. #define MOTOR_PARAM_SET(dir, softSpeed, speed, softSteps, steps, setState, channel) \
  91. do \
  92. { \
  93. motorStep_t_Group[channel].motorDir = (uint8_t)dir; \
  94. motorStep_t_Group[channel].motorSoftSpeed = (uint32_t)softSpeed; \
  95. motorStep_t_Group[channel].motorSpeed = (uint32_t)speed; \
  96. motorStep_t_Group[channel].motorSoftSteps = (uint32_t)softSteps; \
  97. motorStep_t_Group[channel].motorSteps = (uint32_t)steps; \
  98. motorStep_t_Group[channel].motorSetState = (uint8_t)setState; \
  99. } while(0)
  100. #define MOTOR_PARAM_GET(dir, softSpeed, speed, softSteps, steps, setState, channel) \
  101. do \
  102. { \
  103. dir = motorStep_t_Group[(uint8_t)channel].motorDir; \
  104. softSpeed = motorStep_t_Group[(uint8_t)channel].motorSoftSpeed; \
  105. speed = motorStep_t_Group[(uint8_t)channel].motorSpeed; \
  106. softSteps = motorStep_t_Group[(uint8_t)channel].motorSoftSteps; \
  107. steps = motorStep_t_Group[(uint8_t)channel].motorSteps; \
  108. setState = motorStep_t_Group[(uint8_t)channel].motorSetState; \
  109. } while(0)
  110. void MotorSetAble(uint8_t enable, uint8_t channel);
  111. void MotorSetDirection(uint8_t dir, uint8_t channel);
  112. void MotorSetStepLevel(uint8_t level, uint8_t channel);
  113. uint8_t MotorGetDiagLevel(uint8_t channel);
  114. void MotorStart(uint8_t dir, uint32_t speed, uint8_t channel);
  115. void MotorStartSpeed(uint32_t speed, uint8_t channel);
  116. void MotorStop(uint8_t channel);
  117. void MotorStateMachine_Step(void);
  118. void MotorStateMachine_Step_PWM(void);
  119. #define MOTOR_DIR_IN 1
  120. #define MOTOR_DIR_OUT 0
  121. #define MOTOR_SET_REVERSE 4
  122. #define MOTOR_SET_START_DIRECTLY 3
  123. #define MOTOR_SET_START_SOFT 2
  124. #define MOTOR_SET_STOP 0
  125. #define MOTOR_SET_SCRAM 10
  126. #define MOTOR_SET_FINISHED 5
  127. #define MOTOR_SET_RUNNING 1
  128. #define PWM_T01C1 0x11
  129. #define PWM_T01C2 0x12
  130. #define PWM_T01C3 0x13
  131. #define PWM_T01C4 0x14
  132. #define PWM_T02C1 0x21
  133. #define PWM_T02C2 0x22
  134. #define PWM_T02C3 0x23
  135. #define PWM_T02C4 0x24
  136. #define PWM_T03C1 0x31
  137. #define PWM_T03C2 0x32
  138. #define PWM_T03C3 0x33
  139. #define PWM_T03C4 0x34
  140. #define PWM_T04C1 0x41
  141. #define PWM_T04C2 0x42
  142. #define PWM_T04C3 0x43
  143. #define PWM_T04C4 0x44
  144. #define PWM_T05C1 0x51
  145. #define PWM_T05C2 0x52
  146. #define PWM_T05C3 0x53
  147. #define PWM_T05C4 0x54
  148. #define PWM_T06C1 0x61
  149. #define PWM_T06C2 0x62
  150. #define PWM_T06C3 0x63
  151. #define PWM_T06C4 0x64
  152. #define PWM_T07C1 0x71
  153. #define PWM_T07C2 0x72
  154. #define PWM_T07C3 0x73
  155. #define PWM_T07C4 0x74
  156. #define PWM_T08C1 0x81
  157. #define PWM_T08C2 0x82
  158. #define PWM_T08C3 0x83
  159. #define PWM_T08C4 0x84
  160. #define PWM_T09C1 0x91
  161. #define PWM_T09C2 0x92
  162. #define PWM_T09C3 0x93
  163. #define PWM_T09C4 0x94
  164. #define PWM_T10C1 0xA1
  165. #define PWM_T10C2 0xA2
  166. #define PWM_T10C3 0xA3
  167. #define PWM_T10C4 0xA4
  168. #define PWM_T11C1 0xB1
  169. #define PWM_T11C2 0xB2
  170. #define PWM_T11C3 0xB3
  171. #define PWM_T11C4 0xB4
  172. #define PWM_T12C1 0xC1
  173. #define PWM_T12C2 0xC2
  174. #define PWM_T12C3 0xC3
  175. #define PWM_T12C4 0xC4
  176. #define PWM_T13C1 0xD1
  177. #define PWM_T13C2 0xD2
  178. #define PWM_T13C3 0xD3
  179. #define PWM_T13C4 0xD4
  180. #define PWM_T14C1 0xE1
  181. #define PWM_T14C2 0xE2
  182. #define PWM_T14C3 0xE3
  183. #define PWM_T14C4 0xE4
  184. #define PWM_T15C1 0xF1
  185. #define PWM_T15C2 0xF2
  186. #define PWM_T15C3 0xF3
  187. #define PWM_T15C4 0xF4
  188. #endif /* __MOTOR_STEP_ATY_H */
  189. /******************************** End Of File *********************************/