Skip to content

Commit 5798b7e

Browse files
chore: create a sync pid (#61)
* create a sync pid * fix sync * fix tolerance
1 parent 2a420e9 commit 5798b7e

9 files changed

Lines changed: 207 additions & 98 deletions

File tree

‎examples/controllers/TemperatureControllerPid/TemperatureController.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ namespace
8585
public:
8686
ScaledPidIncremental(float scale, controllers::PidDriver<T>& driver,
8787
std::chrono::system_clock::duration sampleTime,
88-
typename controllers::PidController<T>::Tunings tunings,
89-
typename controllers::PidController<T>::Limits limits)
88+
controllers::PidTunings<T> tunings,
89+
controllers::PidLimits<T> limits)
9090
: pidController(driver, sampleTime, tunings, limits)
9191
, scale(scale)
9292
, squaredScale(scale * scale)
@@ -115,7 +115,7 @@ namespace
115115
}
116116

117117
private:
118-
controllers::PidIncremental<T> pidController;
118+
controllers::PidIncrementalAsynchronous<T> pidController;
119119
float scale;
120120
float squaredScale;
121121
};

‎numerical/analysis/test/TestFastFourierTransformRadix2Impl.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TYPED_TEST(TestFastFourierTransform, zero_input_produces_zero_output)
6565
auto& result = this->fft->Forward(this->timeDomain);
6666

6767
for (const auto& value : result)
68-
EXPECT_NEAR(math::ToFloat(CalculateMagnitude(value)), 0.0f, controllers::GetTolerance<TypeParam>());
68+
EXPECT_NEAR(math::ToFloat(CalculateMagnitude(value)), 0.0f, math::Tolerance<TypeParam>());
6969
}
7070

7171
TYPED_TEST(TestFastFourierTransform, dc_signal_appears_in_zero_frequency_bin)
@@ -79,10 +79,10 @@ TYPED_TEST(TestFastFourierTransform, dc_signal_appears_in_zero_frequency_bin)
7979

8080
EXPECT_NEAR(math::ToFloat(CalculateMagnitude(result[0])),
8181
static_cast<float>(TestFastFourierTransform<TypeParam>::Length) * 0.1f,
82-
controllers::GetTolerance<TypeParam>());
82+
math::Tolerance<TypeParam>());
8383

8484
for (size_t i = 1; i < result.size(); ++i)
85-
EXPECT_NEAR(math::ToFloat(CalculateMagnitude(result[i])), 0.0f, controllers::GetTolerance<TypeParam>());
85+
EXPECT_NEAR(math::ToFloat(CalculateMagnitude(result[i])), 0.0f, math::Tolerance<TypeParam>());
8686
}
8787

8888
TYPED_TEST(TestFastFourierTransform, forward_and_inverse_transform_recovers_original_signal)
@@ -100,7 +100,7 @@ TYPED_TEST(TestFastFourierTransform, forward_and_inverse_transform_recovers_orig
100100
auto& recovered = this->fft->Inverse(frequency);
101101

102102
for (size_t i = 0; i < signal.size(); ++i)
103-
EXPECT_NEAR(math::ToFloat(recovered[i]), signal[i], controllers::GetTolerance<TypeParam>());
103+
EXPECT_NEAR(math::ToFloat(recovered[i]), signal[i], math::Tolerance<TypeParam>());
104104
}
105105

106106
TYPED_TEST(TestFastFourierTransform, nyquist_frequency_detection)
@@ -115,5 +115,5 @@ TYPED_TEST(TestFastFourierTransform, nyquist_frequency_detection)
115115

116116
EXPECT_NEAR(math::ToFloat(CalculateMagnitude(result[TestFastFourierTransform<TypeParam>::Length / 2])),
117117
static_cast<float>(TestFastFourierTransform<TypeParam>::Length) * 0.1f,
118-
controllers::GetTolerance<TypeParam>());
118+
math::Tolerance<TypeParam>());
119119
}

