-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid.cpp
53 lines (43 loc) · 1.1 KB
/
pid.cpp
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
47
48
49
50
51
52
53
#define D_SMOOTHING 3
int p_array[D_SMOOTHING];
int d_pointer = 0;
int get_next_d_pointer ( int current_d_pointer )
{
return (current_d_pointer + 1) % D_SMOOTHING;
}
void init(void)
{
for (int x = 0; x < D_SMOOTHING; x++)
p_array[x] = 0;
}
int get_force_demand(int position, int target, int stiffness)
{
return (target - position)*stiffness;
}
void main(void)
{
int position = get_position();
int force = get_force();
int target_force = get_target_force(position, target_position, stiffness);
int p = position - target;
p_array[d_pointer] = p;
int next_d_pointer = get_next_d_pointer(d_pointer);
int d = p_array[next_d_pointer] = p_array[p_pointer];
int i += p;
if (i_coeficient)
{
if (i > i_max) i = i_max;
else if (i < -i_max) i = -i_max;
if (i > 0)
{
if (i > i_decay) i -= i_decay;
else i = 0;
}
else if (i > 0)
{
if (i < -i_decay) i += i_decay;
else i = 0;
}
}
output = p * p_coeficient + d * d_coefficient + i * i_coeficient;
}