Files
2026-07-08 10:30:07 +08:00

89 lines
1.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "esc.h"
/*
* 当前 CubeMX TIM1 配置:
* Prescaler = 239
* Period = 19999
*
* 如果 TIM1 时钟是 240MHz
* 240MHz / (239 + 1) = 1MHz
* 所以 1 个计数 = 1us
*
* 因此 CCR = 1000 就是 1000us
*/
void ESC_Init(void)
{
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
/* 上电默认最低油门,防止电机突然启动 */
ESC_SetThrottleUs(ESC_MIN_US);
}
void ESC_SetThrottleUs(uint16_t us)
{
if (us < ESC_MIN_US)
{
us = ESC_MIN_US;
}
if (us > ESC_MAX_US)
{
us = ESC_MAX_US;
}
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, us);
}
void ESC_Stop(void)
{
ESC_SetThrottleUs(ESC_MIN_US);
}
/*
* 电调解锁:
* 一般 ESC 上电后需要先收到一段时间最低油门信号
*/
void ESC_Arm(void)
{
ESC_SetThrottleUs(ESC_MIN_US);
HAL_Delay(3000);
}
/*
* 低速测试:
* 第一次不要直接上 1500 或 2000,先用 1100~1200
*/
void ESC_TestLowSpeed(void)
{
ESC_SetThrottleUs(ESC_MIN_US);
HAL_Delay(2000);
ESC_SetThrottleUs(1100);
HAL_Delay(3000);
ESC_SetThrottleUs(1150);
HAL_Delay(3000);
ESC_SetThrottleUs(1200);
HAL_Delay(3000);
ESC_Stop();
HAL_Delay(2000);
}
/*
* 电调油门校准:
* 不确定电调是否需要校准时,不要随便调用。
* 常见流程:
* 1. 先输出最大油门
* 2. 给 ESC 上电
* 3. 听到提示音后切到最低油门
*/
void ESC_Calibrate(void)
{
ESC_SetThrottleUs(ESC_MAX_US);
HAL_Delay(5000);
ESC_SetThrottleUs(ESC_MIN_US);
HAL_Delay(5000);
}