-
Notifications
You must be signed in to change notification settings - Fork 2
/
pid.c
executable file
·40 lines (33 loc) · 1.04 KB
/
pid.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
#include "pid.h"
void Pid_Reset(pid_type* pid)
{
// set prev and integrated error to zero
pid->prev_error = 0;
pid->int_error = 0;
}
void Pid_Update(pid_type* pid, float current_error, float dt)
{
float diff;
float p_term;
float i_term;
float d_term;
// integration with windup guarding
pid->int_error += (current_error * dt);
if (pid->int_error < -(pid->windup_guard))
pid->int_error = -(pid->windup_guard);
else if (pid->int_error > pid->windup_guard)
pid->int_error = pid->windup_guard;
// differentiation
if (dt != 0)
diff = ((current_error - pid->prev_error) / dt);
else
dt = 0;
// scaling
p_term = (pid->proportional_gain * current_error);
i_term = (pid->integral_gain * pid->int_error);
d_term = (pid->derivative_gain * diff);
// summation of terms
pid->control = p_term + i_term + d_term;
// save current error as previous error for next iteration
pid->prev_error = current_error;
}