Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
pid.hpp
1
10#ifndef QLIBS_PID
11#define QLIBS_PID
12
13#include <include/qlibs_types.hpp>
14#include <include/numa.hpp>
15
19namespace qlibs {
28 enum class pidMode {
31 };
32
36 enum class pidType {
41 };
42
46 enum class pidDirection {
49 };
50
52 class pidController;
63
70 friend class pidController;
71 public:
75 static const uint32_t UNDEFINED;
76 protected:
78 real_t p00{ 1.0_re }; /*covariance value*/
79 real_t p01{ 0.0_re }; /*covariance value*/
80 real_t p10{ 0.0_re }; /*covariance value*/
81 real_t p11{ 1.0_re }; /*covariance value*/
82 real_t b1{ 0.1_re }; /*estimation value*/
83 real_t a1{ 0.9_re }; /*estimation value*/
84 real_t uk{ 0.0_re }; /*process input*/
85 real_t yk{ 0.0_re }; /*process output*/
86 real_t l{ 0.9898_re }; /*memory factor [ 0.9 < l < 1 ]*/
87 real_t k{ 1.0_re }; /*process static gain*/
88 real_t tao{ 1.0_re }; /*process time constant*/
89 real_t mu{ 0.95_re }; /*variation attenuation*/
90 real_t speed{ 0.25_re }; /*final controller speed*/
91 uint32_t it{ UNDEFINED }; /*enable time*/
92 static bool isValidValue( const real_t x ) noexcept;
94 void initialize( const pidGains current,
95 const real_t dt ) noexcept;
96 inline void enable( const uint32_t tEnable ) noexcept
97 {
98 it = ( 0UL == tEnable ) ? pidAutoTuning::UNDEFINED : tEnable;
99 }
100 inline bool isComplete( void ) const noexcept
101 {
102 return ( ( 0UL == it ) && ( it != pidAutoTuning::UNDEFINED ) );
103 }
104 inline void setMemoryFactor( const real_t lambda ) noexcept
105 {
106 l = lambda;
107 }
108 inline void setMomentum( const real_t Mu ) noexcept
109 {
110 mu = Mu;
111 }
112 inline void setEstimatedControllerSpeed( const real_t alpha ) noexcept
113 {
114 speed = alpha;
115 }
116 inline void setEstimatedControllerType( const pidType t ) noexcept
117 {
118 type = t;
119 }
120 static inline bool isValidParam( const real_t p ) noexcept
121 {
122 return ( p > 0.0_re ) && ( p <= 1.0_re );
123 }
124 bool step( const real_t u,
125 const real_t y,
126 const real_t dt ) noexcept;
128 public:
129 pidAutoTuning() = default;
130 pidGains getEstimates( void ) const noexcept;
131 };
132
137 class pidController : public pidGains, public nState, private nonCopyable {
138 private:
139 real_t b, c, sat_Min, sat_Max, epsilon, kw, kt, D, u1, beta, uSat;
140 real_t dt{ 1.0_re };
141 real_t m, mInput;
142 const real_t *yr{ nullptr };
143 real_t alpha, gamma; /*MRAC additive controller parameters*/
144 nState m_state; /*MRAC additive controller state*/
145 nState b_state; /*Bumpless-transfer state*/
146 pidAutoTuning *adapt{ nullptr };
149 bool isInitialized{ false };
150 real_t error( real_t w, real_t y, real_t k = 1.0_re ) noexcept;
151 static real_t saturate( real_t x,
152 const real_t vMin,
153 const real_t vMax ) noexcept;
154 void adaptGains( const real_t u,
155 const real_t y ) noexcept;
156 public:
157 virtual ~pidController() {}
158 pidController() = default;
159
168 bool setup( const real_t kc,
169 const real_t ki,
170 const real_t kd,
171 const real_t dT ) noexcept;
172
179 bool setup( const pidGains &g,
180 const real_t dT ) noexcept
181 {
182 return setup( g.Kc, g.Ki, g.Kd, dT );
183 }
184
190 bool setDirection( const pidDirection d ) noexcept;
191
200 bool setParams( const real_t kc,
201 const real_t ti,
202 const real_t td ) noexcept;
203
211 bool setGains( const real_t kc,
212 const real_t ki,
213 const real_t kd ) noexcept;
214
220 bool setGains( const pidGains &g ) noexcept;
221
229 bool setExtraGains( const real_t Kw,
230 const real_t Kt ) noexcept;
231
238 bool setSaturation( const real_t Min,
239 const real_t Max ) noexcept;
240
246 bool setSeries( void ) noexcept;
247
253 bool setEpsilon( const real_t eps ) noexcept;
254
260 bool setDerivativeFilter( const real_t Beta ) noexcept;
261
268 bool setDerivativeFilterTimeConstant( const real_t Tf ) noexcept;
269
281 bool setMode( const pidMode Mode ) noexcept;
282
293 bool setReferenceWeighting( const real_t gb,
294 const real_t gc ) noexcept;
295
303 bool setManualInput( const real_t manualInput ) noexcept;
304
312 bool setModelReferenceControl( const real_t &modelRef,
313 const real_t Gamma = 0.5_re,
314 const real_t Alpha = 0.01_re ) noexcept;
315
321 bool removeModelReferenceControl( void ) noexcept;
322
333 real_t control( const real_t w,
334 const real_t y ) noexcept;
335
342 bool bindAutoTuning( pidAutoTuning &at ) noexcept;
343
348 inline bool unbindAutoTuning( void ) noexcept
349 {
350 const bool retValue = nullptr != adapt;
351 adapt = nullptr;
352 return retValue;
353 }
354
365 bool enableAutoTuning( const uint32_t tEnable ) noexcept;
366
372 bool isAutoTuningComplete( void ) const noexcept;
373
381 bool setAutoTuningParameters( const real_t Mu,
382 const real_t Alpha,
383 const real_t lambda ) noexcept;
384
390 bool setAutoTuningControllerType( const pidType t ) noexcept;
391
396 bool reset( void ) noexcept;
397
402 inline pidGains getGains( void ) const noexcept
403 {
404 return { Kc, Ki, Kd };
405 }
406 };
407
409}
410
411#endif /*QLIBS_PID*/
A numerical state object.
Definition numa.hpp:50
A PID Auto-tuning object.
Definition pid.hpp:69
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:75
A PID controller object.
Definition pid.hpp:137
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:157
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:402
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:348
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
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:179
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
The qLibs++ library namespace.
Definition fp16.cpp:4
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