blob: b9765fc676df4f9414ef847957f06d6ec5bdaa5e [file] [log] [blame]
Brian Silverman273d8a32014-05-10 22:19:09 -07001#ifndef FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_
2#define FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_
Austin Schuhdc1c84a2013-02-23 16:33:10 -08003
Austin Schuh849f0032013-03-03 23:59:53 -08004#include <assert.h>
Austin Schuhdc1c84a2013-02-23 16:33:10 -08005
Austin Schuhcda86af2014-02-16 16:16:39 -08006#include <iostream>
Austin Schuhc5fceb82017-02-25 16:24:12 -08007#include <memory>
8#include <utility>
9#include <vector>
Austin Schuh3ad5ed82017-02-25 21:36:19 -080010#include <chrono>
Brian Silvermanc571e052013-03-13 17:58:56 -070011
Austin Schuhdc1c84a2013-02-23 16:33:10 -080012#include "Eigen/Dense"
Austin Schuh3ad5ed82017-02-25 21:36:19 -080013#include "unsupported/Eigen/MatrixFunctions"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080014
Austin Schuhcda86af2014-02-16 16:16:39 -080015#include "aos/common/logging/logging.h"
Brian Silverman0a151c92014-05-02 15:28:44 -070016#include "aos/common/macros.h"
17
Austin Schuhe91f14c2017-02-25 19:43:57 -080018template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -080019 typename PlantType, typename ObserverType, typename Scalar>
Austin Schuhe91f14c2017-02-25 19:43:57 -080020class StateFeedbackLoop;
21
Brian Silverman5808bcb2014-09-14 21:40:43 -040022// For everything in this file, "inputs" and "outputs" are defined from the
23// perspective of the plant. This means U is an input and Y is an output
24// (because you give the plant U (powers) and it gives you back a Y (sensor
25// values). This is the opposite of what they mean from the perspective of the
26// controller (U is an output because that's what goes to the motors and Y is an
27// input because that's what comes back from the sensors).
28
Austin Schuh20388b62017-11-23 22:40:46 -080029template <int number_of_states, int number_of_inputs, int number_of_outputs,
30 typename Scalar = double>
Austin Schuh64f17a52017-02-25 14:41:58 -080031struct StateFeedbackPlantCoefficients final {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080032 public:
33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
34
Austin Schuhe3490622013-03-13 01:24:30 -070035 StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other)
Austin Schuh64f17a52017-02-25 14:41:58 -080036 : A(other.A),
Austin Schuhc5fceb82017-02-25 16:24:12 -080037 A_inv(other.A_inv),
Austin Schuh64f17a52017-02-25 14:41:58 -080038 B(other.B),
Austin Schuh64f17a52017-02-25 14:41:58 -080039 C(other.C),
40 D(other.D),
41 U_min(other.U_min),
42 U_max(other.U_max) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080043
Austin Schuhe3490622013-03-13 01:24:30 -070044 StateFeedbackPlantCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -080045 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A,
46 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A_inv,
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)
Austin Schuh3ad5ed82017-02-25 21:36:19 -080052 : A(A), A_inv(A_inv), B(B), C(C), D(D), U_min(U_min), U_max(U_max) {}
Austin Schuhe3490622013-03-13 01:24:30 -070053
Austin Schuh20388b62017-11-23 22:40:46 -080054 const Eigen::Matrix<Scalar, number_of_states, number_of_states> A;
55 const Eigen::Matrix<Scalar, number_of_states, number_of_states> A_inv;
56 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> B;
57 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> C;
58 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> D;
59 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_min;
60 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_max;
Austin Schuhe3490622013-03-13 01:24:30 -070061};
62
Austin Schuh20388b62017-11-23 22:40:46 -080063template <int number_of_states, int number_of_inputs, int number_of_outputs,
64 typename Scalar = double>
Austin Schuhe3490622013-03-13 01:24:30 -070065class StateFeedbackPlant {
66 public:
67 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070068
69 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080070 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -080071 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh66c19882017-02-25 13:36:28 -080072 *coefficients)
Austin Schuhc5fceb82017-02-25 16:24:12 -080073 : coefficients_(::std::move(*coefficients)), index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070074 Reset();
75 }
76
77 StateFeedbackPlant(StateFeedbackPlant &&other)
Austin Schuhc5fceb82017-02-25 16:24:12 -080078 : index_(other.index_) {
Brian Silverman0a151c92014-05-02 15:28:44 -070079 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -070080 X_.swap(other.X_);
81 Y_.swap(other.Y_);
Brian Silverman0a151c92014-05-02 15:28:44 -070082 }
83
Austin Schuh1a387962015-01-31 16:36:20 -080084 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -070085
Austin Schuh20388b62017-11-23 22:40:46 -080086 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080087 return coefficients().A;
Austin Schuhe3490622013-03-13 01:24:30 -070088 }
Austin Schuh20388b62017-11-23 22:40:46 -080089 Scalar A(int i, int j) const { return A()(i, j); }
90 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A_inv() const {
Austin Schuhc5fceb82017-02-25 16:24:12 -080091 return coefficients().A_inv;
92 }
Austin Schuh20388b62017-11-23 22:40:46 -080093 Scalar A_inv(int i, int j) const { return A_inv()(i, j); }
94 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080095 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -070096 }
Austin Schuh20388b62017-11-23 22:40:46 -080097 Scalar B(int i, int j) const { return B()(i, j); }
98 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080099 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -0700100 }
Austin Schuh20388b62017-11-23 22:40:46 -0800101 Scalar C(int i, int j) const { return C()(i, j); }
102 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800103 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -0700104 }
Austin Schuh20388b62017-11-23 22:40:46 -0800105 Scalar D(int i, int j) const { return D()(i, j); }
106 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800107 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700108 }
Austin Schuh20388b62017-11-23 22:40:46 -0800109 Scalar U_min(int i, int j) const { return U_min()(i, j); }
110 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800111 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700112 }
Austin Schuh20388b62017-11-23 22:40:46 -0800113 Scalar U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700114
Austin Schuh20388b62017-11-23 22:40:46 -0800115 const Eigen::Matrix<Scalar, number_of_states, 1> &X() const { return X_; }
116 Scalar X(int i, int j) const { return X()(i, j); }
117 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y() const { return Y_; }
118 Scalar Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700119
Austin Schuh20388b62017-11-23 22:40:46 -0800120 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X() { return X_; }
121 Scalar &mutable_X(int i, int j) { return mutable_X()(i, j); }
122 Eigen::Matrix<Scalar, number_of_outputs, 1> &mutable_Y() { return Y_; }
123 Scalar &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700124
Austin Schuhb6a6d822016-02-08 00:20:40 -0800125 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800126 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800127 &coefficients(int index) const {
128 return *coefficients_[index];
Austin Schuhe3490622013-03-13 01:24:30 -0700129 }
130
Austin Schuhc5fceb82017-02-25 16:24:12 -0800131 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800132 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800133 &coefficients() const {
134 return *coefficients_[index_];
135 }
136
137 int index() const { return index_; }
138 void set_index(int index) {
139 assert(index >= 0);
140 assert(index < static_cast<int>(coefficients_.size()));
141 index_ = index;
Austin Schuhe3490622013-03-13 01:24:30 -0700142 }
143
144 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700145 X_.setZero();
146 Y_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800147 }
148
Austin Schuh849f0032013-03-03 23:59:53 -0800149 // Assert that U is within the hardware range.
Austin Schuh20388b62017-11-23 22:40:46 -0800150 virtual void CheckU(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400151 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuh20388b62017-11-23 22:40:46 -0800152 if (U(i, 0) > U_max(i, 0) + static_cast<Scalar>(0.00001) ||
153 U(i, 0) < U_min(i, 0) - static_cast<Scalar>(0.00001)) {
Austin Schuh66c19882017-02-25 13:36:28 -0800154 LOG(FATAL, "U out of range\n");
155 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800156 }
157 }
Austin Schuh849f0032013-03-03 23:59:53 -0800158
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800159 // Computes the new X and Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800160 void Update(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh849f0032013-03-03 23:59:53 -0800161 // Powers outside of the range are more likely controller bugs than things
162 // that the plant should deal with.
Austin Schuh66c19882017-02-25 13:36:28 -0800163 CheckU(U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800164 X_ = Update(X(), U);
Austin Schuh01c7b252017-03-05 00:59:31 -0800165 UpdateY(U);
166 }
167
168 // Computes the new Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800169 void UpdateY(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh66c19882017-02-25 13:36:28 -0800170 Y_ = C() * X() + D() * U;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800171 }
172
Austin Schuh20388b62017-11-23 22:40:46 -0800173 Eigen::Matrix<Scalar, number_of_states, 1> Update(
174 const Eigen::Matrix<Scalar, number_of_states, 1> X,
175 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800176 return A() * X + B() * U;
177 }
178
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800179 protected:
180 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800181 static const int kNumStates = number_of_states;
182 static const int kNumOutputs = number_of_outputs;
183 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700184
185 private:
Austin Schuh20388b62017-11-23 22:40:46 -0800186 Eigen::Matrix<Scalar, number_of_states, 1> X_;
187 Eigen::Matrix<Scalar, number_of_outputs, 1> Y_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700188
Austin Schuhb6a6d822016-02-08 00:20:40 -0800189 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800190 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh64f17a52017-02-25 14:41:58 -0800191 coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700192
Austin Schuhc5fceb82017-02-25 16:24:12 -0800193 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700194
195 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800196};
197
Austin Schuh32501832017-02-25 18:32:56 -0800198// A container for all the controller coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800199template <int number_of_states, int number_of_inputs, int number_of_outputs,
200 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800201struct StateFeedbackControllerCoefficients final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700202 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700203
Austin Schuh20388b62017-11-23 22:40:46 -0800204 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> K;
205 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> Kff;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700206
Austin Schuh32501832017-02-25 18:32:56 -0800207 StateFeedbackControllerCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -0800208 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K,
209 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff)
Austin Schuh32501832017-02-25 18:32:56 -0800210 : K(K), Kff(Kff) {}
211};
212
Austin Schuh20388b62017-11-23 22:40:46 -0800213template <int number_of_states, int number_of_inputs, int number_of_outputs,
214 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800215class StateFeedbackController {
216 public:
217 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
218
219 explicit StateFeedbackController(
220 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800221 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
222 *controllers)
Austin Schuh32501832017-02-25 18:32:56 -0800223 : coefficients_(::std::move(*controllers)) {}
224
225 StateFeedbackController(StateFeedbackController &&other)
226 : index_(other.index_) {
227 ::std::swap(coefficients_, other.coefficients_);
228 }
229
Austin Schuh20388b62017-11-23 22:40:46 -0800230 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K() const {
Austin Schuh32501832017-02-25 18:32:56 -0800231 return coefficients().K;
232 }
Austin Schuh20388b62017-11-23 22:40:46 -0800233 Scalar K(int i, int j) const { return K()(i, j); }
234 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff() const {
Austin Schuh32501832017-02-25 18:32:56 -0800235 return coefficients().Kff;
236 }
Austin Schuh20388b62017-11-23 22:40:46 -0800237 Scalar Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800238
Austin Schuhe91f14c2017-02-25 19:43:57 -0800239 void Reset() {}
240
Austin Schuh32501832017-02-25 18:32:56 -0800241 // Sets the current controller to be index, clamped to be within range.
242 void set_index(int index) {
243 if (index < 0) {
244 index_ = 0;
245 } else if (index >= static_cast<int>(coefficients_.size())) {
246 index_ = static_cast<int>(coefficients_.size()) - 1;
247 } else {
248 index_ = index;
249 }
250 }
251
252 int index() const { return index_; }
253
254 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800255 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800256 &coefficients(int index) const {
257 return *coefficients_[index];
258 }
259
260 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800261 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800262 &coefficients() const {
263 return *coefficients_[index_];
264 }
265
266 private:
267 int index_ = 0;
268 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800269 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800270 coefficients_;
271};
272
Austin Schuh32501832017-02-25 18:32:56 -0800273// A container for all the observer coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800274template <int number_of_states, int number_of_inputs, int number_of_outputs,
275 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800276struct StateFeedbackObserverCoefficients final {
277 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
278
Austin Schuh20388b62017-11-23 22:40:46 -0800279 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> L;
Austin Schuh32501832017-02-25 18:32:56 -0800280
281 StateFeedbackObserverCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -0800282 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &L)
Austin Schuh32501832017-02-25 18:32:56 -0800283 : L(L) {}
284};
285
Austin Schuh20388b62017-11-23 22:40:46 -0800286template <int number_of_states, int number_of_inputs, int number_of_outputs,
287 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800288class StateFeedbackObserver {
289 public:
290 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
291
292 explicit StateFeedbackObserver(
293 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800294 number_of_states, number_of_inputs, number_of_outputs, Scalar>>> *observers)
Austin Schuh32501832017-02-25 18:32:56 -0800295 : coefficients_(::std::move(*observers)) {}
296
297 StateFeedbackObserver(StateFeedbackObserver &&other)
Austin Schuhe91f14c2017-02-25 19:43:57 -0800298 : X_hat_(other.X_hat_), index_(other.index_) {
Austin Schuh32501832017-02-25 18:32:56 -0800299 ::std::swap(coefficients_, other.coefficients_);
300 }
301
Austin Schuh20388b62017-11-23 22:40:46 -0800302 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &L() const {
Austin Schuh32501832017-02-25 18:32:56 -0800303 return coefficients().L;
304 }
Austin Schuh20388b62017-11-23 22:40:46 -0800305 Scalar L(int i, int j) const { return L()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800306
Austin Schuh20388b62017-11-23 22:40:46 -0800307 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800308 return X_hat_;
309 }
Austin Schuh20388b62017-11-23 22:40:46 -0800310 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800311
Austin Schuh6501d232017-11-23 20:35:27 -0800312 void Reset(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800313 number_of_outputs, Scalar> * /*loop*/) {
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800314 X_hat_.setZero();
315 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800316
Austin Schuh6501d232017-11-23 20:35:27 -0800317 void Predict(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800318 number_of_outputs, Scalar> *plant,
319 const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh6501d232017-11-23 20:35:27 -0800320 ::std::chrono::nanoseconds /*dt*/) {
321 mutable_X_hat() = plant->Update(X_hat(), new_u);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800322 }
323
Austin Schuh6501d232017-11-23 20:35:27 -0800324 void Correct(const StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800325 number_of_outputs, Scalar> &plant,
326 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U,
327 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Austin Schuh6501d232017-11-23 20:35:27 -0800328 mutable_X_hat() +=
329 plant.A_inv() * L() * (Y - plant.C() * X_hat() - plant.D() * U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800330 }
331
Austin Schuh32501832017-02-25 18:32:56 -0800332 // Sets the current controller to be index, clamped to be within range.
333 void set_index(int index) {
334 if (index < 0) {
335 index_ = 0;
336 } else if (index >= static_cast<int>(coefficients_.size())) {
337 index_ = static_cast<int>(coefficients_.size()) - 1;
338 } else {
339 index_ = index;
340 }
341 }
342
343 int index() const { return index_; }
344
345 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800346 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800347 &coefficients(int index) const {
348 return *coefficients_[index];
349 }
350
351 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800352 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800353 &coefficients() const {
354 return *coefficients_[index_];
355 }
356
357 private:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800358 // Internal state estimate.
Austin Schuh20388b62017-11-23 22:40:46 -0800359 Eigen::Matrix<Scalar, number_of_states, 1> X_hat_;
Austin Schuhe91f14c2017-02-25 19:43:57 -0800360
Austin Schuh32501832017-02-25 18:32:56 -0800361 int index_ = 0;
362 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800363 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800364 coefficients_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700365};
366
Austin Schuhe91f14c2017-02-25 19:43:57 -0800367template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800368 typename Scalar = double,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800369 typename PlantType = StateFeedbackPlant<
Austin Schuh20388b62017-11-23 22:40:46 -0800370 number_of_states, number_of_inputs, number_of_outputs, Scalar>,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800371 typename ObserverType = StateFeedbackObserver<
Austin Schuh20388b62017-11-23 22:40:46 -0800372 number_of_states, number_of_inputs, number_of_outputs, Scalar>>
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800373class StateFeedbackLoop {
374 public:
375 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
376
Austin Schuh32501832017-02-25 18:32:56 -0800377 explicit StateFeedbackLoop(
Austin Schuhe91f14c2017-02-25 19:43:57 -0800378 PlantType &&plant,
Austin Schuh32501832017-02-25 18:32:56 -0800379 StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800380 number_of_outputs, Scalar> &&controller,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800381 ObserverType &&observer)
Austin Schuhc5fceb82017-02-25 16:24:12 -0800382 : plant_(::std::move(plant)),
Austin Schuh32501832017-02-25 18:32:56 -0800383 controller_(::std::move(controller)),
384 observer_(::std::move(observer)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700385 Reset();
386 }
387
Austin Schuhc5fceb82017-02-25 16:24:12 -0800388 StateFeedbackLoop(StateFeedbackLoop &&other)
Austin Schuh32501832017-02-25 18:32:56 -0800389 : plant_(::std::move(other.plant_)),
390 controller_(::std::move(other.controller_)),
391 observer_(::std::move(other.observer_)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700392 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800393 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700394 U_.swap(other.U_);
395 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800396 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700397 }
398
Austin Schuh1a387962015-01-31 16:36:20 -0800399 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700400
Austin Schuh20388b62017-11-23 22:40:46 -0800401 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800402 return observer().X_hat();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700403 }
Austin Schuh20388b62017-11-23 22:40:46 -0800404 Scalar X_hat(int i, int j) const { return X_hat()(i, j); }
405 const Eigen::Matrix<Scalar, number_of_states, 1> &R() const { return R_; }
406 Scalar R(int i, int j) const { return R()(i, j); }
407 const Eigen::Matrix<Scalar, number_of_states, 1> &next_R() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800408 return next_R_;
409 }
Austin Schuh20388b62017-11-23 22:40:46 -0800410 Scalar next_R(int i, int j) const { return next_R()(i, j); }
411 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U() const { return U_; }
412 Scalar U(int i, int j) const { return U()(i, j); }
413 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_uncapped() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700414 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700415 }
Austin Schuh20388b62017-11-23 22:40:46 -0800416 Scalar U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
417 const Eigen::Matrix<Scalar, number_of_inputs, 1> &ff_U() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800418 return ff_U_;
419 }
Austin Schuh20388b62017-11-23 22:40:46 -0800420 Scalar ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700421
Austin Schuh20388b62017-11-23 22:40:46 -0800422 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800423 return observer_.mutable_X_hat();
424 }
Austin Schuh20388b62017-11-23 22:40:46 -0800425 Scalar &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); }
426 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_R() { return R_; }
427 Scalar &mutable_R(int i, int j) { return mutable_R()(i, j); }
428 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_next_R() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800429 return next_R_;
430 }
Austin Schuh20388b62017-11-23 22:40:46 -0800431 Scalar &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
432 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U() { return U_; }
433 Scalar &mutable_U(int i, int j) { return mutable_U()(i, j); }
434 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700435 return U_uncapped_;
436 }
Austin Schuh20388b62017-11-23 22:40:46 -0800437 Scalar &mutable_U_uncapped(int i, int j) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700438 return mutable_U_uncapped()(i, j);
439 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800440
Austin Schuhe91f14c2017-02-25 19:43:57 -0800441 const PlantType &plant() const { return plant_; }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800442 PlantType *mutable_plant() { return &plant_; }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800443
Austin Schuh32501832017-02-25 18:32:56 -0800444 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800445 number_of_outputs, Scalar>
Austin Schuh66c19882017-02-25 13:36:28 -0800446 &controller() const {
Austin Schuh32501832017-02-25 18:32:56 -0800447 return controller_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700448 }
449
Austin Schuhe91f14c2017-02-25 19:43:57 -0800450 const ObserverType &observer() const { return observer_; }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700451
Austin Schuh9644e1c2013-03-12 00:40:36 -0700452 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700453 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800454 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700455 U_.setZero();
456 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800457 ff_U_.setZero();
Austin Schuhe91f14c2017-02-25 19:43:57 -0800458
459 plant_.Reset();
460 controller_.Reset();
Austin Schuh6501d232017-11-23 20:35:27 -0800461 observer_.Reset(this->mutable_plant());
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800462 }
463
464 // If U is outside the hardware range, limit it before the plant tries to use
465 // it.
466 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400467 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800468 if (U(i, 0) > plant().U_max(i, 0)) {
469 U_(i, 0) = plant().U_max(i, 0);
470 } else if (U(i, 0) < plant().U_min(i, 0)) {
471 U_(i, 0) = plant().U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800472 }
473 }
474 }
475
Austin Schuhf9286cd2014-02-11 00:51:09 -0800476 // Corrects X_hat given the observation in Y.
Austin Schuh20388b62017-11-23 22:40:46 -0800477 void Correct(const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Austin Schuh6501d232017-11-23 20:35:27 -0800478 observer_.Correct(this->plant(), U(), Y);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800479 }
480
Austin Schuh20388b62017-11-23 22:40:46 -0800481 const Eigen::Matrix<Scalar, number_of_states, 1> error() const {
Austin Schuh3f862bb2016-02-27 14:48:05 -0800482 return R() - X_hat();
483 }
484
Austin Schuhb6a6d822016-02-08 00:20:40 -0800485 // Returns the calculated controller power.
Austin Schuh20388b62017-11-23 22:40:46 -0800486 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> ControllerOutput() {
Austin Schuh32501832017-02-25 18:32:56 -0800487 // TODO(austin): Should this live in StateSpaceController?
Austin Schuhb6a6d822016-02-08 00:20:40 -0800488 ff_U_ = FeedForward();
Austin Schuh32501832017-02-25 18:32:56 -0800489 return controller().K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800490 }
491
492 // Calculates the feed forwards power.
Austin Schuh20388b62017-11-23 22:40:46 -0800493 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> FeedForward() {
Austin Schuh32501832017-02-25 18:32:56 -0800494 // TODO(austin): Should this live in StateSpaceController?
495 return controller().Kff() * (next_R() - plant().A() * R());
Austin Schuhb6a6d822016-02-08 00:20:40 -0800496 }
497
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800498 // stop_motors is whether or not to output all 0s.
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800499 void Update(bool stop_motors,
500 ::std::chrono::nanoseconds dt = ::std::chrono::milliseconds(5)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800501 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700502 U_.setZero();
503 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800504 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800505 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800506 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800507 CapU();
508 }
509
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800510 UpdateObserver(U_, dt);
Austin Schuh093535c2016-03-05 23:21:00 -0800511
512 UpdateFFReference();
513 }
514
515 // Updates R() after any CapU operations happen on U().
516 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800517 ff_U_ -= U_uncapped() - U();
Austin Schuh32501832017-02-25 18:32:56 -0800518 if (!controller().Kff().isZero(0)) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800519 R_ = plant().A() * R() + plant().B() * ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800520 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000521 }
522
Austin Schuh20388b62017-11-23 22:40:46 -0800523 void UpdateObserver(const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800524 ::std::chrono::nanoseconds dt) {
Austin Schuh6501d232017-11-23 20:35:27 -0800525 observer_.Predict(this->mutable_plant(), new_u, dt);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800526 }
527
Austin Schuh32501832017-02-25 18:32:56 -0800528 // Sets the current controller to be index.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800529 void set_index(int index) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800530 plant_.set_index(index);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800531 controller_.set_index(index);
532 observer_.set_index(index);
Austin Schuh9644e1c2013-03-12 00:40:36 -0700533 }
534
Austin Schuh32501832017-02-25 18:32:56 -0800535 int index() const { return plant_.index(); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700536
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800537 protected:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800538 PlantType plant_;
Austin Schuhc5fceb82017-02-25 16:24:12 -0800539
Austin Schuh20388b62017-11-23 22:40:46 -0800540 StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs,
541 Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800542 controller_;
543
Austin Schuhe91f14c2017-02-25 19:43:57 -0800544 ObserverType observer_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700545
Brian Silverman273d8a32014-05-10 22:19:09 -0700546 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800547 static constexpr int kNumStates = number_of_states;
548 static constexpr int kNumOutputs = number_of_outputs;
549 static constexpr int kNumInputs = number_of_inputs;
550
551 // Portion of U which is based on the feed-forwards.
Austin Schuh20388b62017-11-23 22:40:46 -0800552 Eigen::Matrix<Scalar, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700553
Brian Silverman273d8a32014-05-10 22:19:09 -0700554 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800555 // Current goal (Used by the feed-back controller).
Austin Schuh20388b62017-11-23 22:40:46 -0800556 Eigen::Matrix<Scalar, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800557 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
Austin Schuh20388b62017-11-23 22:40:46 -0800558 Eigen::Matrix<Scalar, number_of_states, 1> next_R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800559 // Computed output after being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800560 Eigen::Matrix<Scalar, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800561 // Computed output before being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800562 Eigen::Matrix<Scalar, number_of_inputs, 1> U_uncapped_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700563
Brian Silverman0a151c92014-05-02 15:28:44 -0700564 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800565};
566
Brian Silverman273d8a32014-05-10 22:19:09 -0700567#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_