AM32 电调学习–setInput()函数解析
#define INTERVAL_TIMER TIM2 ; 过零时间测量
TIM_InitStruct.Prescaler = 31; 0.5us
#define TEN_KHZ_TIMER TIM6 ;tenKhzRoutine frq=1M load=1M/20k
*** TIM_InitStruct.Prescaler = 63 1us ***
#define UTILITY_TIMER TIM17
TIM_InitStruct.Prescaler = 63; 1us
#define COM_TIMER TIM14 //PeriodElapsedCallback 过零后等待时间中断
TIM_InitStruct.Prescaler = 31; 0.5us
TIM3: ***TIM_InitStruct.Prescaler = 0 ***
一、常用变量及其范围
- bi_direction:3d开关设置参数,非互补pwm时为0
- dshot: 输入方式设置
- RC_CAR_REVERSE: 设置标志
- uint16_t newinput = 0; 读取到的输入值
- forward:目前转动方向
- uint16_t adjusted_input = 0; 经处理 newinput 后的输入值
bi_direction==1:
非dshot输入时:
if (newinput > (1000 + (servo_dead_band << 1)))
adjusted_input = map(newinput, 1000 + (servo_dead_band << 1), 2000, 47, 2047);
if (newinput < (1000 - (servo_dead_band << 1)))
adjusted_input = map(newinput, 0, 1000 - (servo_dead_band << 1), 2047, 47);
dshot输入时:
if (newinput > 1047)
adjusted_input = ((newinput - 1048) * 2 + 47) - reversing_dead_band;
if (newinput <= 1047 && newinput > 47)
adjusted_input = ((newinput - 48) * 2 + 47) - reversing_dead_band;
bi_direction==0:
adjusted_input = newinput;
- char prop_brake_active = 0; 刹车标志,有引标志后不再换相
- char return_to_center = 0; 刹车结束标志
- char old_routine = 1; 盲启动标志
- bemf_timeout_happened 堵转记数
if (INTERVAL_TIMER_COUNT > 45000 && running == 1) bemf_timeout_happened++; - char bemf_timeout = 10; 堵转最大尝试次数,若超过,则停机
- use_sin_start 设置标志
- uint16_t input = 0; 经处理 adjusted_input 后的输入值
use_sin_start:
if (adjusted_input < 30) { // dead band ?
input = 0;
}
if (adjusted_input > 30 && adjusted_input < (sine_mode_changeover_thottle_level * 20)) {
input = map(adjusted_input, 30,
(sine_mode_changeover_thottle_level * 20), 47, 160);
}
if (adjusted_input >= (sine_mode_changeover_thottle_level * 20)) {
input = map(adjusted_input, (sine_mode_changeover_thottle_level * 20),
2047, 160, 2047);
}
!use_sin_start:
use_speed_control_loop:
input = (uint16_t)input_override; // speed control pid override
if (input_override > 2047) {
input = 2047;
}
if (input_override < 48) {
input = 48;
}
else:
input = adjusted_input;
-
float input_override = 0;
input_override += doPidCalculations(&speedPid, e_com_time, target_e_com_time) / 10000; -
char armed = 0; 正常识别电机标志。
if (armed_timeout_count > LOOP_FREQUENCY_HZ) { // one second
if (zero_input_count > 30) {
armed = 1;
- uint16_t last_duty_cycle = 0; 上一次输出占空比
- uint8_t max_duty_cycle_change = 2; 占空比最大允许改变
if (last_duty_cycle < 150) {
max_duty_cycle_change = RAMP_SPEED_STARTUP;//2
} else {
if (average_interval > 500) {
max_duty_cycle_change = RAMP_SPEED_LOW_RPM; //10
} else {
max_duty_cycle_change = RAMP_SPEED_HIGH_RPM;//40
}
}
- uint16_t duty_cycle_setpoint = 0; 根据input计算出的占空比
if (use_sin_start) {
duty_cycle_setpoint = map(input, 137, 2047, minimum_duty_cycle, TIMER1_MAX_ARR);
} else {
duty_cycle_setpoint = map(input, 47, 2047, minimum_duty_cycle, TIMER1_MAX_ARR);
}
- int16_t phase_A_position;
- int16_t phase_B_position;
- int16_t phase_C_position;
phase_A_position = ((step - 1) * 60) + enter_sine_angle;
if (phase_A_position > 359) {
phase_A_position -= 360;
}
phase_B_position = phase_A_position + 119;
if (phase_B_position > 359) {
phase_B_position -= 360;
}
phase_C_position = phase_A_position + 239;
if (phase_C_position > 359) {
phase_C_position -= 360;
}
- uint16_t min_startup_duty = 120;
- uint16_t startup_max_duty_cycle = 300 + DEAD_TIME; 启动时最大值
二、函数流程分析
- newinput转换到adjusted_input
- 如果堵转尝试次数到,则停机,否则
- adjusted_input转换到input
- if (input >= 47 + (80 * use_sin_start) && armed) 则计算占空比duty_cycle_setpoint
- if (input < 47 + (80 * use_sin_start)) ,则命令处理
- 若盲启阶段,if (input >= 47 && (zero_crosses < (20 >> stall_protection)))
if (input >= 47 && (zero_crosses < (20 >> stall_protection))) {
if (duty_cycle_setpoint < min_startup_duty) {
duty_cycle_setpoint = min_startup_duty;
}
if (duty_cycle_setpoint > startup_max_duty_cycle) {
duty_cycle_setpoint = startup_max_duty_cycle;
}
}
- 履带车低速加油门处理
if (stall_protection_adjust > 0 && input > 47) {
duty_cycle_setpoint = duty_cycle_setpoint + (uint16_t)stall_protection_adjust;
}