blob: c9b5528b6069caf0b05d2a6c7e39264d91a7ee12 [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,
19 typename PlantType, typename ObserverType>
20class 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 Schuhdc1c84a2013-02-23 16:33:10 -080029template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh64f17a52017-02-25 14:41:58 -080030struct StateFeedbackPlantCoefficients final {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080031 public:
32 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
33
Austin Schuhe3490622013-03-13 01:24:30 -070034 StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other)
Austin Schuh64f17a52017-02-25 14:41:58 -080035 : A(other.A),
Austin Schuhc5fceb82017-02-25 16:24:12 -080036 A_inv(other.A_inv),
Austin Schuh64f17a52017-02-25 14:41:58 -080037 B(other.B),
Austin Schuh64f17a52017-02-25 14:41:58 -080038 C(other.C),
39 D(other.D),
40 U_min(other.U_min),
41 U_max(other.U_max) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080042
Austin Schuhe3490622013-03-13 01:24:30 -070043 StateFeedbackPlantCoefficients(
Austin Schuhdc1c84a2013-02-23 16:33:10 -080044 const Eigen::Matrix<double, number_of_states, number_of_states> &A,
Austin Schuhc5fceb82017-02-25 16:24:12 -080045 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
Austin Schuhdc1c84a2013-02-23 16:33:10 -080046 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B,
47 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C,
48 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D,
Brian Silverman5808bcb2014-09-14 21:40:43 -040049 const Eigen::Matrix<double, number_of_inputs, 1> &U_max,
50 const Eigen::Matrix<double, number_of_inputs, 1> &U_min)
Austin Schuh3ad5ed82017-02-25 21:36:19 -080051 : 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 -070052
Austin Schuh64f17a52017-02-25 14:41:58 -080053 const Eigen::Matrix<double, number_of_states, number_of_states> A;
Austin Schuhc5fceb82017-02-25 16:24:12 -080054 const Eigen::Matrix<double, number_of_states, number_of_states> A_inv;
Austin Schuh64f17a52017-02-25 14:41:58 -080055 const Eigen::Matrix<double, number_of_states, number_of_inputs> B;
Austin Schuh64f17a52017-02-25 14:41:58 -080056 const Eigen::Matrix<double, number_of_outputs, number_of_states> C;
57 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D;
58 const Eigen::Matrix<double, number_of_inputs, 1> U_min;
59 const Eigen::Matrix<double, number_of_inputs, 1> U_max;
Austin Schuhe3490622013-03-13 01:24:30 -070060};
61
62template <int number_of_states, int number_of_inputs, int number_of_outputs>
63class StateFeedbackPlant {
64 public:
65 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070066
67 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080068 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh66c19882017-02-25 13:36:28 -080069 number_of_states, number_of_inputs, number_of_outputs>>>
70 *coefficients)
Austin Schuhc5fceb82017-02-25 16:24:12 -080071 : coefficients_(::std::move(*coefficients)), index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070072 Reset();
73 }
74
75 StateFeedbackPlant(StateFeedbackPlant &&other)
Austin Schuhc5fceb82017-02-25 16:24:12 -080076 : index_(other.index_) {
Brian Silverman0a151c92014-05-02 15:28:44 -070077 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -070078 X_.swap(other.X_);
79 Y_.swap(other.Y_);
Brian Silverman0a151c92014-05-02 15:28:44 -070080 }
81
Austin Schuh1a387962015-01-31 16:36:20 -080082 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -070083
Austin Schuhe3490622013-03-13 01:24:30 -070084 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080085 return coefficients().A;
Austin Schuhe3490622013-03-13 01:24:30 -070086 }
87 double A(int i, int j) const { return A()(i, j); }
Austin Schuhc5fceb82017-02-25 16:24:12 -080088 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv() const {
89 return coefficients().A_inv;
90 }
91 double A_inv(int i, int j) const { return A_inv()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -070092 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080093 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -070094 }
95 double B(int i, int j) const { return B()(i, j); }
96 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080097 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -070098 }
99 double C(int i, int j) const { return C()(i, j); }
100 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800101 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -0700102 }
103 double D(int i, int j) const { return D()(i, j); }
104 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800105 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700106 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700107 double U_min(int i, int j) const { return U_min()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700108 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800109 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700110 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700111 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700112
113 const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700114 double X(int i, int j) const { return X()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700115 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700116 double Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700117
Brian Silverman0ca790b2014-06-12 21:33:08 -0700118 Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700119 double &mutable_X(int i, int j) { return mutable_X()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700120 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700121 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700122
Austin Schuhb6a6d822016-02-08 00:20:40 -0800123 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuhc5fceb82017-02-25 16:24:12 -0800124 number_of_outputs>
125 &coefficients(int index) const {
126 return *coefficients_[index];
Austin Schuhe3490622013-03-13 01:24:30 -0700127 }
128
Austin Schuhc5fceb82017-02-25 16:24:12 -0800129 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
130 number_of_outputs>
131 &coefficients() const {
132 return *coefficients_[index_];
133 }
134
135 int index() const { return index_; }
136 void set_index(int index) {
137 assert(index >= 0);
138 assert(index < static_cast<int>(coefficients_.size()));
139 index_ = index;
Austin Schuhe3490622013-03-13 01:24:30 -0700140 }
141
142 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700143 X_.setZero();
144 Y_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800145 }
146
Austin Schuh849f0032013-03-03 23:59:53 -0800147 // Assert that U is within the hardware range.
Austin Schuh66c19882017-02-25 13:36:28 -0800148 virtual void CheckU(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400149 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuh66c19882017-02-25 13:36:28 -0800150 if (U(i, 0) > U_max(i, 0) + 0.00001 || U(i, 0) < U_min(i, 0) - 0.00001) {
151 LOG(FATAL, "U out of range\n");
152 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800153 }
154 }
Austin Schuh849f0032013-03-03 23:59:53 -0800155
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800156 // Computes the new X and Y given the control input.
Austin Schuh66c19882017-02-25 13:36:28 -0800157 void Update(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Austin Schuh849f0032013-03-03 23:59:53 -0800158 // Powers outside of the range are more likely controller bugs than things
159 // that the plant should deal with.
Austin Schuh66c19882017-02-25 13:36:28 -0800160 CheckU(U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800161 X_ = Update(X(), U);
Austin Schuh01c7b252017-03-05 00:59:31 -0800162 UpdateY(U);
163 }
164
165 // Computes the new Y given the control input.
166 void UpdateY(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Austin Schuh66c19882017-02-25 13:36:28 -0800167 Y_ = C() * X() + D() * U;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800168 }
169
Austin Schuhe91f14c2017-02-25 19:43:57 -0800170 Eigen::Matrix<double, number_of_states, 1> Update(
171 const Eigen::Matrix<double, number_of_states, 1> X,
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800172 const Eigen::Matrix<double, number_of_inputs, 1> &U) const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800173 return A() * X + B() * U;
174 }
175
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800176 protected:
177 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800178 static const int kNumStates = number_of_states;
179 static const int kNumOutputs = number_of_outputs;
180 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700181
182 private:
Brian Silverman273d8a32014-05-10 22:19:09 -0700183 Eigen::Matrix<double, number_of_states, 1> X_;
184 Eigen::Matrix<double, number_of_outputs, 1> Y_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700185
Austin Schuhb6a6d822016-02-08 00:20:40 -0800186 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh64f17a52017-02-25 14:41:58 -0800187 number_of_states, number_of_inputs, number_of_outputs>>>
188 coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700189
Austin Schuhc5fceb82017-02-25 16:24:12 -0800190 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700191
192 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800193};
194
Austin Schuh32501832017-02-25 18:32:56 -0800195// A container for all the controller coefficients.
Austin Schuh9644e1c2013-03-12 00:40:36 -0700196template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh32501832017-02-25 18:32:56 -0800197struct StateFeedbackControllerCoefficients final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700198 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700199
Brian Silverman5808bcb2014-09-14 21:40:43 -0400200 const Eigen::Matrix<double, number_of_inputs, number_of_states> K;
Austin Schuh86093ad2016-02-06 14:29:34 -0800201 const Eigen::Matrix<double, number_of_inputs, number_of_states> Kff;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700202
Austin Schuh32501832017-02-25 18:32:56 -0800203 StateFeedbackControllerCoefficients(
Brian Silverman5808bcb2014-09-14 21:40:43 -0400204 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
Austin Schuhc5fceb82017-02-25 16:24:12 -0800205 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff)
Austin Schuh32501832017-02-25 18:32:56 -0800206 : K(K), Kff(Kff) {}
207};
208
209template <int number_of_states, int number_of_inputs, int number_of_outputs>
210class StateFeedbackController {
211 public:
212 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
213
214 explicit StateFeedbackController(
215 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
216 number_of_states, number_of_inputs, number_of_outputs>>> *controllers)
217 : coefficients_(::std::move(*controllers)) {}
218
219 StateFeedbackController(StateFeedbackController &&other)
220 : index_(other.index_) {
221 ::std::swap(coefficients_, other.coefficients_);
222 }
223
224 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const {
225 return coefficients().K;
226 }
227 double K(int i, int j) const { return K()(i, j); }
228 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff() const {
229 return coefficients().Kff;
230 }
231 double Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800232
Austin Schuhe91f14c2017-02-25 19:43:57 -0800233 void Reset() {}
234
Austin Schuh32501832017-02-25 18:32:56 -0800235 // Sets the current controller to be index, clamped to be within range.
236 void set_index(int index) {
237 if (index < 0) {
238 index_ = 0;
239 } else if (index >= static_cast<int>(coefficients_.size())) {
240 index_ = static_cast<int>(coefficients_.size()) - 1;
241 } else {
242 index_ = index;
243 }
244 }
245
246 int index() const { return index_; }
247
248 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
249 number_of_outputs>
250 &coefficients(int index) const {
251 return *coefficients_[index];
252 }
253
254 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
255 number_of_outputs>
256 &coefficients() const {
257 return *coefficients_[index_];
258 }
259
260 private:
261 int index_ = 0;
262 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
263 number_of_states, number_of_inputs, number_of_outputs>>>
264 coefficients_;
265};
266
Austin Schuh32501832017-02-25 18:32:56 -0800267// A container for all the observer coefficients.
268template <int number_of_states, int number_of_inputs, int number_of_outputs>
269struct StateFeedbackObserverCoefficients final {
270 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
271
272 const Eigen::Matrix<double, number_of_states, number_of_outputs> L;
273
274 StateFeedbackObserverCoefficients(
275 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L)
276 : L(L) {}
277};
278
279template <int number_of_states, int number_of_inputs, int number_of_outputs>
280class StateFeedbackObserver {
281 public:
282 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
283
284 explicit StateFeedbackObserver(
285 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
286 number_of_states, number_of_inputs, number_of_outputs>>> *observers)
287 : coefficients_(::std::move(*observers)) {}
288
289 StateFeedbackObserver(StateFeedbackObserver &&other)
Austin Schuhe91f14c2017-02-25 19:43:57 -0800290 : X_hat_(other.X_hat_), index_(other.index_) {
Austin Schuh32501832017-02-25 18:32:56 -0800291 ::std::swap(coefficients_, other.coefficients_);
292 }
293
294 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const {
295 return coefficients().L;
296 }
297 double L(int i, int j) const { return L()(i, j); }
298
Austin Schuhe91f14c2017-02-25 19:43:57 -0800299 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
300 return X_hat_;
301 }
302 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
303
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800304 void Reset(
305 StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs,
306 StateFeedbackPlant<number_of_states, number_of_inputs,
307 number_of_outputs>,
308 StateFeedbackObserver> * /*loop*/) {
309 X_hat_.setZero();
310 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800311
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800312 void Predict(
313 StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs,
314 StateFeedbackPlant<number_of_states, number_of_inputs,
315 number_of_outputs>,
316 StateFeedbackObserver> *loop,
317 const Eigen::Matrix<double, number_of_inputs, 1> &new_u,
318 ::std::chrono::nanoseconds /*dt*/) {
319 mutable_X_hat() = loop->plant().Update(X_hat(), new_u);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800320 }
321
322 void Correct(const StateFeedbackLoop<
323 number_of_states, number_of_inputs, number_of_outputs,
324 StateFeedbackPlant<number_of_states, number_of_inputs,
325 number_of_outputs>,
326 StateFeedbackObserver> &loop,
327 const Eigen::Matrix<double, number_of_inputs, 1> &U,
328 const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
329 mutable_X_hat() += loop.plant().A_inv() * L() *
330 (Y - loop.plant().C() * X_hat() - loop.plant().D() * U);
331 }
332
Austin Schuh32501832017-02-25 18:32:56 -0800333 // Sets the current controller to be index, clamped to be within range.
334 void set_index(int index) {
335 if (index < 0) {
336 index_ = 0;
337 } else if (index >= static_cast<int>(coefficients_.size())) {
338 index_ = static_cast<int>(coefficients_.size()) - 1;
339 } else {
340 index_ = index;
341 }
342 }
343
344 int index() const { return index_; }
345
346 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
347 number_of_outputs>
348 &coefficients(int index) const {
349 return *coefficients_[index];
350 }
351
352 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
353 number_of_outputs>
354 &coefficients() const {
355 return *coefficients_[index_];
356 }
357
358 private:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800359 // Internal state estimate.
360 Eigen::Matrix<double, number_of_states, 1> X_hat_;
361
Austin Schuh32501832017-02-25 18:32:56 -0800362 int index_ = 0;
363 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
364 number_of_states, number_of_inputs, number_of_outputs>>>
365 coefficients_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700366};
367
Austin Schuhe91f14c2017-02-25 19:43:57 -0800368template <int number_of_states, int number_of_inputs, int number_of_outputs,
369 typename PlantType = StateFeedbackPlant<
370 number_of_states, number_of_inputs, number_of_outputs>,
371 typename ObserverType = StateFeedbackObserver<
372 number_of_states, number_of_inputs, number_of_outputs>>
Austin 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,
380 number_of_outputs> &&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
Brian Silverman273d8a32014-05-10 22:19:09 -0700401 const Eigen::Matrix<double, 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 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700404 double X_hat(int i, int j) const { return X_hat()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700405 const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700406 double R(int i, int j) const { return R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800407 const Eigen::Matrix<double, number_of_states, 1> &next_R() const {
408 return next_R_;
409 }
410 double next_R(int i, int j) const { return next_R()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700411 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700412 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700413 const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const {
414 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700415 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700416 double U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800417 const Eigen::Matrix<double, number_of_inputs, 1> &ff_U() const {
418 return ff_U_;
419 }
420 double ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700421
Austin Schuhe91f14c2017-02-25 19:43:57 -0800422 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() {
423 return observer_.mutable_X_hat();
424 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700425 double &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700426 Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700427 double &mutable_R(int i, int j) { return mutable_R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800428 Eigen::Matrix<double, number_of_states, 1> &mutable_next_R() {
429 return next_R_;
430 }
431 double &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700432 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700433 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700434 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700435 return U_uncapped_;
436 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700437 double &mutable_U_uncapped(int i, int j) {
438 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,
445 number_of_outputs>
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 Schuh3ad5ed82017-02-25 21:36:19 -0800461 observer_.Reset(this);
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.
477 void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800478 observer_.Correct(*this, U(), Y);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800479 }
480
Austin Schuh3f862bb2016-02-27 14:48:05 -0800481 const Eigen::Matrix<double, number_of_states, 1> error() const {
482 return R() - X_hat();
483 }
484
Austin Schuhb6a6d822016-02-08 00:20:40 -0800485 // Returns the calculated controller power.
486 virtual const Eigen::Matrix<double, 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.
493 virtual const Eigen::Matrix<double, 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 Schuh3ad5ed82017-02-25 21:36:19 -0800523 void UpdateObserver(const Eigen::Matrix<double, number_of_inputs, 1> &new_u,
524 ::std::chrono::nanoseconds dt) {
525 observer_.Predict(this, new_u, dt);
Austin 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 Schuh32501832017-02-25 18:32:56 -0800540 StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs>
541 controller_;
542
Austin Schuhe91f14c2017-02-25 19:43:57 -0800543 ObserverType observer_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700544
Brian Silverman273d8a32014-05-10 22:19:09 -0700545 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800546 static constexpr int kNumStates = number_of_states;
547 static constexpr int kNumOutputs = number_of_outputs;
548 static constexpr int kNumInputs = number_of_inputs;
549
550 // Portion of U which is based on the feed-forwards.
551 Eigen::Matrix<double, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700552
Brian Silverman273d8a32014-05-10 22:19:09 -0700553 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800554 // Current goal (Used by the feed-back controller).
Brian Silverman273d8a32014-05-10 22:19:09 -0700555 Eigen::Matrix<double, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800556 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
557 Eigen::Matrix<double, number_of_states, 1> next_R_;
558 // Computed output after being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700559 Eigen::Matrix<double, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800560 // Computed output before being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700561 Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_;
562
Brian Silverman0a151c92014-05-02 15:28:44 -0700563 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800564};
565
Brian Silverman273d8a32014-05-10 22:19:09 -0700566#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_