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