5namespace tinyrobotics {
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
57 PIDAutoTuner(
float target,
float relayAmp,
float dt)
58 : target(target), relayAmp(relayAmp), dt(dt) {}
72 float update(
float y) {
76 if (y > target + hysteresis)
78 else if (y < target - hysteresis)
86 bool isFinished()
const {
return success; }
92 if (!success)
return r;
94 float amplitude = (avgMax - avgMin) / 2.0f;
97 float Ku = (4.0f * relayAmp) / (
static_cast<
float>(M_PI) * amplitude);
106 r.Ki = 2.0f * r.Kp / Tu;
107 r.Kd = r.Kp * Tu / 8.0f;
112 void setHysteresis(
float h) { hysteresis = h; }
115 enum class State { INIT, RUNNING };
120 float hysteresis = 0.01f;
134 float lastPeakTime = 0;
137 bool lastWasMax =
false;
139 bool success =
false;
141 State state = State::INIT;
143 void detectPeaks(
float y) {
145 if (y > maxY) maxY = y;
146 if (y < minY) minY = y;
149 if ((lastY < y) && (lastWasMax ==
false)) {
151 }
else if ((lastY > y) && (lastWasMax ==
false)) {
155 }
else if ((lastY > y) && (lastWasMax ==
true)) {
157 }
else if ((lastY < y) && (lastWasMax ==
true)) {
166 void registerPeak(
bool isMax) {
169 float period = time - lastPeakTime;
173 if (peakCount < 4)
return;
177 avgMax = 0.8f * avgMax + 0.2f * maxY;
179 avgMin = 0.8f * avgMin + 0.2f * minY;
181 avgPeriod = 0.8f * avgPeriod + 0.2f * period;
188 if (peakCount > 12) {
Automatic PID tuning using the relay (Åström-Hägglund) method.
Definition: PIDAutoTuner.h:46
Definition: PIDAutoTuner.h:48