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()), |
| 29 | B_(other.B()), |
| 30 | C_(other.C()), |
| 31 | D_(other.D()), |
| 32 | U_min_(other.U_min()), |
| 33 | U_max_(other.U_max()) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 36 | StateFeedbackPlantCoefficients( |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 37 | const Eigen::Matrix<double, number_of_states, number_of_states> &A, |
| 38 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B, |
| 39 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C, |
| 40 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D, |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 41 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max, |
| 42 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min) |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 43 | : A_(A), |
| 44 | B_(B), |
| 45 | C_(C), |
| 46 | D_(D), |
| 47 | U_min_(U_min), |
| 48 | U_max_(U_max) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 51 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
| 52 | return A_; |
| 53 | } |
| 54 | double A(int i, int j) const { return A()(i, j); } |
| 55 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
| 56 | return B_; |
| 57 | } |
| 58 | double B(int i, int j) const { return B()(i, j); } |
| 59 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
| 60 | return C_; |
| 61 | } |
| 62 | double C(int i, int j) const { return C()(i, j); } |
| 63 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
| 64 | return D_; |
| 65 | } |
| 66 | double D(int i, int j) const { return D()(i, j); } |
| 67 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
| 68 | return U_min_; |
| 69 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 70 | 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] | 71 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
| 72 | return U_max_; |
| 73 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | private: |
| 77 | const Eigen::Matrix<double, number_of_states, number_of_states> A_; |
| 78 | const Eigen::Matrix<double, number_of_states, number_of_inputs> B_; |
| 79 | const Eigen::Matrix<double, number_of_outputs, number_of_states> C_; |
| 80 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D_; |
| 81 | const Eigen::Matrix<double, number_of_inputs, 1> U_min_; |
| 82 | const Eigen::Matrix<double, number_of_inputs, 1> U_max_; |
| 83 | |
| 84 | StateFeedbackPlantCoefficients &operator=( |
| 85 | StateFeedbackPlantCoefficients other) = delete; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 89 | class StateFeedbackPlant { |
| 90 | public: |
| 91 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 92 | |
| 93 | StateFeedbackPlant( |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 94 | ::std::vector< ::std::unique_ptr<StateFeedbackPlantCoefficients< |
| 95 | number_of_states, number_of_inputs, number_of_outputs>>> * |
| 96 | coefficients) |
| 97 | : coefficients_(::std::move(*coefficients)), plant_index_(0) { |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 98 | Reset(); |
| 99 | } |
| 100 | |
| 101 | StateFeedbackPlant(StateFeedbackPlant &&other) |
| 102 | : plant_index_(other.plant_index_) { |
| 103 | ::std::swap(coefficients_, other.coefficients_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 104 | X_.swap(other.X_); |
| 105 | Y_.swap(other.Y_); |
| 106 | U_.swap(other.U_); |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 109 | virtual ~StateFeedbackPlant() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 110 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 111 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 112 | return coefficients().A(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 113 | } |
| 114 | double A(int i, int j) const { return A()(i, j); } |
| 115 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 116 | return coefficients().B(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 117 | } |
| 118 | double B(int i, int j) const { return B()(i, j); } |
| 119 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 120 | return coefficients().C(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 121 | } |
| 122 | double C(int i, int j) const { return C()(i, j); } |
| 123 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 124 | return coefficients().D(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 125 | } |
| 126 | double D(int i, int j) const { return D()(i, j); } |
| 127 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 128 | return coefficients().U_min(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 129 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 130 | 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] | 131 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 132 | return coefficients().U_max(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 133 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 134 | 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] | 135 | |
| 136 | const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 137 | double X(int i, int j) const { return X()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 138 | const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 139 | double Y(int i, int j) const { return Y()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 140 | const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; } |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 141 | double U(int i, int j) const { return U()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 142 | |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 143 | Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 144 | double &mutable_X(int i, int j) { return mutable_X()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 145 | Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 146 | double &mutable_Y(int i, int j) { return mutable_Y()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 147 | Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 148 | double &mutable_U(int i, int j) { return mutable_U()(i, j); } |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 149 | |
| 150 | const StateFeedbackPlantCoefficients< |
| 151 | number_of_states, number_of_inputs, number_of_outputs> |
| 152 | &coefficients() const { |
| 153 | return *coefficients_[plant_index_]; |
| 154 | } |
| 155 | |
| 156 | int plant_index() const { return plant_index_; } |
| 157 | void set_plant_index(int plant_index) { |
| 158 | if (plant_index < 0) { |
| 159 | plant_index_ = 0; |
| 160 | } else if (plant_index >= static_cast<int>(coefficients_.size())) { |
Brian Silverman | b8cd689 | 2013-03-17 23:36:24 -0700 | [diff] [blame] | 161 | plant_index_ = static_cast<int>(coefficients_.size()) - 1; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 162 | } else { |
| 163 | plant_index_ = plant_index; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 168 | X_.setZero(); |
| 169 | Y_.setZero(); |
| 170 | U_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 173 | // Assert that U is within the hardware range. |
| 174 | virtual void CheckU() { |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 175 | for (int i = 0; i < kNumInputs; ++i) { |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 176 | assert(U(i, 0) <= U_max(i, 0) + 0.00001); |
| 177 | assert(U(i, 0) >= U_min(i, 0) - 0.00001); |
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 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 181 | // Computes the new X and Y given the control input. |
| 182 | void Update() { |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 183 | // Powers outside of the range are more likely controller bugs than things |
| 184 | // that the plant should deal with. |
| 185 | CheckU(); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 186 | X_ = A() * X() + B() * U(); |
| 187 | Y_ = C() * X() + D() * U(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | protected: |
| 191 | // these are accessible from non-templated subclasses |
Austin Schuh | b1cdb38 | 2013-03-01 22:53:52 -0800 | [diff] [blame] | 192 | static const int kNumStates = number_of_states; |
| 193 | static const int kNumOutputs = number_of_outputs; |
| 194 | static const int kNumInputs = number_of_inputs; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 197 | Eigen::Matrix<double, number_of_states, 1> X_; |
| 198 | Eigen::Matrix<double, number_of_outputs, 1> Y_; |
| 199 | Eigen::Matrix<double, number_of_inputs, 1> U_; |
| 200 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 201 | ::std::vector< ::std::unique_ptr<StateFeedbackPlantCoefficients< |
| 202 | number_of_states, number_of_inputs, number_of_outputs>>> coefficients_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 203 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 204 | int plant_index_; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 205 | |
| 206 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 209 | // A Controller is a structure which holds a plant and the K and L matrices. |
| 210 | // This is designed such that multiple controllers can share one set of state to |
| 211 | // support gain scheduling easily. |
| 212 | 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] | 213 | struct StateFeedbackController final { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 214 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 215 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 216 | const Eigen::Matrix<double, number_of_states, number_of_outputs> L; |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 217 | const Eigen::Matrix<double, number_of_inputs, number_of_states> K; |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 218 | const Eigen::Matrix<double, number_of_states, number_of_states> A_inv; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 219 | StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 220 | number_of_outputs> plant; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 221 | |
| 222 | StateFeedbackController( |
| 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 | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 225 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv, |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 226 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 227 | number_of_outputs> &plant) |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 228 | : L(L), |
| 229 | K(K), |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 230 | A_inv(A_inv), |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 231 | plant(plant) { |
| 232 | } |
| 233 | }; |
| 234 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 235 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 236 | class StateFeedbackLoop { |
| 237 | public: |
| 238 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 239 | |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 240 | StateFeedbackLoop(const StateFeedbackController< |
| 241 | number_of_states, number_of_inputs, number_of_outputs> &controller) |
| 242 | : controller_index_(0) { |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 243 | controllers_.emplace_back(new StateFeedbackController< |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 244 | number_of_states, number_of_inputs, number_of_outputs>(controller)); |
| 245 | Reset(); |
| 246 | } |
| 247 | |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 248 | StateFeedbackLoop( |
| 249 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L, |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 250 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &K, |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 251 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv, |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 252 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 253 | number_of_outputs> &plant) |
| 254 | : controller_index_(0) { |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 255 | controllers_.emplace_back( |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 256 | new StateFeedbackController<number_of_states, number_of_inputs, |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 257 | number_of_outputs>(L, K, A_inv, plant)); |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 258 | |
| 259 | Reset(); |
| 260 | } |
| 261 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 262 | StateFeedbackLoop(::std::vector< ::std::unique_ptr<StateFeedbackController< |
| 263 | number_of_states, number_of_inputs, number_of_outputs>>> *controllers) |
| 264 | : controllers_(::std::move(*controllers)), controller_index_(0) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 265 | Reset(); |
| 266 | } |
| 267 | |
| 268 | StateFeedbackLoop(StateFeedbackLoop &&other) { |
| 269 | X_hat_.swap(other.X_hat_); |
| 270 | R_.swap(other.R_); |
| 271 | U_.swap(other.U_); |
| 272 | U_uncapped_.swap(other.U_uncapped_); |
| 273 | ::std::swap(controllers_, other.controllers_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 274 | controller_index_ = other.controller_index_; |
| 275 | } |
| 276 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 277 | virtual ~StateFeedbackLoop() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 278 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 279 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 280 | return controller().plant.A(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 281 | } |
| 282 | double A(int i, int j) const { return A()(i, j); } |
| 283 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 284 | return controller().plant.B(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 285 | } |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 286 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv() const { |
| 287 | return controller().A_inv; |
| 288 | } |
| 289 | 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] | 290 | double B(int i, int j) const { return B()(i, j); } |
| 291 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 292 | return controller().plant.C(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 293 | } |
| 294 | double C(int i, int j) const { return C()(i, j); } |
| 295 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 296 | return controller().plant.D(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 297 | } |
| 298 | double D(int i, int j) const { return D()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 299 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
| 300 | return controller().plant.U_min(); |
| 301 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 302 | 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] | 303 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
| 304 | return controller().plant.U_max(); |
| 305 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 306 | 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] | 307 | |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 308 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 309 | return controller().K; |
| 310 | } |
| 311 | double K(int i, int j) const { return K()(i, j); } |
| 312 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const { |
| 313 | return controller().L; |
| 314 | } |
| 315 | double L(int i, int j) const { return L()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 316 | |
| 317 | const Eigen::Matrix<double, number_of_states, 1> &X_hat() const { |
| 318 | return X_hat_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 319 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 320 | 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] | 321 | const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 322 | double R(int i, int j) const { return R()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 323 | const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 324 | double U(int i, int j) const { return U()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 325 | const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const { |
| 326 | return U_uncapped_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 327 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 328 | double U_uncapped(int i, int j) const { return U_uncapped()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 329 | |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 330 | 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] | 331 | 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] | 332 | Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 333 | double &mutable_R(int i, int j) { return mutable_R()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 334 | Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 335 | double &mutable_U(int i, int j) { return mutable_U()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 336 | Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 337 | return U_uncapped_; |
| 338 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 339 | double &mutable_U_uncapped(int i, int j) { |
| 340 | return mutable_U_uncapped()(i, j); |
| 341 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 342 | |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 343 | const StateFeedbackController<number_of_states, number_of_inputs, |
| 344 | number_of_outputs> &controller() const { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 345 | return *controllers_[controller_index_]; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 348 | const StateFeedbackController<number_of_states, number_of_inputs, |
| 349 | number_of_outputs> &controller( |
| 350 | int index) const { |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 351 | return *controllers_[index]; |
| 352 | } |
| 353 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 354 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 355 | X_hat_.setZero(); |
| 356 | R_.setZero(); |
| 357 | U_.setZero(); |
| 358 | U_uncapped_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // If U is outside the hardware range, limit it before the plant tries to use |
| 362 | // it. |
| 363 | virtual void CapU() { |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 364 | for (int i = 0; i < kNumInputs; ++i) { |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 365 | if (U(i, 0) > U_max(i, 0)) { |
| 366 | U_(i, 0) = U_max(i, 0); |
| 367 | } else if (U(i, 0) < U_min(i, 0)) { |
| 368 | U_(i, 0) = U_min(i, 0); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 373 | // Corrects X_hat given the observation in Y. |
| 374 | void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 375 | X_hat_ += A_inv() * L() * (Y - C() * X_hat_ - D() * U()); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 378 | // stop_motors is whether or not to output all 0s. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 379 | void Update(bool stop_motors) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 380 | if (stop_motors) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 381 | U_.setZero(); |
| 382 | U_uncapped_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 383 | } else { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 384 | U_ = U_uncapped_ = K() * (R() - X_hat()); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 385 | CapU(); |
| 386 | } |
| 387 | |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 388 | UpdateObserver(); |
| 389 | } |
| 390 | |
| 391 | void UpdateObserver() { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 392 | X_hat_ = A() * X_hat() + B() * U(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 395 | // Sets the current controller to be index, clamped to be within range. |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 396 | void set_controller_index(int index) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 397 | if (index < 0) { |
| 398 | controller_index_ = 0; |
| 399 | } else if (index >= static_cast<int>(controllers_.size())) { |
Brian Silverman | b8cd689 | 2013-03-17 23:36:24 -0700 | [diff] [blame] | 400 | controller_index_ = static_cast<int>(controllers_.size()) - 1; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 401 | } else { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 402 | controller_index_ = index; |
| 403 | } |
| 404 | } |
| 405 | |
Austin Schuh | d34569d | 2014-02-18 20:26:38 -0800 | [diff] [blame] | 406 | int controller_index() const { return controller_index_; } |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 407 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 408 | protected: |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 409 | ::std::vector< ::std::unique_ptr<StateFeedbackController< |
| 410 | number_of_states, number_of_inputs, number_of_outputs>>> controllers_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 411 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 412 | // These are accessible from non-templated subclasses. |
Austin Schuh | b1cdb38 | 2013-03-01 22:53:52 -0800 | [diff] [blame] | 413 | static const int kNumStates = number_of_states; |
| 414 | static const int kNumOutputs = number_of_outputs; |
| 415 | static const int kNumInputs = number_of_inputs; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 416 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 417 | private: |
| 418 | Eigen::Matrix<double, number_of_states, 1> X_hat_; |
| 419 | Eigen::Matrix<double, number_of_states, 1> R_; |
| 420 | Eigen::Matrix<double, number_of_inputs, 1> U_; |
| 421 | Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_; |
| 422 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 423 | int controller_index_; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 424 | |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 425 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 426 | }; |
| 427 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 428 | #endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_ |