-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTunePID.cpp
299 lines (260 loc) · 8.75 KB
/
AutoTunePID.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "AutoTunePID.h"
AutoTunePID::AutoTunePID(float minOutput, float maxOutput, TuningMethod method)
: _minOutput(minOutput)
, _maxOutput(maxOutput)
, _method(method)
, _operationalMode(OperationalMode::Normal)
, _oscillationMode(OscillationMode::Normal)
, _oscillationSteps(10)
, _setpoint(0)
, _lambda(0.5f) // Default lambda value
, _kp(0)
, _ki(0)
, _kd(0)
, _error(0)
, _previousError(0)
, _integral(0)
, _output(0)
, _lastUpdate(0)
, _ultimateGain(0)
, _oscillationPeriod(0)
, _processTimeConstant(0) // Initialize process time constant (T)
, _deadTime(0) // Initialize dead time (L)
, _integralTime(0) // Initialize integral time (Ti)
, _derivativeTime(0) // Initialize derivative time (Td)
, _inputFilterEnabled(false)
, _outputFilterEnabled(false)
, _inputFilteredValue(0)
, _outputFilteredValue(0)
, _inputFilterAlpha(0.1)
, _outputFilterAlpha(0.1)
, _antiWindupEnabled(true)
, _integralWindupThreshold(0.8f * (maxOutput - minOutput))
{
}
void AutoTunePID::setSetpoint(float setpoint)
{
_setpoint = setpoint;
}
void AutoTunePID::setTuningMethod(TuningMethod method)
{
_method = method;
}
void AutoTunePID::setManualGains(float kp, float ki, float kd)
{
_kp = kp;
_ki = ki;
_kd = kd;
}
void AutoTunePID::enableInputFilter(float alpha)
{
_inputFilterEnabled = true;
_inputFilterAlpha = constrain(alpha, 0.01, 1.0);
}
void AutoTunePID::enableOutputFilter(float alpha)
{
_outputFilterEnabled = true;
_outputFilterAlpha = constrain(alpha, 0.01, 1.0);
}
void AutoTunePID::enableAntiWindup(bool enable, float threshold)
{
_antiWindupEnabled = enable;
_integralWindupThreshold = threshold * (_maxOutput - _minOutput);
}
void AutoTunePID::setOperationalMode(OperationalMode mode)
{
_operationalMode = mode;
if (mode == OperationalMode::Hold) {
_integral = 0;
_previousError = 0;
_output = 0;
}
}
void AutoTunePID::setOscillationMode(OscillationMode mode)
{
_oscillationMode = mode;
// Set default oscillation steps based on the oscillation mode
switch (mode) {
case OscillationMode::Normal:
_oscillationSteps = 10;
break;
case OscillationMode::Half:
_oscillationSteps = 20;
break;
case OscillationMode::Mild:
_oscillationSteps = 40;
break;
}
}
void AutoTunePID::setOscillationSteps(int steps)
{
if (steps > 0) {
_oscillationSteps = steps;
}
}
void AutoTunePID::setLambda(float lambda)
{
_lambda = lambda;
}
void AutoTunePID::update(float currentInput)
{
unsigned long now = millis();
if (now - _lastUpdate < 100)
return; // Maintain consistent sample time
_lastUpdate = now;
// Update input (with filter if enabled)
if (_inputFilterEnabled && _operationalMode != OperationalMode::Tune) {
currentInput = computeFilteredValue(currentInput, _inputFilteredValue, _inputFilterAlpha);
}
_input = currentInput; // Store the current input value
if (_operationalMode == OperationalMode::Tune) {
performAutoTune(currentInput);
} else {
_error = _setpoint - _input;
// Reset integral term if error is zero (faster and smoother zeroing)
if (abs(_error) < 0.001) {
_integral = 0;
} else {
_integral += _error * 0.1f; // Smoother integral accumulation
}
_derivative = _error - _previousError;
computePID();
applyAntiWindup();
_previousError = _error;
}
// Update output (with filter if enabled)
if (_outputFilterEnabled && _operationalMode != OperationalMode::Tune) {
_output = computeFilteredValue(_output, _outputFilteredValue, _outputFilterAlpha);
}
}
void AutoTunePID::performAutoTune(float currentInput)
{
static unsigned long lastToggleTime = 0;
static bool outputState = true;
static int oscillationCount = 0;
static unsigned long oscillationStartTime = 0;
unsigned long currentTime = millis();
// Determine the output range based on the oscillation mode
float highOutput, lowOutput;
switch (_oscillationMode) {
case OscillationMode::Normal:
highOutput = _maxOutput;
lowOutput = _minOutput;
break;
case OscillationMode::Half:
highOutput = (_maxOutput + _minOutput) / 2.0f + (_maxOutput - _minOutput) / 4.0f; // 3/4 of the range
lowOutput = (_maxOutput + _minOutput) / 2.0f - (_maxOutput - _minOutput) / 4.0f; // 1/4 of the range
break;
case OscillationMode::Mild:
highOutput = (_maxOutput + _minOutput) / 2.0f + (_maxOutput - _minOutput) / 8.0f; // 5/8 of the range
lowOutput = (_maxOutput + _minOutput) / 2.0f - (_maxOutput - _minOutput) / 8.0f; // 3/8 of the range
break;
}
// Toggle output every second to induce oscillations
if (currentTime - lastToggleTime >= 1000) {
outputState = !outputState;
_output = outputState ? highOutput : lowOutput;
lastToggleTime = currentTime;
if (oscillationCount == 0) {
oscillationStartTime = currentTime;
}
oscillationCount++;
// After the specified number of oscillations, calculate Ku and Tu
if (oscillationCount >= _oscillationSteps) {
_oscillationPeriod = (currentTime - oscillationStartTime) / (float)(_oscillationSteps * 1000); // Period in seconds
_ultimateGain = (4.0f * (highOutput - lowOutput)) / (PI * (highOutput - lowOutput)); // Simplified amplitude
// Estimate T and L from the system response
_processTimeConstant = _oscillationPeriod / 2.0f; // Approximate T as half the oscillation period
_deadTime = _oscillationPeriod / 4.0f; // Approximate L as a quarter of the oscillation period
// Calculate Ti and Td based on T and L
_integralTime = 2.0f * _deadTime;
_derivativeTime = _deadTime / 2.0f;
// Calculate PID gains based on the selected tuning method
switch (_method) {
case TuningMethod::ZieglerNichols:
calculateZieglerNicholsGains();
break;
case TuningMethod::CohenCoon:
calculateCohenCoonGains();
break;
case TuningMethod::IMC:
calculateIMCGains();
break;
case TuningMethod::TyreusLuyben:
calculateTyreusLuybenGains();
break;
case TuningMethod::LambdaTuning:
calculateLambdaTuningGains();
break;
default:
break;
}
// Reset oscillation count and return to normal operation
oscillationCount = 0;
_operationalMode = OperationalMode::Normal; // Switch back to Normal mode after tuning
}
}
}
void AutoTunePID::calculateZieglerNicholsGains()
{
_kp = 0.6f * _ultimateGain;
_ki = _kp / _integralTime;
_kd = _kp * _derivativeTime;
}
void AutoTunePID::calculateCohenCoonGains()
{
_kp = (1.35f / _ultimateGain) * (_processTimeConstant / _deadTime + 0.185f);
_ki = _kp / (_processTimeConstant + 0.611f * _deadTime);
_kd = _kp * 0.185f * _deadTime;
}
void AutoTunePID::calculateIMCGains()
{
const float lambda = 0.5f * _oscillationPeriod;
_kp = _processTimeConstant / (lambda + _deadTime);
_ki = _kp / _integralTime;
_kd = _kp * _derivativeTime;
}
void AutoTunePID::calculateTyreusLuybenGains()
{
_kp = 0.45f * _ultimateGain;
_ki = _kp / _integralTime;
_kd = 0.0f;
}
void AutoTunePID::calculateLambdaTuningGains()
{
// Ensure lambda is set and valid
if (_lambda <= 0) {
_lambda = 0.5f * _processTimeConstant; // Default value for lambda
}
// Calculate Kp, Ki, and Kd using the Lambda Tuning (CLD) formula
_kp = _processTimeConstant / (_ultimateGain * (_lambda + _deadTime));
_ki = _kp / _processTimeConstant;
_kd = _kp * 0.5f * _deadTime;
}
void AutoTunePID::computePID()
{
// Calculate error
_error = _setpoint - _input;
// If error is very small, treat it as zero
if (abs(_error) < 0.001) {
_error = 0;
}
// Calculate PID terms
float P = _kp * _error;
float I = _ki * _integral;
float D = _kd * _derivative;
// Calculate output
_output = P + I + D;
_output = constrain(_output, _minOutput, _maxOutput);
}
void AutoTunePID::applyAntiWindup()
{
if (_antiWindupEnabled && abs(_integral) > _integralWindupThreshold) {
_integral = constrain(_integral, -_integralWindupThreshold, _integralWindupThreshold);
}
}
float AutoTunePID::computeFilteredValue(float input, float& filteredValue, float alpha) const
{
filteredValue = (alpha * input) + ((1.0f - alpha) * filteredValue);
return filteredValue;
}