‎numerical/analysis/test/TestPowerDensitySpectrum.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ TYPED_TEST(TestPowerSpectralDensity, when_input_smaller_than_fft_size_throws_ass
109109

110110
TYPED_TEST(TestPowerSpectralDensity, overlapping_segments_are_properly_averaged)
111111
{
112-
float tolerance = controllers::GetTolerance<TypeParam>();
112+
float tolerance = math::Tolerance<TypeParam>();
113113

114114
typename TestFixture::PowerDensitySpectrum::VectorReal::template WithMaxSize<2 * TestFixture::length> input;
115115

‎numerical/controllers/implementations/PidIncremental.hpp‎

Lines changed: 145 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,25 @@
77

88
namespace controllers
99
{
10-
1110
template<typename QNumberType>
12-
class PidIncremental
13-
: public PidController<QNumberType>
11+
class PidIncrementalBase
1412
{
1513
public:
16-
using PidBase = PidController<QNumberType>;
14+
void SetPoint(QNumberType setPoint);
15+
void SetLimits(PidLimits<QNumberType> limits);
16+
void SetTunings(PidTunings<QNumberType> tunnings);
17+
void Enable();
18+
QNumberType Process(QNumberType processVariable);
1719

18-
PidIncremental(PidDriver<QNumberType>& driver, std::chrono::system_clock::duration sampleTime, typename PidBase::Tunings tunnings, typename PidBase::Limits limits);
19-
20-
// Implementation of PidController
21-
void SetPoint(QNumberType setPoint) override;
22-
void SetLimits(PidBase::Limits limits) override;
23-
void SetTunings(PidBase::Tunings tunnings) override;
24-
void Enable() override;
25-
void Disable() override;
20+
protected:
21+
PidIncrementalBase(PidTunings<QNumberType> tunnings, PidLimits<QNumberType> limits);
2622

2723
private:
2824
QNumberType Clamp(QNumberType input);
29-
void UpdateLasts(QNumberType& controllerOutput, QNumberType& error, QNumberType& measuredProcessVariable);
30-
QNumberType Process(QNumberType measuredProcessVariable);
3125

3226
private:
33-
PidDriver<QNumberType>& driver;
34-
std::chrono::system_clock::duration sampleTime;
35-
PidBase::Limits limits;
36-
3727
std::optional<QNumberType> setPoint;
28+
PidLimits<QNumberType> limits;
3829
QNumberType a0 = 0;
3930
QNumberType a1 = 0;
4031
QNumberType a2 = 0;
@@ -47,69 +38,92 @@ namespace controllers
4738
QNumberType e_2 = QNumberType(0.0f);
4839
};
4940

41+
template<typename QNumberType>
42+
class PidIncrementalAsynchronous
43+
: public AsynchronousPidController<QNumberType>
44+
, private PidIncrementalBase<QNumberType>
45+
{
46+
public:
47+
PidIncrementalAsynchronous(PidDriver<QNumberType>& driver, std::chrono::system_clock::duration sampleTime, PidTunings<QNumberType> tunnings, PidLimits<QNumberType> limits);
48+
49+
// Implementation of AsynchronousPidController
50+
void SetPoint(QNumberType setPoint) override;
51+
void SetLimits(PidLimits<QNumberType> limits) override;
52+
void SetTunings(PidTunings<QNumberType> tunnings) override;
53+
void Enable() override;
54+
void Disable() override;
55+
56+
private:
57+
PidDriver<QNumberType>& driver;
58+
std::chrono::system_clock::duration sampleTime;
59+
};
60+
61+
template<typename QNumberType>
62+
class PidIncrementalSynchronous
63+
: public SynchronousPidController<QNumberType>
64+
, private PidIncrementalBase<QNumberType>
65+
{
66+
public:
67+
PidIncrementalSynchronous(PidTunings<QNumberType> tunnings, PidLimits<QNumberType> limits);
68+
69+
// Implementation of SynchronousPidController
70+
void SetPoint(QNumberType setPoint) override;
71+
void SetLimits(PidLimits<QNumberType> limits) override;
72+
void SetTunings(PidTunings<QNumberType> tunnings) override;
73+
void Enable() override;
74+
void Disable() override;
75+
QNumberType Process(QNumberType processVariable) override;
76+
};
77+
5078
//// Implementation ////
5179

5280
template<typename QNumberType>
53-
PidIncremental<QNumberType>::PidIncremental(PidDriver<QNumberType>& driver, std::chrono::system_clock::duration sampleTime, typename PidBase::Tunings tunnings, typename PidBase::Limits limits)
54-
: driver{ driver }
55-
, limits{ limits }
81+
PidIncrementalBase<QNumberType>::PidIncrementalBase(PidTunings<QNumberType> tunnings, PidLimits<QNumberType> limits)
82+
: limits{ limits }
5683
, a0{ tunnings.kp + tunnings.ki + tunnings.kd }
5784
, a1{ -tunnings.kp - (tunnings.kd + tunnings.kd) }
5885
, a2{ tunnings.kd }
5986
{
6087
really_assert(limits.min < limits.max);
61-
62-
driver.Read([this](auto measuredProcessVariable)
63-
{
64-
this->driver.ControlAction(Process(measuredProcessVariable));
65-
});
6688
}
6789

6890
template<class QNumberType>
69-
void PidIncremental<QNumberType>::SetPoint(QNumberType _setPoint)
91+
void PidIncrementalBase<QNumberType>::SetPoint(QNumberType _setPoint)
7092
{
7193
this->setPoint.emplace(_setPoint);
7294
}
7395

7496
template<class QNumberType>
75-
void PidIncremental<QNumberType>::Enable()
97+
void PidIncrementalBase<QNumberType>::Enable()
7698
{
7799
u = QNumberType(0.0f);
78100
u_1 = QNumberType(0.0f);
79101

80102
e = QNumberType(0.0f);
81103
e_1 = QNumberType(0.0f);
82104
e_2 = QNumberType(0.0f);
83-
84-
driver.Start(sampleTime);
85-
}
86-
87-
template<typename QNumberType>
88-
void PidIncremental<QNumberType>::Disable()
89-
{
90-
driver.Stop();
91105
}
92106

93107
template<class QNumberType>
94-
void PidIncremental<QNumberType>::SetLimits(PidIncremental<QNumberType>::PidBase::Limits _limits)
108+
void PidIncrementalBase<QNumberType>::SetLimits(PidLimits<QNumberType> _limits)
95109
{
96110
really_assert(_limits.max > _limits.min);
97111

98112
this->limits = _limits;
99113
}
100114

101115
template<class QNumberType>
102-
void PidIncremental<QNumberType>::SetTunings(PidIncremental<QNumberType>::PidBase::Tunings tunnings)
116+
void PidIncrementalBase<QNumberType>::SetTunings(PidTunings<QNumberType> tunings)
103117
{
104-
a0 = tunnings.kp + tunnings.ki + tunnings.kd;
105-
a1 = -tunnings.kp - (tunnings.kd + tunnings.kd);
106-
a2 = tunnings.kd;
118+
a0 = tunings.kp + tunings.ki + tunings.kd;
119+
a1 = -tunings.kp - (tunings.kd + tunings.kd);
120+
a2 = tunings.kd;
107121
}
108122

109123
template<class QNumberType>
110124
OPTIMIZE_FOR_SPEED
111125
QNumberType
112-
PidIncremental<QNumberType>::Clamp(QNumberType input)
126+
PidIncrementalBase<QNumberType>::Clamp(QNumberType input)
113127
{
114128
if (input > limits.max)
115129
return limits.max;
@@ -123,17 +137,103 @@ namespace controllers
123137
template<class QNumberType>
124138
OPTIMIZE_FOR_SPEED
125139
QNumberType
126-
PidIncremental<QNumberType>::Process(QNumberType measuredProcessVariable)
140+
PidIncrementalBase<QNumberType>::Process(QNumberType processVariable)
127141
{
128142
if (!setPoint.has_value())
129-
return measuredProcessVariable;
143+
return processVariable;
130144

131145
u_1 = u;
132146
e_2 = e_1;
133147
e_1 = e;
134-
e = *setPoint - measuredProcessVariable;
148+
e = *setPoint - processVariable;
135149
u = Clamp(u_1 + a0 * e + a1 * e_1 + a2 * e_2);
136150

137151
return u;
138152
}
153+
154+
template<typename QNumberType>
155+
PidIncrementalAsynchronous<QNumberType>::PidIncrementalAsynchronous(PidDriver<QNumberType>& driver, std::chrono::system_clock::duration sampleTime, PidTunings<QNumberType> tunnings, PidLimits<QNumberType> limits)
156+
: PidIncrementalBase<QNumberType>{ tunnings, limits }
157+
, driver{ driver }
158+
, sampleTime{ sampleTime }
159+
{
160+
driver.Read([this](auto processVariable)
161+
{
162+
this->driver.ControlAction(this->PidIncrementalBase<QNumberType>::Process(processVariable));
163+
});
164+
}
165+
166+
template<class QNumberType>
167+
void PidIncrementalAsynchronous<QNumberType>::SetPoint(QNumberType _setPoint)
168+
{
169+
PidIncrementalBase<QNumberType>::SetPoint(_setPoint);
170+
}
171+
172+
template<class QNumberType>
173+
void PidIncrementalAsynchronous<QNumberType>::Enable()
174+
{
175+
PidIncrementalBase<QNumberType>::Enable();
176+
driver.Start(sampleTime);
177+
}
178+
179+
template<typename QNumberType>
180+
void PidIncrementalAsynchronous<QNumberType>::Disable()
181+
{
182+
driver.Stop();
183+
}
184+
185+
template<class QNumberType>
186+
void PidIncrementalAsynchronous<QNumberType>::SetLimits(PidLimits<QNumberType> limits)
187+
{
188+
PidIncrementalBase<QNumberType>::SetLimits(limits);
189+
}
190+
191+
template<class QNumberType>
192+
void PidIncrementalAsynchronous<QNumberType>::SetTunings(PidTunings<QNumberType> tunings)
193+
{
194+
PidIncrementalBase<QNumberType>::SetTunings(tunings);
195+
}
196+
197+
template<class QNumberType>
198+
PidIncrementalSynchronous<QNumberType>::PidIncrementalSynchronous(PidTunings<QNumberType> tunnings, PidLimits<QNumberType> limits)
199+
: PidIncrementalBase<QNumberType>{ tunnings, limits }
200+
{
201+
}
202+
203+
template<class QNumberType>
204+
void PidIncrementalSynchronous<QNumberType>::SetPoint(QNumberType setPoint)
205+
{
206+
PidIncrementalBase<QNumberType>::SetPoint(setPoint);
207+
}
208+
209+
template<class QNumberType>
210+
void PidIncrementalSynchronous<QNumberType>::SetLimits(PidLimits<QNumberType> limits)
211+
{
212+
PidIncrementalBase<QNumberType>::SetLimits(limits);
213+
}
214+
215+
template<class QNumberType>
216+
void PidIncrementalSynchronous<QNumberType>::SetTunings(PidTunings<QNumberType> tunnings)
217+
{
218+
PidIncrementalBase<QNumberType>::SetTunings(tunnings);
219+
}
220+
221+
template<class QNumberType>
222+
void PidIncrementalSynchronous<QNumberType>::Enable()
223+
{
224+
PidIncrementalBase<QNumberType>::Enable();
225+
}
226+
227+
template<class QNumberType>
228+
void PidIncrementalSynchronous<QNumberType>::Disable()
229+
{
230+
}
231+
232+
template<class QNumberType>
233+
OPTIMIZE_FOR_SPEED
234+
QNumberType
235+
PidIncrementalSynchronous<QNumberType>::Process(QNumberType processVariable)
236+
{
237+
return PidIncrementalBase<QNumberType>::Process(processVariable);
238+
}
139239
}

0 commit comments

Comments
 (0)