MOTOR_DC_ATY.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file MOTOR_DC_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 DCMotor control
  20. *
  21. * @version
  22. * - 1_01_230426 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __MOTOR_DC_ATY_C
  27. #define __MOTOR_DC_ATY_C
  28. #include "MOTOR_DC_ATY.h"
  29. /******************************* For user *************************************/
  30. /******************************************************************************/
  31. uint8_t motorEn = 0;
  32. uint8_t motorDir = MOTOR_DIR_OUT;
  33. uint32_t motorSoftSpeed = 0;
  34. uint32_t motorSpeed = 0;
  35. uint32_t motorSoftTime = 0;
  36. uint32_t motorTime = 0;
  37. uint32_t motorStartCounter = 0;
  38. uint32_t motorStopCounter = 0;
  39. 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
  40. /**
  41. * @brief start motor with direction set
  42. * @param dir direction to move
  43. * @param speed direction to move
  44. */
  45. void MotorStart(uint8_t dir, uint32_t speed)
  46. {
  47. // MOTOR_EN_SET_ENABLE;
  48. if(dir == MOTOR_DIR_IN)
  49. MOTOR_IN;
  50. else if(dir == MOTOR_DIR_OUT)
  51. MOTOR_OUT;
  52. __UNUSED(speed);
  53. // PwmFreqSet(speed, Motor_Channel);
  54. #ifdef __DEBUG_MOTOR_DC_ATY
  55. printf("\r\nMotor Start: %d - %d", dir, speed);
  56. #endif /* __DEBUG_MOTOR_DC_ATY */
  57. }
  58. /**
  59. * @brief start motor with only speed
  60. * @param speed direction to move
  61. */
  62. void MotorStartSpeed(uint32_t speed)
  63. {
  64. __UNUSED(speed);
  65. // MOTOR_EN_SET_ENABLE;
  66. // PwmFreqSet(speed, Motor_Channel);
  67. #ifdef __DEBUG_MOTOR_DC_ATY
  68. printf("\r\nMotor Start: %d", speed);
  69. #endif /* __DEBUG_MOTOR_DC_ATY */
  70. }
  71. /**
  72. * @brief stop motor
  73. */
  74. void MotorStop(void)
  75. {
  76. // MOTOR_PARAM_SET(MOTOR_DIR_OUT, 0, 0, 0, 0, motorSetState);
  77. if(motorEn != 0){
  78. // PWM_Stop(0, Motor_Channel);
  79. // MOTOR_EN_SET_DISABLE;
  80. MOTOR_STOP;
  81. motorStartCounter = 0;
  82. motorStopCounter = 0;
  83. #ifdef __DEBUG_MOTOR_DC_ATY
  84. printf("\r\nMotor Stop");
  85. #endif /* __DEBUG_MOTOR_DC_ATY */
  86. }
  87. MOTOR_STANDBY;
  88. }
  89. /**
  90. * @brief deal step motor state
  91. * @note put at 1ms cycle;
  92. * set motorSetState = 4 to change motor dir with speed no change
  93. * set motorSetState = 3 to start motor directly, motorSoftTime need to be 0
  94. * set motorSetState = 2 to strat motor with soft, motorDir/motorSoftSpeed/motorSpeed/motorSoftTime/motorTime need to be set
  95. * set motorSetState = 0 to stop motor
  96. * set motorSetState = 10 to scram motor
  97. * when motorSetState = 5 means last cycle finished, set others to start a new cycle
  98. * when motorSetState = 1 means motor running and state not changing
  99. */
  100. void MotorStateMachine_DC(void)
  101. {
  102. if(motorSetState == 4){
  103. motorStartCounter = 0;
  104. motorStopCounter = 0;
  105. motorSetState = 1;
  106. motorDir = !motorDir;
  107. }
  108. else if(motorSetState == 3){
  109. motorStartCounter = motorSoftTime;
  110. motorSetState = 1;
  111. MotorStart(motorDir, motorSpeed);
  112. }
  113. else if(motorSetState == 2){
  114. motorStartCounter = 0;
  115. motorSetState = 1;
  116. MotorStart(motorDir, motorSoftSpeed);
  117. }
  118. else if(motorSetState == 1){
  119. if(motorStartCounter == motorSoftTime)
  120. motorSetState = 3;
  121. else if(motorStartCounter == (motorSoftTime + motorTime))
  122. motorSetState = 5;
  123. }
  124. else if(motorSetState == 10){
  125. motorSetState = 0;
  126. MotorStop();
  127. }
  128. else{ MotorStop(); } // 5 0
  129. if(motorEn == 0){
  130. motorStartCounter = 0;
  131. motorStopCounter++;
  132. }
  133. else{
  134. motorStartCounter++;
  135. motorStopCounter = 0;
  136. }
  137. }
  138. #endif /* __MOTOR_DC_ATY_C */
  139. /******************************** End Of File *********************************/