梯形加减速算法
梯形加减速算法
介绍
输入距离,算法计算,输出加速段、匀速段、减速段时间。考虑实际距离与设置参数(运行速度、加/减速度)之间的关系,存在以下三种情况:
- 可达最高运行速度
- 存在匀速运行阶段
- 不可达最高运行速度
代码实现
#include <stdio.h>
#include <math.h>
int main()
{
// input
float distance = 10;
// parameter setting
float vel = 10;
float acc = 10;
// output
float acc_time = 0;
float vel_time = 0;
float dec_time = 0;
// algorithm design
// first condition
// /\
// / \
if((vel * vel) / (2 * acc) - (distance / 2) < 1e-3)
{
acc_time = vel / acc;
vel_time = 0;
dec_time = acc_time;
}
// second condition
// _ _ _
// / \
if((vel * vel) / (2 * acc) < distance / 2)
{
acc_time = vel / acc;
vel_time = (distance - 2 * ((vel * vel) / (2 * acc))) / vel;
dec_time = acc_time;
}
// third condition
//
// /\
if((vel * vel) / (2 * acc) > distance / 2)
{
acc_time = sqrt(2 * distance / 2 / acc);
vel_time = 0;
dec_time = acc_time;
}
// print
printf("acc_time = %f\n", acc_time);
printf("vel_time = %f\n", vel_time);
printf("dec_time = %f\n", dec_time);
return 0;
}
结果验证
- 可达最高运行速度
distance = 10
acc_time = 1.000000
vel_time = 0.000000
dec_time = 1.000000
- 存在匀速运行阶段
distance = 20
acc_time = 1.000000
vel_time = 1.000000
dec_time = 1.000000
- 不可达最高运行速度
distance = 8
acc_time = 0.894427
vel_time = 0.000000
dec_time = 0.894427
浙公网安备 33010602011771号