|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if defined(__GNUC__) || defined(__clang__) |
| 4 | +#pragma GCC optimize("O3", "fast-math") |
| 5 | +#endif |
| 6 | + |
| 7 | +#include "numerical/controllers/implementations/Lqr.hpp" |
| 8 | +#include "numerical/math/CompilerOptimizations.hpp" |
| 9 | +#include "numerical/math/LinearTimeInvariant.hpp" |
| 10 | +#include "numerical/math/Matrix.hpp" |
| 11 | + |
| 12 | +namespace controllers |
| 13 | +{ |
| 14 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 15 | + class IntegralStateFeedbackLqi |
| 16 | + { |
| 17 | + static_assert(std::is_floating_point_v<T>, "IntegralStateFeedbackLqi supports floating-point types"); |
| 18 | + |
| 19 | + static constexpr std::size_t AugmentedSize = StateSize + OutputSize; |
| 20 | + |
| 21 | + public: |
| 22 | + using StateVector = math::Vector<T, StateSize>; |
| 23 | + using InputVector = math::Vector<T, InputSize>; |
| 24 | + using OutputVector = math::Vector<T, OutputSize>; |
| 25 | + using GainStateMatrix = math::Matrix<T, InputSize, StateSize>; |
| 26 | + using GainIntegralMatrix = math::Matrix<T, InputSize, OutputSize>; |
| 27 | + using IntegralVector = math::Vector<T, OutputSize>; |
| 28 | + |
| 29 | + IntegralStateFeedbackLqi(const GainStateMatrix& kx, const GainIntegralMatrix& ki, T ts); |
| 30 | + |
| 31 | + IntegralStateFeedbackLqi( |
| 32 | + const math::LinearTimeInvariant<T, StateSize, InputSize, OutputSize>& plant, |
| 33 | + const math::SquareMatrix<T, AugmentedSize>& Q, |
| 34 | + const math::SquareMatrix<T, InputSize>& R, |
| 35 | + T ts); |
| 36 | + |
| 37 | + OPTIMIZE_FOR_SPEED InputVector ComputeControl( |
| 38 | + const StateVector& x, |
| 39 | + const OutputVector& reference, |
| 40 | + const OutputVector& measured); |
| 41 | + |
| 42 | + void Reset(); |
| 43 | + |
| 44 | + [[nodiscard]] const GainStateMatrix& GetGainState() const; |
| 45 | + [[nodiscard]] const GainIntegralMatrix& GetGainIntegral() const; |
| 46 | + |
| 47 | + private: |
| 48 | + GainStateMatrix gainState{}; |
| 49 | + GainIntegralMatrix gainIntegral{}; |
| 50 | + IntegralVector integral{}; |
| 51 | + T sampleTime{}; |
| 52 | + }; |
| 53 | + |
| 54 | + // Implementation // |
| 55 | + |
| 56 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 57 | + IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::IntegralStateFeedbackLqi( |
| 58 | + const GainStateMatrix& kx, const GainIntegralMatrix& ki, T ts) |
| 59 | + : gainState{ kx } |
| 60 | + , gainIntegral{ ki } |
| 61 | + , sampleTime{ ts } |
| 62 | + {} |
| 63 | + |
| 64 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 65 | + IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::IntegralStateFeedbackLqi( |
| 66 | + const math::LinearTimeInvariant<T, StateSize, InputSize, OutputSize>& plant, |
| 67 | + const math::SquareMatrix<T, AugmentedSize>& Q, |
| 68 | + const math::SquareMatrix<T, InputSize>& R, |
| 69 | + T ts) |
| 70 | + : sampleTime{ ts } |
| 71 | + { |
| 72 | + math::SquareMatrix<T, AugmentedSize> Aa{}; |
| 73 | + math::Matrix<T, AugmentedSize, InputSize> Ba{}; |
| 74 | + |
| 75 | + Aa.SetBlock(plant.A, 0, 0); |
| 76 | + Aa.SetBlock(plant.C * T(-ts), StateSize, 0); |
| 77 | + for (std::size_t r = 0; r < OutputSize; ++r) |
| 78 | + Aa.at(StateSize + r, StateSize + r) = T(1); |
| 79 | + Ba.SetBlock(plant.B, 0, 0); |
| 80 | + |
| 81 | + Lqr<T, AugmentedSize, InputSize> lqr{ Aa, Ba, Q, R }; |
| 82 | + const auto& Ka = lqr.GetGain(); |
| 83 | + |
| 84 | + gainState = Ka.template GetBlock<InputSize, StateSize>(0, 0); |
| 85 | + gainIntegral = Ka.template GetBlock<InputSize, OutputSize>(0, StateSize); |
| 86 | + } |
| 87 | + |
| 88 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 89 | + OPTIMIZE_FOR_SPEED |
| 90 | + typename IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::InputVector |
| 91 | + IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::ComputeControl( |
| 92 | + const StateVector& x, |
| 93 | + const OutputVector& reference, |
| 94 | + const OutputVector& measured) |
| 95 | + { |
| 96 | + auto error = reference - measured; |
| 97 | + integral = integral + error * sampleTime; |
| 98 | + return (gainState * x + gainIntegral * integral) * T(-1); |
| 99 | + } |
| 100 | + |
| 101 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 102 | + void IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::Reset() |
| 103 | + { |
| 104 | + integral = IntegralVector{}; |
| 105 | + } |
| 106 | + |
| 107 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 108 | + const typename IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::GainStateMatrix& |
| 109 | + IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::GetGainState() const |
| 110 | + { |
| 111 | + return gainState; |
| 112 | + } |
| 113 | + |
| 114 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 115 | + const typename IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::GainIntegralMatrix& |
| 116 | + IntegralStateFeedbackLqi<T, StateSize, InputSize, OutputSize>::GetGainIntegral() const |
| 117 | + { |
| 118 | + return gainIntegral; |
| 119 | + } |
| 120 | + |
| 121 | +#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD |
| 122 | + extern template class IntegralStateFeedbackLqi<float, 2, 1, 1>; |
| 123 | +#endif |
| 124 | +} |
0 commit comments