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
96 friend class pidController;
97 public:
101 static const uint32_t UNDEFINED;
102 protected:
104 real_t p00{ 1.0_re }; /*covariance value*/
105 real_t p01{ 0.0_re }; /*covariance value*/
106 real_t p10{ 0.0_re }; /*covariance value*/
107 real_t p11{ 1.0_re }; /*covariance value*/
108 real_t b1{ 0.1_re }; /*estimation value*/
109 real_t a1{ 0.9_re }; /*estimation value*/
110 real_t uk{ 0.0_re }; /*process input*/
111 real_t yk{ 0.0_re }; /*process output*/
112 real_t l{ 0.9898_re }; /*memory factor [ 0.9 < l < 1 ]*/
113 real_t k{ 1.0_re }; /*process static gain*/
114 real_t tao{ 1.0_re }; /*process time constant*/
115 real_t mu{ 0.95_re }; /*variation attenuation*/
116 real_t speed{ 0.25_re }; /*final controller speed*/
117 uint32_t it{ UNDEFINED }; /*enable time*/
118 static bool isValidValue( const real_t x ) noexcept;
120 void initialize( const pidGains current,
121 const real_t dt ) noexcept;
122 inline void enable( const uint32_t tEnable ) noexcept
123 {
124 it = ( 0UL == tEnable ) ? pidAutoTuning::UNDEFINED : tEnable;
125 }
126 inline bool isComplete( void ) const noexcept
127 {
128 return ( ( 0UL == it ) && ( it != pidAutoTuning::UNDEFINED ) );
129 }
130 inline void setMemoryFactor( const real_t lambda ) noexcept
131 {
132 l = lambda;
133 }
134 inline void setMomentum( const real_t Mu ) noexcept
135 {
136 mu = Mu;
137 }
138 inline void setEstimatedControllerSpeed( const real_t alpha ) noexcept
139 {
140 speed = alpha;
141 }
142 inline void setEstimatedControllerType( const pidType t ) noexcept
143 {
144 type = t;
145 }
146 static inline bool isValidParam( const real_t p ) noexcept
147 {
148 return ( p > 0.0_re ) && ( p <= 1.0_re );
149 }
150 bool step( const real_t u,
151 const real_t y,
152 const real_t dt ) noexcept;
154 public:
155 pidAutoTuning() = default;
156 pidGains getEstimates( void ) const noexcept;
157 };
158
163 class pidController : public pidGains, public nState, private nonCopyable {
164 private:
165 real_t b, c, sat_Min, sat_Max, epsilon, kw, kt, D, u1, beta, uSat;
166 real_t dt{ 1.0_re };
167 real_t m, mInput;
168 const real_t *yr{ nullptr };
169 real_t alpha, gamma; /*MRAC additive controller parameters*/
170 nState m_state; /*MRAC additive controller state*/
171 nState b_state; /*Bumpless-transfer state*/
172 pidAutoTuning *adapt{ nullptr };
175 bool isInitialized{ false };
176 real_t error( real_t w, real_t y, real_t k = 1.0_re ) noexcept;
177 static real_t saturate( real_t x,
178 const real_t vMin,
179 const real_t vMax ) noexcept;
180 void adaptGains( const real_t u,
181 const real_t y ) noexcept;
182 public:
183 virtual ~pidController() {}
184 pidController() = default;
185
194 bool setup( const real_t kc,
195 const real_t ki,
196 const real_t kd,
197 const real_t dT ) noexcept;
198
205 bool setup( const pidGains &g,
206 const real_t dT ) noexcept
207 {
208 return setup( g.Kc, g.Ki, g.Kd, dT );
209 }
210
216 bool setDirection( const pidDirection d ) noexcept;
217
226 bool setParams( const real_t kc,
227 const real_t ti,
228 const real_t td ) noexcept;
229
237 bool setGains( const real_t kc,
238 const real_t ki,
239 const real_t kd ) noexcept;
240
246 bool setGains( const pidGains &g ) noexcept;
247
255 bool setExtraGains( const real_t Kw,
256 const real_t Kt ) noexcept;
257
264 bool setSaturation( const real_t Min,
265 const real_t Max ) noexcept;
266
272 bool setSeries( void ) noexcept;
273
279 bool setEpsilon( const real_t eps ) noexcept;
280
286 bool setDerivativeFilter( const real_t Beta ) noexcept;
287
294 bool setDerivativeFilterTimeConstant( const real_t Tf ) noexcept;
295
307 bool setMode( const pidMode Mode ) noexcept;
308
319 bool setReferenceWeighting( const real_t gb,
320 const real_t gc ) noexcept;
321
329 bool setManualInput( const real_t manualInput ) noexcept;
330
338 bool setModelReferenceControl( const real_t &modelRef,
339 const real_t Gamma = 0.5_re,
340 const real_t Alpha = 0.01_re ) noexcept;
341
347 bool removeModelReferenceControl( void ) noexcept;
348
359 real_t control( const real_t w,
360 const real_t y ) noexcept;
361
362
374 const real_t y )
375 {
376 return control( w, y );
377 }
378
385 bool bindAutoTuning( pidAutoTuning &at ) noexcept;
386
391 inline bool unbindAutoTuning( void ) noexcept
392 {
393 const bool retValue = nullptr != adapt;
394 adapt = nullptr;
395 return retValue;
396 }
397
408 bool enableAutoTuning( const uint32_t tEnable ) noexcept;
409
415 bool isAutoTuningComplete( void ) const noexcept;
416
424 bool setAutoTuningParameters( const real_t Mu,
425 const real_t Alpha,
426 const real_t lambda ) noexcept;
427
433 bool setAutoTuningControllerType( const pidType t ) noexcept;
434
439 bool reset( void ) noexcept;
440
445 inline pidGains getGains( void ) const noexcept
446 {
447 return { Kc, Ki, Kd };
448 }
449 };
450
452}
453
454#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:69
A PID Auto-tuning object.
Definition pid.hpp:95
pidGains getEstimates(void) const noexcept
Definition pid.cpp:367
static const uint32_t UNDEFINED
Constant to keep the auto-tuner enabled indefinitely.
Definition pid.hpp:101
friend class pidController
Definition pid.hpp:96
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:140
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:202
bool setAutoTuningParameters(const real_t Mu, const real_t Alpha, const real_t lambda) noexcept
Change parameters of the auto-tuning algorithm.
Definition pid.cpp:469
bool setGains(const real_t kc, const real_t ki, const real_t kd) noexcept
Set/Change the PID controller gains.
Definition pid.cpp:63
bool isAutoTuningComplete(void) const noexcept
Verifies that the auto tuning process has finished with new gains set on the controller.
Definition pid.cpp:458
virtual ~pidController()
Definition pid.hpp:183
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:445
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:446
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:391
bool setDerivativeFilter(const real_t Beta) noexcept
Set the tuning parameter for the derivative filter.
Definition pid.cpp:152
bool setDerivativeFilterTimeConstant(const real_t Tf) noexcept
Set the time constant for the derivative filter.
Definition pid.cpp:164
real_t operator()(const real_t w, const real_t y)
Computes the control action for given PID controller instance.
Definition pid.hpp:373
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:232
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:188
bool setDirection(const pidDirection d) noexcept
Set the PID control action direction.
Definition pid.cpp:35
bool removeModelReferenceControl(void) noexcept
Removes the Enable the additive MRAC(Model Reference Adaptive Control) feature from the control loop.
Definition pid.cpp:249
bool reset(void) noexcept
Reset the internal PID controller calculations.
Definition pid.cpp:214
bool setAutoTuningControllerType(const pidType t) noexcept
Select the PID type to tune.
Definition pid.cpp:487
bool bindAutoTuning(pidAutoTuning &at) noexcept
Binds the specified instance to enable the PID controller auto tuning algorithm.
Definition pid.cpp:433
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:176
bool setup(const pidGains &g, const real_t dT) noexcept
Setup and initialize the PID controller instance.
Definition pid.hpp:205
real_t control(const real_t w, const real_t y) noexcept
Computes the control action for given PID controller instance.
Definition pid.cpp:272
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