blob: e535ae731f4b4594d96a4a8056896a1e6464327e [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()),
29 B_(other.B()),
30 C_(other.C()),
31 D_(other.D()),
32 U_min_(other.U_min()),
Austin Schuhb6a6d822016-02-08 00:20:40 -080033 U_max_(other.U_max()) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080034
Austin Schuhe3490622013-03-13 01:24:30 -070035 StateFeedbackPlantCoefficients(
Austin Schuhdc1c84a2013-02-23 16:33:10 -080036 const Eigen::Matrix<double, number_of_states, number_of_states> &A,
37 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B,
38 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C,
39 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D,
Brian Silverman5808bcb2014-09-14 21:40:43 -040040 const Eigen::Matrix<double, number_of_inputs, 1> &U_max,
41 const Eigen::Matrix<double, number_of_inputs, 1> &U_min)
Austin Schuhb6a6d822016-02-08 00:20:40 -080042 : A_(A), B_(B), C_(C), D_(D), U_min_(U_min), U_max_(U_max) {}
Austin Schuhe3490622013-03-13 01:24:30 -070043
Brian Silverman273d8a32014-05-10 22:19:09 -070044 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
45 return A_;
46 }
47 double A(int i, int j) const { return A()(i, j); }
48 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
49 return B_;
50 }
51 double B(int i, int j) const { return B()(i, j); }
52 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
53 return C_;
54 }
55 double C(int i, int j) const { return C()(i, j); }
56 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
57 return D_;
58 }
59 double D(int i, int j) const { return D()(i, j); }
60 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
61 return U_min_;
62 }
Brian Silvermana21c3a22014-06-12 21:49:15 -070063 double U_min(int i, int j) const { return U_min()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070064 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
65 return U_max_;
66 }
Brian Silvermana21c3a22014-06-12 21:49:15 -070067 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070068
69 private:
70 const Eigen::Matrix<double, number_of_states, number_of_states> A_;
71 const Eigen::Matrix<double, number_of_states, number_of_inputs> B_;
72 const Eigen::Matrix<double, number_of_outputs, number_of_states> C_;
73 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D_;
74 const Eigen::Matrix<double, number_of_inputs, 1> U_min_;
75 const Eigen::Matrix<double, number_of_inputs, 1> U_max_;
76
77 StateFeedbackPlantCoefficients &operator=(
78 StateFeedbackPlantCoefficients other) = delete;
Austin Schuhe3490622013-03-13 01:24:30 -070079};
80
81template <int number_of_states, int number_of_inputs, int number_of_outputs>
82class StateFeedbackPlant {
83 public:
84 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070085
86 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080087 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh1a387962015-01-31 16:36:20 -080088 number_of_states, number_of_inputs, number_of_outputs>>> *
89 coefficients)
90 : coefficients_(::std::move(*coefficients)), plant_index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070091 Reset();
92 }
93
94 StateFeedbackPlant(StateFeedbackPlant &&other)
95 : plant_index_(other.plant_index_) {
96 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -070097 X_.swap(other.X_);
98 Y_.swap(other.Y_);
99 U_.swap(other.U_);
Brian Silverman0a151c92014-05-02 15:28:44 -0700100 }
101
Austin Schuh1a387962015-01-31 16:36:20 -0800102 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700103
Austin Schuhe3490622013-03-13 01:24:30 -0700104 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700105 return coefficients().A();
Austin Schuhe3490622013-03-13 01:24:30 -0700106 }
107 double A(int i, int j) const { return A()(i, j); }
108 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700109 return coefficients().B();
Austin Schuhe3490622013-03-13 01:24:30 -0700110 }
111 double B(int i, int j) const { return B()(i, j); }
112 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700113 return coefficients().C();
Austin Schuhe3490622013-03-13 01:24:30 -0700114 }
115 double C(int i, int j) const { return C()(i, j); }
116 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700117 return coefficients().D();
Austin Schuhe3490622013-03-13 01:24:30 -0700118 }
119 double D(int i, int j) const { return D()(i, j); }
120 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700121 return coefficients().U_min();
Austin Schuhe3490622013-03-13 01:24:30 -0700122 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700123 double U_min(int i, int j) const { return U_min()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700124 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700125 return coefficients().U_max();
Austin Schuhe3490622013-03-13 01:24:30 -0700126 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700127 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700128
129 const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700130 double X(int i, int j) const { return X()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700131 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700132 double Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700133 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silverman5808bcb2014-09-14 21:40:43 -0400134 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700135
Brian Silverman0ca790b2014-06-12 21:33:08 -0700136 Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700137 double &mutable_X(int i, int j) { return mutable_X()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700138 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700139 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700140 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700141 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700142
Austin Schuhb6a6d822016-02-08 00:20:40 -0800143 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
144 number_of_outputs> &
145 coefficients() const {
Austin Schuhe3490622013-03-13 01:24:30 -0700146 return *coefficients_[plant_index_];
147 }
148
149 int plant_index() const { return plant_index_; }
150 void set_plant_index(int plant_index) {
Austin Schuh6ca0f792016-03-12 14:06:14 -0800151 assert(plant_index >= 0);
152 assert(plant_index < static_cast<int>(coefficients_.size()));
153 plant_index_ = plant_index;
Austin Schuhe3490622013-03-13 01:24:30 -0700154 }
155
156 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700157 X_.setZero();
158 Y_.setZero();
159 U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800160 }
161
Austin Schuh849f0032013-03-03 23:59:53 -0800162 // Assert that U is within the hardware range.
163 virtual void CheckU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400164 for (int i = 0; i < kNumInputs; ++i) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700165 assert(U(i, 0) <= U_max(i, 0) + 0.00001);
166 assert(U(i, 0) >= U_min(i, 0) - 0.00001);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800167 }
168 }
Austin Schuh849f0032013-03-03 23:59:53 -0800169
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800170 // Computes the new X and Y given the control input.
171 void Update() {
Austin Schuh849f0032013-03-03 23:59:53 -0800172 // Powers outside of the range are more likely controller bugs than things
173 // that the plant should deal with.
174 CheckU();
Brian Silverman273d8a32014-05-10 22:19:09 -0700175 X_ = A() * X() + B() * U();
176 Y_ = C() * X() + D() * U();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800177 }
178
179 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:
Brian Silverman273d8a32014-05-10 22:19:09 -0700186 Eigen::Matrix<double, number_of_states, 1> X_;
187 Eigen::Matrix<double, number_of_outputs, 1> Y_;
188 Eigen::Matrix<double, number_of_inputs, 1> U_;
189
Austin Schuhb6a6d822016-02-08 00:20:40 -0800190 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh1a387962015-01-31 16:36:20 -0800191 number_of_states, number_of_inputs, number_of_outputs>>> coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700192
Austin Schuhe3490622013-03-13 01:24:30 -0700193 int plant_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 Schuh9644e1c2013-03-12 00:40:36 -0700198// A Controller is a structure which holds a plant and the K and L matrices.
199// This is designed such that multiple controllers can share one set of state to
200// support gain scheduling easily.
201template <int number_of_states, int number_of_inputs, int number_of_outputs>
Brian Silverman273d8a32014-05-10 22:19:09 -0700202struct StateFeedbackController final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700203 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700204
Austin Schuh9644e1c2013-03-12 00:40:36 -0700205 const Eigen::Matrix<double, number_of_states, number_of_outputs> L;
Brian Silverman5808bcb2014-09-14 21:40:43 -0400206 const Eigen::Matrix<double, number_of_inputs, number_of_states> K;
Austin Schuh86093ad2016-02-06 14:29:34 -0800207 const Eigen::Matrix<double, number_of_inputs, number_of_states> Kff;
Austin Schuh1a387962015-01-31 16:36:20 -0800208 const Eigen::Matrix<double, number_of_states, number_of_states> A_inv;
Austin Schuhe3490622013-03-13 01:24:30 -0700209 StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
210 number_of_outputs> plant;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700211
212 StateFeedbackController(
213 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L,
Brian Silverman5808bcb2014-09-14 21:40:43 -0400214 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
Austin Schuh86093ad2016-02-06 14:29:34 -0800215 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff,
Austin Schuh1a387962015-01-31 16:36:20 -0800216 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
Austin Schuhe3490622013-03-13 01:24:30 -0700217 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
218 number_of_outputs> &plant)
Austin Schuhb6a6d822016-02-08 00:20:40 -0800219 : L(L), K(K), Kff(Kff), A_inv(A_inv), plant(plant) {}
Brian Silverman2a04b262016-02-12 19:52:36 -0500220
221 // TODO(Brian): Remove this overload once they're all converted.
222 StateFeedbackController(
223 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L,
224 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
225 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
226 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
227 number_of_outputs> &plant)
228 : L(L),
229 K(K),
230 Kff(::Eigen::Matrix<double, number_of_inputs,
231 number_of_states>::Zero()),
232 A_inv(A_inv),
233 plant(plant) {}
Austin Schuh9644e1c2013-03-12 00:40:36 -0700234};
235
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800236template <int number_of_states, int number_of_inputs, int number_of_outputs>
237class StateFeedbackLoop {
238 public:
239 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
240
Brian Silverman0a151c92014-05-02 15:28:44 -0700241 StateFeedbackLoop(const StateFeedbackController<
242 number_of_states, number_of_inputs, number_of_outputs> &controller)
243 : controller_index_(0) {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800244 controllers_.emplace_back(
245 new StateFeedbackController<number_of_states, number_of_inputs,
246 number_of_outputs>(controller));
Brian Silverman0a151c92014-05-02 15:28:44 -0700247 Reset();
248 }
249
Austin Schuhb6a6d822016-02-08 00:20:40 -0800250 StateFeedbackLoop(::std::vector<::std::unique_ptr<StateFeedbackController<
Austin Schuh1a387962015-01-31 16:36:20 -0800251 number_of_states, number_of_inputs, number_of_outputs>>> *controllers)
252 : controllers_(::std::move(*controllers)), controller_index_(0) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700253 Reset();
254 }
255
256 StateFeedbackLoop(StateFeedbackLoop &&other) {
257 X_hat_.swap(other.X_hat_);
258 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800259 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700260 U_.swap(other.U_);
261 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800262 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700263 ::std::swap(controllers_, other.controllers_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700264 controller_index_ = other.controller_index_;
265 }
266
Austin Schuh1a387962015-01-31 16:36:20 -0800267 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700268
Austin Schuh9644e1c2013-03-12 00:40:36 -0700269 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700270 return controller().plant.A();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700271 }
272 double A(int i, int j) const { return A()(i, j); }
273 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700274 return controller().plant.B();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700275 }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800276 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv()
277 const {
Austin Schuh703b8d42015-02-01 14:56:34 -0800278 return controller().A_inv;
279 }
280 double A_inv(int i, int j) const { return A_inv()(i, j); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700281 double B(int i, int j) const { return B()(i, j); }
282 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700283 return controller().plant.C();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700284 }
285 double C(int i, int j) const { return C()(i, j); }
286 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700287 return controller().plant.D();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700288 }
289 double D(int i, int j) const { return D()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700290 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
291 return controller().plant.U_min();
292 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700293 double U_min(int i, int j) const { return U_min()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700294 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
295 return controller().plant.U_max();
296 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700297 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700298
Brian Silverman5808bcb2014-09-14 21:40:43 -0400299 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700300 return controller().K;
301 }
302 double K(int i, int j) const { return K()(i, j); }
Austin Schuh86093ad2016-02-06 14:29:34 -0800303 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff() const {
304 return controller().Kff;
305 }
306 double Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700307 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const {
308 return controller().L;
309 }
310 double L(int i, int j) const { return L()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700311
312 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
313 return X_hat_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700314 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700315 double X_hat(int i, int j) const { return X_hat()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700316 const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700317 double R(int i, int j) const { return R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800318 const Eigen::Matrix<double, number_of_states, 1> &next_R() const {
319 return next_R_;
320 }
321 double next_R(int i, int j) const { return next_R()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700322 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700323 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700324 const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const {
325 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700326 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700327 double U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800328 const Eigen::Matrix<double, number_of_inputs, 1> &ff_U() const {
329 return ff_U_;
330 }
331 double ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700332
Brian Silverman0ca790b2014-06-12 21:33:08 -0700333 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700334 double &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700335 Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700336 double &mutable_R(int i, int j) { return mutable_R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800337 Eigen::Matrix<double, number_of_states, 1> &mutable_next_R() {
338 return next_R_;
339 }
340 double &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700341 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700342 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700343 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700344 return U_uncapped_;
345 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700346 double &mutable_U_uncapped(int i, int j) {
347 return mutable_U_uncapped()(i, j);
348 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800349
Brian Silverman2c590c32013-11-04 18:08:54 -0800350 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuhb6a6d822016-02-08 00:20:40 -0800351 number_of_outputs> &
352 controller() const {
Austin Schuhe3490622013-03-13 01:24:30 -0700353 return *controllers_[controller_index_];
Austin Schuh9644e1c2013-03-12 00:40:36 -0700354 }
355
Brian Silverman2c590c32013-11-04 18:08:54 -0800356 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuhb6a6d822016-02-08 00:20:40 -0800357 number_of_outputs> &
358 controller(int index) const {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700359 return *controllers_[index];
360 }
361
Austin Schuh9644e1c2013-03-12 00:40:36 -0700362 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700363 X_hat_.setZero();
364 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800365 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700366 U_.setZero();
367 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800368 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800369 }
370
371 // If U is outside the hardware range, limit it before the plant tries to use
372 // it.
373 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400374 for (int i = 0; i < kNumInputs; ++i) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700375 if (U(i, 0) > U_max(i, 0)) {
376 U_(i, 0) = U_max(i, 0);
377 } else if (U(i, 0) < U_min(i, 0)) {
378 U_(i, 0) = U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800379 }
380 }
381 }
382
Austin Schuhf9286cd2014-02-11 00:51:09 -0800383 // Corrects X_hat given the observation in Y.
384 void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
Austin Schuh703b8d42015-02-01 14:56:34 -0800385 X_hat_ += A_inv() * L() * (Y - C() * X_hat_ - D() * U());
Austin Schuhf9286cd2014-02-11 00:51:09 -0800386 }
387
Austin Schuh3f862bb2016-02-27 14:48:05 -0800388 const Eigen::Matrix<double, number_of_states, 1> error() const {
389 return R() - X_hat();
390 }
391
Austin Schuhb6a6d822016-02-08 00:20:40 -0800392 // Returns the calculated controller power.
393 virtual const Eigen::Matrix<double, number_of_inputs, 1> ControllerOutput() {
394 ff_U_ = FeedForward();
Austin Schuh3f862bb2016-02-27 14:48:05 -0800395 return K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800396 }
397
398 // Calculates the feed forwards power.
399 virtual const Eigen::Matrix<double, number_of_inputs, 1> FeedForward() {
400 return Kff() * (next_R() - A() * R());
401 }
402
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800403 // stop_motors is whether or not to output all 0s.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800404 void Update(bool stop_motors) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800405 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700406 U_.setZero();
407 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800408 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800409 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800410 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800411 CapU();
412 }
413
Austin Schuhc2b77742015-11-26 16:18:27 -0800414 UpdateObserver(U_);
Austin Schuh093535c2016-03-05 23:21:00 -0800415
416 UpdateFFReference();
417 }
418
419 // Updates R() after any CapU operations happen on U().
420 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800421 ff_U_ -= U_uncapped() - U();
422 if (!Kff().isZero(0)) {
423 R_ = A() * R() + B() * ff_U_;
424 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000425 }
426
Austin Schuhc2b77742015-11-26 16:18:27 -0800427 void UpdateObserver(const Eigen::Matrix<double, number_of_inputs, 1> &new_u) {
428 X_hat_ = A() * X_hat() + B() * new_u;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800429 }
430
Brian Silverman273d8a32014-05-10 22:19:09 -0700431 // Sets the current controller to be index, clamped to be within range.
Austin Schuh9644e1c2013-03-12 00:40:36 -0700432 void set_controller_index(int index) {
Austin Schuhe3490622013-03-13 01:24:30 -0700433 if (index < 0) {
434 controller_index_ = 0;
435 } else if (index >= static_cast<int>(controllers_.size())) {
Brian Silvermanb8cd6892013-03-17 23:36:24 -0700436 controller_index_ = static_cast<int>(controllers_.size()) - 1;
Austin Schuhe3490622013-03-13 01:24:30 -0700437 } else {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700438 controller_index_ = index;
439 }
440 }
441
Austin Schuhd34569d2014-02-18 20:26:38 -0800442 int controller_index() const { return controller_index_; }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700443
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800444 protected:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800445 ::std::vector<::std::unique_ptr<StateFeedbackController<
Austin Schuh1a387962015-01-31 16:36:20 -0800446 number_of_states, number_of_inputs, number_of_outputs>>> controllers_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700447
Brian Silverman273d8a32014-05-10 22:19:09 -0700448 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800449 static constexpr int kNumStates = number_of_states;
450 static constexpr int kNumOutputs = number_of_outputs;
451 static constexpr int kNumInputs = number_of_inputs;
452
453 // Portion of U which is based on the feed-forwards.
454 Eigen::Matrix<double, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700455
Brian Silverman273d8a32014-05-10 22:19:09 -0700456 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800457 // Internal state estimate.
Brian Silverman273d8a32014-05-10 22:19:09 -0700458 Eigen::Matrix<double, number_of_states, 1> X_hat_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800459 // Current goal (Used by the feed-back controller).
Brian Silverman273d8a32014-05-10 22:19:09 -0700460 Eigen::Matrix<double, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800461 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
462 Eigen::Matrix<double, number_of_states, 1> next_R_;
463 // Computed output after being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700464 Eigen::Matrix<double, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800465 // Computed output before being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700466 Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_;
467
Austin Schuh9644e1c2013-03-12 00:40:36 -0700468 int controller_index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700469
Brian Silverman0a151c92014-05-02 15:28:44 -0700470 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800471};
472
Brian Silverman273d8a32014-05-10 22:19:09 -0700473#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_