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> |
| 7 | |
Brian Silverman | 20fdbef | 2013-03-09 13:42:03 -0800 | [diff] [blame] | 8 | // Stupid vxworks system headers define it which blows up Eigen... |
| 9 | #undef m_data |
| 10 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 11 | #include "Eigen/Dense" |
| 12 | |
| 13 | 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] | 14 | class StateFeedbackPlantCoefficients { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 15 | public: |
| 16 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 17 | |
| 18 | const Eigen::Matrix<double, number_of_states, number_of_states> A; |
| 19 | const Eigen::Matrix<double, number_of_states, number_of_inputs> B; |
| 20 | const Eigen::Matrix<double, number_of_outputs, number_of_states> C; |
| 21 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D; |
| 22 | const Eigen::Matrix<double, number_of_inputs, 1> U_min; |
| 23 | const Eigen::Matrix<double, number_of_inputs, 1> U_max; |
| 24 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 25 | StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other) |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 26 | : A(other.A), |
| 27 | B(other.B), |
| 28 | C(other.C), |
| 29 | D(other.D), |
| 30 | U_min(other.U_min), |
| 31 | U_max(other.U_max) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 34 | StateFeedbackPlantCoefficients( |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 35 | const Eigen::Matrix<double, number_of_states, number_of_states> &A, |
| 36 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B, |
| 37 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C, |
| 38 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D, |
| 39 | const Eigen::Matrix<double, number_of_outputs, 1> &U_max, |
| 40 | const Eigen::Matrix<double, number_of_outputs, 1> &U_min) |
| 41 | : A(A), |
| 42 | B(B), |
| 43 | C(C), |
| 44 | D(D), |
| 45 | U_min(U_min), |
| 46 | U_max(U_max) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | protected: |
| 50 | // these are accessible from non-templated subclasses |
| 51 | static const int kNumStates = number_of_states; |
| 52 | static const int kNumOutputs = number_of_outputs; |
| 53 | static const int kNumInputs = number_of_inputs; |
| 54 | }; |
| 55 | |
| 56 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 57 | class StateFeedbackPlant { |
| 58 | public: |
| 59 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 60 | ::std::vector<StateFeedbackPlantCoefficients< |
| 61 | number_of_states, number_of_inputs, number_of_outputs> *> coefficients_; |
| 62 | |
| 63 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
| 64 | return coefficients().A; |
| 65 | } |
| 66 | double A(int i, int j) const { return A()(i, j); } |
| 67 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
| 68 | return coefficients().B; |
| 69 | } |
| 70 | double B(int i, int j) const { return B()(i, j); } |
| 71 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
| 72 | return coefficients().C; |
| 73 | } |
| 74 | double C(int i, int j) const { return C()(i, j); } |
| 75 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
| 76 | return coefficients().D; |
| 77 | } |
| 78 | double D(int i, int j) const { return D()(i, j); } |
| 79 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
| 80 | return coefficients().U_min; |
| 81 | } |
| 82 | double U_min(int i, int j) const { return U_min()(i, j); } |
| 83 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
| 84 | return coefficients().U_max; |
| 85 | } |
| 86 | double U_max(int i, int j) const { return U_max()(i, j); } |
| 87 | |
| 88 | const StateFeedbackPlantCoefficients< |
| 89 | number_of_states, number_of_inputs, number_of_outputs> |
| 90 | &coefficients() const { |
| 91 | return *coefficients_[plant_index_]; |
| 92 | } |
| 93 | |
| 94 | int plant_index() const { return plant_index_; } |
| 95 | void set_plant_index(int plant_index) { |
| 96 | if (plant_index < 0) { |
| 97 | plant_index_ = 0; |
| 98 | } else if (plant_index >= static_cast<int>(coefficients_.size())) { |
Brian Silverman | b8cd689 | 2013-03-17 23:36:24 -0700 | [diff] [blame] | 99 | plant_index_ = static_cast<int>(coefficients_.size()) - 1; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 100 | } else { |
| 101 | plant_index_ = plant_index; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void Reset() { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 106 | X.setZero(); |
| 107 | Y.setZero(); |
| 108 | U.setZero(); |
| 109 | } |
| 110 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 111 | Eigen::Matrix<double, number_of_states, 1> X; |
| 112 | Eigen::Matrix<double, number_of_outputs, 1> Y; |
| 113 | Eigen::Matrix<double, number_of_inputs, 1> U; |
| 114 | |
| 115 | StateFeedbackPlant( |
| 116 | const ::std::vector<StateFeedbackPlantCoefficients< |
| 117 | number_of_states, number_of_inputs, |
| 118 | number_of_outputs> *> &coefficients) |
| 119 | : coefficients_(coefficients), |
| 120 | plant_index_(0) { |
| 121 | Reset(); |
| 122 | } |
| 123 | |
| 124 | StateFeedbackPlant(StateFeedbackPlant &&other) |
| 125 | : plant_index_(0) { |
| 126 | Reset(); |
| 127 | ::std::swap(coefficients_, other.coefficients_); |
| 128 | } |
| 129 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 130 | virtual ~StateFeedbackPlant() {} |
| 131 | |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 132 | // Assert that U is within the hardware range. |
| 133 | virtual void CheckU() { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 134 | for (int i = 0; i < kNumOutputs; ++i) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 135 | assert(U(i, 0) <= U_max(i, 0)); |
| 136 | assert(U(i, 0) >= U_min(i, 0)); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 139 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 140 | // Computes the new X and Y given the control input. |
| 141 | void Update() { |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 142 | // Powers outside of the range are more likely controller bugs than things |
| 143 | // that the plant should deal with. |
| 144 | CheckU(); |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 145 | X = A() * X + B() * U; |
| 146 | Y = C() * X + D() * U; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | protected: |
| 150 | // these are accessible from non-templated subclasses |
Austin Schuh | b1cdb38 | 2013-03-01 22:53:52 -0800 | [diff] [blame] | 151 | static const int kNumStates = number_of_states; |
| 152 | static const int kNumOutputs = number_of_outputs; |
| 153 | static const int kNumInputs = number_of_inputs; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | int plant_index_; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 157 | }; |
| 158 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 159 | // A Controller is a structure which holds a plant and the K and L matrices. |
| 160 | // This is designed such that multiple controllers can share one set of state to |
| 161 | // support gain scheduling easily. |
| 162 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 163 | struct StateFeedbackController { |
| 164 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 165 | const Eigen::Matrix<double, number_of_states, number_of_outputs> L; |
| 166 | const Eigen::Matrix<double, number_of_outputs, number_of_states> K; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 167 | StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 168 | number_of_outputs> plant; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 169 | |
| 170 | StateFeedbackController( |
| 171 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L, |
| 172 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &K, |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 173 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 174 | number_of_outputs> &plant) |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 175 | : L(L), |
| 176 | K(K), |
| 177 | plant(plant) { |
| 178 | } |
| 179 | }; |
| 180 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 181 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 182 | class StateFeedbackLoop { |
| 183 | public: |
| 184 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 185 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 186 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
| 187 | return controller().plant.A; |
| 188 | } |
| 189 | double A(int i, int j) const { return A()(i, j); } |
| 190 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
| 191 | return controller().plant.B; |
| 192 | } |
| 193 | double B(int i, int j) const { return B()(i, j); } |
| 194 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
| 195 | return controller().plant.C; |
| 196 | } |
| 197 | double C(int i, int j) const { return C()(i, j); } |
| 198 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
| 199 | return controller().plant.D; |
| 200 | } |
| 201 | double D(int i, int j) const { return D()(i, j); } |
| 202 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &K() const { |
| 203 | return controller().K; |
| 204 | } |
| 205 | double K(int i, int j) const { return K()(i, j); } |
| 206 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const { |
| 207 | return controller().L; |
| 208 | } |
| 209 | double L(int i, int j) const { return L()(i, j); } |
| 210 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
| 211 | return controller().plant.U_min; |
| 212 | } |
| 213 | double U_min(int i, int j) const { return U_min()(i, j); } |
| 214 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
| 215 | return controller().plant.U_max; |
| 216 | } |
| 217 | 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] | 218 | |
| 219 | Eigen::Matrix<double, number_of_states, 1> X_hat; |
| 220 | Eigen::Matrix<double, number_of_states, 1> R; |
| 221 | Eigen::Matrix<double, number_of_inputs, 1> U; |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 222 | Eigen::Matrix<double, number_of_inputs, 1> U_uncapped; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 223 | Eigen::Matrix<double, number_of_outputs, 1> U_ff; |
| 224 | Eigen::Matrix<double, number_of_outputs, 1> Y; |
| 225 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 226 | ::std::vector<StateFeedbackController<number_of_states, number_of_inputs, |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 227 | number_of_outputs> *> controllers_; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 228 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 229 | const StateFeedbackController< |
| 230 | number_of_states, number_of_inputs, number_of_outputs> |
| 231 | &controller() const { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 232 | return *controllers_[controller_index_]; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void Reset() { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 236 | X_hat.setZero(); |
| 237 | R.setZero(); |
| 238 | U.setZero(); |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 239 | U_uncapped.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 240 | U_ff.setZero(); |
| 241 | Y.setZero(); |
| 242 | } |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 243 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 244 | StateFeedbackLoop( |
| 245 | const StateFeedbackController<number_of_states, number_of_inputs, |
| 246 | number_of_outputs> &controller) |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 247 | : controller_index_(0) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 248 | controllers_.push_back( |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 249 | new StateFeedbackController<number_of_states, number_of_inputs, |
| 250 | number_of_outputs>(controller)); |
| 251 | Reset(); |
| 252 | } |
| 253 | |
| 254 | StateFeedbackLoop( |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 255 | const ::std::vector<StateFeedbackController< |
| 256 | number_of_states, number_of_inputs, |
| 257 | number_of_outputs> *> &controllers) |
| 258 | : controllers_(controllers), |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 259 | controller_index_(0) { |
| 260 | Reset(); |
| 261 | } |
| 262 | |
| 263 | StateFeedbackLoop( |
| 264 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L, |
| 265 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &K, |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 266 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 267 | number_of_outputs> &plant) |
| 268 | : controller_index_(0) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 269 | controllers_.push_back( |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 270 | new StateFeedbackController<number_of_states, number_of_inputs, |
| 271 | number_of_outputs>(L, K, plant)); |
| 272 | |
| 273 | Reset(); |
| 274 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 275 | virtual ~StateFeedbackLoop() {} |
| 276 | |
| 277 | virtual void FeedForward() { |
| 278 | for (int i = 0; i < number_of_outputs; ++i) { |
| 279 | U_ff[i] = 0.0; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // If U is outside the hardware range, limit it before the plant tries to use |
| 284 | // it. |
| 285 | virtual void CapU() { |
| 286 | for (int i = 0; i < kNumOutputs; ++i) { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 287 | if (U(i, 0) > U_max(i, 0)) { |
| 288 | U(i, 0) = U_max(i, 0); |
| 289 | } else if (U(i, 0) < U_min(i, 0)) { |
| 290 | U(i, 0) = U_min(i, 0); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // update_observer is whether or not to use the values in Y. |
| 296 | // stop_motors is whether or not to output all 0s. |
| 297 | void Update(bool update_observer, bool stop_motors) { |
| 298 | if (stop_motors) { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 299 | U.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 300 | } else { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 301 | U = U_uncapped = K() * (R - X_hat); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 302 | CapU(); |
| 303 | } |
| 304 | |
| 305 | if (update_observer) { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 306 | X_hat = (A() - L() * C()) * X_hat + L() * Y + B() * U; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 307 | } else { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 308 | X_hat = A() * X_hat + B() * U; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 312 | // Sets the current controller to be index and verifies that it isn't out of |
| 313 | // range. |
| 314 | void set_controller_index(int index) { |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 315 | if (index < 0) { |
| 316 | controller_index_ = 0; |
| 317 | } else if (index >= static_cast<int>(controllers_.size())) { |
Brian Silverman | b8cd689 | 2013-03-17 23:36:24 -0700 | [diff] [blame] | 318 | controller_index_ = static_cast<int>(controllers_.size()) - 1; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 319 | } else { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 320 | controller_index_ = index; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void controller_index() const { return controller_index_; } |
| 325 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 326 | protected: |
| 327 | // these are accessible from non-templated subclasses |
Austin Schuh | b1cdb38 | 2013-03-01 22:53:52 -0800 | [diff] [blame] | 328 | static const int kNumStates = number_of_states; |
| 329 | static const int kNumOutputs = number_of_outputs; |
| 330 | static const int kNumInputs = number_of_inputs; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 331 | |
| 332 | int controller_index_; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | #endif // FRC971_CONTROL_LOOPS_STATEFEEDBACKLOOP_H_ |