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