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