MOTOR_STEP_ATY.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @file MOTOR_STEP_ATY.c
  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_C
  31. #define __MOTOR_STEP_ATY_C
  32. #include "MOTOR_STEP_ATY.h"
  33. /******************************* For user *************************************/
  34. /******************************************************************************/
  35. uint8_t motorEn = 0;
  36. uint8_t motorDir = MOTOR_DIR_OUT;
  37. uint32_t motorSoftSpeed = 0;
  38. uint32_t motorSpeed = 0;
  39. uint32_t motorSoftTime = 0;
  40. uint32_t motorTime = 0;
  41. uint32_t motorStartCounter = 0;
  42. uint32_t motorStopCounter = 0; // for stall detection or driver error(TMC2209)
  43. uint8_t motorSetState = 0; // 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
  44. /**
  45. * @brief start motor with direction set
  46. * @param dir direction to move
  47. * @param speed direction to move
  48. */
  49. void MotorStart(uint8_t dir, uint32_t speed)
  50. {
  51. if(dir == MOTOR_DIR_IN)
  52. MOTOR_DIR_SET_IN;
  53. else if(dir == MOTOR_DIR_OUT)
  54. MOTOR_DIR_SET_OUT;
  55. MOTOR_EN_SET_ENABLE;
  56. PwmFreqSet(speed, Motor_Channel);
  57. #ifdef __DEBUG_MOTOR_STEP_ATY
  58. printf("\r\nMotor Start: %d - %d", dir, speed);
  59. #endif /* __DEBUG_MOTOR_STEP_ATY */
  60. }
  61. /**
  62. * @brief start motor with only speed
  63. * @param speed direction to move
  64. */
  65. void MotorStartSpeed(uint32_t speed)
  66. {
  67. MOTOR_EN_SET_ENABLE;
  68. PwmFreqSet(speed, Motor_Channel);
  69. #ifdef __DEBUG_MOTOR_STEP_ATY
  70. printf("\r\nMotor Start: %d", speed);
  71. #endif /* __DEBUG_MOTOR_STEP_ATY */
  72. }
  73. /**
  74. * @brief stop motor
  75. */
  76. void MotorStop(void)
  77. {
  78. // MOTOR_PARAM_SET(MOTOR_DIR_OUT, 0, 0, 0, 0, motorSetState);
  79. if(motorEn == 0)
  80. return;
  81. else{
  82. PWM_Stop(0, Motor_Channel);
  83. MOTOR_EN_SET_DISABLE;
  84. motorStartCounter = 0;
  85. motorStopCounter = 0;
  86. #ifdef __DEBUG_MOTOR_STEP_ATY
  87. printf("\r\nMotor Stop");
  88. #endif /* __DEBUG_MOTOR_STEP_ATY */
  89. }
  90. }
  91. /**
  92. * @brief process motor error
  93. * @note put at 1ms cycle
  94. */
  95. void MotorSelfCycle(void)
  96. {
  97. if(MOTOR_DIAG_GET_H)
  98. {
  99. if(motorEn == 1)
  100. MOTOR_EN_SET_DISABLE;
  101. if(motorStopCounter >= 100)
  102. MOTOR_EN_SET_ENABLE;
  103. }
  104. }
  105. /**
  106. * @brief deal step motor state
  107. * @note put at 1ms cycle;
  108. * set motorSetState = 4 to change motor dir with speed no change
  109. * set motorSetState = 3 to start motor directly, motorSoftTime need to be 0
  110. * set motorSetState = 2 to strat motor with soft, motorDir/motorSoftSpeed/motorSpeed/motorSoftTime/motorTime need to be set
  111. * set motorSetState = 0 to stop motor
  112. * set motorSetState = 10 to scram motor
  113. * when motorSetState = 5 means last cycle finished, set others to start a new cycle
  114. * when motorSetState = 1 means motor running and state not changing
  115. */
  116. void MotorStateMachine_Step(void)
  117. {
  118. if(motorSetState == 4){
  119. motorStartCounter = 0;
  120. motorStopCounter = 0;
  121. motorSetState = 2;
  122. motorDir = !motorDir;
  123. }
  124. else if(motorSetState == 3){
  125. motorStartCounter = motorSoftTime;
  126. motorSetState = 1;
  127. MotorStart(motorDir, motorSpeed);
  128. }
  129. else if(motorSetState == 2){
  130. motorStartCounter = 0;
  131. motorSetState = 1;
  132. MotorStart(motorDir, motorSoftSpeed);
  133. }
  134. else if(motorSetState == 1){
  135. if(motorStartCounter == motorSoftTime)
  136. motorSetState = 3;
  137. else if(motorStartCounter == (motorSoftTime + motorTime))
  138. motorSetState = 5;
  139. }
  140. else if(motorSetState == 10){
  141. motorSetState = 0;
  142. MotorStop();
  143. }
  144. else{ MotorStop(); } // 5 0
  145. if(motorEn == 0){
  146. motorStartCounter = 0;
  147. motorStopCounter++;
  148. }
  149. else{
  150. motorStartCounter++;
  151. motorStopCounter = 0;
  152. }
  153. }
  154. #endif /* __MOTOR_STEP_ATY_C */
  155. /******************************** End Of File *********************************/