Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_ |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 3 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cassert> |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 5 | #include <chrono> |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 6 | #include <memory> |
| 7 | #include <utility> |
| 8 | #include <vector> |
Brian Silverman | c571e05 | 2013-03-13 17:58:56 -0700 | [diff] [blame] | 9 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 10 | #include "Eigen/Dense" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 11 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 12 | #include "unsupported/Eigen/MatrixFunctions" |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 13 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 14 | #if defined(__linux__) |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 15 | #include "absl/log/check.h" |
| 16 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 17 | |
| 18 | #include "aos/logging/logging.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 19 | #endif |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 20 | #include "aos/macros.h" |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 21 | #include "frc971/zeroing/wrap.h" |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 22 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 23 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 24 | typename PlantType, typename ObserverType, typename Scalar> |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 25 | class StateFeedbackLoop; |
| 26 | |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 27 | // For everything in this file, "inputs" and "outputs" are defined from the |
| 28 | // perspective of the plant. This means U is an input and Y is an output |
| 29 | // (because you give the plant U (powers) and it gives you back a Y (sensor |
| 30 | // values). This is the opposite of what they mean from the perspective of the |
| 31 | // controller (U is an output because that's what goes to the motors and Y is an |
| 32 | // input because that's what comes back from the sensors). |
| 33 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 34 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 35 | typename Scalar = double> |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 36 | struct StateFeedbackPlantCoefficients final { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 37 | public: |
| 38 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 39 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 40 | StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other) |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 41 | : A(other.A), |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 42 | B(other.B), |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 43 | C(other.C), |
| 44 | D(other.D), |
| 45 | U_min(other.U_min), |
James Kuszmaul | 03be124 | 2020-02-21 14:52:04 -0800 | [diff] [blame] | 46 | U_max(other.U_max), |
Ravago Jones | c471ebe | 2023-07-05 20:37:00 -0700 | [diff] [blame] | 47 | U_limit_coefficient(other.U_limit_coefficient), |
| 48 | U_limit_constant(other.U_limit_constant), |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 49 | dt(other.dt), |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 50 | delayed_u(other.delayed_u), |
| 51 | wrap_point(other.wrap_point) {} |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 52 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 53 | StateFeedbackPlantCoefficients( |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 54 | const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A, |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 55 | const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B, |
| 56 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C, |
| 57 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D, |
| 58 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max, |
James Kuszmaul | 03be124 | 2020-02-21 14:52:04 -0800 | [diff] [blame] | 59 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min, |
Ravago Jones | c471ebe | 2023-07-05 20:37:00 -0700 | [diff] [blame] | 60 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> |
| 61 | &U_limit_coefficient, |
| 62 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_limit_constant, |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 63 | const std::chrono::nanoseconds dt, size_t delayed_u, |
| 64 | const Eigen::Matrix<Scalar, number_of_outputs, 1> &wrap_point) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 65 | : A(A), |
| 66 | B(B), |
| 67 | C(C), |
| 68 | D(D), |
| 69 | U_min(U_min), |
| 70 | U_max(U_max), |
Ravago Jones | c471ebe | 2023-07-05 20:37:00 -0700 | [diff] [blame] | 71 | U_limit_coefficient(U_limit_coefficient), |
| 72 | U_limit_constant(U_limit_constant), |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 73 | dt(dt), |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 74 | delayed_u(delayed_u), |
| 75 | wrap_point(wrap_point) {} |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 76 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 77 | const Eigen::Matrix<Scalar, number_of_states, number_of_states> A; |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 78 | const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> B; |
| 79 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> C; |
| 80 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> D; |
| 81 | const Eigen::Matrix<Scalar, number_of_inputs, 1> U_min; |
| 82 | const Eigen::Matrix<Scalar, number_of_inputs, 1> U_max; |
Ravago Jones | c471ebe | 2023-07-05 20:37:00 -0700 | [diff] [blame] | 83 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> |
| 84 | U_limit_coefficient; |
| 85 | const Eigen::Matrix<Scalar, number_of_inputs, 1> U_limit_constant; |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 86 | |
James Kuszmaul | 03be124 | 2020-02-21 14:52:04 -0800 | [diff] [blame] | 87 | const std::chrono::nanoseconds dt; |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 88 | |
| 89 | // If true, this adds a single cycle output delay model to the plant. This is |
| 90 | // useful for modeling a control loop cycle where you sample, compute, and |
| 91 | // then queue the outputs to be ready to be executed when the next cycle |
| 92 | // happens. |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 93 | const size_t delayed_u; |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 94 | |
| 95 | // We will assume that each element of the Y vector wraps at the |
| 96 | // specified point. For any given element that is zero, we will |
| 97 | // assume no wrapping. |
| 98 | const Eigen::Matrix<Scalar, number_of_outputs, 1> wrap_point; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 101 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 102 | typename Scalar = double> |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 103 | class StateFeedbackPlant { |
| 104 | public: |
| 105 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 106 | |
| 107 | StateFeedbackPlant( |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 108 | ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients< |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 109 | number_of_states, number_of_inputs, number_of_outputs, Scalar>>> |
Austin Schuh | b02bf5b | 2021-07-31 21:28:21 -0700 | [diff] [blame] | 110 | &&coefficients) |
| 111 | : coefficients_(::std::move(coefficients)), index_(0) { |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 112 | if (coefficients_.size() > 1u) { |
| 113 | for (size_t i = 1; i < coefficients_.size(); ++i) { |
| 114 | if (coefficients_[i]->delayed_u != coefficients_[0]->delayed_u) { |
| 115 | abort(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | last_U_ = Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic>( |
| 120 | number_of_inputs, |
| 121 | std::max(static_cast<size_t>(1u), coefficients_[0]->delayed_u)); |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 122 | Reset(); |
| 123 | } |
| 124 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 125 | StateFeedbackPlant(StateFeedbackPlant &&other) : index_(other.index_) { |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 126 | ::std::swap(coefficients_, other.coefficients_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 127 | X_.swap(other.X_); |
| 128 | Y_.swap(other.Y_); |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 129 | last_U_.swap(other.last_U_); |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 132 | virtual ~StateFeedbackPlant() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 133 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 134 | const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 135 | return coefficients().A; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 136 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 137 | Scalar A(int i, int j) const { return A()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 138 | const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 139 | return coefficients().B; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 140 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 141 | Scalar B(int i, int j) const { return B()(i, j); } |
| 142 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 143 | return coefficients().C; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 144 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 145 | Scalar C(int i, int j) const { return C()(i, j); } |
| 146 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 147 | return coefficients().D; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 148 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 149 | Scalar D(int i, int j) const { return D()(i, j); } |
| 150 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 151 | return coefficients().U_min; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 152 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 153 | Scalar U_min(int i, int j) const { return U_min()(i, j); } |
| 154 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 155 | return coefficients().U_max; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 156 | } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 157 | Scalar U_max(int i, int j = 0) const { return U_max()(i, j); } |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 158 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> & |
| 159 | U_limit_coefficient() const { |
Ravago Jones | c471ebe | 2023-07-05 20:37:00 -0700 | [diff] [blame] | 160 | return coefficients().U_limit_coefficient; |
| 161 | } |
| 162 | Scalar U_limit_coefficient(int i, int j) const { |
| 163 | return U_limit_coefficient()(i, j); |
| 164 | } |
| 165 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_limit_constant() const { |
| 166 | return coefficients().U_limit_constant; |
| 167 | } |
| 168 | Scalar U_limit_constant(int i, int j = 0) const { |
| 169 | return U_limit_constant()(i, j); |
| 170 | } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 171 | |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 172 | const std::chrono::nanoseconds dt() const { return coefficients().dt; } |
| 173 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 174 | const Eigen::Matrix<Scalar, number_of_states, 1> &X() const { return X_; } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 175 | Scalar X(int i, int j = 0) const { return X()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 176 | const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y() const { return Y_; } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 177 | Scalar Y(int i, int j = 0) const { return Y()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 178 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 179 | Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X() { return X_; } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 180 | Scalar &mutable_X(int i, int j = 0) { return mutable_X()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 181 | Eigen::Matrix<Scalar, number_of_outputs, 1> &mutable_Y() { return Y_; } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 182 | Scalar &mutable_Y(int i, int j = 0) { return mutable_Y()(i, j); } |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 183 | |
James Kuszmaul | 109ed8d | 2019-02-17 21:41:04 -0800 | [diff] [blame] | 184 | size_t coefficients_size() const { return coefficients_.size(); } |
| 185 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 186 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 187 | number_of_outputs, Scalar> & |
| 188 | coefficients(int index) const { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 189 | return *coefficients_[index]; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 192 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 193 | number_of_outputs, Scalar> & |
| 194 | coefficients() const { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 195 | return *coefficients_[index_]; |
| 196 | } |
| 197 | |
| 198 | int index() const { return index_; } |
| 199 | void set_index(int index) { |
| 200 | assert(index >= 0); |
| 201 | assert(index < static_cast<int>(coefficients_.size())); |
| 202 | index_ = index; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 206 | X_.setZero(); |
| 207 | Y_.setZero(); |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 208 | last_U_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 211 | // Assert that U is within the hardware range. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 212 | virtual void CheckU(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) { |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 213 | for (int i = 0; i < kNumInputs; ++i) { |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 214 | if (U(i, 0) > U_max(i, 0) + static_cast<Scalar>(0.00001) || |
| 215 | U(i, 0) < U_min(i, 0) - static_cast<Scalar>(0.00001)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 216 | #if defined(__linux__) |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 217 | AOS_LOG(FATAL, "U out of range\n"); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 218 | #else |
| 219 | abort(); |
| 220 | #endif |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 221 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 224 | |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 225 | const Eigen::Matrix<Scalar, number_of_inputs, 1> last_U( |
| 226 | size_t index = 0) const { |
| 227 | return last_U_.template block<number_of_inputs, 1>(0, index); |
| 228 | } |
| 229 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 230 | // Computes the new X and Y given the control input. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 231 | void Update(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) { |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 232 | // Powers outside of the range are more likely controller bugs than things |
| 233 | // that the plant should deal with. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 234 | CheckU(U); |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 235 | if (coefficients().delayed_u > 0) { |
| 236 | #if defined(__linux__) |
| 237 | DCHECK_EQ(static_cast<ssize_t>(coefficients().delayed_u), last_U_.cols()); |
| 238 | #endif |
| 239 | X_ = Update(X(), last_U(coefficients().delayed_u - 1)); |
| 240 | UpdateY(last_U(coefficients().delayed_u - 1)); |
| 241 | for (int i = coefficients().delayed_u; i > 1; --i) { |
| 242 | last_U_.template block<number_of_inputs, 1>(0, i - 1) = |
| 243 | last_U_.template block<number_of_inputs, 1>(0, i - 2); |
| 244 | } |
| 245 | last_U_.template block<number_of_inputs, 1>(0, 0) = U; |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 246 | } else { |
| 247 | X_ = Update(X(), U); |
| 248 | UpdateY(U); |
| 249 | } |
Austin Schuh | 01c7b25 | 2017-03-05 00:59:31 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Computes the new Y given the control input. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 253 | void UpdateY(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) { |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 254 | Y_ = C() * X() + D() * U; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 257 | Eigen::Matrix<Scalar, number_of_states, 1> Update( |
| 258 | const Eigen::Matrix<Scalar, number_of_states, 1> X, |
| 259 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) const { |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 260 | return A() * X + B() * U; |
| 261 | } |
| 262 | |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 263 | // Takes the provided state and wraps all the individual values such that, |
| 264 | // for any given i in [0, number_of_states), the wrapped X[i] will be in |
| 265 | // the range [-wrap_point[i] / 2, wrap_point[i] / 2] if wrap_point[i] > 0. |
| 266 | Eigen::Matrix<Scalar, number_of_outputs, 1> HandleWrapping( |
| 267 | const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) const { |
| 268 | Eigen::Matrix<Scalar, number_of_outputs, 1> wrapped; |
| 269 | for (int index = 0; index < number_of_outputs; ++index) { |
| 270 | const Scalar wrap_point = coefficients().wrap_point[index]; |
| 271 | wrapped[index] = |
| 272 | wrap_point > 0 |
| 273 | ? frc971::zeroing::Wrap(/*nearest=*/static_cast<Scalar>(0.0), |
| 274 | Y(index), wrap_point) |
| 275 | : Y(index); |
| 276 | } |
| 277 | return wrapped; |
| 278 | } |
| 279 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 280 | protected: |
| 281 | // these are accessible from non-templated subclasses |
Austin Schuh | b1cdb38 | 2013-03-01 22:53:52 -0800 | [diff] [blame] | 282 | static const int kNumStates = number_of_states; |
| 283 | static const int kNumOutputs = number_of_outputs; |
| 284 | static const int kNumInputs = number_of_inputs; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 285 | |
| 286 | private: |
Nathan Leong | dd72800 | 2024-02-03 15:26:53 -0800 | [diff] [blame] | 287 | // X_ and Y_ must be explicitly initialized to avoid having gcc complain about |
| 288 | // uninitialized values when using the move constructor. |
| 289 | Eigen::Matrix<Scalar, number_of_states, 1> X_ = |
| 290 | Eigen::Matrix<Scalar, number_of_states, 1>::Zero(); |
| 291 | Eigen::Matrix<Scalar, number_of_outputs, 1> Y_ = |
| 292 | Eigen::Matrix<Scalar, number_of_outputs, 1>::Zero(); |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 293 | Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic> last_U_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 294 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 295 | ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients< |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 296 | number_of_states, number_of_inputs, number_of_outputs, Scalar>>> |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 297 | coefficients_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 298 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 299 | int index_; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 300 | |
| 301 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 302 | }; |
| 303 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 304 | // A container for all the controller coefficients. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 305 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 306 | typename Scalar = double> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 307 | struct StateFeedbackControllerCoefficients final { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 308 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 309 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 310 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> K; |
| 311 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> Kff; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 312 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 313 | StateFeedbackControllerCoefficients( |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 314 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K, |
| 315 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 316 | : K(K), Kff(Kff) {} |
| 317 | }; |
| 318 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 319 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 320 | typename Scalar = double> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 321 | class StateFeedbackController { |
| 322 | public: |
| 323 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 324 | |
| 325 | explicit StateFeedbackController( |
| 326 | ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients< |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 327 | number_of_states, number_of_inputs, number_of_outputs, Scalar>>> |
Austin Schuh | b02bf5b | 2021-07-31 21:28:21 -0700 | [diff] [blame] | 328 | &&controllers) |
| 329 | : coefficients_(::std::move(controllers)) {} |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 330 | |
| 331 | StateFeedbackController(StateFeedbackController &&other) |
| 332 | : index_(other.index_) { |
| 333 | ::std::swap(coefficients_, other.coefficients_); |
| 334 | } |
| 335 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 336 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K() const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 337 | return coefficients().K; |
| 338 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 339 | Scalar K(int i, int j) const { return K()(i, j); } |
| 340 | const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff() const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 341 | return coefficients().Kff; |
| 342 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 343 | Scalar Kff(int i, int j) const { return Kff()(i, j); } |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 344 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 345 | void Reset() {} |
| 346 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 347 | // Sets the current controller to be index, clamped to be within range. |
| 348 | void set_index(int index) { |
| 349 | if (index < 0) { |
| 350 | index_ = 0; |
| 351 | } else if (index >= static_cast<int>(coefficients_.size())) { |
| 352 | index_ = static_cast<int>(coefficients_.size()) - 1; |
| 353 | } else { |
| 354 | index_ = index; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | int index() const { return index_; } |
| 359 | |
| 360 | const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 361 | number_of_outputs, Scalar> & |
| 362 | coefficients(int index) const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 363 | return *coefficients_[index]; |
| 364 | } |
| 365 | |
| 366 | const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 367 | number_of_outputs, Scalar> & |
| 368 | coefficients() const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 369 | return *coefficients_[index_]; |
| 370 | } |
| 371 | |
| 372 | private: |
| 373 | int index_ = 0; |
| 374 | ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients< |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 375 | number_of_states, number_of_inputs, number_of_outputs, Scalar>>> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 376 | coefficients_; |
| 377 | }; |
| 378 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 379 | // A container for all the observer coefficients. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 380 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 381 | typename Scalar = double> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 382 | struct StateFeedbackObserverCoefficients final { |
| 383 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 384 | |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 385 | const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> KalmanGain; |
James Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 386 | const Eigen::Matrix<Scalar, number_of_states, number_of_states> Q; |
| 387 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> R; |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 388 | |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 389 | // If true, this adds a single cycle output delay model to the plant. This is |
| 390 | // useful for modeling a control loop cycle where you sample, compute, and |
| 391 | // then queue the outputs to be ready to be executed when the next cycle |
| 392 | // happens. |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 393 | const size_t delayed_u; |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 394 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 395 | StateFeedbackObserverCoefficients( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 396 | const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> |
| 397 | &KalmanGain, |
James Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 398 | const Eigen::Matrix<Scalar, number_of_states, number_of_states> &Q, |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 399 | const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> &R, |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 400 | size_t delayed_u) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 401 | : KalmanGain(KalmanGain), Q(Q), R(R), delayed_u(delayed_u) {} |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 402 | }; |
| 403 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 404 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 405 | typename Scalar = double> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 406 | class StateFeedbackObserver { |
| 407 | public: |
| 408 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 409 | |
| 410 | explicit StateFeedbackObserver( |
| 411 | ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients< |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 412 | number_of_states, number_of_inputs, number_of_outputs, Scalar>>> |
Austin Schuh | b02bf5b | 2021-07-31 21:28:21 -0700 | [diff] [blame] | 413 | &&observers) |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 414 | : coefficients_(::std::move(observers)) { |
| 415 | last_U_ = Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic>( |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 416 | number_of_inputs, |
| 417 | std::max(static_cast<size_t>(1u), coefficients().delayed_u)); |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 418 | } |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 419 | |
| 420 | StateFeedbackObserver(StateFeedbackObserver &&other) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 421 | : X_hat_(other.X_hat_), last_U_(other.last_U_), index_(other.index_) { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 422 | ::std::swap(coefficients_, other.coefficients_); |
| 423 | } |
| 424 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 425 | const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &KalmanGain() |
| 426 | const { |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 427 | return coefficients().KalmanGain; |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 428 | } |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 429 | Scalar KalmanGain(int i, int j) const { return KalmanGain()(i, j); } |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 430 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 431 | const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const { |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 432 | return X_hat_; |
| 433 | } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 434 | Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() { return X_hat_; } |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 435 | |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 436 | const Eigen::Matrix<Scalar, number_of_inputs, 1> last_U( |
| 437 | size_t index = 0) const { |
| 438 | return last_U_.template block<number_of_inputs, 1>(0, index); |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Austin Schuh | 6501d23 | 2017-11-23 20:35:27 -0800 | [diff] [blame] | 441 | void Reset(StateFeedbackPlant<number_of_states, number_of_inputs, |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 442 | number_of_outputs, Scalar> * /*loop*/) { |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 443 | X_hat_.setZero(); |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 444 | last_U_.setZero(); |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 445 | } |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 446 | |
Austin Schuh | 6501d23 | 2017-11-23 20:35:27 -0800 | [diff] [blame] | 447 | void Predict(StateFeedbackPlant<number_of_states, number_of_inputs, |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 448 | number_of_outputs, Scalar> *plant, |
| 449 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u, |
Austin Schuh | 6501d23 | 2017-11-23 20:35:27 -0800 | [diff] [blame] | 450 | ::std::chrono::nanoseconds /*dt*/) { |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 451 | if (plant->coefficients().delayed_u > 0) { |
| 452 | mutable_X_hat() = |
| 453 | plant->Update(X_hat(), last_U(coefficients().delayed_u - 1)); |
| 454 | for (int i = coefficients().delayed_u; i > 1; --i) { |
| 455 | last_U_.template block<number_of_inputs, 1>(0, i - 1) = |
| 456 | last_U_.template block<number_of_inputs, 1>(0, i - 2); |
| 457 | } |
| 458 | last_U_.template block<number_of_inputs, 1>(0, 0) = new_u; |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 459 | } else { |
| 460 | mutable_X_hat() = plant->Update(X_hat(), new_u); |
| 461 | } |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Austin Schuh | 6501d23 | 2017-11-23 20:35:27 -0800 | [diff] [blame] | 464 | void Correct(const StateFeedbackPlant<number_of_states, number_of_inputs, |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 465 | number_of_outputs, Scalar> &plant, |
| 466 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U, |
| 467 | const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) { |
James Kuszmaul | 9ea3ff9 | 2024-06-14 15:02:15 -0700 | [diff] [blame^] | 468 | mutable_X_hat() += |
| 469 | KalmanGain() * |
| 470 | plant.HandleWrapping(Y - plant.C() * X_hat() - plant.D() * U); |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 473 | // Sets the current controller to be index, clamped to be within range. |
| 474 | void set_index(int index) { |
| 475 | if (index < 0) { |
| 476 | index_ = 0; |
| 477 | } else if (index >= static_cast<int>(coefficients_.size())) { |
| 478 | index_ = static_cast<int>(coefficients_.size()) - 1; |
| 479 | } else { |
| 480 | index_ = index; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | int index() const { return index_; } |
| 485 | |
| 486 | const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 487 | number_of_outputs, Scalar> & |
| 488 | coefficients(int index) const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 489 | return *coefficients_[index]; |
| 490 | } |
| 491 | |
| 492 | const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 493 | number_of_outputs, Scalar> & |
| 494 | coefficients() const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 495 | return *coefficients_[index_]; |
| 496 | } |
| 497 | |
| 498 | private: |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 499 | // Internal state estimate. |
James Kuszmaul | eeb98e9 | 2024-01-14 22:15:32 -0800 | [diff] [blame] | 500 | Eigen::Matrix<Scalar, number_of_states, 1> X_hat_ = |
| 501 | Eigen::Matrix<Scalar, number_of_states, 1>::Zero(); |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame] | 502 | Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic> last_U_; |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 503 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 504 | int index_ = 0; |
| 505 | ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients< |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 506 | number_of_states, number_of_inputs, number_of_outputs, Scalar>>> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 507 | coefficients_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 508 | }; |
| 509 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 510 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 511 | typename Scalar = double, |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 512 | typename PlantType = StateFeedbackPlant< |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 513 | number_of_states, number_of_inputs, number_of_outputs, Scalar>, |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 514 | typename ObserverType = StateFeedbackObserver< |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 515 | number_of_states, number_of_inputs, number_of_outputs, Scalar>> |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 516 | class StateFeedbackLoop { |
| 517 | public: |
| 518 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 519 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 520 | explicit StateFeedbackLoop( |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 521 | PlantType &&plant, |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 522 | StateFeedbackController<number_of_states, number_of_inputs, |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 523 | number_of_outputs, Scalar> &&controller, |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 524 | ObserverType &&observer) |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 525 | : plant_(::std::move(plant)), |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 526 | controller_(::std::move(controller)), |
| 527 | observer_(::std::move(observer)) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 528 | Reset(); |
| 529 | } |
| 530 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 531 | StateFeedbackLoop(StateFeedbackLoop &&other) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 532 | : plant_(::std::move(other.plant_)), |
| 533 | controller_(::std::move(other.controller_)), |
| 534 | observer_(::std::move(other.observer_)) { |
Austin Schuh | 36f8c4e | 2020-02-29 20:29:41 -0800 | [diff] [blame] | 535 | ff_U_.swap(other.ff_U_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 536 | R_.swap(other.R_); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 537 | next_R_.swap(other.next_R_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 538 | U_.swap(other.U_); |
| 539 | U_uncapped_.swap(other.U_uncapped_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 542 | virtual ~StateFeedbackLoop() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 543 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 544 | const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const { |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 545 | return observer().X_hat(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 546 | } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 547 | Scalar X_hat(int i, int j = 0) const { return X_hat()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 548 | const Eigen::Matrix<Scalar, number_of_states, 1> &R() const { return R_; } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 549 | Scalar R(int i, int j = 0) const { return R()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 550 | const Eigen::Matrix<Scalar, number_of_states, 1> &next_R() const { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 551 | return next_R_; |
| 552 | } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 553 | Scalar next_R(int i, int j = 0) const { return next_R()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 554 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U() const { return U_; } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 555 | Scalar U(int i, int j = 0) const { return U()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 556 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_uncapped() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 557 | return U_uncapped_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 558 | } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 559 | Scalar U_uncapped(int i, int j = 0) const { return U_uncapped()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 560 | const Eigen::Matrix<Scalar, number_of_inputs, 1> &ff_U() const { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 561 | return ff_U_; |
| 562 | } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 563 | Scalar ff_U(int i, int j = 0) const { return ff_U()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 564 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 565 | Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() { |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 566 | return observer_.mutable_X_hat(); |
| 567 | } |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 568 | Scalar &mutable_X_hat(int i, int j = 0) { return mutable_X_hat()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 569 | Eigen::Matrix<Scalar, number_of_states, 1> &mutable_R() { return R_; } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 570 | Scalar &mutable_R(int i, int j = 0) { return mutable_R()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 571 | Eigen::Matrix<Scalar, number_of_states, 1> &mutable_next_R() { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 572 | return next_R_; |
| 573 | } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 574 | Scalar &mutable_next_R(int i, int j = 0) { return mutable_next_R()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 575 | Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U() { return U_; } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 576 | Scalar &mutable_U(int i, int j = 0) { return mutable_U()(i, j); } |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 577 | Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U_uncapped() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 578 | return U_uncapped_; |
| 579 | } |
Austin Schuh | 95771d9 | 2021-01-23 14:42:25 -0800 | [diff] [blame] | 580 | Scalar &mutable_U_uncapped(int i, int j = 0) { |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 581 | return mutable_U_uncapped()(i, j); |
| 582 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 583 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 584 | const PlantType &plant() const { return plant_; } |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 585 | PlantType *mutable_plant() { return &plant_; } |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 586 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 587 | const StateFeedbackController<number_of_states, number_of_inputs, |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 588 | number_of_outputs, Scalar> & |
| 589 | controller() const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 590 | return controller_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 593 | const ObserverType &observer() const { return observer_; } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 594 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 595 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 596 | R_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 597 | next_R_.setZero(); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 598 | U_.setZero(); |
| 599 | U_uncapped_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 600 | ff_U_.setZero(); |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 601 | |
| 602 | plant_.Reset(); |
| 603 | controller_.Reset(); |
Austin Schuh | 6501d23 | 2017-11-23 20:35:27 -0800 | [diff] [blame] | 604 | observer_.Reset(this->mutable_plant()); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // If U is outside the hardware range, limit it before the plant tries to use |
| 608 | // it. |
| 609 | virtual void CapU() { |
Ravago Jones | c471ebe | 2023-07-05 20:37:00 -0700 | [diff] [blame] | 610 | // TODO(Ravago): this runs before the state update step, so it's limiting |
| 611 | // the future control based on the old state. This gets even worse when we |
| 612 | // have delayed_u. |
| 613 | Eigen::Matrix<Scalar, number_of_inputs, 1> U_max_computed = |
| 614 | plant().U_limit_coefficient() * plant().X() + |
| 615 | plant().U_limit_constant(); |
| 616 | Eigen::Matrix<Scalar, number_of_inputs, 1> U_min_computed = |
| 617 | plant().U_limit_coefficient() * plant().X() - |
| 618 | plant().U_limit_constant(); |
| 619 | |
| 620 | U_max_computed = U_max_computed.cwiseMin(plant().U_max()); |
| 621 | U_min_computed = U_min_computed.cwiseMax(plant().U_min()); |
| 622 | |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 623 | for (int i = 0; i < kNumInputs; ++i) { |
Ravago Jones | c471ebe | 2023-07-05 20:37:00 -0700 | [diff] [blame] | 624 | if (U(i, 0) > U_max_computed(i, 0)) { |
| 625 | U_(i, 0) = U_max_computed(i, 0); |
| 626 | } else if (U(i, 0) < U_min_computed(i, 0)) { |
| 627 | U_(i, 0) = U_min_computed(i, 0); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 632 | // Corrects X_hat given the observation in Y. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 633 | void Correct(const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) { |
Austin Schuh | 6501d23 | 2017-11-23 20:35:27 -0800 | [diff] [blame] | 634 | observer_.Correct(this->plant(), U(), Y); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 637 | const Eigen::Matrix<Scalar, number_of_states, 1> error() const { |
Austin Schuh | 3f862bb | 2016-02-27 14:48:05 -0800 | [diff] [blame] | 638 | return R() - X_hat(); |
| 639 | } |
| 640 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 641 | // Returns the calculated controller power. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 642 | virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> ControllerOutput() { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 643 | // TODO(austin): Should this live in StateSpaceController? |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 644 | ff_U_ = FeedForward(); |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 645 | return controller().K() * error() + ff_U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | // Calculates the feed forwards power. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 649 | virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> FeedForward() { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 650 | // TODO(austin): Should this live in StateSpaceController? |
| 651 | return controller().Kff() * (next_R() - plant().A() * R()); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 654 | void UpdateController(bool stop_motors) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 655 | if (stop_motors) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 656 | U_.setZero(); |
| 657 | U_uncapped_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 658 | ff_U_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 659 | } else { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 660 | U_ = U_uncapped_ = ControllerOutput(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 661 | CapU(); |
| 662 | } |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 663 | UpdateFFReference(); |
| 664 | } |
| 665 | |
| 666 | // stop_motors is whether or not to output all 0s. |
| 667 | void Update(bool stop_motors, |
| 668 | ::std::chrono::nanoseconds dt = ::std::chrono::milliseconds(0)) { |
| 669 | UpdateController(stop_motors); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 670 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 671 | UpdateObserver(U_, dt); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // Updates R() after any CapU operations happen on U(). |
| 675 | void UpdateFFReference() { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 676 | ff_U_ -= U_uncapped() - U(); |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 677 | if (!controller().Kff().isZero(0)) { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 678 | R_ = plant().A() * R() + plant().B() * ff_U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 679 | } |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 682 | void UpdateObserver(const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u, |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 683 | ::std::chrono::nanoseconds dt) { |
Austin Schuh | 6501d23 | 2017-11-23 20:35:27 -0800 | [diff] [blame] | 684 | observer_.Predict(this->mutable_plant(), new_u, dt); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 687 | // Sets the current controller to be index. |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 688 | void set_index(int index) { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 689 | plant_.set_index(index); |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 690 | controller_.set_index(index); |
| 691 | observer_.set_index(index); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 694 | int index() const { return plant_.index(); } |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 695 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 696 | protected: |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 697 | PlantType plant_; |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 698 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 699 | StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs, |
| 700 | Scalar> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 701 | controller_; |
| 702 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame] | 703 | ObserverType observer_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 704 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 705 | // These are accessible from non-templated subclasses. |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 706 | static constexpr int kNumStates = number_of_states; |
| 707 | static constexpr int kNumOutputs = number_of_outputs; |
| 708 | static constexpr int kNumInputs = number_of_inputs; |
| 709 | |
| 710 | // Portion of U which is based on the feed-forwards. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 711 | Eigen::Matrix<Scalar, number_of_inputs, 1> ff_U_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 712 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 713 | private: |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 714 | // Current goal (Used by the feed-back controller). |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 715 | Eigen::Matrix<Scalar, number_of_states, 1> R_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 716 | // Goal to go to in the next cycle (Used by Feed-Forward controller.) |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 717 | Eigen::Matrix<Scalar, number_of_states, 1> next_R_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 718 | // Computed output after being capped. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 719 | Eigen::Matrix<Scalar, number_of_inputs, 1> U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 720 | // Computed output before being capped. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 721 | Eigen::Matrix<Scalar, number_of_inputs, 1> U_uncapped_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 722 | |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 723 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 724 | }; |
| 725 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 726 | #endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_ |