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