|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if defined(__GNUC__) || defined(__clang__) |
| 4 | +#pragma GCC optimize("O3", "fast-math") |
| 5 | +#endif |
| 6 | + |
| 7 | +#include "numerical/math/CompilerOptimizations.hpp" |
| 8 | +#include "numerical/math/LinearTimeInvariant.hpp" |
| 9 | +#include "numerical/solvers/GaussianElimination.hpp" |
| 10 | +#include <array> |
| 11 | +#include <cstddef> |
| 12 | +#include <type_traits> |
| 13 | + |
| 14 | +namespace controllers |
| 15 | +{ |
| 16 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 17 | + class LuenbergerObserver |
| 18 | + { |
| 19 | + static_assert(std::is_floating_point_v<T>, "LuenbergerObserver supports floating-point types"); |
| 20 | + static_assert(StateSize > 0, "StateSize must be positive"); |
| 21 | + static_assert(InputSize > 0, "InputSize must be positive"); |
| 22 | + static_assert(OutputSize > 0, "OutputSize must be positive"); |
| 23 | + |
| 24 | + public: |
| 25 | + using Plant = math::LinearTimeInvariant<T, StateSize, InputSize, OutputSize>; |
| 26 | + using StateMatrix = math::SquareMatrix<T, StateSize>; |
| 27 | + using GainMatrix = math::Matrix<T, StateSize, OutputSize>; |
| 28 | + using StateVector = math::Vector<T, StateSize>; |
| 29 | + using InputVector = math::Vector<T, InputSize>; |
| 30 | + using OutputVector = math::Vector<T, OutputSize>; |
| 31 | + |
| 32 | + LuenbergerObserver(const Plant& plant, const GainMatrix& observerGain); |
| 33 | + |
| 34 | + static GainMatrix AckermannGain(const Plant& plant, const std::array<T, StateSize>& desiredPoles); |
| 35 | + |
| 36 | + OPTIMIZE_FOR_SPEED StateVector Update(const InputVector& u, const OutputVector& y); |
| 37 | + |
| 38 | + [[nodiscard]] const StateVector& Estimate() const; |
| 39 | + |
| 40 | + void Reset(const StateVector& x0); |
| 41 | + |
| 42 | + private: |
| 43 | + static StateMatrix BuildObservabilityMatrix(const StateMatrix& A, const math::Matrix<T, OutputSize, StateSize>& C); |
| 44 | + static StateMatrix EvaluateCharacteristicPoly(const StateMatrix& A, const std::array<T, StateSize>& poles); |
| 45 | + |
| 46 | + Plant plant; |
| 47 | + GainMatrix L; |
| 48 | + StateVector xhat; |
| 49 | + }; |
| 50 | + |
| 51 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 52 | + LuenbergerObserver<T, StateSize, InputSize, OutputSize>::LuenbergerObserver( |
| 53 | + const Plant& plantModel, const GainMatrix& observerGain) |
| 54 | + : plant{ plantModel } |
| 55 | + , L{ observerGain } |
| 56 | + , xhat{} |
| 57 | + {} |
| 58 | + |
| 59 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 60 | + OPTIMIZE_FOR_SPEED |
| 61 | + typename LuenbergerObserver<T, StateSize, InputSize, OutputSize>::StateVector |
| 62 | + LuenbergerObserver<T, StateSize, InputSize, OutputSize>::Update( |
| 63 | + const InputVector& u, const OutputVector& y) |
| 64 | + { |
| 65 | + OutputVector yhat = plant.C * xhat + plant.D * u; |
| 66 | + OutputVector innovation = y - yhat; |
| 67 | + xhat = plant.A * xhat + plant.B * u + L * innovation; |
| 68 | + return xhat; |
| 69 | + } |
| 70 | + |
| 71 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 72 | + const typename LuenbergerObserver<T, StateSize, InputSize, OutputSize>::StateVector& |
| 73 | + LuenbergerObserver<T, StateSize, InputSize, OutputSize>::Estimate() const |
| 74 | + { |
| 75 | + return xhat; |
| 76 | + } |
| 77 | + |
| 78 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 79 | + void LuenbergerObserver<T, StateSize, InputSize, OutputSize>::Reset(const StateVector& x0) |
| 80 | + { |
| 81 | + xhat = x0; |
| 82 | + } |
| 83 | + |
| 84 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 85 | + typename LuenbergerObserver<T, StateSize, InputSize, OutputSize>::StateMatrix |
| 86 | + LuenbergerObserver<T, StateSize, InputSize, OutputSize>::BuildObservabilityMatrix( |
| 87 | + const StateMatrix& A, const math::Matrix<T, OutputSize, StateSize>& C) |
| 88 | + { |
| 89 | + static_assert(OutputSize == 1, "Ackermann's formula requires SISO output (OutputSize == 1)"); |
| 90 | + StateMatrix O{}; |
| 91 | + math::Matrix<T, OutputSize, StateSize> CAk = C; |
| 92 | + for (std::size_t k = 0; k < StateSize; ++k) |
| 93 | + { |
| 94 | + for (std::size_t col = 0; col < StateSize; ++col) |
| 95 | + O.at(k, col) = CAk.at(0, col); |
| 96 | + CAk = CAk * A; |
| 97 | + } |
| 98 | + return O; |
| 99 | + } |
| 100 | + |
| 101 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 102 | + typename LuenbergerObserver<T, StateSize, InputSize, OutputSize>::StateMatrix |
| 103 | + LuenbergerObserver<T, StateSize, InputSize, OutputSize>::EvaluateCharacteristicPoly( |
| 104 | + const StateMatrix& A, const std::array<T, StateSize>& poles) |
| 105 | + { |
| 106 | + StateMatrix result = StateMatrix::Identity(); |
| 107 | + for (std::size_t i = 0; i < StateSize; ++i) |
| 108 | + { |
| 109 | + StateMatrix shifted = A; |
| 110 | + for (std::size_t r = 0; r < StateSize; ++r) |
| 111 | + shifted.at(r, r) = shifted.at(r, r) - poles[i]; |
| 112 | + result = result * shifted; |
| 113 | + } |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 118 | + typename LuenbergerObserver<T, StateSize, InputSize, OutputSize>::GainMatrix |
| 119 | + LuenbergerObserver<T, StateSize, InputSize, OutputSize>::AckermannGain( |
| 120 | + const Plant& plant, const std::array<T, StateSize>& desiredPoles) |
| 121 | + { |
| 122 | + static_assert(OutputSize == 1, "Ackermann's formula requires SISO output (OutputSize == 1)"); |
| 123 | + |
| 124 | + StateMatrix O = BuildObservabilityMatrix(plant.A, plant.C); |
| 125 | + StateMatrix phi = EvaluateCharacteristicPoly(plant.A, desiredPoles); |
| 126 | + |
| 127 | + StateVector eLast{}; |
| 128 | + eLast.at(StateSize - 1, 0) = T(1); |
| 129 | + |
| 130 | + StateVector OinvEn = solvers::SolveSystem<T, StateSize, 1>(O, eLast); |
| 131 | + |
| 132 | + StateVector Lgain = phi * OinvEn; |
| 133 | + |
| 134 | + GainMatrix result{}; |
| 135 | + for (std::size_t i = 0; i < StateSize; ++i) |
| 136 | + result.at(i, 0) = Lgain.at(i, 0); |
| 137 | + return result; |
| 138 | + } |
| 139 | + |
| 140 | +#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD |
| 141 | + extern template class LuenbergerObserver<float, 2, 1, 1>; |
| 142 | + extern template class LuenbergerObserver<float, 3, 1, 1>; |
| 143 | + extern template class LuenbergerObserver<float, 2, 2, 1>; |
| 144 | +#endif |
| 145 | +} |
0 commit comments