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 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 16 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 17 | typename PlantType, typename ObserverType> |
| 18 | class StateFeedbackLoop; |
| 19 | |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 20 | // For everything in this file, "inputs" and "outputs" are defined from the |
| 21 | // perspective of the plant. This means U is an input and Y is an output |
| 22 | // (because you give the plant U (powers) and it gives you back a Y (sensor |
| 23 | // values). This is the opposite of what they mean from the perspective of the |
| 24 | // controller (U is an output because that's what goes to the motors and Y is an |
| 25 | // input because that's what comes back from the sensors). |
| 26 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 27 | 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] | 28 | struct StateFeedbackPlantCoefficients final { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 29 | public: |
| 30 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 31 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 32 | StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other) |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 33 | : A(other.A), |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 34 | A_inv(other.A_inv), |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 35 | A_continuous(other.A_continuous), |
| 36 | B(other.B), |
| 37 | B_continuous(other.B_continuous), |
| 38 | C(other.C), |
| 39 | D(other.D), |
| 40 | U_min(other.U_min), |
| 41 | U_max(other.U_max) {} |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 42 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 43 | StateFeedbackPlantCoefficients( |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 44 | const Eigen::Matrix<double, number_of_states, number_of_states> &A, |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 45 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv, |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 46 | const Eigen::Matrix<double, number_of_states, number_of_states> |
| 47 | &A_continuous, |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 48 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B, |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 49 | const Eigen::Matrix<double, number_of_states, number_of_inputs> |
| 50 | &B_continuous, |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 51 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C, |
| 52 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D, |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 53 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max, |
| 54 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min) |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 55 | : A(A), |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 56 | A_inv(A_inv), |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 57 | A_continuous(A_continuous), |
| 58 | B(B), |
| 59 | B_continuous(B_continuous), |
| 60 | C(C), |
| 61 | D(D), |
| 62 | U_min(U_min), |
| 63 | U_max(U_max) {} |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 64 | |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 65 | const Eigen::Matrix<double, number_of_states, number_of_states> A; |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 66 | const Eigen::Matrix<double, number_of_states, number_of_states> A_inv; |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 67 | const Eigen::Matrix<double, number_of_states, number_of_states> A_continuous; |
| 68 | const Eigen::Matrix<double, number_of_states, number_of_inputs> B; |
| 69 | const Eigen::Matrix<double, number_of_states, number_of_inputs> B_continuous; |
| 70 | const Eigen::Matrix<double, number_of_outputs, number_of_states> C; |
| 71 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D; |
| 72 | const Eigen::Matrix<double, number_of_inputs, 1> U_min; |
| 73 | const Eigen::Matrix<double, number_of_inputs, 1> U_max; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 77 | class StateFeedbackPlant { |
| 78 | public: |
| 79 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 80 | |
| 81 | StateFeedbackPlant( |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 82 | ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients< |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 83 | number_of_states, number_of_inputs, number_of_outputs>>> |
| 84 | *coefficients) |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 85 | : coefficients_(::std::move(*coefficients)), index_(0) { |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 86 | Reset(); |
| 87 | } |
| 88 | |
| 89 | StateFeedbackPlant(StateFeedbackPlant &&other) |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 90 | : index_(other.index_) { |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 91 | ::std::swap(coefficients_, other.coefficients_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 92 | X_.swap(other.X_); |
| 93 | Y_.swap(other.Y_); |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 96 | virtual ~StateFeedbackPlant() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 97 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 98 | const Eigen::Matrix<double, number_of_states, number_of_states> &A() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 99 | return coefficients().A; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 100 | } |
| 101 | double A(int i, int j) const { return A()(i, j); } |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 102 | const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv() const { |
| 103 | return coefficients().A_inv; |
| 104 | } |
| 105 | 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] | 106 | const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 107 | return coefficients().B; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 108 | } |
| 109 | double B(int i, int j) const { return B()(i, j); } |
| 110 | const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 111 | return coefficients().C; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 112 | } |
| 113 | double C(int i, int j) const { return C()(i, j); } |
| 114 | const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 115 | return coefficients().D; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 116 | } |
| 117 | double D(int i, int j) const { return D()(i, j); } |
| 118 | const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 119 | return coefficients().U_min; |
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_min(int i, int j) const { return U_min()(i, j); } |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 122 | const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const { |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 123 | return coefficients().U_max; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 124 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 125 | 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] | 126 | |
| 127 | const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 128 | double X(int i, int j) const { return X()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 129 | const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 130 | double Y(int i, int j) const { return Y()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 131 | |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 132 | Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 133 | double &mutable_X(int i, int j) { return mutable_X()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 134 | Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 135 | double &mutable_Y(int i, int j) { return mutable_Y()(i, j); } |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 136 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 137 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 138 | number_of_outputs> |
| 139 | &coefficients(int index) const { |
| 140 | return *coefficients_[index]; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 143 | const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs, |
| 144 | number_of_outputs> |
| 145 | &coefficients() const { |
| 146 | return *coefficients_[index_]; |
| 147 | } |
| 148 | |
| 149 | int index() const { return index_; } |
| 150 | void set_index(int index) { |
| 151 | assert(index >= 0); |
| 152 | assert(index < static_cast<int>(coefficients_.size())); |
| 153 | index_ = index; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 157 | X_.setZero(); |
| 158 | Y_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 161 | // Assert that U is within the hardware range. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 162 | virtual void CheckU(const Eigen::Matrix<double, number_of_inputs, 1> &U) { |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 163 | for (int i = 0; i < kNumInputs; ++i) { |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 164 | if (U(i, 0) > U_max(i, 0) + 0.00001 || U(i, 0) < U_min(i, 0) - 0.00001) { |
| 165 | LOG(FATAL, "U out of range\n"); |
| 166 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 169 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 170 | // Computes the new X and Y given the control input. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 171 | void Update(const Eigen::Matrix<double, number_of_inputs, 1> &U) { |
Austin Schuh | 849f003 | 2013-03-03 23:59:53 -0800 | [diff] [blame] | 172 | // Powers outside of the range are more likely controller bugs than things |
| 173 | // that the plant should deal with. |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 174 | CheckU(U); |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 175 | X_ = Update(X(), U); |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 176 | Y_ = C() * X() + D() * U; |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 179 | Eigen::Matrix<double, number_of_states, 1> Update( |
| 180 | const Eigen::Matrix<double, number_of_states, 1> X, |
| 181 | const Eigen::Matrix<double, number_of_inputs, 1> &U) { |
| 182 | return A() * X + B() * U; |
| 183 | } |
| 184 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 185 | protected: |
| 186 | // these are accessible from non-templated subclasses |
Austin Schuh | b1cdb38 | 2013-03-01 22:53:52 -0800 | [diff] [blame] | 187 | static const int kNumStates = number_of_states; |
| 188 | static const int kNumOutputs = number_of_outputs; |
| 189 | static const int kNumInputs = number_of_inputs; |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 190 | |
| 191 | private: |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 192 | Eigen::Matrix<double, number_of_states, 1> X_; |
| 193 | Eigen::Matrix<double, number_of_outputs, 1> Y_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 194 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 195 | ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients< |
Austin Schuh | 64f17a5 | 2017-02-25 14:41:58 -0800 | [diff] [blame] | 196 | number_of_states, number_of_inputs, number_of_outputs>>> |
| 197 | coefficients_; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 198 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 199 | int index_; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 200 | |
| 201 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 202 | }; |
| 203 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 204 | // A container for all the controller coefficients. |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 205 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 206 | struct StateFeedbackControllerCoefficients final { |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 207 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 208 | |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 209 | const Eigen::Matrix<double, number_of_inputs, number_of_states> K; |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 210 | const Eigen::Matrix<double, number_of_inputs, number_of_states> Kff; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 211 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 212 | StateFeedbackControllerCoefficients( |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 213 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &K, |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 214 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 215 | : K(K), Kff(Kff) {} |
| 216 | }; |
| 217 | |
| 218 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 219 | class StateFeedbackController { |
| 220 | public: |
| 221 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 222 | |
| 223 | explicit StateFeedbackController( |
| 224 | ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients< |
| 225 | number_of_states, number_of_inputs, number_of_outputs>>> *controllers) |
| 226 | : coefficients_(::std::move(*controllers)) {} |
| 227 | |
| 228 | StateFeedbackController(StateFeedbackController &&other) |
| 229 | : index_(other.index_) { |
| 230 | ::std::swap(coefficients_, other.coefficients_); |
| 231 | } |
| 232 | |
| 233 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const { |
| 234 | return coefficients().K; |
| 235 | } |
| 236 | double K(int i, int j) const { return K()(i, j); } |
| 237 | const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff() const { |
| 238 | return coefficients().Kff; |
| 239 | } |
| 240 | double Kff(int i, int j) const { return Kff()(i, j); } |
| 241 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const { |
| 242 | return coefficients().L; |
| 243 | } |
| 244 | double L(int i, int j) const { return L()(i, j); } |
| 245 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 246 | void Reset() {} |
| 247 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 248 | // Sets the current controller to be index, clamped to be within range. |
| 249 | void set_index(int index) { |
| 250 | if (index < 0) { |
| 251 | index_ = 0; |
| 252 | } else if (index >= static_cast<int>(coefficients_.size())) { |
| 253 | index_ = static_cast<int>(coefficients_.size()) - 1; |
| 254 | } else { |
| 255 | index_ = index; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | int index() const { return index_; } |
| 260 | |
| 261 | const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs, |
| 262 | number_of_outputs> |
| 263 | &coefficients(int index) const { |
| 264 | return *coefficients_[index]; |
| 265 | } |
| 266 | |
| 267 | const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs, |
| 268 | number_of_outputs> |
| 269 | &coefficients() const { |
| 270 | return *coefficients_[index_]; |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | int index_ = 0; |
| 275 | ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients< |
| 276 | number_of_states, number_of_inputs, number_of_outputs>>> |
| 277 | coefficients_; |
| 278 | }; |
| 279 | |
| 280 | |
| 281 | // A container for all the observer coefficients. |
| 282 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 283 | struct StateFeedbackObserverCoefficients final { |
| 284 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 285 | |
| 286 | const Eigen::Matrix<double, number_of_states, number_of_outputs> L; |
| 287 | |
| 288 | StateFeedbackObserverCoefficients( |
| 289 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L) |
| 290 | : L(L) {} |
| 291 | }; |
| 292 | |
| 293 | template <int number_of_states, int number_of_inputs, int number_of_outputs> |
| 294 | class StateFeedbackObserver { |
| 295 | public: |
| 296 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 297 | |
| 298 | explicit StateFeedbackObserver( |
| 299 | ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients< |
| 300 | number_of_states, number_of_inputs, number_of_outputs>>> *observers) |
| 301 | : coefficients_(::std::move(*observers)) {} |
| 302 | |
| 303 | StateFeedbackObserver(StateFeedbackObserver &&other) |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 304 | : X_hat_(other.X_hat_), index_(other.index_) { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 305 | ::std::swap(coefficients_, other.coefficients_); |
| 306 | } |
| 307 | |
| 308 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const { |
| 309 | return coefficients().L; |
| 310 | } |
| 311 | double L(int i, int j) const { return L()(i, j); } |
| 312 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 313 | const Eigen::Matrix<double, number_of_states, 1> &X_hat() const { |
| 314 | return X_hat_; |
| 315 | } |
| 316 | Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; } |
| 317 | |
| 318 | void Reset() { X_hat_.setZero(); } |
| 319 | |
| 320 | void Predict(const StateFeedbackLoop< |
| 321 | number_of_states, number_of_inputs, number_of_outputs, |
| 322 | StateFeedbackPlant<number_of_states, number_of_inputs, |
| 323 | number_of_outputs>, |
| 324 | StateFeedbackObserver> &loop, |
| 325 | const Eigen::Matrix<double, number_of_inputs, 1> &new_u) { |
| 326 | mutable_X_hat() = loop.plant().A() * X_hat() + loop.plant().B() * new_u; |
| 327 | } |
| 328 | |
| 329 | void Correct(const StateFeedbackLoop< |
| 330 | number_of_states, number_of_inputs, number_of_outputs, |
| 331 | StateFeedbackPlant<number_of_states, number_of_inputs, |
| 332 | number_of_outputs>, |
| 333 | StateFeedbackObserver> &loop, |
| 334 | const Eigen::Matrix<double, number_of_inputs, 1> &U, |
| 335 | const Eigen::Matrix<double, number_of_outputs, 1> &Y) { |
| 336 | mutable_X_hat() += loop.plant().A_inv() * L() * |
| 337 | (Y - loop.plant().C() * X_hat() - loop.plant().D() * U); |
| 338 | } |
| 339 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 340 | // Sets the current controller to be index, clamped to be within range. |
| 341 | void set_index(int index) { |
| 342 | if (index < 0) { |
| 343 | index_ = 0; |
| 344 | } else if (index >= static_cast<int>(coefficients_.size())) { |
| 345 | index_ = static_cast<int>(coefficients_.size()) - 1; |
| 346 | } else { |
| 347 | index_ = index; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | int index() const { return index_; } |
| 352 | |
| 353 | const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs, |
| 354 | number_of_outputs> |
| 355 | &coefficients(int index) const { |
| 356 | return *coefficients_[index]; |
| 357 | } |
| 358 | |
| 359 | const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs, |
| 360 | number_of_outputs> |
| 361 | &coefficients() const { |
| 362 | return *coefficients_[index_]; |
| 363 | } |
| 364 | |
| 365 | private: |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 366 | // Internal state estimate. |
| 367 | Eigen::Matrix<double, number_of_states, 1> X_hat_; |
| 368 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 369 | int index_ = 0; |
| 370 | ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients< |
| 371 | number_of_states, number_of_inputs, number_of_outputs>>> |
| 372 | coefficients_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 373 | }; |
| 374 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 375 | template <int number_of_states, int number_of_inputs, int number_of_outputs, |
| 376 | typename PlantType = StateFeedbackPlant< |
| 377 | number_of_states, number_of_inputs, number_of_outputs>, |
| 378 | typename ObserverType = StateFeedbackObserver< |
| 379 | number_of_states, number_of_inputs, number_of_outputs>> |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 380 | class StateFeedbackLoop { |
| 381 | public: |
| 382 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 383 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 384 | explicit StateFeedbackLoop( |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 385 | PlantType &&plant, |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 386 | StateFeedbackController<number_of_states, number_of_inputs, |
| 387 | number_of_outputs> &&controller, |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 388 | ObserverType &&observer) |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 389 | : plant_(::std::move(plant)), |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 390 | controller_(::std::move(controller)), |
| 391 | observer_(::std::move(observer)) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 392 | Reset(); |
| 393 | } |
| 394 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 395 | StateFeedbackLoop(StateFeedbackLoop &&other) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 396 | : plant_(::std::move(other.plant_)), |
| 397 | controller_(::std::move(other.controller_)), |
| 398 | observer_(::std::move(other.observer_)) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 399 | R_.swap(other.R_); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 400 | next_R_.swap(other.next_R_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 401 | U_.swap(other.U_); |
| 402 | U_uncapped_.swap(other.U_uncapped_); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 403 | ff_U_.swap(other.ff_U_); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 406 | virtual ~StateFeedbackLoop() {} |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 407 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 408 | const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 409 | return observer().L(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 410 | } |
| 411 | double L(int i, int j) const { return L()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 412 | |
| 413 | const Eigen::Matrix<double, number_of_states, 1> &X_hat() const { |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 414 | return observer().X_hat(); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 415 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 416 | 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] | 417 | const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 418 | double R(int i, int j) const { return R()(i, j); } |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 419 | const Eigen::Matrix<double, number_of_states, 1> &next_R() const { |
| 420 | return next_R_; |
| 421 | } |
| 422 | 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] | 423 | const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 424 | double U(int i, int j) const { return U()(i, j); } |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 425 | const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const { |
| 426 | return U_uncapped_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 427 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 428 | 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] | 429 | const Eigen::Matrix<double, number_of_inputs, 1> &ff_U() const { |
| 430 | return ff_U_; |
| 431 | } |
| 432 | 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] | 433 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 434 | Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { |
| 435 | return observer_.mutable_X_hat(); |
| 436 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 437 | 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] | 438 | Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 439 | double &mutable_R(int i, int j) { return mutable_R()(i, j); } |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 440 | Eigen::Matrix<double, number_of_states, 1> &mutable_next_R() { |
| 441 | return next_R_; |
| 442 | } |
| 443 | 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] | 444 | Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 445 | double &mutable_U(int i, int j) { return mutable_U()(i, j); } |
Brian Silverman | 0ca790b | 2014-06-12 21:33:08 -0700 | [diff] [blame] | 446 | Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 447 | return U_uncapped_; |
| 448 | } |
Brian Silverman | a21c3a2 | 2014-06-12 21:49:15 -0700 | [diff] [blame] | 449 | double &mutable_U_uncapped(int i, int j) { |
| 450 | return mutable_U_uncapped()(i, j); |
| 451 | } |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 452 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 453 | const PlantType &plant() const { return plant_; } |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 454 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 455 | const StateFeedbackController<number_of_states, number_of_inputs, |
| 456 | number_of_outputs> |
Austin Schuh | 66c1988 | 2017-02-25 13:36:28 -0800 | [diff] [blame] | 457 | &controller() const { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 458 | return controller_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 461 | const ObserverType &observer() const { return observer_; } |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 462 | |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 463 | void Reset() { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 464 | R_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 465 | next_R_.setZero(); |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 466 | U_.setZero(); |
| 467 | U_uncapped_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 468 | ff_U_.setZero(); |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 469 | |
| 470 | plant_.Reset(); |
| 471 | controller_.Reset(); |
| 472 | observer_.Reset(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // If U is outside the hardware range, limit it before the plant tries to use |
| 476 | // it. |
| 477 | virtual void CapU() { |
Brian Silverman | 5808bcb | 2014-09-14 21:40:43 -0400 | [diff] [blame] | 478 | for (int i = 0; i < kNumInputs; ++i) { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 479 | if (U(i, 0) > plant().U_max(i, 0)) { |
| 480 | U_(i, 0) = plant().U_max(i, 0); |
| 481 | } else if (U(i, 0) < plant().U_min(i, 0)) { |
| 482 | U_(i, 0) = plant().U_min(i, 0); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 487 | // Corrects X_hat given the observation in Y. |
| 488 | void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) { |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 489 | observer_.Correct(*this, U(), Y); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Austin Schuh | 3f862bb | 2016-02-27 14:48:05 -0800 | [diff] [blame] | 492 | const Eigen::Matrix<double, number_of_states, 1> error() const { |
| 493 | return R() - X_hat(); |
| 494 | } |
| 495 | |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 496 | // Returns the calculated controller power. |
| 497 | virtual const Eigen::Matrix<double, number_of_inputs, 1> ControllerOutput() { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 498 | // TODO(austin): Should this live in StateSpaceController? |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 499 | ff_U_ = FeedForward(); |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 500 | return controller().K() * error() + ff_U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // Calculates the feed forwards power. |
| 504 | virtual const Eigen::Matrix<double, number_of_inputs, 1> FeedForward() { |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 505 | // TODO(austin): Should this live in StateSpaceController? |
| 506 | return controller().Kff() * (next_R() - plant().A() * R()); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 509 | // stop_motors is whether or not to output all 0s. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 510 | void Update(bool stop_motors) { |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 511 | if (stop_motors) { |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 512 | U_.setZero(); |
| 513 | U_uncapped_.setZero(); |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 514 | ff_U_.setZero(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 515 | } else { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 516 | U_ = U_uncapped_ = ControllerOutput(); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 517 | CapU(); |
| 518 | } |
| 519 | |
Austin Schuh | c2b7774 | 2015-11-26 16:18:27 -0800 | [diff] [blame] | 520 | UpdateObserver(U_); |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 521 | |
| 522 | UpdateFFReference(); |
| 523 | } |
| 524 | |
| 525 | // Updates R() after any CapU operations happen on U(). |
| 526 | void UpdateFFReference() { |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 527 | ff_U_ -= U_uncapped() - U(); |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 528 | if (!controller().Kff().isZero(0)) { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 529 | R_ = plant().A() * R() + plant().B() * ff_U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 530 | } |
Ben Fredrickson | 890c3fe | 2014-03-02 00:15:16 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Austin Schuh | c2b7774 | 2015-11-26 16:18:27 -0800 | [diff] [blame] | 533 | void UpdateObserver(const Eigen::Matrix<double, number_of_inputs, 1> &new_u) { |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 534 | observer_.Predict(*this, new_u); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 537 | // Sets the current controller to be index. |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 538 | void set_index(int index) { |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 539 | plant_.set_index(index); |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 540 | controller_.set_index(index); |
| 541 | observer_.set_index(index); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 544 | int index() const { return plant_.index(); } |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 545 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 546 | protected: |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 547 | PlantType plant_; |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 548 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 549 | StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs> |
| 550 | controller_; |
| 551 | |
Austin Schuh | e91f14c | 2017-02-25 19:43:57 -0800 | [diff] [blame^] | 552 | ObserverType observer_; |
Austin Schuh | 2054f5f | 2013-10-27 14:54:10 -0700 | [diff] [blame] | 553 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 554 | // These are accessible from non-templated subclasses. |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 555 | static constexpr int kNumStates = number_of_states; |
| 556 | static constexpr int kNumOutputs = number_of_outputs; |
| 557 | static constexpr int kNumInputs = number_of_inputs; |
| 558 | |
| 559 | // Portion of U which is based on the feed-forwards. |
| 560 | Eigen::Matrix<double, number_of_inputs, 1> ff_U_; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 561 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 562 | private: |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 563 | // Current goal (Used by the feed-back controller). |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 564 | Eigen::Matrix<double, number_of_states, 1> R_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 565 | // Goal to go to in the next cycle (Used by Feed-Forward controller.) |
| 566 | Eigen::Matrix<double, number_of_states, 1> next_R_; |
| 567 | // Computed output after being capped. |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 568 | Eigen::Matrix<double, number_of_inputs, 1> U_; |
Austin Schuh | b6a6d82 | 2016-02-08 00:20:40 -0800 | [diff] [blame] | 569 | // Computed output before being capped. |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 570 | Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_; |
| 571 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 572 | int index_; |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 573 | |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 574 | DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 575 | }; |
| 576 | |
Brian Silverman | 273d8a3 | 2014-05-10 22:19:09 -0700 | [diff] [blame] | 577 | #endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_ |