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