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