/** * @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 - * * https://mengze.top/MZ-ATY_VCJS * - CC 4.0 BY-NC-SA - * * https://creativecommons.org/licenses/by-nc-sa/4.0/ * - 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 motorStartCounter = 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; motorStartCounter = 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){ motorStartCounter = 0; motorStopCounter = 0; motorSetState = 1; motorDir = !motorDir; } else if(motorSetState == 3){ motorStartCounter = motorSoftTime; motorSetState = 1; MotorStart(motorDir, motorSpeed); } else if(motorSetState == 2){ motorStartCounter = 0; motorSetState = 1; MotorStart(motorDir, motorSoftSpeed); } else if(motorSetState == 1){ if(motorStartCounter == motorSoftTime) motorSetState = 3; else if(motorStartCounter == (motorSoftTime + motorTime)) motorSetState = 5; } else if(motorSetState == 10){ motorSetState = 0; MotorStop(); } else{ MotorStop(); } // 5 0 if(motorEn == 0){ motorStartCounter = 0; motorStopCounter++; } else{ motorStartCounter++; motorStopCounter = 0; } } #endif /* __MOTOR_DC_ATY_C */ /******************************** End Of File *********************************/