diff --git a/Core/Inc/motor.h b/Core/Inc/motor.h index d20f3d3..35d7a45 100644 --- a/Core/Inc/motor.h +++ b/Core/Inc/motor.h @@ -5,8 +5,6 @@ #include "main.h" -// duty in [-100, 100] - void MOTOR_Start(void); void MOTOR_Slip(void); void MOTOR_Stop(void); diff --git a/Core/Src/motor.c b/Core/Src/motor.c index f1b8041..33d20f5 100644 --- a/Core/Src/motor.c +++ b/Core/Src/motor.c @@ -17,6 +17,8 @@ void MOTOR_Start(void) HAL_TIM_PWM_Start(MOTOR_TIM, MOTOR_R_CHAN); } +/// @brief 自由滑行 +/// @param void MOTOR_Slip(void) { HAL_GPIO_WritePin(L_FORWARD_GPIO_Port, L_FORWARD_Pin | L_BACKWARD_Pin | R_FORWARD_Pin | R_BACKWARD_Pin, GPIO_PIN_RESET); @@ -24,6 +26,8 @@ void MOTOR_Slip(void) __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_R_CHAN, MAX_SPEED); } +/// @brief 刹车 +/// @param void MOTOR_Stop(void) { HAL_GPIO_WritePin(L_FORWARD_GPIO_Port, L_FORWARD_Pin | L_BACKWARD_Pin | R_FORWARD_Pin | R_BACKWARD_Pin, GPIO_PIN_RESET); @@ -31,6 +35,10 @@ void MOTOR_Stop(void) __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_R_CHAN, 0); } +/// @brief 控制电机占空比 + +/// @param leftDuty 绝对值 20-100, 支持正负 +/// @param rightDuty 绝对值 20-100,支持正负 void MOTOR_SetDuty(int8_t leftDuty, int8_t rightDuty) { leftDuty = preProcess(leftDuty, MOTOR_L); @@ -47,13 +55,12 @@ void MOTOR_SetDuty(int8_t leftDuty, int8_t rightDuty) int8_t preProcess(int8_t duty, uint8_t motor) { - if (duty > 100) - duty = 100; - else if (duty < -100) - duty = -100; - if (duty >= 0) { + if (duty > 100) + duty = 100; + else if (duty < 20) + duty = 20; if (motor & MOTOR_L) { HAL_GPIO_WritePin(L_FORWARD_GPIO_Port, L_FORWARD_Pin, GPIO_PIN_SET); @@ -67,6 +74,10 @@ int8_t preProcess(int8_t duty, uint8_t motor) } else { + if (duty < -100) + duty = -100; + else if (duty > -20) + duty = -20; if (motor & MOTOR_L) { HAL_GPIO_WritePin(L_FORWARD_GPIO_Port, L_FORWARD_Pin, GPIO_PIN_RESET);