-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc_traj.c
47 lines (36 loc) · 1.09 KB
/
mc_traj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "mc_traj.h"
#include <math.h>
static float fclampf(float x, float lo, float hi)
{
return fminf(fmaxf(x, lo), hi);
}
void mc_traj_step(struct mc_traj *t, float target, float dt)
{
target = fclampf(target, t->pos_min, t->pos_max);
float s = target - t->pos;
// How fast can we go and still brake?
//
float v = copysignf(sqrtf(2 * t->dec_max * fabsf(s)), s);
// Apply velocity and acceleration limits
//
v = fclampf(v, t->vel_min, t->vel_max);
if (t->vel >= 0)
v = fclampf(v, t->vel - t->dec_max * dt, t->vel + t->acc_max * dt);
else
v = fclampf(v, t->vel - t->acc_max * dt, t->vel + t->dec_max * dt);
// Update output values
//
bool pos_reached = fabsf(s) < fmaxf(-t->vel_min, t->vel_max) * dt;
bool vel_reached = fabsf(v) < t->dec_max * dt;
t->at_target = pos_reached && vel_reached;
if (t->at_target) {
t->acc = v ? -v / dt : 0;
t->vel = v;
t->pos = target;
}
else {
t->acc = (v - t->vel) / dt;
t->vel = v;
t->pos += v * dt + 0.5f * t->acc * dt * dt;
}
}