-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_controller.h
74 lines (61 loc) · 2.57 KB
/
pid_controller.h
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
/*******************************************************************************
* File: pid_controller.c
* Author: Vladimir Pasashnikov, [email protected]
*
* Implements PID controller:
* - Full PID controller with parameters Kp, Ki, and Kd using backward difference relationship
* - Back calculation anti-windup scheme with tracking gain Kt
* - Derivative part as low path filter with parameter with parameter N
* - Setpoints for proportional and derivative paths, with parameters Wp and Wd respectively
* Based on http://controlsystemslab.com/advanced-pid-controller-implementation/
* Site seems to be down, link to old version via web archive - https://web.archive.org/web/20170716045041/http://controlsystemslab.com/advanced-pid-controller-implementation/
*
* Created on 15 January 2018
******************************************************************************/
#ifndef PID_CONTROLLER_H
#define PID_CONTROLLER_H
#include <libq.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
/*
* PID controller variables are all in Q15.16
*/
_Q16 e0, e1, e2; // True error, z, z^-1, and z^-2
_Q16 ep0, ep1, ep2; // Error, proportional path, z, z^-1, and z^-2
_Q16 ed0, ed1, ed2; // Error, derivative path, z, z^-1, and z^-2
_Q16 eus0, eus1, eus2; // Anti-windup calculation path, z, z^-1, and z^-2
_Q16 u0, u1, u2; // Controller output, z, z^-1, and z^-2
} PidControllerState;
typedef struct {
/*
* PID controller parameters are all in Q15.16.
*/
_Q16 kp; // proportional gain
_Q16 ki; // integral gain
_Q16 kt; // tracking gain
_Q16 kd; // derivative gain
_Q16 wp; // proportional weight
_Q16 wd; // derivative weight
int n; // derivative filter coefficient
/* PID control parameter limits - e.g. DAC max/min outputs or PWN max/min values */
_Q16 output_max_limit;
_Q16 output_min_limit;
/* Sampling time interval */
_Q16 ts;
/*
* coefficients of PID algorithm derived from classic control parameters above.
*/
_Q16 a1, a2;
_Q16 b1, b2, b3;
_Q16 c1, c2, c3, c4, c5, c6;
_Q16 d1, d2, d3;
} PidControllerParameters;
void UpdatePid (PidControllerParameters *controller);
_Q16 CalculatePIDOutput (PidControllerParameters *c, PidControllerState *p, _Q16 reference_command, _Q16 plant_output);
#ifdef __cplusplus
}
#endif
#endif /* PID_CONTROLLER_H */