forked from pms67/PID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PID_Test.c
65 lines (44 loc) · 1.44 KB
/
PID_Test.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
#include <stdio.h>
#include <stdlib.h>
#include "PID.h"
/* Controller parameters */
#define PID_KP 2.0f
#define PID_KI 0.5f
#define PID_KD 0.25f
#define PID_TAU 0.02f
#define PID_LIM_MIN -10.0f
#define PID_LIM_MAX 10.0f
#define PID_LIM_MIN_INT -5.0f
#define PID_LIM_MAX_INT 5.0f
#define SAMPLE_TIME_S 0.01f
/* Maximum run-time of simulation */
#define SIMULATION_TIME_MAX 4.0f
/* Simulated dynamical system (first order) */
float TestSystem_Update(float inp);
int main()
{
/* Initialise PID controller */
PIDController pid = { PID_KP, PID_KI, PID_KD,
PID_TAU,
PID_LIM_MIN, PID_LIM_MAX,
PID_LIM_MIN_INT, PID_LIM_MAX_INT,
SAMPLE_TIME_S };
PIDController_Init(&pid);
/* Simulate response using test system */
float setpoint = 1.0f;
printf("Time (s)\tSystem Output\tControllerOutput\r\n");
for (float t = 0.0f; t <= SIMULATION_TIME_MAX; t += SAMPLE_TIME_S) {
/* Get measurement from system */
float measurement = TestSystem_Update(pid.out);
/* Compute new control signal */
PIDController_Update(&pid, setpoint, measurement);
printf("%f\t%f\t%f\r\n", t, measurement, pid.out);
}
return 0;
}
float TestSystem_Update(float inp) {
static float output = 0.0f;
static const float alpha = 0.02f;
output = (SAMPLE_TIME_S * inp + output) / (1.0f + alpha * SAMPLE_TIME_S);
return output;
}