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 | |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 4 | #include <assert.h> |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 5 | |
Brian Silverman | c571e05 | 2013-03-13 17:58:56 -0700 | [diff] [blame] | 6 | #include <vector> |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 7 | #include <memory> |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 8 | #include <iostream> |
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" |
| 11 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 12 | #include "aos/common/logging/logging.h" |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 13 | #include "aos/common/macros.h" |
| 14 | |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 15 | // For everything in this file, "inputs" and "outputs" are defined from the |
| 16 | // perspective of the plant. This means U is an input and Y is an output |
| 17 | // (because you give the plant U (powers) and it gives you back a Y (sensor |
| 18 | // values). This is the opposite of what they mean from the perspective of the |
| 19 | // controller (U is an output because that's what goes to the motors and Y is an |
| 20 | // input because that's what comes back from the sensors). |
| 21 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 22 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 23 | class StateFeedbackPlantCoefficients final { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 24 | public: |
| 25 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 26 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 27 | StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other) |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 28 | : A_(other.A()), |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 29 | A_continuous_(other.A_continuous()), |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 30 | B_(other.B()), |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 31 | B_continuous_(other.B_continuous()), |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 32 | C_(other.C()), |
| 33 | D_(other.D()), |
| 34 | U_min_(other.U_min()), |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 35 | U_max_(other.U_max()) {} |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 36 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 37 | StateFeedbackPlantCoefficients( |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 38 | const Eigen::Matrix<double, number_of_states, number_of_states> &A, |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 39 | const Eigen::Matrix<double, number_of_states, number_of_states> |
| 40 | &A_continuous, |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 41 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B, |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 42 | const Eigen::Matrix<double, number_of_states, number_of_inputs> |
| 43 | &B_continuous, |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 44 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C, |
| 45 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D, |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 46 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max, |
| 47 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min) |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 48 | : A_(A), |
| 49 | A_continuous_(A_continuous), |
| 50 | B_(B), |
| 51 | B_continuous_(B_continuous), |
| 52 | C_(C), |
| 53 | D_(D), |
| 54 | U_min_(U_min), |
| 55 | U_max_(U_max) {} |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 56 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 57 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
| 58 | return A_; |
| 59 | } |
| 60 | double A(int i, int j) const { return A()(i, j); } |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 61 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_continuous() |
| 62 | const { |
| 63 | return A_continuous_; |
| 64 | } |
| 65 | double A_continuous(int i, int j) const { return A_continuous()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 66 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
| 67 | return B_; |
| 68 | } |
| 69 | double B(int i, int j) const { return B()(i, j); } |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 70 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B_continuous() const { |
| 71 | return B_continuous_; |
| 72 | } |
| 73 | double B_continuous(int i, int j) const { return B_continuous()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 74 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
| 75 | return C_; |
| 76 | } |
| 77 | double C(int i, int j) const { return C()(i, j); } |
| 78 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
| 79 | return D_; |
| 80 | } |
| 81 | double D(int i, int j) const { return D()(i, j); } |
| 82 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
| 83 | return U_min_; |
| 84 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 85 | double U_min(int i, int j) const { return U_min()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 86 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
| 87 | return U_max_; |
| 88 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 89 | double U_max(int i, int j) const { return U_max()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | const Eigen::Matrix<double, number_of_states, number_of_states> A_; |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 93 | const Eigen::Matrix<double, number_of_states, number_of_states> A_continuous_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 94 | const Eigen::Matrix<double, number_of_states, number_of_inputs> B_; |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 95 | const Eigen::Matrix<double, number_of_states, number_of_inputs> B_continuous_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 96 | const Eigen::Matrix<double, number_of_outputs, number_of_states> C_; |
| 97 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D_; |
| 98 | const Eigen::Matrix<double, number_of_inputs, 1> U_min_; |
| 99 | const Eigen::Matrix<double, number_of_inputs, 1> U_max_; |
| 100 | |
| 101 | StateFeedbackPlantCoefficients &operator=( |
| 102 | StateFeedbackPlantCoefficients other) = delete; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 106 | class StateFeedbackPlant { |
| 107 | public: |
| 108 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 109 | |
| 110 | StateFeedbackPlant( |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 111 | ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients< |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 112 | number_of_states, number_of_inputs, number_of_outputs>>> |
| 113 | *coefficients) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 114 | : coefficients_(::std::move(*coefficients)), plant_index_(0) { |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 115 | Reset(); |
| 116 | } |
| 117 | |
| 118 | StateFeedbackPlant(StateFeedbackPlant &&other) |
| 119 | : plant_index_(other.plant_index_) { |
| 120 | ::std::swap(coefficients_, other.coefficients_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 121 | X_.swap(other.X_); |
| 122 | Y_.swap(other.Y_); |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 125 | virtual ~StateFeedbackPlant() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 126 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 127 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 128 | return coefficients().A(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 129 | } |
| 130 | double A(int i, int j) const { return A()(i, j); } |
| 131 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 132 | return coefficients().B(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 133 | } |
| 134 | double B(int i, int j) const { return B()(i, j); } |
| 135 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 136 | return coefficients().C(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 137 | } |
| 138 | double C(int i, int j) const { return C()(i, j); } |
| 139 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 140 | return coefficients().D(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 141 | } |
| 142 | double D(int i, int j) const { return D()(i, j); } |
| 143 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 144 | return coefficients().U_min(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 145 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 146 | double U_min(int i, int j) const { return U_min()(i, j); } |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 147 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 148 | return coefficients().U_max(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 149 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 150 | double U_max(int i, int j) const { return U_max()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 151 | |
| 152 | const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 153 | double X(int i, int j) const { return X()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 154 | const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 155 | double Y(int i, int j) const { return Y()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 156 | |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 157 | Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 158 | double &mutable_X(int i, int j) { return mutable_X()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 159 | Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 160 | double &mutable_Y(int i, int j) { return mutable_Y()(i, j); } |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 161 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 162 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 163 | number_of_outputs> & |
| 164 | coefficients() const { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 165 | return *coefficients_[plant_index_]; |
| 166 | } |
| 167 | |
| 168 | int plant_index() const { return plant_index_; } |
| 169 | void set_plant_index(int plant_index) { |
Austin Schuh | 6ca0f79 | 2016-03-12 14:06:14 -0800 | [diff] [blame] | 170 | assert(plant_index >= 0); |
| 171 | assert(plant_index < static_cast<int>(coefficients_.size())); |
| 172 | plant_index_ = plant_index; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 176 | X_.setZero(); |
| 177 | Y_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 180 | // Assert that U is within the hardware range. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 181 | virtual void CheckU(const Eigen::Matrix<double, number_of_inputs, 1> &U) { |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 182 | for (int i = 0; i < kNumInputs; ++i) { |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 183 | if (U(i, 0) > U_max(i, 0) + 0.00001 || U(i, 0) < U_min(i, 0) - 0.00001) { |
| 184 | LOG(FATAL, "U out of range\n"); |
| 185 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 188 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 189 | // Computes the new X and Y given the control input. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 190 | void Update(const Eigen::Matrix<double, number_of_inputs, 1> &U) { |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 191 | // Powers outside of the range are more likely controller bugs than things |
| 192 | // that the plant should deal with. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 193 | CheckU(U); |
| 194 | X_ = A() * X() + B() * U; |
| 195 | Y_ = C() * X() + D() * U; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | protected: |
| 199 | // these are accessible from non-templated subclasses |
Austin Schuh | b1cdb38 | 2013-03-01 22:53:52 -0800 | [diff] [blame] | 200 | static const int kNumStates = number_of_states; |
| 201 | static const int kNumOutputs = number_of_outputs; |
| 202 | static const int kNumInputs = number_of_inputs; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 203 | |
| 204 | private: |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 205 | Eigen::Matrix<double, number_of_states, 1> X_; |
| 206 | Eigen::Matrix<double, number_of_outputs, 1> Y_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 207 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 208 | ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients< |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 209 | number_of_states, number_of_inputs, number_of_outputs>>> coefficients_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 210 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 211 | int plant_index_; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 212 | |
| 213 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 216 | // A Controller is a structure which holds a plant and the K and L matrices. |
| 217 | // This is designed such that multiple controllers can share one set of state to |
| 218 | // support gain scheduling easily. |
| 219 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 220 | struct StateFeedbackControllerConstants final { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 221 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 222 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 223 | const Eigen::Matrix<double, number_of_states, number_of_outputs> L; |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 224 | const Eigen::Matrix<double, number_of_inputs, number_of_states> K; |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 225 | const Eigen::Matrix<double, number_of_inputs, number_of_states> Kff; |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 226 | const Eigen::Matrix<double, number_of_states, number_of_states> A_inv; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 227 | StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 228 | number_of_outputs> |
| 229 | plant; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 230 | |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 231 | StateFeedbackControllerConstants( |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 232 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L, |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 233 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &K, |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 234 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff, |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 235 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv, |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 236 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 237 | number_of_outputs> &plant) |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 238 | : L(L), K(K), Kff(Kff), A_inv(A_inv), plant(plant) {} |
Brian Silverman | 2a04b26 | 2016-02-12 19:52:36 -0500 | [diff] [blame] | 239 | |
| 240 | // TODO(Brian): Remove this overload once they're all converted. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 241 | StateFeedbackControllerConstants( |
Brian Silverman | 2a04b26 | 2016-02-12 19:52:36 -0500 | [diff] [blame] | 242 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L, |
| 243 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &K, |
| 244 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv, |
| 245 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 246 | number_of_outputs> &plant) |
| 247 | : L(L), |
| 248 | K(K), |
| 249 | Kff(::Eigen::Matrix<double, number_of_inputs, |
| 250 | number_of_states>::Zero()), |
| 251 | A_inv(A_inv), |
| 252 | plant(plant) {} |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 255 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 256 | class StateFeedbackLoop { |
| 257 | public: |
| 258 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 259 | |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 260 | StateFeedbackLoop( |
| 261 | const StateFeedbackControllerConstants<number_of_states, number_of_inputs, |
| 262 | number_of_outputs> &controller) |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 263 | : controller_index_(0) { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 264 | controllers_.emplace_back( |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 265 | new StateFeedbackControllerConstants<number_of_states, number_of_inputs, |
| 266 | number_of_outputs>(controller)); |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 267 | Reset(); |
| 268 | } |
| 269 | |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 270 | StateFeedbackLoop( |
| 271 | ::std::vector<::std::unique_ptr<StateFeedbackControllerConstants< |
| 272 | number_of_states, number_of_inputs, number_of_outputs>>> *controllers) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 273 | : controllers_(::std::move(*controllers)), controller_index_(0) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 274 | Reset(); |
| 275 | } |
| 276 | |
| 277 | StateFeedbackLoop(StateFeedbackLoop &&other) { |
| 278 | X_hat_.swap(other.X_hat_); |
| 279 | R_.swap(other.R_); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 280 | next_R_.swap(other.next_R_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 281 | U_.swap(other.U_); |
| 282 | U_uncapped_.swap(other.U_uncapped_); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 283 | ff_U_.swap(other.ff_U_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 284 | ::std::swap(controllers_, other.controllers_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 285 | controller_index_ = other.controller_index_; |
| 286 | } |
| 287 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 288 | virtual ~StateFeedbackLoop() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 289 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 290 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 291 | return controller().plant.A(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 292 | } |
| 293 | double A(int i, int j) const { return A()(i, j); } |
| 294 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 295 | return controller().plant.B(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 296 | } |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 297 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv() |
| 298 | const { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 299 | return controller().A_inv; |
| 300 | } |
| 301 | double A_inv(int i, int j) const { return A_inv()(i, j); } |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 302 | double B(int i, int j) const { return B()(i, j); } |
| 303 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 304 | return controller().plant.C(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 305 | } |
| 306 | double C(int i, int j) const { return C()(i, j); } |
| 307 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 308 | return controller().plant.D(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 309 | } |
| 310 | double D(int i, int j) const { return D()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 311 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
| 312 | return controller().plant.U_min(); |
| 313 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 314 | double U_min(int i, int j) const { return U_min()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 315 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
| 316 | return controller().plant.U_max(); |
| 317 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 318 | double U_max(int i, int j) const { return U_max()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 319 | |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 320 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 321 | return controller().K; |
| 322 | } |
| 323 | double K(int i, int j) const { return K()(i, j); } |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 324 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff() const { |
| 325 | return controller().Kff; |
| 326 | } |
| 327 | double Kff(int i, int j) const { return Kff()(i, j); } |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 328 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const { |
| 329 | return controller().L; |
| 330 | } |
| 331 | double L(int i, int j) const { return L()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 332 | |
| 333 | const Eigen::Matrix<double, number_of_states, 1> &X_hat() const { |
| 334 | return X_hat_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 335 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 336 | double X_hat(int i, int j) const { return X_hat()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 337 | const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 338 | double R(int i, int j) const { return R()(i, j); } |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 339 | const Eigen::Matrix<double, number_of_states, 1> &next_R() const { |
| 340 | return next_R_; |
| 341 | } |
| 342 | double next_R(int i, int j) const { return next_R()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 343 | const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 344 | double U(int i, int j) const { return U()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 345 | const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const { |
| 346 | return U_uncapped_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 347 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 348 | double U_uncapped(int i, int j) const { return U_uncapped()(i, j); } |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 349 | const Eigen::Matrix<double, number_of_inputs, 1> &ff_U() const { |
| 350 | return ff_U_; |
| 351 | } |
| 352 | double ff_U(int i, int j) const { return ff_U()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 353 | |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 354 | Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 355 | double &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 356 | Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 357 | double &mutable_R(int i, int j) { return mutable_R()(i, j); } |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 358 | Eigen::Matrix<double, number_of_states, 1> &mutable_next_R() { |
| 359 | return next_R_; |
| 360 | } |
| 361 | double &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 362 | Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 363 | double &mutable_U(int i, int j) { return mutable_U()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 364 | Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 365 | return U_uncapped_; |
| 366 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 367 | double &mutable_U_uncapped(int i, int j) { |
| 368 | return mutable_U_uncapped()(i, j); |
| 369 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 370 | |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 371 | const StateFeedbackControllerConstants<number_of_states, number_of_inputs, |
| 372 | number_of_outputs> |
| 373 | &controller() const { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 374 | return *controllers_[controller_index_]; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 377 | const StateFeedbackControllerConstants<number_of_states, number_of_inputs, |
| 378 | number_of_outputs> |
| 379 | &controller(int index) const { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 380 | return *controllers_[index]; |
| 381 | } |
| 382 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 383 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 384 | X_hat_.setZero(); |
| 385 | R_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 386 | next_R_.setZero(); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 387 | U_.setZero(); |
| 388 | U_uncapped_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 389 | ff_U_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // If U is outside the hardware range, limit it before the plant tries to use |
| 393 | // it. |
| 394 | virtual void CapU() { |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 395 | for (int i = 0; i < kNumInputs; ++i) { |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 396 | if (U(i, 0) > U_max(i, 0)) { |
| 397 | U_(i, 0) = U_max(i, 0); |
| 398 | } else if (U(i, 0) < U_min(i, 0)) { |
| 399 | U_(i, 0) = U_min(i, 0); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 404 | // Corrects X_hat given the observation in Y. |
| 405 | void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 406 | X_hat_ += A_inv() * L() * (Y - C() * X_hat_ - D() * U()); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Austin Schuh | 3f862bb | 2016-02-27 14:48:05 -0800 | [diff] [blame] | 409 | const Eigen::Matrix<double, number_of_states, 1> error() const { |
| 410 | return R() - X_hat(); |
| 411 | } |
| 412 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 413 | // Returns the calculated controller power. |
| 414 | virtual const Eigen::Matrix<double, number_of_inputs, 1> ControllerOutput() { |
| 415 | ff_U_ = FeedForward(); |
Austin Schuh | 3f862bb | 2016-02-27 14:48:05 -0800 | [diff] [blame] | 416 | return K() * error() + ff_U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // Calculates the feed forwards power. |
| 420 | virtual const Eigen::Matrix<double, number_of_inputs, 1> FeedForward() { |
| 421 | return Kff() * (next_R() - A() * R()); |
| 422 | } |
| 423 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 424 | // stop_motors is whether or not to output all 0s. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 425 | void Update(bool stop_motors) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 426 | if (stop_motors) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 427 | U_.setZero(); |
| 428 | U_uncapped_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 429 | ff_U_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 430 | } else { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 431 | U_ = U_uncapped_ = ControllerOutput(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 432 | CapU(); |
| 433 | } |
| 434 | |
Austin Schuh | c2b7774 | 2015-11-26 16:18:27 -0800 | [diff] [blame] | 435 | UpdateObserver(U_); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 436 | |
| 437 | UpdateFFReference(); |
| 438 | } |
| 439 | |
| 440 | // Updates R() after any CapU operations happen on U(). |
| 441 | void UpdateFFReference() { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 442 | ff_U_ -= U_uncapped() - U(); |
| 443 | if (!Kff().isZero(0)) { |
| 444 | R_ = A() * R() + B() * ff_U_; |
| 445 | } |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Austin Schuh | c2b7774 | 2015-11-26 16:18:27 -0800 | [diff] [blame] | 448 | void UpdateObserver(const Eigen::Matrix<double, number_of_inputs, 1> &new_u) { |
| 449 | X_hat_ = A() * X_hat() + B() * new_u; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 452 | // Sets the current controller to be index, clamped to be within range. |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 453 | void set_controller_index(int index) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 454 | if (index < 0) { |
| 455 | controller_index_ = 0; |
| 456 | } else if (index >= static_cast<int>(controllers_.size())) { |
Brian Silverman | b8cd689 | 2013-03-17 23:36:24 -0700 | [diff] [blame] | 457 | controller_index_ = static_cast<int>(controllers_.size()) - 1; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 458 | } else { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 459 | controller_index_ = index; |
| 460 | } |
| 461 | } |
| 462 | |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 463 | int controller_index() const { return controller_index_; } |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 464 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 465 | protected: |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame^] | 466 | ::std::vector<::std::unique_ptr<StateFeedbackControllerConstants< |
| 467 | number_of_states, number_of_inputs, number_of_outputs>>> |
| 468 | controllers_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 469 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 470 | // These are accessible from non-templated subclasses. |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 471 | static constexpr int kNumStates = number_of_states; |
| 472 | static constexpr int kNumOutputs = number_of_outputs; |
| 473 | static constexpr int kNumInputs = number_of_inputs; |
| 474 | |
| 475 | // Portion of U which is based on the feed-forwards. |
| 476 | Eigen::Matrix<double, number_of_inputs, 1> ff_U_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 477 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 478 | private: |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 479 | // Internal state estimate. |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 480 | Eigen::Matrix<double, number_of_states, 1> X_hat_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 481 | // Current goal (Used by the feed-back controller). |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 482 | Eigen::Matrix<double, number_of_states, 1> R_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 483 | // Goal to go to in the next cycle (Used by Feed-Forward controller.) |
| 484 | Eigen::Matrix<double, number_of_states, 1> next_R_; |
| 485 | // Computed output after being capped. |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 486 | Eigen::Matrix<double, number_of_inputs, 1> U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 487 | // Computed output before being capped. |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 488 | Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_; |
| 489 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 490 | int controller_index_; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 491 | |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 492 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 493 | }; |
| 494 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 495 | #endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_ |