blob: 465da33661fb1500d521b6d30f1817213c2b9f18 [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()),
33 U_max_(other.U_max()) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080034 }
35
Austin Schuhe3490622013-03-13 01:24:30 -070036 StateFeedbackPlantCoefficients(
Austin Schuhdc1c84a2013-02-23 16:33:10 -080037 const Eigen::Matrix<double, number_of_states, number_of_states> &A,
38 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B,
39 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C,
40 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D,
Brian Silverman5808bcb2014-09-14 21:40:43 -040041 const Eigen::Matrix<double, number_of_inputs, 1> &U_max,
42 const Eigen::Matrix<double, number_of_inputs, 1> &U_min)
Brian Silverman273d8a32014-05-10 22:19:09 -070043 : A_(A),
44 B_(B),
45 C_(C),
46 D_(D),
47 U_min_(U_min),
48 U_max_(U_max) {
Austin Schuhe3490622013-03-13 01:24:30 -070049 }
50
Brian Silverman273d8a32014-05-10 22:19:09 -070051 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
52 return A_;
53 }
54 double A(int i, int j) const { return A()(i, j); }
55 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
56 return B_;
57 }
58 double B(int i, int j) const { return B()(i, j); }
59 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
60 return C_;
61 }
62 double C(int i, int j) const { return C()(i, j); }
63 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
64 return D_;
65 }
66 double D(int i, int j) const { return D()(i, j); }
67 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
68 return U_min_;
69 }
Brian Silvermana21c3a22014-06-12 21:49:15 -070070 double U_min(int i, int j) const { return U_min()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070071 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
72 return U_max_;
73 }
Brian Silvermana21c3a22014-06-12 21:49:15 -070074 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -070075
76 private:
77 const Eigen::Matrix<double, number_of_states, number_of_states> A_;
78 const Eigen::Matrix<double, number_of_states, number_of_inputs> B_;
79 const Eigen::Matrix<double, number_of_outputs, number_of_states> C_;
80 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D_;
81 const Eigen::Matrix<double, number_of_inputs, 1> U_min_;
82 const Eigen::Matrix<double, number_of_inputs, 1> U_max_;
83
84 StateFeedbackPlantCoefficients &operator=(
85 StateFeedbackPlantCoefficients other) = delete;
Austin Schuhe3490622013-03-13 01:24:30 -070086};
87
88template <int number_of_states, int number_of_inputs, int number_of_outputs>
89class StateFeedbackPlant {
90 public:
91 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070092
93 StateFeedbackPlant(
Austin Schuh1a387962015-01-31 16:36:20 -080094 ::std::vector< ::std::unique_ptr<StateFeedbackPlantCoefficients<
95 number_of_states, number_of_inputs, number_of_outputs>>> *
96 coefficients)
97 : coefficients_(::std::move(*coefficients)), plant_index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070098 Reset();
99 }
100
101 StateFeedbackPlant(StateFeedbackPlant &&other)
102 : plant_index_(other.plant_index_) {
103 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700104 X_.swap(other.X_);
105 Y_.swap(other.Y_);
106 U_.swap(other.U_);
Brian Silverman0a151c92014-05-02 15:28:44 -0700107 }
108
Austin Schuh1a387962015-01-31 16:36:20 -0800109 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700110
Austin Schuhe3490622013-03-13 01:24:30 -0700111 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700112 return coefficients().A();
Austin Schuhe3490622013-03-13 01:24:30 -0700113 }
114 double A(int i, int j) const { return A()(i, j); }
115 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700116 return coefficients().B();
Austin Schuhe3490622013-03-13 01:24:30 -0700117 }
118 double B(int i, int j) const { return B()(i, j); }
119 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700120 return coefficients().C();
Austin Schuhe3490622013-03-13 01:24:30 -0700121 }
122 double C(int i, int j) const { return C()(i, j); }
123 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700124 return coefficients().D();
Austin Schuhe3490622013-03-13 01:24:30 -0700125 }
126 double D(int i, int j) const { return D()(i, j); }
127 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700128 return coefficients().U_min();
Austin Schuhe3490622013-03-13 01:24:30 -0700129 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700130 double U_min(int i, int j) const { return U_min()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700131 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700132 return coefficients().U_max();
Austin Schuhe3490622013-03-13 01:24:30 -0700133 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700134 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700135
136 const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700137 double X(int i, int j) const { return X()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700138 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700139 double Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700140 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silverman5808bcb2014-09-14 21:40:43 -0400141 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700142
Brian Silverman0ca790b2014-06-12 21:33:08 -0700143 Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700144 double &mutable_X(int i, int j) { return mutable_X()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700145 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700146 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700147 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700148 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700149
150 const StateFeedbackPlantCoefficients<
151 number_of_states, number_of_inputs, number_of_outputs>
152 &coefficients() const {
153 return *coefficients_[plant_index_];
154 }
155
156 int plant_index() const { return plant_index_; }
157 void set_plant_index(int plant_index) {
158 if (plant_index < 0) {
159 plant_index_ = 0;
160 } else if (plant_index >= static_cast<int>(coefficients_.size())) {
Brian Silvermanb8cd6892013-03-17 23:36:24 -0700161 plant_index_ = static_cast<int>(coefficients_.size()) - 1;
Austin Schuhe3490622013-03-13 01:24:30 -0700162 } else {
163 plant_index_ = plant_index;
164 }
165 }
166
167 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700168 X_.setZero();
169 Y_.setZero();
170 U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800171 }
172
Austin Schuh849f0032013-03-03 23:59:53 -0800173 // Assert that U is within the hardware range.
174 virtual void CheckU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400175 for (int i = 0; i < kNumInputs; ++i) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700176 assert(U(i, 0) <= U_max(i, 0) + 0.00001);
177 assert(U(i, 0) >= U_min(i, 0) - 0.00001);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800178 }
179 }
Austin Schuh849f0032013-03-03 23:59:53 -0800180
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800181 // Computes the new X and Y given the control input.
182 void Update() {
Austin Schuh849f0032013-03-03 23:59:53 -0800183 // Powers outside of the range are more likely controller bugs than things
184 // that the plant should deal with.
185 CheckU();
Brian Silverman273d8a32014-05-10 22:19:09 -0700186 X_ = A() * X() + B() * U();
187 Y_ = C() * X() + D() * U();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800188 }
189
190 protected:
191 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800192 static const int kNumStates = number_of_states;
193 static const int kNumOutputs = number_of_outputs;
194 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700195
196 private:
Brian Silverman273d8a32014-05-10 22:19:09 -0700197 Eigen::Matrix<double, number_of_states, 1> X_;
198 Eigen::Matrix<double, number_of_outputs, 1> Y_;
199 Eigen::Matrix<double, number_of_inputs, 1> U_;
200
Austin Schuh1a387962015-01-31 16:36:20 -0800201 ::std::vector< ::std::unique_ptr<StateFeedbackPlantCoefficients<
202 number_of_states, number_of_inputs, number_of_outputs>>> coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700203
Austin Schuhe3490622013-03-13 01:24:30 -0700204 int plant_index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700205
206 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800207};
208
Austin Schuh9644e1c2013-03-12 00:40:36 -0700209// A Controller is a structure which holds a plant and the K and L matrices.
210// This is designed such that multiple controllers can share one set of state to
211// support gain scheduling easily.
212template <int number_of_states, int number_of_inputs, int number_of_outputs>
Brian Silverman273d8a32014-05-10 22:19:09 -0700213struct StateFeedbackController final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700214 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700215
Austin Schuh9644e1c2013-03-12 00:40:36 -0700216 const Eigen::Matrix<double, number_of_states, number_of_outputs> L;
Brian Silverman5808bcb2014-09-14 21:40:43 -0400217 const Eigen::Matrix<double, number_of_inputs, number_of_states> K;
Austin Schuh1a387962015-01-31 16:36:20 -0800218 const Eigen::Matrix<double, number_of_states, number_of_states> A_inv;
Austin Schuhe3490622013-03-13 01:24:30 -0700219 StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
220 number_of_outputs> plant;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700221
222 StateFeedbackController(
223 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L,
Brian Silverman5808bcb2014-09-14 21:40:43 -0400224 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
Austin Schuh1a387962015-01-31 16:36:20 -0800225 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
Austin Schuhe3490622013-03-13 01:24:30 -0700226 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
227 number_of_outputs> &plant)
Austin Schuh9644e1c2013-03-12 00:40:36 -0700228 : L(L),
229 K(K),
Austin Schuh1a387962015-01-31 16:36:20 -0800230 A_inv(A_inv),
Austin Schuh9644e1c2013-03-12 00:40:36 -0700231 plant(plant) {
232 }
233};
234
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800235template <int number_of_states, int number_of_inputs, int number_of_outputs>
236class StateFeedbackLoop {
237 public:
238 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
239
Brian Silverman0a151c92014-05-02 15:28:44 -0700240 StateFeedbackLoop(const StateFeedbackController<
241 number_of_states, number_of_inputs, number_of_outputs> &controller)
242 : controller_index_(0) {
Austin Schuh1a387962015-01-31 16:36:20 -0800243 controllers_.emplace_back(new StateFeedbackController<
Brian Silverman0a151c92014-05-02 15:28:44 -0700244 number_of_states, number_of_inputs, number_of_outputs>(controller));
245 Reset();
246 }
247
Brian Silverman0a151c92014-05-02 15:28:44 -0700248 StateFeedbackLoop(
249 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L,
Brian Silverman5808bcb2014-09-14 21:40:43 -0400250 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
Austin Schuh1a387962015-01-31 16:36:20 -0800251 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
Brian Silverman0a151c92014-05-02 15:28:44 -0700252 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
253 number_of_outputs> &plant)
254 : controller_index_(0) {
Austin Schuh1a387962015-01-31 16:36:20 -0800255 controllers_.emplace_back(
Brian Silverman0a151c92014-05-02 15:28:44 -0700256 new StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh1a387962015-01-31 16:36:20 -0800257 number_of_outputs>(L, K, A_inv, plant));
Brian Silverman0a151c92014-05-02 15:28:44 -0700258
259 Reset();
260 }
261
Austin Schuh1a387962015-01-31 16:36:20 -0800262 StateFeedbackLoop(::std::vector< ::std::unique_ptr<StateFeedbackController<
263 number_of_states, number_of_inputs, number_of_outputs>>> *controllers)
264 : controllers_(::std::move(*controllers)), controller_index_(0) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700265 Reset();
266 }
267
268 StateFeedbackLoop(StateFeedbackLoop &&other) {
269 X_hat_.swap(other.X_hat_);
270 R_.swap(other.R_);
271 U_.swap(other.U_);
272 U_uncapped_.swap(other.U_uncapped_);
273 ::std::swap(controllers_, other.controllers_);
274 Y_.swap(other.Y_);
275 new_y_ = other.new_y_;
276 controller_index_ = other.controller_index_;
277 }
278
Austin Schuh1a387962015-01-31 16:36:20 -0800279 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700280
Austin Schuh9644e1c2013-03-12 00:40:36 -0700281 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700282 return controller().plant.A();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700283 }
284 double A(int i, int j) const { return A()(i, j); }
285 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700286 return controller().plant.B();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700287 }
288 double B(int i, int j) const { return B()(i, j); }
289 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700290 return controller().plant.C();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700291 }
292 double C(int i, int j) const { return C()(i, j); }
293 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700294 return controller().plant.D();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700295 }
296 double D(int i, int j) const { return D()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700297 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
298 return controller().plant.U_min();
299 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700300 double U_min(int i, int j) const { return U_min()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700301 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
302 return controller().plant.U_max();
303 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700304 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700305
Brian Silverman5808bcb2014-09-14 21:40:43 -0400306 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700307 return controller().K;
308 }
309 double K(int i, int j) const { return K()(i, j); }
310 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const {
311 return controller().L;
312 }
313 double L(int i, int j) const { return L()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700314
315 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
316 return X_hat_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700317 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700318 double X_hat(int i, int j) const { return X_hat()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700319 const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700320 double R(int i, int j) const { return R()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700321 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700322 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700323 const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const {
324 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700325 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700326 double U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700327 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700328 double Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700329
Brian Silverman0ca790b2014-06-12 21:33:08 -0700330 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700331 double &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700332 Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700333 double &mutable_R(int i, int j) { return mutable_R()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700334 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700335 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700336 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700337 return U_uncapped_;
338 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700339 double &mutable_U_uncapped(int i, int j) {
340 return mutable_U_uncapped()(i, j);
341 }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700342 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700343 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800344
Brian Silverman2c590c32013-11-04 18:08:54 -0800345 const StateFeedbackController<number_of_states, number_of_inputs,
346 number_of_outputs> &controller() const {
Austin Schuhe3490622013-03-13 01:24:30 -0700347 return *controllers_[controller_index_];
Austin Schuh9644e1c2013-03-12 00:40:36 -0700348 }
349
Brian Silverman2c590c32013-11-04 18:08:54 -0800350 const StateFeedbackController<number_of_states, number_of_inputs,
351 number_of_outputs> &controller(
352 int index) const {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700353 return *controllers_[index];
354 }
355
Austin Schuh9644e1c2013-03-12 00:40:36 -0700356 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700357 X_hat_.setZero();
358 R_.setZero();
359 U_.setZero();
360 U_uncapped_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800361 }
362
363 // If U is outside the hardware range, limit it before the plant tries to use
364 // it.
365 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400366 for (int i = 0; i < kNumInputs; ++i) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700367 if (U(i, 0) > U_max(i, 0)) {
368 U_(i, 0) = U_max(i, 0);
369 } else if (U(i, 0) < U_min(i, 0)) {
370 U_(i, 0) = U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800371 }
372 }
373 }
374
Austin Schuhf9286cd2014-02-11 00:51:09 -0800375 // Corrects X_hat given the observation in Y.
376 void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
Austin Schuhcda86af2014-02-16 16:16:39 -0800377 /*
378 auto eye =
379 Eigen::Matrix<double, number_of_states, number_of_states>::Identity();
380 //LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0));
381 ::std::cout << "Identity " << eye << ::std::endl;
382 ::std::cout << "X_hat " << X_hat << ::std::endl;
383 ::std::cout << "LC " << L() * C() << ::std::endl;
384 ::std::cout << "L " << L() << ::std::endl;
385 ::std::cout << "C " << C() << ::std::endl;
386 ::std::cout << "y " << Y << ::std::endl;
387 ::std::cout << "z " << (Y - C() * X_hat) << ::std::endl;
388 ::std::cout << "correction " << L() * (Y - C() * X_hat) << ::std::endl;
389 X_hat = (eye - L() * C()) * X_hat + L() * Y;
390 ::std::cout << "X_hat after " << X_hat << ::std::endl;
391 ::std::cout << ::std::endl;
392 */
393 //LOG(DEBUG, "X_hat(2, 0) = %f\n", X_hat(2, 0));
Austin Schuhf9286cd2014-02-11 00:51:09 -0800394 Y_ = Y;
395 new_y_ = true;
396 }
397
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800398 // stop_motors is whether or not to output all 0s.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800399 void Update(bool stop_motors) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800400 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700401 U_.setZero();
402 U_uncapped_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800403 } else {
Brian Silverman273d8a32014-05-10 22:19:09 -0700404 U_ = U_uncapped_ = K() * (R() - X_hat());
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800405 CapU();
406 }
407
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000408 UpdateObserver();
409 }
410
411 void UpdateObserver() {
Austin Schuhf9286cd2014-02-11 00:51:09 -0800412 if (new_y_) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700413 X_hat_ = (A() - L() * C()) * X_hat() + L() * Y() + B() * U();
Austin Schuhf9286cd2014-02-11 00:51:09 -0800414 new_y_ = false;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800415 } else {
Brian Silverman273d8a32014-05-10 22:19:09 -0700416 X_hat_ = A() * X_hat() + B() * U();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800417 }
418 }
419
Brian Silverman273d8a32014-05-10 22:19:09 -0700420 // Sets the current controller to be index, clamped to be within range.
Austin Schuh9644e1c2013-03-12 00:40:36 -0700421 void set_controller_index(int index) {
Austin Schuhe3490622013-03-13 01:24:30 -0700422 if (index < 0) {
423 controller_index_ = 0;
424 } else if (index >= static_cast<int>(controllers_.size())) {
Brian Silvermanb8cd6892013-03-17 23:36:24 -0700425 controller_index_ = static_cast<int>(controllers_.size()) - 1;
Austin Schuhe3490622013-03-13 01:24:30 -0700426 } else {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700427 controller_index_ = index;
428 }
429 }
430
Austin Schuhd34569d2014-02-18 20:26:38 -0800431 int controller_index() const { return controller_index_; }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700432
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800433 protected:
Austin Schuh1a387962015-01-31 16:36:20 -0800434 ::std::vector< ::std::unique_ptr<StateFeedbackController<
435 number_of_states, number_of_inputs, number_of_outputs>>> controllers_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700436
Brian Silverman273d8a32014-05-10 22:19:09 -0700437 // These are accessible from non-templated subclasses.
Austin Schuhb1cdb382013-03-01 22:53:52 -0800438 static const int kNumStates = number_of_states;
439 static const int kNumOutputs = number_of_outputs;
440 static const int kNumInputs = number_of_inputs;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700441
Brian Silverman273d8a32014-05-10 22:19:09 -0700442 private:
443 Eigen::Matrix<double, number_of_states, 1> X_hat_;
444 Eigen::Matrix<double, number_of_states, 1> R_;
445 Eigen::Matrix<double, number_of_inputs, 1> U_;
446 Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_;
447
Austin Schuhf9286cd2014-02-11 00:51:09 -0800448 // Temporary storage for a measurement until I can figure out why I can't
449 // correct when the measurement is made.
450 Eigen::Matrix<double, number_of_outputs, 1> Y_;
451 bool new_y_ = false;
452
Austin Schuh9644e1c2013-03-12 00:40:36 -0700453 int controller_index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700454
Brian Silverman0a151c92014-05-02 15:28:44 -0700455 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800456};
457
Brian Silverman273d8a32014-05-10 22:19:09 -0700458#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_