Tue, 18 May 2021 16:52:23 +0200
Version 0.3.13. Recipe import parse BMS_COOLING_TO to set the cooling temperature.
0 | 1 | |
2 | #include <stddef.h> | |
3 | #include <string.h> | |
4 | #include <stdlib.h> | |
5 | #include <stdbool.h> | |
6 | #include <stdint.h> | |
7 | #include <math.h> | |
8 | ||
9 | #include "freertos/FreeRTOS.h" | |
10 | #include "freertos/task.h" | |
11 | #include "esp_system.h" | |
12 | #include "esp_log.h" | |
13 | ||
14 | #include "PID_v1.h" | |
15 | ||
16 | double dispKp; // we'll hold on to the tuning parameters in user-entered | |
17 | double dispKi; // format for display purposes | |
18 | double dispKd; | |
19 | ||
20 | double kp; // (P)roportional Tuning Parameter | |
21 | double ki; // (I)ntegral Tuning Parameter | |
22 | double kd; // (D)erivative Tuning Parameter | |
23 | ||
24 | int controllerDirection; | |
25 | ||
26 | double *myInput; // Pointers to the Input, Output, and Setpoint variables | |
27 | double *myOutput; // This creates a hard link between the variables and the | |
28 | double *mySetpoint; // PID, freeing the user from having to constantly tell us | |
29 | // what these values are. with pointers we'll just know. | |
30 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
31 | unsigned long lastTime; ///< Last time of the time window. |
0 | 32 | double outputSum, lastInput; |
33 | ||
34 | unsigned long SampleTime; | |
35 | double outMin, outMax; | |
82 | 36 | bool inAuto; |
0 | 37 | |
38 | ||
39 | static const char *TAG = "pid"; | |
40 | ||
41 | void PID_Initialize(void); | |
42 | ||
43 | ||
44 | ||
82 | 45 | void PID(double* Input, double* Output, double* Setpoint, double Kp, double Ki, double Kd, PID_DIRECTION Direction) { |
0 | 46 | myOutput = Output; |
47 | myInput = Input; | |
48 | mySetpoint = Setpoint; | |
49 | inAuto = false; | |
50 | ||
51 | PID_SetOutputLimits(0, 255); | |
52 | ||
53 | SampleTime = 100; | |
54 | ||
55 | PID_SetControllerDirection(Direction); | |
82 | 56 | PID_SetTunings(Kp, Ki, Kd); |
0 | 57 | |
58 | lastTime = (xTaskGetTickCount() * portTICK_PERIOD_MS ) - SampleTime; | |
59 | } | |
60 | ||
61 | ||
62 | ||
63 | /* | |
64 | * This, as they say, is where the magic happens. this function should be called | |
65 | * every time "void loop()" executes. the function will decide for itself whether a new | |
66 | * pid Output needs to be computed. returns true when the output is computed, | |
67 | * false when nothing has been done. | |
68 | */ | |
69 | bool PID_Compute(void) | |
70 | { | |
71 | if (!inAuto) | |
72 | return false; | |
73 | ||
74 | unsigned long now = xTaskGetTickCount() * portTICK_PERIOD_MS; | |
75 | unsigned long timeChange = (now - lastTime); | |
76 | if (timeChange >= SampleTime) { | |
77 | /*Compute all the working error variables*/ | |
78 | double input = *myInput; | |
79 | double error = *mySetpoint - input; | |
80 | double dInput = (input - lastInput); | |
81 | outputSum+= (ki * error); | |
82 | 82 | outputSum-= kp * dInput; |
0 | 83 | |
84 | if (outputSum > outMax) | |
85 | outputSum= outMax; | |
86 | else if (outputSum < outMin) | |
87 | outputSum= outMin; | |
88 | ||
82 | 89 | double output = 0; |
0 | 90 | |
91 | /*Compute Rest of PID Output*/ | |
92 | output += outputSum - kd * dInput; | |
93 | ||
94 | if (output > outMax) | |
95 | output = outMax; | |
96 | else if (output < outMin) | |
97 | output = outMin; | |
98 | *myOutput = output; | |
99 | ||
100 | /*Remember some variables for next time*/ | |
101 | lastInput = input; | |
102 | lastTime = now; | |
103 | return true; | |
104 | } else | |
105 | return false; | |
106 | } | |
107 | ||
108 | ||
109 | ||
110 | /* | |
111 | * This function allows the controller's dynamic performance to be adjusted. | |
112 | * it's called automatically from the constructor, but tunings can also | |
113 | * be adjusted on the fly during normal operation | |
114 | */ | |
82 | 115 | void PID_SetTunings(double Kp, double Ki, double Kd) |
0 | 116 | { |
117 | if (Kp<0 || Ki<0 || Kd<0) { | |
118 | ESP_LOGE(TAG, "SetTunings negative input"); | |
119 | return; | |
120 | } | |
121 | ||
122 | dispKp = Kp; dispKi = Ki; dispKd = Kd; | |
123 | ||
82 | 124 | ESP_LOGI(TAG, "SetTunings(%.3f, %.3f, %.3f)", Kp, Ki, Kd); |
0 | 125 | double SampleTimeInSec = ((double)SampleTime)/1000; |
126 | kp = Kp; | |
127 | ki = Ki * SampleTimeInSec; | |
128 | kd = Kd / SampleTimeInSec; | |
129 | ||
130 | if (controllerDirection == PID_REVERSE) { | |
131 | kp = (0 - kp); | |
132 | ki = (0 - ki); | |
133 | kd = (0 - kd); | |
134 | } | |
135 | } | |
136 | ||
137 | ||
138 | ||
139 | /* | |
140 | * sets the period, in Milliseconds, at which the calculation is performed | |
141 | */ | |
142 | void PID_SetSampleTime(int NewSampleTime) | |
143 | { | |
144 | ESP_LOGI(TAG, "SetSampleTime(%d)", NewSampleTime); | |
145 | ||
146 | if (NewSampleTime > 0) { | |
147 | double ratio = (double)NewSampleTime / (double)SampleTime; | |
148 | ki *= ratio; | |
149 | kd /= ratio; | |
150 | SampleTime = (unsigned long)NewSampleTime; | |
151 | } | |
152 | } | |
153 | ||
154 | ||
155 | ||
156 | /* | |
157 | * This function will be used far more often than SetInputLimits. while | |
158 | * the input to the controller will generally be in the 0-1023 range (which is | |
159 | * the default already,) the output will be a little different. maybe they'll | |
160 | * be doing a time window and will need 0-8000 or something. or maybe they'll | |
161 | * want to clamp it from 0-125. who knows. at any rate, that can all be done | |
162 | * here. | |
163 | */ | |
164 | void PID_SetOutputLimits(double Min, double Max) | |
165 | { | |
166 | if(Min >= Max) { | |
167 | ESP_LOGE(TAG, "SetOutputLimits Min >= Max"); | |
168 | return; | |
169 | } | |
170 | ||
171 | ESP_LOGI(TAG, "SetOutputLimits(%.0f, %.0f)", Min, Max); | |
172 | outMin = Min; | |
173 | outMax = Max; | |
174 | ||
175 | if(inAuto) { | |
176 | if(*myOutput > outMax) | |
177 | *myOutput = outMax; | |
178 | else if(*myOutput < outMin) | |
179 | *myOutput = outMin; | |
180 | ||
181 | if(outputSum > outMax) | |
182 | outputSum= outMax; | |
183 | else if(outputSum < outMin) | |
184 | outputSum= outMin; | |
185 | } | |
186 | } | |
187 | ||
188 | ||
189 | ||
190 | /* | |
191 | * Allows the controller Mode to be set to manual (0) or Automatic (non-zero) | |
192 | * when the transition from manual to auto occurs, the controller is | |
193 | * automatically initialized | |
194 | */ | |
195 | void PID_SetMode(PID_MODE Mode) | |
196 | { | |
197 | bool newAuto = (Mode == PID_AUTOMATIC); | |
198 | ||
199 | ESP_LOGI(TAG, "SetMode(%s)", (Mode) ? "AUTOMATIC":"MANUAL"); | |
200 | if(newAuto && !inAuto) { /*we just went from manual to auto*/ | |
201 | PID_Initialize(); | |
202 | } | |
203 | inAuto = newAuto; | |
204 | } | |
205 | ||
206 | ||
207 | ||
208 | /* | |
209 | * does all the things that need to happen to ensure a bumpless transfer | |
210 | * from manual to automatic mode. | |
211 | */ | |
212 | void PID_Initialize() | |
213 | { | |
214 | outputSum = *myOutput; | |
215 | lastInput = *myInput; | |
216 | if(outputSum > outMax) | |
217 | outputSum = outMax; | |
218 | else if(outputSum < outMin) | |
219 | outputSum = outMin; | |
220 | } | |
221 | ||
222 | ||
223 | ||
224 | /* | |
225 | * The PID will either be connected to a DIRECT acting process (+Output leads | |
226 | * to +Input) or a REVERSE acting process(+Output leads to -Input.) we need to | |
227 | * know which one, because otherwise we may increase the output when we should | |
228 | * be decreasing. This is called from the constructor. | |
229 | */ | |
230 | void PID_SetControllerDirection(PID_DIRECTION Direction) | |
231 | { | |
232 | ESP_LOGI(TAG, "SetControllerDirection(%s)", (Direction) ? "REVERSE":"DIRECT"); | |
233 | ||
234 | if(inAuto && Direction !=controllerDirection) { | |
235 | kp = (0 - kp); | |
236 | ki = (0 - ki); | |
237 | kd = (0 - kd); | |
238 | } | |
239 | controllerDirection = Direction; | |
240 | } | |
241 | ||
242 | ||
243 | ||
244 | /* | |
245 | * Just because you set the Kp=-1 doesn't mean it actually happened. these | |
246 | * functions query the internal state of the PID. they're here for display | |
247 | * purposes. this are the functions the PID Front-end uses for example | |
248 | */ | |
249 | double PID_GetKp() | |
250 | { | |
251 | return dispKp; | |
252 | } | |
253 | ||
254 | double PID_GetKi() | |
255 | { | |
256 | return dispKi; | |
257 | } | |
258 | ||
259 | double PID_GetKd() | |
260 | { | |
261 | return dispKd; | |
262 | } | |
263 | ||
264 | PID_MODE PID_GetMode() | |
265 | { | |
266 | return inAuto ? PID_AUTOMATIC : PID_MANUAL; | |
267 | } | |
268 | ||
269 | PID_DIRECTION PID_GetDirection() | |
270 | { | |
271 | return controllerDirection; | |
272 | } | |
273 | ||
274 |