blob: a81f2cc1df585f784d4bd217a2780591807f9594 [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
Brian Silvermanc571e052013-03-13 17:58:56 -07006#include <vector>
Austin Schuh1a387962015-01-31 16:36:20 -08007#include <memory>
Austin Schuhcda86af2014-02-16 16:16:39 -08008#include <iostream>
Brian Silvermanc571e052013-03-13 17:58:56 -07009
Austin Schuhdc1c84a2013-02-23 16:33:10 -080010#include "Eigen/Dense"
11
Austin Schuhcda86af2014-02-16 16:16:39 -080012#include "aos/common/logging/logging.h"
Brian Silverman0a151c92014-05-02 15:28:44 -070013#include "aos/common/macros.h"
14
Brian Silverman5808bcb2014-09-14 21:40:43 -040015// For everything in this file, "inputs" and "outputs" are defined from the
16// perspective of the plant. This means U is an input and Y is an output
17// (because you give the plant U (powers) and it gives you back a Y (sensor
18// values). This is the opposite of what they mean from the perspective of the
19// controller (U is an output because that's what goes to the motors and Y is an
20// input because that's what comes back from the sensors).
21
Austin Schuhdc1c84a2013-02-23 16:33:10 -080022template <int number_of_states, int number_of_inputs, int number_of_outputs>
Brian Silverman273d8a32014-05-10 22:19:09 -070023class StateFeedbackPlantCoefficients final {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080024 public:
25 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
26
Austin Schuhe3490622013-03-13 01:24:30 -070027 StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other)
Brian Silverman273d8a32014-05-10 22:19:09 -070028 : A_(other.A()),
Austin Schuh6c20f202017-02-18 22:31:44 -080029 A_continuous_(other.A_continuous()),
Brian Silverman273d8a32014-05-10 22:19:09 -070030 B_(other.B()),
Austin Schuh6c20f202017-02-18 22:31:44 -080031 B_continuous_(other.B_continuous()),
Brian Silverman273d8a32014-05-10 22:19:09 -070032 C_(other.C()),
33 D_(other.D()),
34 U_min_(other.U_min()),
Austin Schuhb6a6d822016-02-08 00:20:40 -080035 U_max_(other.U_max()) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080036
Austin Schuhe3490622013-03-13 01:24:30 -070037 StateFeedbackPlantCoefficients(
Austin Schuhdc1c84a2013-02-23 16:33:10 -080038 const Eigen::Matrix<double, number_of_states, number_of_states> &A,
Austin Schuh6c20f202017-02-18 22:31:44 -080039 const Eigen::Matrix<double, number_of_states, number_of_states>
40 &A_continuous,
Austin Schuhdc1c84a2013-02-23 16:33:10 -080041 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B,
Austin Schuh6c20f202017-02-18 22:31:44 -080042 const Eigen::Matrix<double, number_of_states, number_of_inputs>
43 &B_continuous,
Austin Schuhdc1c84a2013-02-23 16:33:10 -080044 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C,
45 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D,
Brian Silverman5808bcb2014-09-14 21:40:43 -040046 const Eigen::Matrix<double, number_of_inputs, 1> &U_max,
47 const Eigen::Matrix<double, number_of_inputs, 1> &U_min)
Austin Schuh6c20f202017-02-18 22:31:44 -080048 : A_(A),
49 A_continuous_(A_continuous),
50 B_(B),
51 B_continuous_(B_continuous),
52 C_(C),
53 D_(D),
54 U_min_(U_min),
55 U_max_(U_max) {}
Austin Schuhe3490622013-03-13 01:24:30 -070056
Brian Silverman273d8a32014-05-10 22:19:09 -070057 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
58 return A_;
59 }
60 double A(int i, int j) const { return A()(i, j); }
Austin Schuh6c20f202017-02-18 22:31:44 -080061 const Eigen::Matrix<double, number_of_states, number_of_states> &A_continuous()
62 const {
63 return A_continuous_;
64 }
65 double A_continuous(int i, int j) const { return A_continuous()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070066 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
67 return B_;
68 }
69 double B(int i, int j) const { return B()(i, j); }
Austin Schuh6c20f202017-02-18 22:31:44 -080070 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B_continuous() const {
71 return B_continuous_;
72 }
73 double B_continuous(int i, int j) const { return B_continuous()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070074 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
75 return C_;
76 }
77 double C(int i, int j) const { return C()(i, j); }
78 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
79 return D_;
80 }
81 double D(int i, int j) const { return D()(i, j); }
82 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
83 return U_min_;
84 }
Brian Silvermana21c3a22014-06-12 21:49:15 -070085 double U_min(int i, int j) const { return U_min()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070086 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
87 return U_max_;
88 }
Brian Silvermana21c3a22014-06-12 21:49:15 -070089 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070090
91 private:
92 const Eigen::Matrix<double, number_of_states, number_of_states> A_;
Austin Schuh6c20f202017-02-18 22:31:44 -080093 const Eigen::Matrix<double, number_of_states, number_of_states> A_continuous_;
Brian Silverman273d8a32014-05-10 22:19:09 -070094 const Eigen::Matrix<double, number_of_states, number_of_inputs> B_;
Austin Schuh6c20f202017-02-18 22:31:44 -080095 const Eigen::Matrix<double, number_of_states, number_of_inputs> B_continuous_;
Brian Silverman273d8a32014-05-10 22:19:09 -070096 const Eigen::Matrix<double, number_of_outputs, number_of_states> C_;
97 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D_;
98 const Eigen::Matrix<double, number_of_inputs, 1> U_min_;
99 const Eigen::Matrix<double, number_of_inputs, 1> U_max_;
100
101 StateFeedbackPlantCoefficients &operator=(
102 StateFeedbackPlantCoefficients other) = delete;
Austin Schuhe3490622013-03-13 01:24:30 -0700103};
104
105template <int number_of_states, int number_of_inputs, int number_of_outputs>
106class StateFeedbackPlant {
107 public:
108 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -0700109
110 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -0800111 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh1a387962015-01-31 16:36:20 -0800112 number_of_states, number_of_inputs, number_of_outputs>>> *
113 coefficients)
114 : coefficients_(::std::move(*coefficients)), plant_index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700115 Reset();
116 }
117
118 StateFeedbackPlant(StateFeedbackPlant &&other)
119 : plant_index_(other.plant_index_) {
120 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700121 X_.swap(other.X_);
122 Y_.swap(other.Y_);
123 U_.swap(other.U_);
Brian Silverman0a151c92014-05-02 15:28:44 -0700124 }
125
Austin Schuh1a387962015-01-31 16:36:20 -0800126 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700127
Austin Schuhe3490622013-03-13 01:24:30 -0700128 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700129 return coefficients().A();
Austin Schuhe3490622013-03-13 01:24:30 -0700130 }
131 double A(int i, int j) const { return A()(i, j); }
132 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700133 return coefficients().B();
Austin Schuhe3490622013-03-13 01:24:30 -0700134 }
135 double B(int i, int j) const { return B()(i, j); }
136 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700137 return coefficients().C();
Austin Schuhe3490622013-03-13 01:24:30 -0700138 }
139 double C(int i, int j) const { return C()(i, j); }
140 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700141 return coefficients().D();
Austin Schuhe3490622013-03-13 01:24:30 -0700142 }
143 double D(int i, int j) const { return D()(i, j); }
144 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700145 return coefficients().U_min();
Austin Schuhe3490622013-03-13 01:24:30 -0700146 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700147 double U_min(int i, int j) const { return U_min()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700148 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700149 return coefficients().U_max();
Austin Schuhe3490622013-03-13 01:24:30 -0700150 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700151 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700152
153 const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700154 double X(int i, int j) const { return X()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700155 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700156 double Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700157 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silverman5808bcb2014-09-14 21:40:43 -0400158 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700159
Brian Silverman0ca790b2014-06-12 21:33:08 -0700160 Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700161 double &mutable_X(int i, int j) { return mutable_X()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700162 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700163 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700164 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700165 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700166
Austin Schuhb6a6d822016-02-08 00:20:40 -0800167 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
168 number_of_outputs> &
169 coefficients() const {
Austin Schuhe3490622013-03-13 01:24:30 -0700170 return *coefficients_[plant_index_];
171 }
172
173 int plant_index() const { return plant_index_; }
174 void set_plant_index(int plant_index) {
Austin Schuh6ca0f792016-03-12 14:06:14 -0800175 assert(plant_index >= 0);
176 assert(plant_index < static_cast<int>(coefficients_.size()));
177 plant_index_ = plant_index;
Austin Schuhe3490622013-03-13 01:24:30 -0700178 }
179
180 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700181 X_.setZero();
182 Y_.setZero();
183 U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800184 }
185
Austin Schuh849f0032013-03-03 23:59:53 -0800186 // Assert that U is within the hardware range.
187 virtual void CheckU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400188 for (int i = 0; i < kNumInputs; ++i) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700189 assert(U(i, 0) <= U_max(i, 0) + 0.00001);
190 assert(U(i, 0) >= U_min(i, 0) - 0.00001);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800191 }
192 }
Austin Schuh849f0032013-03-03 23:59:53 -0800193
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800194 // Computes the new X and Y given the control input.
195 void Update() {
Austin Schuh849f0032013-03-03 23:59:53 -0800196 // Powers outside of the range are more likely controller bugs than things
197 // that the plant should deal with.
198 CheckU();
Brian Silverman273d8a32014-05-10 22:19:09 -0700199 X_ = A() * X() + B() * U();
200 Y_ = C() * X() + D() * U();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800201 }
202
203 protected:
204 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800205 static const int kNumStates = number_of_states;
206 static const int kNumOutputs = number_of_outputs;
207 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700208
209 private:
Brian Silverman273d8a32014-05-10 22:19:09 -0700210 Eigen::Matrix<double, number_of_states, 1> X_;
211 Eigen::Matrix<double, number_of_outputs, 1> Y_;
212 Eigen::Matrix<double, number_of_inputs, 1> U_;
213
Austin Schuhb6a6d822016-02-08 00:20:40 -0800214 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh1a387962015-01-31 16:36:20 -0800215 number_of_states, number_of_inputs, number_of_outputs>>> coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700216
Austin Schuhe3490622013-03-13 01:24:30 -0700217 int plant_index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700218
219 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800220};
221
Austin Schuh9644e1c2013-03-12 00:40:36 -0700222// A Controller is a structure which holds a plant and the K and L matrices.
223// This is designed such that multiple controllers can share one set of state to
224// support gain scheduling easily.
225template <int number_of_states, int number_of_inputs, int number_of_outputs>
Brian Silverman273d8a32014-05-10 22:19:09 -0700226struct StateFeedbackController final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700227 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700228
Austin Schuh9644e1c2013-03-12 00:40:36 -0700229 const Eigen::Matrix<double, number_of_states, number_of_outputs> L;
Brian Silverman5808bcb2014-09-14 21:40:43 -0400230 const Eigen::Matrix<double, number_of_inputs, number_of_states> K;
Austin Schuh86093ad2016-02-06 14:29:34 -0800231 const Eigen::Matrix<double, number_of_inputs, number_of_states> Kff;
Austin Schuh1a387962015-01-31 16:36:20 -0800232 const Eigen::Matrix<double, number_of_states, number_of_states> A_inv;
Austin Schuhe3490622013-03-13 01:24:30 -0700233 StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
234 number_of_outputs> plant;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700235
236 StateFeedbackController(
237 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L,
Brian Silverman5808bcb2014-09-14 21:40:43 -0400238 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
Austin Schuh86093ad2016-02-06 14:29:34 -0800239 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff,
Austin Schuh1a387962015-01-31 16:36:20 -0800240 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
Austin Schuhe3490622013-03-13 01:24:30 -0700241 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
242 number_of_outputs> &plant)
Austin Schuhb6a6d822016-02-08 00:20:40 -0800243 : L(L), K(K), Kff(Kff), A_inv(A_inv), plant(plant) {}
Brian Silverman2a04b262016-02-12 19:52:36 -0500244
245 // TODO(Brian): Remove this overload once they're all converted.
246 StateFeedbackController(
247 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L,
248 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
249 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
250 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
251 number_of_outputs> &plant)
252 : L(L),
253 K(K),
254 Kff(::Eigen::Matrix<double, number_of_inputs,
255 number_of_states>::Zero()),
256 A_inv(A_inv),
257 plant(plant) {}
Austin Schuh9644e1c2013-03-12 00:40:36 -0700258};
259
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800260template <int number_of_states, int number_of_inputs, int number_of_outputs>
261class StateFeedbackLoop {
262 public:
263 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
264
Brian Silverman0a151c92014-05-02 15:28:44 -0700265 StateFeedbackLoop(const StateFeedbackController<
266 number_of_states, number_of_inputs, number_of_outputs> &controller)
267 : controller_index_(0) {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800268 controllers_.emplace_back(
269 new StateFeedbackController<number_of_states, number_of_inputs,
270 number_of_outputs>(controller));
Brian Silverman0a151c92014-05-02 15:28:44 -0700271 Reset();
272 }
273
Austin Schuhb6a6d822016-02-08 00:20:40 -0800274 StateFeedbackLoop(::std::vector<::std::unique_ptr<StateFeedbackController<
Austin Schuh1a387962015-01-31 16:36:20 -0800275 number_of_states, number_of_inputs, number_of_outputs>>> *controllers)
276 : controllers_(::std::move(*controllers)), controller_index_(0) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700277 Reset();
278 }
279
280 StateFeedbackLoop(StateFeedbackLoop &&other) {
281 X_hat_.swap(other.X_hat_);
282 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800283 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700284 U_.swap(other.U_);
285 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800286 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700287 ::std::swap(controllers_, other.controllers_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700288 controller_index_ = other.controller_index_;
289 }
290
Austin Schuh1a387962015-01-31 16:36:20 -0800291 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700292
Austin Schuh9644e1c2013-03-12 00:40:36 -0700293 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700294 return controller().plant.A();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700295 }
296 double A(int i, int j) const { return A()(i, j); }
297 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700298 return controller().plant.B();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700299 }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800300 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv()
301 const {
Austin Schuh703b8d42015-02-01 14:56:34 -0800302 return controller().A_inv;
303 }
304 double A_inv(int i, int j) const { return A_inv()(i, j); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700305 double B(int i, int j) const { return B()(i, j); }
306 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700307 return controller().plant.C();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700308 }
309 double C(int i, int j) const { return C()(i, j); }
310 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700311 return controller().plant.D();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700312 }
313 double D(int i, int j) const { return D()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700314 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
315 return controller().plant.U_min();
316 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700317 double U_min(int i, int j) const { return U_min()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700318 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
319 return controller().plant.U_max();
320 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700321 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700322
Brian Silverman5808bcb2014-09-14 21:40:43 -0400323 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700324 return controller().K;
325 }
326 double K(int i, int j) const { return K()(i, j); }
Austin Schuh86093ad2016-02-06 14:29:34 -0800327 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff() const {
328 return controller().Kff;
329 }
330 double Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700331 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const {
332 return controller().L;
333 }
334 double L(int i, int j) const { return L()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700335
336 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
337 return X_hat_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700338 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700339 double X_hat(int i, int j) const { return X_hat()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700340 const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700341 double R(int i, int j) const { return R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800342 const Eigen::Matrix<double, number_of_states, 1> &next_R() const {
343 return next_R_;
344 }
345 double next_R(int i, int j) const { return next_R()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700346 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700347 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700348 const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const {
349 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700350 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700351 double U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800352 const Eigen::Matrix<double, number_of_inputs, 1> &ff_U() const {
353 return ff_U_;
354 }
355 double ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700356
Brian Silverman0ca790b2014-06-12 21:33:08 -0700357 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700358 double &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700359 Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700360 double &mutable_R(int i, int j) { return mutable_R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800361 Eigen::Matrix<double, number_of_states, 1> &mutable_next_R() {
362 return next_R_;
363 }
364 double &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700365 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700366 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700367 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700368 return U_uncapped_;
369 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700370 double &mutable_U_uncapped(int i, int j) {
371 return mutable_U_uncapped()(i, j);
372 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800373
Brian Silverman2c590c32013-11-04 18:08:54 -0800374 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuhb6a6d822016-02-08 00:20:40 -0800375 number_of_outputs> &
376 controller() const {
Austin Schuhe3490622013-03-13 01:24:30 -0700377 return *controllers_[controller_index_];
Austin Schuh9644e1c2013-03-12 00:40:36 -0700378 }
379
Brian Silverman2c590c32013-11-04 18:08:54 -0800380 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuhb6a6d822016-02-08 00:20:40 -0800381 number_of_outputs> &
382 controller(int index) const {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700383 return *controllers_[index];
384 }
385
Austin Schuh9644e1c2013-03-12 00:40:36 -0700386 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700387 X_hat_.setZero();
388 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800389 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700390 U_.setZero();
391 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800392 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800393 }
394
395 // If U is outside the hardware range, limit it before the plant tries to use
396 // it.
397 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400398 for (int i = 0; i < kNumInputs; ++i) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700399 if (U(i, 0) > U_max(i, 0)) {
400 U_(i, 0) = U_max(i, 0);
401 } else if (U(i, 0) < U_min(i, 0)) {
402 U_(i, 0) = U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800403 }
404 }
405 }
406
Austin Schuhf9286cd2014-02-11 00:51:09 -0800407 // Corrects X_hat given the observation in Y.
408 void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800409 X_hat_ += A_inv() * L() * (Y - C() * X_hat_ - D() * U());
Austin Schuhf9286cd2014-02-11 00:51:09 -0800410 }
411
Austin Schuh3f862bb2016-02-27 14:48:05 -0800412 const Eigen::Matrix<double, number_of_states, 1> error() const {
413 return R() - X_hat();
414 }
415
Austin Schuhb6a6d822016-02-08 00:20:40 -0800416 // Returns the calculated controller power.
417 virtual const Eigen::Matrix<double, number_of_inputs, 1> ControllerOutput() {
418 ff_U_ = FeedForward();
Austin Schuh3f862bb2016-02-27 14:48:05 -0800419 return K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800420 }
421
422 // Calculates the feed forwards power.
423 virtual const Eigen::Matrix<double, number_of_inputs, 1> FeedForward() {
424 return Kff() * (next_R() - A() * R());
425 }
426
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800427 // stop_motors is whether or not to output all 0s.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800428 void Update(bool stop_motors) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800429 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700430 U_.setZero();
431 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800432 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800433 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800434 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800435 CapU();
436 }
437
Austin Schuhc2b77742015-11-26 16:18:27 -0800438 UpdateObserver(U_);
Austin Schuh093535c2016-03-05 23:21:00 -0800439
440 UpdateFFReference();
441 }
442
443 // Updates R() after any CapU operations happen on U().
444 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800445 ff_U_ -= U_uncapped() - U();
446 if (!Kff().isZero(0)) {
447 R_ = A() * R() + B() * ff_U_;
448 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000449 }
450
Austin Schuhc2b77742015-11-26 16:18:27 -0800451 void UpdateObserver(const Eigen::Matrix<double, number_of_inputs, 1> &new_u) {
452 X_hat_ = A() * X_hat() + B() * new_u;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800453 }
454
Brian Silverman273d8a32014-05-10 22:19:09 -0700455 // Sets the current controller to be index, clamped to be within range.
Austin Schuh9644e1c2013-03-12 00:40:36 -0700456 void set_controller_index(int index) {
Austin Schuhe3490622013-03-13 01:24:30 -0700457 if (index < 0) {
458 controller_index_ = 0;
459 } else if (index >= static_cast<int>(controllers_.size())) {
Brian Silvermanb8cd6892013-03-17 23:36:24 -0700460 controller_index_ = static_cast<int>(controllers_.size()) - 1;
Austin Schuhe3490622013-03-13 01:24:30 -0700461 } else {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700462 controller_index_ = index;
463 }
464 }
465
Austin Schuhd34569d2014-02-18 20:26:38 -0800466 int controller_index() const { return controller_index_; }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700467
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800468 protected:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800469 ::std::vector<::std::unique_ptr<StateFeedbackController<
Austin Schuh1a387962015-01-31 16:36:20 -0800470 number_of_states, number_of_inputs, number_of_outputs>>> controllers_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700471
Brian Silverman273d8a32014-05-10 22:19:09 -0700472 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800473 static constexpr int kNumStates = number_of_states;
474 static constexpr int kNumOutputs = number_of_outputs;
475 static constexpr int kNumInputs = number_of_inputs;
476
477 // Portion of U which is based on the feed-forwards.
478 Eigen::Matrix<double, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700479
Brian Silverman273d8a32014-05-10 22:19:09 -0700480 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800481 // Internal state estimate.
Brian Silverman273d8a32014-05-10 22:19:09 -0700482 Eigen::Matrix<double, number_of_states, 1> X_hat_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800483 // Current goal (Used by the feed-back controller).
Brian Silverman273d8a32014-05-10 22:19:09 -0700484 Eigen::Matrix<double, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800485 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
486 Eigen::Matrix<double, number_of_states, 1> next_R_;
487 // Computed output after being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700488 Eigen::Matrix<double, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800489 // Computed output before being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700490 Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_;
491
Austin Schuh9644e1c2013-03-12 00:40:36 -0700492 int controller_index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700493
Brian Silverman0a151c92014-05-02 15:28:44 -0700494 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800495};
496
Brian Silverman273d8a32014-05-10 22:19:09 -0700497#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_