12#include <include/qlibs_types.hpp>
13#include <include/tdl.hpp>
14#include <include/numa.hpp>
29 #if !defined( LTISYS_EVAL_MODEL_CONTROLLABLE ) || !defined( LTISYS_EVAL_MODEL_OBSERVABLE )
30 #define LTISYS_EVAL_MODEL_CONTROLLABLE
46 template<
size_t order>
52 template<
size_t order>
60 template<
size_t order>
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 ];
113 return static_cast<size_t>( (
value/dt ) + 0.5_re);
122 return static_cast<size_t>( (
value/dt ) + 0.5_re);
155 return static_cast<size_t>( ( td.
value/dt ) + 0.5_re );
169 return static_cast<size_t>( ( Time/dt ) + 0.5_re );
183 return static_cast<size_t>( ( Time.
value/dt ) + 0.5_re );
187 class ITransportDelay {
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;
211 template<
size_t numberOfDelays>
214 real_t buf[ numberOfDelays + 1 ];
225 static_assert( numberOfDelays >= 1 ,
"Delay taps should be greater than 0" );
226 dl.setup( buf, initValue);
236 dl.insertSample( xInput );
237 return dl.getOldest();
247 return delay( xInput );
256 return numberOfDelays;
265 template<
size_t delay>
275 template<
size_t NB,
size_t NA>
291 for (
size_t i = 0; i < NB; ++i ) {
292 num[ i ] = numerator[ i ];
294 for (
size_t i = 0; i < NA; ++i ) {
295 den[ i ] = denominator[ i ];
315 void normalizeTransferFunction(
real_t *num,
382 explicit operator bool() const noexcept {
414 bool setDelay(
real_t *
const w,
416 const real_t initVal = 0.0_re )
noexcept;
424 bool setSaturation(
const real_t minV,
425 const real_t maxV )
noexcept;
468 const size_t n_a )
noexcept
470 (void)
setup( num, den, x, n_b, n_a );
491 template<
size_t NB,
size_t NA>
496 (void)
setup( num, den, x, NB, NA );
504 template<
size_t NB,
size_t NA>
507 (void)
setup( dtf.num, dtf.den, dtf.states, NB, NA );
539 const size_t n_a )
noexcept;
560 template<
size_t NB,
size_t NA>
565 return setup( num, den, x, NB, NA );
574 template<
size_t NB,
size_t NA>
577 return setup( dtf.num, dtf.den, dtf.states, NB, NA );
595 explicit operator bool() const noexcept {
607 bool setInitStates(
const real_t *xi =
nullptr )
override;
627 const real_t *
const c =
nullptr );
670 const real_t dT )
noexcept
672 (void)
setup( num, den, x, nD, dT );
681 template<
size_t order>
684 (void)
setup( ctf.num, ctf.den, ctf.states, order, dT );
707 template<
size_t systemOrder>
709 real_t (&den)[ systemOrder+1 ],
710 nState (&x)[ systemOrder ],
711 const real_t dT )
noexcept
713 (void)
setup( num, den, x, systemOrder, dT );
744 const real_t dT )
noexcept;
767 template<
size_t systemOrder>
769 real_t (&den)[ systemOrder+1 ],
770 nState (&x)[ systemOrder ],
771 const real_t dT )
noexcept
773 return setup( num, den, x, systemOrder, dT );
783 template<
size_t order>
786 return setup( ctf.num, ctf.den, ctf.states, order, dT );
807 bool setInitStates(
const real_t *xi =
nullptr )
override;
840 ITransportDelay *modelDelay;
841 ltisys *filter{
nullptr };
843 void *alternateData{
nullptr };
859 ITransportDelay& mDelay,
860 const real_t initialCondition = 0.0_re )
861 : model(&modelTf), modelDelay(&mDelay), yp_hat(initialCondition) {}
875 ITransportDelay& mDelay,
876 const real_t initialCondition = 0.0_re )
877 : modelAlternate(modelCustom), modelDelay(&mDelay), yp_hat(initialCondition) {}
894 ITransportDelay& mDelay,
896 const real_t initialCondition = 0.0_re )
897 : model(&modelTf), modelDelay(&mDelay), filter(&filterTf), yp_hat(initialCondition) {}
915 ITransportDelay& mDelay,
917 const real_t initialCondition = 0.0_re )
918 : modelAlternate(modelCustom), modelDelay(&mDelay), filter(&filterTf), yp_hat(initialCondition) {}
935 bool updatePrediction(
const real_t ut,
936 const real_t yt )
noexcept;
960 bool retValue = filter != &filterTf;
976 bool retValue = data != alternateData;
978 alternateData = data;
virtual ~continuousSystem()
Definition ltisys.hpp:641
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:708
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:666
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:768
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:682
bool isInitialized(void) const noexcept override
Check if the LTI continuous system is initialized.
Definition ltisys.hpp:794
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:784
virtual ~discreteSystem()
Definition ltisys.hpp:438
bool isInitialized(void) const noexcept override
Check if the LTI discrete system is initialized.
Definition ltisys.hpp:585
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:505
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:575
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:561
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:464
discreteSystem(real_t(&num)[NB], real_t(&den)[NA], real_t *x) noexcept
Constructor for a the discrete LTI system.
Definition ltisys.hpp:492
A LTI system base class.
Definition ltisys.hpp:304
virtual ~ltisys()
Definition ltisys.hpp:324
ltisysType getType(void) const
Get the LTI system type.
Definition ltisys.hpp:400
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.
virtual bool isInitialized(void) const noexcept=0
Check if the LTI system is initialized.
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:893
bool setModelData(void *data) noexcept
Sets the model data when alternate when a custom delay-free plant model is used.
Definition ltisys.hpp:975
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:914
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:858
real_t getPrediction() const noexcept
Retrieves the current delay-free predicted output of the system.
Definition ltisys.hpp:942
virtual ~smithPredictor()
Definition ltisys.hpp:845
bool setFilter(ltisys &filterTf) noexcept
Sets an optional filter for the internal Smith Predictor model.
Definition ltisys.hpp:959
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:874
A Tapped Delay Line (TDL) object.
Definition tdl.hpp:32
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:825
@ 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