-
Notifications
You must be signed in to change notification settings - Fork 0
/
PID.h
214 lines (185 loc) · 5.42 KB
/
PID.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @author Aaron Berk
*
* @section LICENSE
*
* Copyright (c) 2010 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @section DESCRIPTION
*
* A PID controller is a widely used feedback controller commonly found in
* industry.
*
* This library is a port of Brett Beauregard's Arduino PID library:
*
* http://www.arduino.cc/playground/Code/PIDLibrary
*
* The wikipedia article on PID controllers is a good place to start on
* understanding how they work:
*
* http://en.wikipedia.org/wiki/PID_controller
*
* For a clear and elegant explanation of how to implement and tune a
* controller, the controlguru website by Douglas J. Cooper (who also happened
* to be Brett's controls professor) is an excellent reference:
*
* http://www.controlguru.com/
*/
#ifndef PID_H
#define PID_H
/**
* Includes
*/
#include "mbed.h"
/**
* Defines
*/
#define MANUAL_MODE 0
#define AUTO_MODE 1
/**
* Proportional-integral-derivative controller.
*/
class PID {
public:
/**
* Constructor.
*
* Sets default limits [0-3.3V], calculates tuning parameters, and sets
* manual mode with no bias.
*
* @param Kc - Tuning parameter
* @param tauI - Tuning parameter
* @param tauD - Tuning parameter
* @param interval PID calculation performed every interval seconds.
*/
PID(float Kc, float tauI, float tauD, float interval);
/**
* Scale from inputs to 0-100%.
*
* @param InMin The real world value corresponding to 0%.
* @param InMax The real world value corresponding to 100%.
*/
void setInputLimits(float inMin , float inMax);
/**
* Scale from outputs to 0-100%.
*
* @param outMin The real world value corresponding to 0%.
* @param outMax The real world value corresponding to 100%.
*/
void setOutputLimits(float outMin, float outMax);
/**
* Calculate PID constants.
*
* Allows parameters to be changed on the fly without ruining calculations.
*
* @param Kc - Tuning parameter
* @param tauI - Tuning parameter
* @param tauD - Tuning parameter
*/
void setTunings(float Kc, float tauI, float tauD);
/**
* Reinitializes controller internals. Automatically
* called on a manual to auto transition.
*/
void reset(void);
/**
* Set PID to manual or auto mode.
*
* @param mode 0 -> Manual
* Non-zero -> Auto
*/
void setMode(int mode);
/**
* Set how fast the PID loop is run.
*
* @param interval PID calculation peformed every interval seconds.
*/
void setInterval(float interval);
/**
* Set the set point.
*
* @param sp The set point as a real world value.
*/
void setSetPoint(float sp);
/**
* Set the process value.
*
* @param pv The process value as a real world value.
*/
void setProcessValue(float pv);
/**
* Set the bias.
*
* @param bias The bias for the controller output.
*/
void setBias(float bias);
/**
* PID calculation.
*
* @return The controller output as a float between outMin and outMax.
*/
float compute(void);
//Getters.
float getInMin();
float getInMax();
float getOutMin();
float getOutMax();
float getInterval();
float getPParam();
float getIParam();
float getDParam();
private:
bool usingFeedForward;
bool inAuto;
//Actual tuning parameters used in PID calculation.
float Kc_;
float tauR_;
float tauD_;
//Raw tuning parameters.
float pParam_;
float iParam_;
float dParam_;
//The point we want to reach.
float setPoint_;
//The thing we measure.
float processVariable_;
float prevProcessVariable_;
//The output that affects the process variable.
float controllerOutput_;
float prevControllerOutput_;
//We work in % for calculations so these will scale from
//real world values to 0-100% and back again.
float inMin_;
float inMax_;
float inSpan_;
float outMin_;
float outMax_;
float outSpan_;
//The accumulated error, i.e. integral.
float accError_;
//The controller output bias.
float bias_;
//The interval between samples.
float tSample_;
//Controller output as a real world value.
volatile float realOutput_;
};
#endif /* PID_H */