原始的加减速算法存在以下问题:
1.0f - ((float)route->accel_step_count / (float)(route->accel_step_count + 1))
无法产生正确的线性进度直线加速/减速(PLSR_ACCEL_LINEAR):
// 加速
freq_increment = (target_freq - start_freq) / total_steps
new_freq = start_freq + (freq_increment * completed_steps)
// 减速
freq_decrement = (start_freq - target_freq) / total_steps
new_freq = start_freq - (freq_decrement * completed_steps)
曲线加速/减速(PLSR_ACCEL_CURVE)和正弦加速/减速(PLSR_ACCEL_SINE):
progress = completed_steps / total_steps
freq_ratio = algorithm_function(progress) // 0.0 到 1.0
new_freq = start_freq + (freq_range * freq_ratio)
引入静态变量记录关键参数:
total_accel_steps
/ total_decel_steps
:记录总步数start_freq_accel
/ start_freq_decel
:记录起始频率这样确保:
修正前:
float progress = 1.0f - ((float)route->accel_step_count / (float)(route->accel_step_count + 1));
修正后:
float progress = (float)(total_steps - remaining_steps) / (float)total_steps;
算法类型 | 频率变化特点 | 适用场景 | 计算方式 |
---|---|---|---|
直线加速 | 每步变化量固定 | 需要均匀加速的场合 | 固定增量 |
曲线加速 | 先慢后快的变化 | 需要平滑启动的场合 | 进度函数 |
正弦加速 | 正弦曲线变化 | 需要最平滑过渡的场合 | 进度函数 |
// 记录总步数和起始频率(仅在第一次进入时)
if (total_accel_steps == 0) {
total_accel_steps = route->accel_step_count;
start_freq_accel = route->current_freq;
}
switch (current_section->accel_config.accel_algorithm) {
case PLSR_ACCEL_LINEAR:
// 直线算法:固定增量
break;
case PLSR_ACCEL_CURVE:
case PLSR_ACCEL_SINE:
// 非线性算法:进度计算
break;
}
// 加速/减速完成时重置静态变量
total_accel_steps = 0;
start_freq_accel = 0;