|
| 1 | +// Copyright (c) 2024, Numerical Toolbox Contributors. All rights reserved. |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#if defined(__GNUC__) || defined(__clang__) |
| 5 | +#pragma GCC optimize("O3", "fast-math") |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "numerical/filters/passive/BiquadCascade.hpp" |
| 9 | +#include "numerical/math/CompilerOptimizations.hpp" |
| 10 | +#include "numerical/math/LinearTimeInvariant.hpp" |
| 11 | +#include "numerical/math/Matrix.hpp" |
| 12 | +#include <array> |
| 13 | +#include <cmath> |
| 14 | +#include <cstddef> |
| 15 | +#include <type_traits> |
| 16 | + |
| 17 | +namespace robust_control |
| 18 | +{ |
| 19 | + template<typename T, |
| 20 | + std::size_t StateSize, |
| 21 | + std::size_t InputSize, |
| 22 | + std::size_t OutputSize> |
| 23 | + class DisturbanceObserver |
| 24 | + { |
| 25 | + static_assert(std::is_floating_point_v<T>, "DisturbanceObserver supports floating-point types"); |
| 26 | + static_assert(StateSize > 0 && InputSize > 0 && OutputSize > 0, |
| 27 | + "DisturbanceObserver requires positive dimensions"); |
| 28 | + static_assert(InputSize == OutputSize, |
| 29 | + "DisturbanceObserver requires InputSize == OutputSize for per-channel disturbance pairing"); |
| 30 | + |
| 31 | + public: |
| 32 | + using PlantType = math::LinearTimeInvariant<T, StateSize, InputSize, OutputSize>; |
| 33 | + using InputVector = math::Vector<T, InputSize>; |
| 34 | + using OutputVector = math::Vector<T, OutputSize>; |
| 35 | + |
| 36 | + using QCoeffs = filters::passive::BiquadCoeffs<T>; |
| 37 | + |
| 38 | + DisturbanceObserver(const PlantType& nominalPlant, const QCoeffs& q); |
| 39 | + |
| 40 | + OPTIMIZE_FOR_SPEED InputVector Compute(const InputVector& nominalControl, |
| 41 | + const OutputVector& measuredOutput); |
| 42 | + |
| 43 | + [[nodiscard]] const InputVector& Disturbance() const; |
| 44 | + |
| 45 | + void Reset(); |
| 46 | + |
| 47 | + private: |
| 48 | + static std::array<T, InputSize> ComputeDcGainInverses(const PlantType& plant); |
| 49 | + static std::array<filters::passive::BiquadCascade<T, 1>, InputSize> MakeQFilters(const QCoeffs& q); |
| 50 | + |
| 51 | + PlantType nominalPlant; |
| 52 | + std::array<T, InputSize> dcGainInv; |
| 53 | + std::array<filters::passive::BiquadCascade<T, 1>, InputSize> qInvFilters; |
| 54 | + std::array<filters::passive::BiquadCascade<T, 1>, InputSize> qFilters; |
| 55 | + InputVector disturbance{}; |
| 56 | + InputVector appliedPrev{}; |
| 57 | + }; |
| 58 | + |
| 59 | + namespace detail |
| 60 | + { |
| 61 | + template<typename T, std::size_t N> |
| 62 | + T ComputeSisoSteadyStateDcGain( |
| 63 | + const math::LinearTimeInvariant<T, N, 1, 1>& plant) |
| 64 | + { |
| 65 | + math::Vector<T, N> x{}; |
| 66 | + math::Vector<T, 1> u{}; |
| 67 | + u.at(0, 0) = T{ 1 }; |
| 68 | + for (std::size_t k{ 0 }; k < 512; ++k) |
| 69 | + x = plant.Step(x, u); |
| 70 | + const auto y{ plant.Output(x, u) }; |
| 71 | + return y.at(0, 0); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 76 | + std::array<T, InputSize> |
| 77 | + DisturbanceObserver<T, StateSize, InputSize, OutputSize>::ComputeDcGainInverses( |
| 78 | + const PlantType& plant) |
| 79 | + { |
| 80 | + std::array<T, InputSize> result{}; |
| 81 | + for (std::size_t ch{ 0 }; ch < InputSize; ++ch) |
| 82 | + { |
| 83 | + using SisoPlant = math::LinearTimeInvariant<T, StateSize, 1, 1>; |
| 84 | + SisoPlant siso{}; |
| 85 | + for (std::size_t r{ 0 }; r < StateSize; ++r) |
| 86 | + { |
| 87 | + for (std::size_t c{ 0 }; c < StateSize; ++c) |
| 88 | + siso.A.at(r, c) = plant.A.at(r, c); |
| 89 | + siso.B.at(r, 0) = plant.B.at(r, ch); |
| 90 | + siso.C.at(0, r) = plant.C.at(ch, r); |
| 91 | + } |
| 92 | + siso.D.at(0, 0) = plant.D.at(ch, ch); |
| 93 | + |
| 94 | + const T gain{ detail::ComputeSisoSteadyStateDcGain(siso) }; |
| 95 | + result[ch] = (gain == T{ 0 }) ? T{ 1 } : T{ 1 } / gain; |
| 96 | + } |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 101 | + DisturbanceObserver<T, StateSize, InputSize, OutputSize>::DisturbanceObserver( |
| 102 | + const PlantType& nominalPlant, |
| 103 | + const QCoeffs& q) |
| 104 | + : nominalPlant{ nominalPlant } |
| 105 | + , dcGainInv{ ComputeDcGainInverses(nominalPlant) } |
| 106 | + , qInvFilters{ MakeQFilters(q) } |
| 107 | + , qFilters{ MakeQFilters(q) } |
| 108 | + {} |
| 109 | + |
| 110 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 111 | + std::array<filters::passive::BiquadCascade<T, 1>, InputSize> |
| 112 | + DisturbanceObserver<T, StateSize, InputSize, OutputSize>::MakeQFilters(const QCoeffs& q) |
| 113 | + { |
| 114 | + return [&]<std::size_t... Is>(std::index_sequence<Is...>) |
| 115 | + -> std::array<filters::passive::BiquadCascade<T, 1>, InputSize> |
| 116 | + { |
| 117 | + return { ((void)Is, filters::passive::BiquadCascade<T, 1>{ { q } })... }; |
| 118 | + }(std::make_index_sequence<InputSize>{}); |
| 119 | + } |
| 120 | + |
| 121 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 122 | + OPTIMIZE_FOR_SPEED typename DisturbanceObserver<T, StateSize, InputSize, OutputSize>::InputVector |
| 123 | + DisturbanceObserver<T, StateSize, InputSize, OutputSize>::Compute( |
| 124 | + const InputVector& nominalControl, |
| 125 | + const OutputVector& measuredOutput) |
| 126 | + { |
| 127 | + for (std::size_t ch{ 0 }; ch < InputSize; ++ch) |
| 128 | + { |
| 129 | + const T a{ qInvFilters[ch].Filter(measuredOutput.at(ch, 0) * dcGainInv[ch]) }; |
| 130 | + const T b{ qFilters[ch].Filter(appliedPrev.at(ch, 0)) }; |
| 131 | + disturbance.at(ch, 0) = a - b; |
| 132 | + } |
| 133 | + |
| 134 | + InputVector u{}; |
| 135 | + for (std::size_t ch{ 0 }; ch < InputSize; ++ch) |
| 136 | + u.at(ch, 0) = nominalControl.at(ch, 0) - disturbance.at(ch, 0); |
| 137 | + |
| 138 | + appliedPrev = u; |
| 139 | + return u; |
| 140 | + } |
| 141 | + |
| 142 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 143 | + const typename DisturbanceObserver<T, StateSize, InputSize, OutputSize>::InputVector& |
| 144 | + DisturbanceObserver<T, StateSize, InputSize, OutputSize>::Disturbance() const |
| 145 | + { |
| 146 | + return disturbance; |
| 147 | + } |
| 148 | + |
| 149 | + template<typename T, std::size_t StateSize, std::size_t InputSize, std::size_t OutputSize> |
| 150 | + void DisturbanceObserver<T, StateSize, InputSize, OutputSize>::Reset() |
| 151 | + { |
| 152 | + disturbance = InputVector{}; |
| 153 | + appliedPrev = InputVector{}; |
| 154 | + for (std::size_t ch{ 0 }; ch < InputSize; ++ch) |
| 155 | + { |
| 156 | + qInvFilters[ch].Reset(); |
| 157 | + qFilters[ch].Reset(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD |
| 162 | + extern template class DisturbanceObserver<float, 1, 1, 1>; |
| 163 | +#endif |
| 164 | +} |
0 commit comments