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