|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if defined(__GNUC__) || defined(__clang__) |
| 4 | +#pragma GCC optimize("O3", "fast-math") |
| 5 | +#endif |
| 6 | + |
| 7 | +#include "infra/util/ReallyAssert.hpp" |
| 8 | +#include "numerical/math/CompilerOptimizations.hpp" |
| 9 | +#include "numerical/math/LinearTimeInvariant.hpp" |
| 10 | +#include "numerical/math/Matrix.hpp" |
| 11 | +#include "numerical/solvers/GaussianElimination.hpp" |
| 12 | +#include <cmath> |
| 13 | +#include <cstddef> |
| 14 | +#include <type_traits> |
| 15 | + |
| 16 | +namespace robust_control |
| 17 | +{ |
| 18 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 19 | + class SlidingModeControl |
| 20 | + { |
| 21 | + static_assert(std::is_floating_point_v<T>, "SlidingModeControl supports floating-point types"); |
| 22 | + static_assert(StateSize > 0 && InputSize > 0, "SlidingModeControl requires positive dimensions"); |
| 23 | + |
| 24 | + public: |
| 25 | + using StateVector = math::Vector<T, StateSize>; |
| 26 | + using InputVector = math::Vector<T, InputSize>; |
| 27 | + using SurfaceMatrix = math::Matrix<T, InputSize, StateSize>; |
| 28 | + using SBMatrix = math::SquareMatrix<T, InputSize>; |
| 29 | + using PlantType = math::LinearTimeInvariant<T, StateSize, InputSize, StateSize>; |
| 30 | + |
| 31 | + SlidingModeControl(const PlantType& plant, |
| 32 | + const SurfaceMatrix& surface, |
| 33 | + const InputVector& switchGain, |
| 34 | + T phi); |
| 35 | + |
| 36 | + OPTIMIZE_FOR_SPEED InputVector ComputeControl(const StateVector& x); |
| 37 | + OPTIMIZE_FOR_SPEED InputVector ComputeControl(const StateVector& x, const StateVector& reference); |
| 38 | + |
| 39 | + [[nodiscard]] InputVector Surface(const StateVector& x) const; |
| 40 | + void SetBoundaryLayer(T phi); |
| 41 | + |
| 42 | + [[nodiscard]] static T Sat(T s, T phi); |
| 43 | + |
| 44 | + private: |
| 45 | + OPTIMIZE_FOR_SPEED InputVector ScaledSat(const InputVector& s) const; |
| 46 | + |
| 47 | + PlantType plant; |
| 48 | + SurfaceMatrix surface; |
| 49 | + SBMatrix sbInv; |
| 50 | + InputVector switchGain; |
| 51 | + T boundaryLayer; |
| 52 | + }; |
| 53 | + |
| 54 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 55 | + SlidingModeControl<T, StateSize, InputSize>::SlidingModeControl( |
| 56 | + const PlantType& plant, |
| 57 | + const SurfaceMatrix& surface, |
| 58 | + const InputVector& switchGain, |
| 59 | + T phi) |
| 60 | + : plant{ plant } |
| 61 | + , surface{ surface } |
| 62 | + , switchGain{ switchGain } |
| 63 | + , boundaryLayer{ phi } |
| 64 | + { |
| 65 | + really_assert(phi > T{ 0 }); |
| 66 | + |
| 67 | + const auto sb = surface * plant.B; |
| 68 | + const auto identity = SBMatrix::Identity(); |
| 69 | + sbInv = solvers::SolveSystem<T, InputSize, InputSize>(sb, identity); |
| 70 | + } |
| 71 | + |
| 72 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 73 | + OPTIMIZE_FOR_SPEED typename SlidingModeControl<T, StateSize, InputSize>::InputVector |
| 74 | + SlidingModeControl<T, StateSize, InputSize>::ComputeControl(const StateVector& x) |
| 75 | + { |
| 76 | + const auto s = surface * x; |
| 77 | + const auto sax = surface * (plant.A * x); |
| 78 | + const auto uEq = sbInv * sax * T{ -1 }; |
| 79 | + const auto uSw = sbInv * ScaledSat(s); |
| 80 | + return uEq - uSw; |
| 81 | + } |
| 82 | + |
| 83 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 84 | + OPTIMIZE_FOR_SPEED typename SlidingModeControl<T, StateSize, InputSize>::InputVector |
| 85 | + SlidingModeControl<T, StateSize, InputSize>::ComputeControl(const StateVector& x, const StateVector& reference) |
| 86 | + { |
| 87 | + const auto error = x - reference; |
| 88 | + return ComputeControl(error); |
| 89 | + } |
| 90 | + |
| 91 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 92 | + typename SlidingModeControl<T, StateSize, InputSize>::InputVector |
| 93 | + SlidingModeControl<T, StateSize, InputSize>::Surface(const StateVector& x) const |
| 94 | + { |
| 95 | + return surface * x; |
| 96 | + } |
| 97 | + |
| 98 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 99 | + void SlidingModeControl<T, StateSize, InputSize>::SetBoundaryLayer(T phi) |
| 100 | + { |
| 101 | + really_assert(phi > T{ 0 }); |
| 102 | + boundaryLayer = phi; |
| 103 | + } |
| 104 | + |
| 105 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 106 | + T SlidingModeControl<T, StateSize, InputSize>::Sat(T s, T phi) |
| 107 | + { |
| 108 | + const T ratio = s / phi; |
| 109 | + if (ratio >= T{ 1 }) |
| 110 | + return T{ 1 }; |
| 111 | + if (ratio <= T{ -1 }) |
| 112 | + return T{ -1 }; |
| 113 | + return ratio; |
| 114 | + } |
| 115 | + |
| 116 | + template<typename T, std::size_t StateSize, std::size_t InputSize> |
| 117 | + OPTIMIZE_FOR_SPEED typename SlidingModeControl<T, StateSize, InputSize>::InputVector |
| 118 | + SlidingModeControl<T, StateSize, InputSize>::ScaledSat(const InputVector& s) const |
| 119 | + { |
| 120 | + InputVector result{}; |
| 121 | + for (std::size_t i = 0; i < InputSize; ++i) |
| 122 | + result.at(i, 0) = switchGain.at(i, 0) * Sat(s.at(i, 0), boundaryLayer); |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | +#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD |
| 127 | + extern template class SlidingModeControl<float, 2, 1>; |
| 128 | +#endif |
| 129 | +} |
0 commit comments