Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
ltisys.hpp
1
8
9#ifndef QLIBS_LTISYS
10#define QLIBS_LTISYS
11
12#include <include/qlibs_types.hpp>
13#include <include/tdl.hpp>
14#include <include/numa.hpp>
15
16
20namespace qlibs {
26
28 /*only for continuousSystem*/
29 #if !defined( LTISYS_EVAL_MODEL_CONTROLLABLE ) || !defined( LTISYS_EVAL_MODEL_OBSERVABLE )
30 #define LTISYS_EVAL_MODEL_CONTROLLABLE
31 #endif
33
42
46 template<size_t order>
47 using continuousStates = nState[ order ];
48
52 template<size_t order>
53 using discreteStates = real_t[ order ];
54
60 template<size_t order>
61 struct continuousTF {
63 real_t num[ order+1 ];
64 real_t den[ order+1 ];
65 continuousStates<order> states = {};
67
75 continuousTF( const real_t ( &numerator )[ order + 1 ], const real_t ( &denominator )[ order + 1 ] )
76 {
77 static_assert( order >= 1 , "Order should be greater than 0" );
78 for ( size_t i = 0; i <= order; ++i ) {
79 num[ i ] = numerator[ i ];
80 den[ i ] = denominator[ i ];
81 }
82 }
83 };
84
85
97 struct timeDelay {
100
105 constexpr explicit timeDelay(real_t v) : value(v) {}
106
112 constexpr size_t operator()(const real_t dt) const {
113 return static_cast<size_t>( ( value/dt ) + 0.5_re);
114 }
115
121 constexpr size_t operator[](const real_t dt) const {
122 return static_cast<size_t>( ( value/dt ) + 0.5_re);
123 }
124 };
125
137 constexpr timeDelay operator"" _td(long double v) {
138 return timeDelay(static_cast<real_t>(v));
139 }
140
154 constexpr size_t operator,(const timeDelay td, const real_t dt) {
155 return static_cast<size_t>( ( td.value/dt ) + 0.5_re );
156 }
157
167 constexpr size_t delayFromTime( const real_t Time, const real_t dt )
168 {
169 return static_cast<size_t>( ( Time/dt ) + 0.5_re );
170 }
171
181 constexpr size_t delayFromTime( const timeDelay Time, const real_t dt )
182 {
183 return static_cast<size_t>( ( Time.value/dt ) + 0.5_re );
184 }
185
187 class ITransportDelay {
188 public:
189 virtual ~ITransportDelay() = default;
190 virtual real_t delay(const real_t xInput) noexcept = 0;
191 virtual real_t operator()(const real_t xInput) noexcept = 0;
192 virtual size_t getNumberOfDelays() const noexcept = 0;
193 };
195
211 template<size_t numberOfDelays>
212 class transportDelay : public ITransportDelay {
213 private:
214 real_t buf[ numberOfDelays + 1 ];
215 tdl dl;
216 public:
217 virtual ~transportDelay() {}
223 transportDelay( const real_t initValue = 0.0_re )
224 {
225 static_assert( numberOfDelays >= 1 , "Delay taps should be greater than 0" );
226 dl.setup( buf, initValue);
227 }
228
234 real_t delay( const real_t xInput ) noexcept override
235 {
236 dl.insertSample( xInput );
237 return dl.getOldest();
238 }
239
245 real_t operator()( const real_t xInput ) noexcept override
246 {
247 return delay( xInput );
248 }
249
255 size_t getNumberOfDelays() const noexcept {
256 return numberOfDelays;
257 }
258 };
259
265 template<size_t delay>
267
268
275 template<size_t NB, size_t NA>
276 struct discreteTF {
278 real_t num[ NB ];
279 real_t den[ NA ];
280 discreteStates<(NA>NB)? NA:NB> states = {};
282
290 discreteTF( const real_t ( &numerator )[ NB ], const real_t ( &denominator)[ NA ] ) {
291 for ( size_t i = 0; i < NB; ++i ) {
292 num[ i ] = numerator[ i ];
293 }
294 for ( size_t i = 0; i < NA; ++i ) {
295 den[ i ] = denominator[ i ];
296 }
297 }
298 };
299
300
304 class ltisys : public tdl {
305 protected:
307 real_t *a{ nullptr };
308 real_t *b{ nullptr };
309 size_t n{ 0U };
310 size_t na{ 0U };
311 size_t nb{ 0U };
312 real_t b0{ 0.0_re };
313 real_t min{ -REAL_MAX };
314 real_t max{ +REAL_MAX };
315 void normalizeTransferFunction( real_t *num,
316 real_t *den,
317 size_t n_num,
318 size_t n_den );
319 real_t saturate( real_t y );
321 virtual real_t update( const real_t u ) = 0;
323 public:
324 virtual ~ltisys() {}
325 ltisys() = default;
326
345 real_t excite( real_t u ) noexcept;
346
366 return excite( u );
367 }
368
374 virtual bool isInitialized( void ) const = 0;
375
383 virtual bool setInitStates( const real_t *xi = nullptr ) = 0;
384
390 ltisysType getType( void ) const
391 {
392 return type;
393 }
394
404 bool setDelay( real_t * const w,
405 const size_t nD,
406 const real_t initVal = 0.0_re ) noexcept;
407
414 bool setSaturation( const real_t minV,
415 const real_t maxV ) noexcept;
416 };
417
423 class discreteSystem : public ltisys {
424 private:
425 real_t *xd{ nullptr };
426 real_t update( const real_t u ) override;
427 public:
428 virtual ~discreteSystem() {}
429
455 real_t *den,
456 real_t *x,
457 const size_t n_b,
458 const size_t n_a ) noexcept
459 {
460 (void)setup( num, den, x, n_b, n_a );
461 }
462
481 template<size_t NB, size_t NA>
482 discreteSystem( real_t (&num)[ NB ],
483 real_t (&den)[ NA ],
484 real_t *x ) noexcept
485 {
486 (void)setup( num, den, x, NB, NA );
487 }
488
494 template<size_t NB, size_t NA>
496 {
497 (void)setup( dtf.num, dtf.den, dtf.states, NB, NA );
498 }
499
525 bool setup( real_t *num,
526 real_t *den,
527 real_t *x,
528 const size_t n_b,
529 const size_t n_a ) noexcept;
530
550 template<size_t NB, size_t NA>
551 bool setup( real_t (&num)[ NB ],
552 real_t (&den)[ NA ],
553 real_t *x )
554 {
555 return setup( num, den, x, NB, NA );
556 }
557
564 template<size_t NB, size_t NA>
565 bool setup( discreteTF<NB, NA>& dtf ) noexcept
566 {
567 return setup( dtf.num, dtf.den, dtf.states, NB, NA );
568 }
569
575 bool isInitialized( void ) const override
576 {
577 return ( nullptr != xd ) && ( LTISYS_TYPE_DISCRETE == type );
578 }
579
588 bool setInitStates( const real_t *xi = nullptr ) override;
589
605 static real_t updateFIR( real_t *w,
606 const size_t wsize,
607 const real_t x,
608 const real_t * const c = nullptr );
609 };
610
616 class continuousSystem : public ltisys {
617 private:
618 real_t dt{ 1.0_re };
619 nState *xc{ nullptr };
620 real_t update( const real_t u ) override;
621 public:
622 virtual ~continuousSystem() {}
623
648 real_t *den,
649 nState *x,
650 const size_t nD,
651 const real_t dT ) noexcept
652 {
653 (void)setup( num, den, x, nD, dT );
654 }
655
662 template<size_t order>
664 {
665 (void)setup( ctf.num, ctf.den, ctf.states, order, dT );
666 }
667
688 template<size_t systemOrder>
689 continuousSystem( real_t (&num)[ systemOrder+1 ],
690 real_t (&den)[ systemOrder+1 ],
691 nState (&x)[ systemOrder ],
692 const real_t dT ) noexcept
693 {
694 (void)setup( num, den, x, systemOrder, dT );
695 }
696
721 bool setup( real_t *num,
722 real_t *den,
723 nState *x,
724 const size_t nD,
725 const real_t dT ) noexcept;
726
748 template<size_t systemOrder>
749 bool setup( real_t (&num)[ systemOrder+1 ],
750 real_t (&den)[ systemOrder+1 ],
751 nState (&x)[ systemOrder ],
752 const real_t dT ) noexcept
753 {
754 return setup( num, den, x, systemOrder, dT );
755 }
756
764 template<size_t order>
765 bool setup( continuousTF<order>& ctf, const real_t dT )
766 {
767 return setup( ctf.num, ctf.den, ctf.states, order, dT );
768 }
769
775 bool isInitialized( void ) const override
776 {
777 return ( nullptr != xc ) && ( LTISYS_TYPE_CONTINUOUS == type );
778 }
779
788 bool setInitStates( const real_t *xi = nullptr ) override;
789
802 bool setIntegrationMethod( integrationMethod m );
803 };
804
805
806 using customProcessModel = real_t(*)(real_t, void*);
807
808
818 private:
819 ltisys *model{nullptr};
820 customProcessModel modelAlternate{ nullptr };
821 ITransportDelay *modelDelay;
822 ltisys *filter{ nullptr };
823 real_t yp_hat;
824 void *alternateData{ nullptr };
825 public:
826 virtual ~smithPredictor() {}
840 ITransportDelay& mDelay,
841 const real_t initialCondition = 0.0_re )
842 : model(&modelTf), modelDelay(&mDelay), yp_hat(initialCondition) {}
843
856 ITransportDelay& mDelay,
857 const real_t initialCondition = 0.0_re )
858 : modelAlternate(modelCustom), modelDelay(&mDelay), yp_hat(initialCondition) {}
859
875 ITransportDelay& mDelay,
876 ltisys& filterTf,
877 const real_t initialCondition = 0.0_re )
878 : model(&modelTf), modelDelay(&mDelay), filter(&filterTf), yp_hat(initialCondition) {}
879
896 ITransportDelay& mDelay,
897 ltisys& filterTf,
898 const real_t initialCondition = 0.0_re )
899 : modelAlternate(modelCustom), modelDelay(&mDelay), filter(&filterTf), yp_hat(initialCondition) {}
900
916 bool updatePrediction( const real_t ut,
917 const real_t yt ) noexcept;
918
923 real_t getPrediction() const noexcept {
924 return yp_hat;
925 }
926
940 bool setFilter( ltisys& filterTf ) noexcept {
941 bool retValue = filter != &filterTf;
942 if ( retValue ) {
943 filter = &filterTf;
944 }
945 return retValue;
946 }
947
956 bool setModelData( void* data ) noexcept {
957 bool retValue = data != alternateData;
958 if ( retValue ) {
959 alternateData = data;
960 }
961 return retValue;
962 }
963 };
964
966}
967
968#endif /*QLIBS_LTISYS*/
virtual ~continuousSystem()
Definition ltisys.hpp:622
continuousSystem(real_t(&num)[systemOrder+1], real_t(&den)[systemOrder+1], nState(&x)[systemOrder], const real_t dT) noexcept
Constructor for an instance of a LTI continuous system.
Definition ltisys.hpp:689
continuousSystem(real_t *num, real_t *den, nState *x, const size_t nD, const real_t dT) noexcept
Constructor for an instance of a LTI continuous system.
Definition ltisys.hpp:647
bool setup(real_t(&num)[systemOrder+1], real_t(&den)[systemOrder+1], nState(&x)[systemOrder], const real_t dT) noexcept
Setup and initialize an instance of a LTI continuous system.
Definition ltisys.hpp:749
continuousSystem(continuousTF< order > &ctf, const real_t dT)
Constructor for an instance of a LTI continuous system from a transfer function definition.
Definition ltisys.hpp:663
bool isInitialized(void) const override
Check if the LTI continuous system is initialized.
Definition ltisys.hpp:775
bool setup(real_t *num, real_t *den, nState *x, const size_t nD, const real_t dT) noexcept
Setup and initialize an instance of a LTI continuous system.
Definition ltisys.cpp:156
bool setup(continuousTF< order > &ctf, const real_t dT)
Setup and initialize an instance of a LTI continuous system from a transfer function definition.
Definition ltisys.hpp:765
virtual ~discreteSystem()
Definition ltisys.hpp:428
bool setup(real_t *num, real_t *den, real_t *x, const size_t n_b, const size_t n_a) noexcept
Setup and initialize an instance of the discrete LTI system.
Definition ltisys.cpp:121
discreteSystem(discreteTF< NB, NA > &dtf) noexcept
Constructor for a the discrete LTI system from a transfer function definition.
Definition ltisys.hpp:495
bool setup(discreteTF< NB, NA > &dtf) noexcept
Setup and initialize an instance of the discrete LTI system from a transfer function definition.
Definition ltisys.hpp:565
bool setup(real_t(&num)[NB], real_t(&den)[NA], real_t *x)
Setup and initialize an instance of the discrete LTI system.
Definition ltisys.hpp:551
discreteSystem(real_t *num, real_t *den, real_t *x, const size_t n_b, const size_t n_a) noexcept
Constructor for a the discrete LTI system.
Definition ltisys.hpp:454
discreteSystem(real_t(&num)[NB], real_t(&den)[NA], real_t *x) noexcept
Constructor for a the discrete LTI system.
Definition ltisys.hpp:482
bool isInitialized(void) const override
Check if the LTI discrete system is initialized.
Definition ltisys.hpp:575
A LTI system base class.
Definition ltisys.hpp:304
virtual ~ltisys()
Definition ltisys.hpp:324
virtual bool isInitialized(void) const =0
Check if the LTI system is initialized.
ltisysType getType(void) const
Get the LTI system type.
Definition ltisys.hpp:390
real_t excite(real_t u) noexcept
Drives the LTI system recursively using the provided input sample.
Definition ltisys.cpp:58
virtual bool setInitStates(const real_t *xi=nullptr)=0
Set the initial states for the given system.
ltisys()=default
real_t operator()(const real_t u)
Drives the LTI system recursively using the provided input sample.
Definition ltisys.hpp:365
A numerical state object.
Definition numa.hpp:50
smithPredictor(ltisys &modelTf, ITransportDelay &mDelay, ltisys &filterTf, const real_t initialCondition=0.0_re)
Constructs a Smith Predictor with a plant model, delay model, and optional initial output estimate.
Definition ltisys.hpp:874
bool setModelData(void *data) noexcept
Sets the model data when alternate when a custom delay-free plant model is used.
Definition ltisys.hpp:956
smithPredictor(customProcessModel modelCustom, ITransportDelay &mDelay, ltisys &filterTf, const real_t initialCondition=0.0_re)
Constructs a Smith Predictor with a plant model, delay model, and optional initial output estimate.
Definition ltisys.hpp:895
smithPredictor(ltisys &modelTf, ITransportDelay &mDelay, const real_t initialCondition=0.0_re)
Constructs a Smith Predictor with a plant model, delay model, and optional initial output estimate.
Definition ltisys.hpp:839
real_t getPrediction() const noexcept
Retrieves the current delay-free predicted output of the system.
Definition ltisys.hpp:923
virtual ~smithPredictor()
Definition ltisys.hpp:826
bool setFilter(ltisys &filterTf) noexcept
Sets an optional filter for the internal Smith Predictor model.
Definition ltisys.hpp:940
smithPredictor(customProcessModel modelCustom, ITransportDelay &mDelay, const real_t initialCondition=0.0_re)
Constructs a Smith Predictor with a plant model, delay model, and optional initial output estimate.
Definition ltisys.hpp:855
A Tapped Delay Line (TDL) object.
Definition tdl.hpp:32
tdl()=default
Delays the input by a specified amount of time. You can use this class to simulate a time delay.
Definition ltisys.hpp:212
virtual ~transportDelay()
Definition ltisys.hpp:217
transportDelay(const real_t initValue=0.0_re)
Constructor for the transportDelay class.
Definition ltisys.hpp:223
real_t delay(const real_t xInput) noexcept override
Delays the input by a specified amount of time.
Definition ltisys.hpp:234
size_t getNumberOfDelays() const noexcept
Returns the number of delay steps configured for this instance.
Definition ltisys.hpp:255
real_t operator()(const real_t xInput) noexcept override
Delays the input by a specified amount of time.
Definition ltisys.hpp:245
ltisysType
All the possible natures of a LTI system.
Definition ltisys.hpp:37
transportDelay< delay > discreteDelay
Delays the input by a specified amount of samples. You can use this class to simulate a discrete time...
Definition ltisys.hpp:266
constexpr size_t delayFromTime(const real_t Time, const real_t dt)
Computes the number of discrete delays required for a specified amount of time using a defined time-s...
Definition ltisys.hpp:167
nState[order] continuousStates
Type to specify continuous states.
Definition ltisys.hpp:47
constexpr size_t operator,(const timeDelay td, const real_t dt)
Computes the delay in discrete steps using the comma operator.
Definition ltisys.hpp:154
real_t[order] discreteStates
Type to specify discrete states.
Definition ltisys.hpp:53
real_t(*)(real_t, void *) customProcessModel
Definition ltisys.hpp:806
@ LTISYS_TYPE_CONTINUOUS
Definition ltisys.hpp:39
@ LTISYS_TYPE_DISCRETE
Definition ltisys.hpp:40
@ LTISYS_TYPE_UNKNOWN
Definition ltisys.hpp:38
integrationMethod
An enum with all the available integration methods.
Definition numa.hpp:28
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
Continuous transfer function for easy LTI system definition.
Definition ltisys.hpp:61
continuousTF(const real_t(&numerator)[order+1], const real_t(&denominator)[order+1])
Constructor for the continuousTF class.
Definition ltisys.hpp:75
Discrete transfer function for easy LTI system definition.
Definition ltisys.hpp:276
discreteTF(const real_t(&numerator)[NB], const real_t(&denominator)[NA])
Constructor for the discreteTF class.
Definition ltisys.hpp:290
Represents a time delay value for use in transportDelay constructor.
Definition ltisys.hpp:97
constexpr timeDelay(real_t v)
Construct a new timeDelay object.
Definition ltisys.hpp:105
real_t value
Delay duration in seconds.
Definition ltisys.hpp:99
constexpr size_t operator()(const real_t dt) const
Computes the number of discrete steps equivalent to the delay.
Definition ltisys.hpp:112
constexpr size_t operator[](const real_t dt) const
Alternate syntax to compute delay in steps using indexing operator.
Definition ltisys.hpp:121