| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /**
- * @file MOTOR_DC_ATY.c
- *
- * @param Project DEVICE_GENERAL_ATY_LIB
- *
- * @author ATY
- *
- * @copyright
- * - Copyright 2017 - 2023 MZ-ATY
- * - This code follows:
- * - MZ-ATY Various Contents Joint Statement -
- * <a href="https://mengze.top/MZ-ATY_VCJS">
- * https://mengze.top/MZ-ATY_VCJS</a>
- * - CC 4.0 BY-NC-SA -
- * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
- * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
- * - Your use will be deemed to have accepted the terms of this statement.
- *
- * @brief Familiar functions of DCMotor control
- *
- * @version
- * - 1_01_230426 > ATY
- * -# Preliminary version, first Release
- ********************************************************************************
- */
- #ifndef __MOTOR_DC_ATY_C
- #define __MOTOR_DC_ATY_C
- #include "MOTOR_DC_ATY.h"
- /******************************* For user *************************************/
- /******************************************************************************/
- uint8_t motorEn = 0;
- uint8_t motorDir = MOTOR_DIR_OUT;
- uint32_t motorSoftSpeed = 0;
- uint32_t motorSpeed = 0;
- uint32_t motorSoftTime = 0;
- uint32_t motorTime = 0;
- uint32_t motorSpeedCounter = 0;
- uint32_t motorStopCounter = 0;
- 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
- /**
- * @brief start motor with direction set
- * @param dir direction to move
- * @param speed direction to move
- */
- void MotorStart(uint8_t dir, uint32_t speed)
- {
- // MOTOR_EN_SET_ENABLE;
- if(dir == MOTOR_DIR_IN)
- MOTOR_IN;
- else if(dir == MOTOR_DIR_OUT)
- MOTOR_OUT;
- __UNUSED(speed);
- // PwmFreqSet(speed, Motor_Channel);
- #ifdef __DEBUG_MOTOR_DC_ATY
- printf("\r\nMotor Start: %d - %d", dir, speed);
- #endif /* __DEBUG_MOTOR_DC_ATY */
- }
- /**
- * @brief start motor with only speed
- * @param speed direction to move
- */
- void MotorStartSpeed(uint32_t speed)
- {
- __UNUSED(speed);
- // MOTOR_EN_SET_ENABLE;
- // PwmFreqSet(speed, Motor_Channel);
- #ifdef __DEBUG_MOTOR_DC_ATY
- printf("\r\nMotor Start: %d", speed);
- #endif /* __DEBUG_MOTOR_DC_ATY */
- }
- /**
- * @brief stop motor
- */
- void MotorStop(void)
- {
- // MOTOR_PARAM_SET(MOTOR_DIR_OUT, 0, 0, 0, 0, motorSetState);
- if(motorEn != 0){
- // PWM_Stop(0, Motor_Channel);
- // MOTOR_EN_SET_DISABLE;
- MOTOR_STOP;
- motorSpeedCounter = 0;
- motorStopCounter = 0;
- #ifdef __DEBUG_MOTOR_DC_ATY
- printf("\r\nMotor Stop");
- #endif /* __DEBUG_MOTOR_DC_ATY */
- }
- MOTOR_STANDBY;
- }
- /**
- * @brief deal step motor state
- * @note put at 1ms cycle;
- * set motorSetState = 4 to change motor dir with speed no change
- * set motorSetState = 3 to start motor directly, motorSoftTime need to be 0
- * set motorSetState = 2 to strat motor with soft, motorDir/motorSoftSpeed/motorSpeed/motorSoftTime/motorTime need to be set
- * set motorSetState = 0 to stop motor
- * set motorSetState = 10 to scram motor
- * when motorSetState = 5 means last cycle finished, set others to start a new cycle
- * when motorSetState = 1 means motor running and state not changing
- */
- void MotorStateMachine_DC(void)
- {
- if(motorSetState == 4){
- motorSpeedCounter = 0;
- motorStopCounter = 0;
- motorSetState = 1;
- motorDir = !motorDir;
- }
- else if(motorSetState == 3){
- motorSpeedCounter = motorSoftTime;
- motorSetState = 1;
- MotorStart(motorDir, motorSpeed);
- }
- else if(motorSetState == 2){
- motorSpeedCounter = 0;
- motorSetState = 1;
- MotorStart(motorDir, motorSoftSpeed);
- }
- else if(motorSetState == 1){
- if(motorSpeedCounter == motorSoftTime)
- motorSetState = 3;
- else if(motorSpeedCounter == (motorSoftTime + motorTime))
- motorSetState = 5;
- }
- else if(motorSetState == 10){
- motorSetState = 0;
- MotorStop();
- }
- else{ MotorStop(); } // 5 0
- if(motorEn == 0){
- motorSpeedCounter = 0;
- motorStopCounter++;
- }
- else{
- motorSpeedCounter++;
- motorStopCounter = 0;
- }
- }
- #endif /* __MOTOR_DC_ATY_C */
- /******************************** End Of File *********************************/
|