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