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