This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
pid.c
76 lines (62 loc) · 1.52 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <math.h>
#include "pid.h"
void pid_init(pid_ctrl_t *pid)
{
pid_set_gains(pid, 1., 0., 0.);
pid->integrator = 0.;
pid->previous_error = 0.;
pid->integrator_limit = INFINITY;
pid->frequency = 1.;
}
void pid_set_gains(pid_ctrl_t *pid, float kp, float ki, float kd)
{
pid->kp = kp;
pid->ki = ki;
pid->kd = kd;
}
void pid_get_gains(const pid_ctrl_t *pid, float *kp, float *ki, float *kd)
{
*kp = pid->kp;
*ki = pid->ki;
*kd = pid->kd;
}
float pid_get_integral_limit(const pid_ctrl_t *pid)
{
return pid->integrator_limit;
}
float pid_get_integral(const pid_ctrl_t *pid)
{
return pid->integrator;
}
float pid_process(pid_ctrl_t *pid, float error)
{
float output;
pid->integrator += error;
if (pid->integrator > pid->integrator_limit) {
pid->integrator = pid->integrator_limit;
} else if (pid->integrator < -pid->integrator_limit) {
pid->integrator = -pid->integrator_limit;
}
output = - pid->kp * error;
output += - pid->ki * pid->integrator / pid->frequency;
output += - pid->kd * (error - pid->previous_error) * pid->frequency;
pid->previous_error = error;
return output;
}
void pid_set_integral_limit(pid_ctrl_t *pid, float max)
{
pid->integrator_limit = max;
}
void pid_reset_integral(pid_ctrl_t *pid)
{
pid->integrator = 0.;
}
void pid_set_frequency(pid_ctrl_t *pid, float frequency)
{
pid->frequency = frequency;
}
float pid_get_frequency(const pid_ctrl_t *pid)
{
return pid->frequency;
}