Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
pid.hpp
1
9
10#ifndef QLIBS_PID
11#define QLIBS_PID
12
13#include <include/qlibs_types.hpp>
14#include <include/numa.hpp>
15
19namespace qlibs {
24
32
42
50
52 class pidController;
54
58 struct pidGains {
59 real_t Kc{ 0.0_re };
60 real_t Ki{ 0.0_re };
61 real_t Kd{ 0.0_re };
62
64 constexpr pidGains() = default;
65 constexpr pidGains(real_t kc, real_t ki, real_t kd)
66 : Kc(kc), Ki(ki), Kd(kd) {}
67
68 constexpr pidGains operator+(const pidGains& other) const {
69 return pidGains{
70 this->Kc + other.Kc,
71 this->Ki + other.Ki,
72 this->Kd + other.Kd
73 };
74 }
76 };
77
79 constexpr pidGains operator"" _kc(long double v) {
80 return {static_cast<real_t>(v), 0.0_re, 0.0_re};
81 }
82 constexpr pidGains operator"" _ki(long double v) {
83 return {0.0_re, static_cast<real_t>(v), 0.0_re};
84 }
85 constexpr pidGains operator"" _kd(long double v) {
86 return {0.0_re, 0.0_re, static_cast<real_t>(v)};
87 }
89
90
98 struct systemParams {
99 real_t gain;
100 real_t tau1;
101 real_t tau2;
102 bool stable;
103 };
104
105 struct estimationParams {
106 real_t theta[4]; /*parameter estimations*/
107 real_t P[4][4]; /*covariance matrix*/
108 real_t lambda; /*forgetting factor [ 0.9 < l < 1 ]*/
109 real_t y1, y2; /*past values of y(t)*/
110 real_t u1, u2; /*past values of u(t)*/
111 };
112
113 using EstimationStepFn = void(*)(estimationParams&, real_t);
114 using ComputeParamsFn = void(*)(systemParams&,const estimationParams&, const real_t);
115
117 friend class pidController;
118 public:
122 static const uint32_t UNDEFINED;
123 protected:
125 EstimationStepFn estimationStep{ &pidAutoTuning::estimationStepN1 };
126 ComputeParamsFn computeParameters{ &pidAutoTuning::computeParamsN1 };
127 estimationParams estParams;
128 systemParams sysParams;
129
130 real_t mu{ 0.95_re }; /*variation attenuation*/
131 real_t speed{ 0.25_re }; /*final controller speed*/
132 uint32_t it{ UNDEFINED }; /*enable time*/
133
134
135 static bool resetEstimation( estimationParams &p,
136 const real_t r );
137 static bool getTimeConstant( real_t& tau,
138 const real_t abs_z,
139 const real_t dt );
140 static void computeParamsN1( systemParams& p,
141 const estimationParams& e,
142 const real_t dt );
143 static void computeParamsN2( systemParams& p,
144 const estimationParams& e,
145 const real_t dt );
146
147 static void estimationStepN1( estimationParams &p,
148 const real_t y );
149 static void estimationStepN2( estimationParams &p,
150 const real_t y );
151
152 static bool isValidValue( const real_t x ) noexcept;
154 void initialize( const pidGains current,
155 const real_t dt ) noexcept;
156 inline void enable( const uint32_t tEnable ) noexcept
157 {
158 it = ( 0UL == tEnable ) ? pidAutoTuning::UNDEFINED : tEnable;
159 }
160 inline bool isComplete( void ) const noexcept
161 {
162 return ( ( 0UL == it ) && ( it != pidAutoTuning::UNDEFINED ) );
163 }
164 inline void setMemoryFactor( const real_t lambda ) noexcept
165 {
166 estParams.lambda = lambda;
167 }
168 inline void setMomentum( const real_t Mu ) noexcept
169 {
170 mu = Mu;
171 }
172 inline void setEstimatedControllerSpeed( const real_t alpha ) noexcept
173 {
174 speed = alpha;
175 }
176 inline void setEstimatedControllerType( const pidType t ) noexcept
177 {
178 type = t;
179 }
180 static inline bool isValidParam( const real_t p ) noexcept
181 {
182 return ( p > 0.0_re ) && ( p <= 1.0_re );
183 }
184 bool step( const real_t u,
185 const real_t y,
186 const real_t dt ) noexcept;
188 public:
189 pidAutoTuning() = default;
190 pidGains getEstimates( void ) const noexcept;
191 bool enableOrder2Estimates( const bool en ) noexcept;
192 };
193
198 class pidController : public pidGains, public nState, private nonCopyable {
199 private:
200 real_t b, c, sat_Min, sat_Max, epsilon, kw, kt, D, u1, beta, uSat, gainBlend;
201 real_t dt{ 1.0_re };
202 pidGains nextGains;
203 real_t m, mInput;
204 const real_t *yr{ nullptr };
205 real_t alpha, gamma; /*MRAC additive controller parameters*/
206 nState m_state; /*MRAC additive controller state*/
207 nState b_state; /*Bumpless-transfer state*/
208 pidAutoTuning *adapt{ nullptr };
211 bool isInitialized{ false };
212 real_t error( real_t w, real_t y, real_t k = 1.0_re ) noexcept;
213 static real_t saturate( real_t x,
214 const real_t vMin,
215 const real_t vMax ) noexcept;
216 void adaptGains( const real_t u,
217 const real_t y ) noexcept;
218 public:
219 virtual ~pidController() {}
220 pidController() = default;
221
230 bool setup( const real_t kc,
231 const real_t ki,
232 const real_t kd,
233 const real_t dT ) noexcept;
234
241 bool setup( const pidGains &g,
242 const real_t dT ) noexcept
243 {
244 return setup( g.Kc, g.Ki, g.Kd, dT );
245 }
246
252 bool setDirection( const pidDirection d ) noexcept;
253
262 bool setParams( const real_t kc,
263 const real_t ti,
264 const real_t td ) noexcept;
265
273 bool setGains( const real_t kc,
274 const real_t ki,
275 const real_t kd ) noexcept;
276
282 bool setGains( const pidGains &g ) noexcept;
283
291 bool setExtraGains( const real_t Kw,
292 const real_t Kt ) noexcept;
293
300 bool setSaturation( const real_t Min,
301 const real_t Max ) noexcept;
302
308 bool setSeries( void ) noexcept;
309
315 bool setEpsilon( const real_t eps ) noexcept;
316
322 bool setDerivativeFilter( const real_t Beta ) noexcept;
323
330 bool setDerivativeFilterTimeConstant( const real_t Tf ) noexcept;
331
343 bool setMode( const pidMode Mode ) noexcept;
344
355 bool setReferenceWeighting( const real_t gb,
356 const real_t gc ) noexcept;
357
365 bool setManualInput( const real_t manualInput ) noexcept;
366
374 bool setModelReferenceControl( const real_t &modelRef,
375 const real_t Gamma = 0.5_re,
376 const real_t Alpha = 0.01_re ) noexcept;
377
384 bool removeModelReferenceControl( void ) noexcept;
385
396 real_t control( const real_t w,
397 const real_t y ) noexcept;
398
399
411 const real_t y )
412 {
413 return control( w, y );
414 }
415
422 bool bindAutoTuning( pidAutoTuning &at ) noexcept;
423
428 inline bool unbindAutoTuning( void ) noexcept
429 {
430 const bool retValue = nullptr != adapt;
431 adapt = nullptr;
432 return retValue;
433 }
434
445 bool enableAutoTuning( const uint32_t tEnable ) noexcept;
446
452 bool isAutoTuningComplete( void ) const noexcept;
453
462 bool setAutoTuningParameters( const real_t Mu,
463 const real_t Alpha,
464 const real_t lambda,
465 const real_t Tb = 3.0_re ) noexcept;
466
472 bool setAutoTuningControllerType( const pidType t ) noexcept;
473
478 bool reset( void ) noexcept;
479
484 inline pidGains getGains( void ) const noexcept
485 {
486 return { Kc, Ki, Kd };
487 }
488
494 explicit operator bool() const noexcept {
495 return isInitialized;
496 }
497 };
498
500}
501
502#endif /*QLIBS_PID*/
nState(const real_t x0=0.0_re, const real_t sn_1=0.0_re, const real_t sn_2=0.0_re) noexcept
Constructor for the state object.
Definition numa.hpp:71
A PID Auto-tuning object.
Definition pid.hpp:96
pidGains getEstimates(void) const noexcept
Definition pid.cpp:485
bool enableOrder2Estimates(const bool en) noexcept
static const uint32_t UNDEFINED
Constant to keep the auto-tuner enabled indefinitely.
Definition pid.hpp:122
friend class pidController
Definition pid.hpp:117
bool setup(const real_t kc, const real_t ki, const real_t kd, const real_t dT) noexcept
Setup and initialize the PID controller instance.
Definition pid.cpp:10
bool setEpsilon(const real_t eps) noexcept
Set the minimum value considered as error.
Definition pid.cpp:141
bool setManualInput(const real_t manualInput) noexcept
Set the PID manual input mode. This value will be used as the manual input when the controller it set...
Definition pid.cpp:203
bool setGains(const real_t kc, const real_t ki, const real_t kd) noexcept
Set/Change the PID controller gains.
Definition pid.cpp:61
bool isAutoTuningComplete(void) const noexcept
Verifies that the auto tuning process has finished with new gains set on the controller.
Definition pid.cpp:594
virtual ~pidController()
Definition pid.hpp:219
bool setExtraGains(const real_t Kw, const real_t Kt) noexcept
Set/Change extra PID controller gains.
Definition pid.cpp:93
pidGains getGains(void) const noexcept
Retrieve the current PID gains.
Definition pid.hpp:484
bool setSaturation(const real_t Min, const real_t Max) noexcept
Setup the output saturation for the PID controller.
Definition pid.cpp:107
bool enableAutoTuning(const uint32_t tEnable) noexcept
Set the number of time steps where the auto tuner algorithm will modify the controller gains.
Definition pid.cpp:582
bool setSeries(void) noexcept
Convert the controller gains to conform the series or interacting form.
Definition pid.cpp:121
bool unbindAutoTuning(void) noexcept
Unbind and disable the PID controller auto-tuning algorithm.
Definition pid.hpp:428
bool setDerivativeFilter(const real_t Beta) noexcept
Set the tuning parameter for the derivative filter.
Definition pid.cpp:153
bool setDerivativeFilterTimeConstant(const real_t Tf) noexcept
Set the time constant for the derivative filter.
Definition pid.cpp:165
bool setAutoTuningParameters(const real_t Mu, const real_t Alpha, const real_t lambda, const real_t Tb=3.0_re) noexcept
Change parameters of the auto-tuning algorithm.
Definition pid.cpp:605
real_t operator()(const real_t w, const real_t y)
Computes the control action for given PID controller instance.
Definition pid.hpp:410
bool setModelReferenceControl(const real_t &modelRef, const real_t Gamma=0.5_re, const real_t Alpha=0.01_re) noexcept
Enable the additive MRAC(Model Reference Adaptive Control) feature.
Definition pid.cpp:233
bool setReferenceWeighting(const real_t gb, const real_t gc) noexcept
Set the PID Reference(Set-Point) Weighting. This value is used in order to avoid the increase of the ...
Definition pid.cpp:189
bool setDirection(const pidDirection d) noexcept
Set the PID control action direction.
Definition pid.cpp:35
bool removeModelReferenceControl(void) noexcept
Removes the additive MRAC(Model Reference Adaptive Control) feature from the control loop....
Definition pid.cpp:250
bool reset(void) noexcept
Reset the internal PID controller calculations.
Definition pid.cpp:215
bool setAutoTuningControllerType(const pidType t) noexcept
Select the PID type to tune.
Definition pid.cpp:628
bool bindAutoTuning(pidAutoTuning &at) noexcept
Binds the specified instance to enable the PID controller auto tuning algorithm.
Definition pid.cpp:569
bool setMode(const pidMode Mode) noexcept
Change the controller operational mode. In pidMode::PID_AUTOMATIC, the computed output of the PID con...
Definition pid.cpp:177
bool setup(const pidGains &g, const real_t dT) noexcept
Setup and initialize the PID controller instance.
Definition pid.hpp:241
real_t control(const real_t w, const real_t y) noexcept
Computes the control action for given PID controller instance.
Definition pid.cpp:273
bool setParams(const real_t kc, const real_t ti, const real_t td) noexcept
Set/Change the PID controller gains by using the [Kc, Ti Td ] triplet.
Definition pid.cpp:47
type
An enum with the inference system types supported by qlibs::fis.
Definition fis.hpp:108
pidType
Enumeration class with the operational modes for the PID controller.
Definition pid.hpp:36
pidMode
Enumeration class with the operational modes for the PID controller.
Definition pid.hpp:28
pidDirection
Direction modes of the PID controller.
Definition pid.hpp:46
@ PID_TYPE_PID
Definition pid.hpp:40
@ PID_TYPE_PD
Definition pid.hpp:39
@ PID_TYPE_PI
Definition pid.hpp:38
@ PID_TYPE_P
Definition pid.hpp:37
@ PID_AUTOMATIC
Definition pid.hpp:29
@ PID_MANUAL
Definition pid.hpp:30
@ PID_BACKWARD
Definition pid.hpp:48
@ PID_FORWARD
Definition pid.hpp:47
The qLibs++ library namespace.
Definition mat.hpp:18
float real_t
A type to instantiate a real variable double-precision of 64-bits IEEE 754.
Definition qlibs_types.hpp:43
PID Gains structure.
Definition pid.hpp:58
real_t Kc
Definition pid.hpp:59
real_t Kd
Definition pid.hpp:61
real_t Ki
Definition pid.hpp:60