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