blob: 3ed1a9ebefdfc0ace35422accaa34b0dead34091 [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>
Brian Silvermanc571e052013-03-13 17:58:56 -070010
Austin Schuhdc1c84a2013-02-23 16:33:10 -080011#include "Eigen/Dense"
12
Austin Schuhcda86af2014-02-16 16:16:39 -080013#include "aos/common/logging/logging.h"
Brian Silverman0a151c92014-05-02 15:28:44 -070014#include "aos/common/macros.h"
15
Brian Silverman5808bcb2014-09-14 21:40:43 -040016// For everything in this file, "inputs" and "outputs" are defined from the
17// perspective of the plant. This means U is an input and Y is an output
18// (because you give the plant U (powers) and it gives you back a Y (sensor
19// values). This is the opposite of what they mean from the perspective of the
20// controller (U is an output because that's what goes to the motors and Y is an
21// input because that's what comes back from the sensors).
22
Austin Schuhdc1c84a2013-02-23 16:33:10 -080023template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh64f17a52017-02-25 14:41:58 -080024struct StateFeedbackPlantCoefficients final {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080025 public:
26 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
27
Austin Schuhe3490622013-03-13 01:24:30 -070028 StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other)
Austin Schuh64f17a52017-02-25 14:41:58 -080029 : A(other.A),
Austin Schuhc5fceb82017-02-25 16:24:12 -080030 A_inv(other.A_inv),
Austin Schuh64f17a52017-02-25 14:41:58 -080031 A_continuous(other.A_continuous),
32 B(other.B),
33 B_continuous(other.B_continuous),
34 C(other.C),
35 D(other.D),
36 U_min(other.U_min),
37 U_max(other.U_max) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080038
Austin Schuhe3490622013-03-13 01:24:30 -070039 StateFeedbackPlantCoefficients(
Austin Schuhdc1c84a2013-02-23 16:33:10 -080040 const Eigen::Matrix<double, number_of_states, number_of_states> &A,
Austin Schuhc5fceb82017-02-25 16:24:12 -080041 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
Austin Schuh6c20f202017-02-18 22:31:44 -080042 const Eigen::Matrix<double, number_of_states, number_of_states>
43 &A_continuous,
Austin Schuhdc1c84a2013-02-23 16:33:10 -080044 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B,
Austin Schuh6c20f202017-02-18 22:31:44 -080045 const Eigen::Matrix<double, number_of_states, number_of_inputs>
46 &B_continuous,
Austin Schuhdc1c84a2013-02-23 16:33:10 -080047 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 Schuh64f17a52017-02-25 14:41:58 -080051 : A(A),
Austin Schuhc5fceb82017-02-25 16:24:12 -080052 A_inv(A_inv),
Austin Schuh64f17a52017-02-25 14:41:58 -080053 A_continuous(A_continuous),
54 B(B),
55 B_continuous(B_continuous),
56 C(C),
57 D(D),
58 U_min(U_min),
59 U_max(U_max) {}
Austin Schuhe3490622013-03-13 01:24:30 -070060
Austin Schuh64f17a52017-02-25 14:41:58 -080061 const Eigen::Matrix<double, number_of_states, number_of_states> A;
Austin Schuhc5fceb82017-02-25 16:24:12 -080062 const Eigen::Matrix<double, number_of_states, number_of_states> A_inv;
Austin Schuh64f17a52017-02-25 14:41:58 -080063 const Eigen::Matrix<double, number_of_states, number_of_states> A_continuous;
64 const Eigen::Matrix<double, number_of_states, number_of_inputs> B;
65 const Eigen::Matrix<double, number_of_states, number_of_inputs> B_continuous;
66 const Eigen::Matrix<double, number_of_outputs, number_of_states> C;
67 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D;
68 const Eigen::Matrix<double, number_of_inputs, 1> U_min;
69 const Eigen::Matrix<double, number_of_inputs, 1> U_max;
Austin Schuhe3490622013-03-13 01:24:30 -070070};
71
72template <int number_of_states, int number_of_inputs, int number_of_outputs>
73class StateFeedbackPlant {
74 public:
75 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070076
77 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080078 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh66c19882017-02-25 13:36:28 -080079 number_of_states, number_of_inputs, number_of_outputs>>>
80 *coefficients)
Austin Schuhc5fceb82017-02-25 16:24:12 -080081 : coefficients_(::std::move(*coefficients)), index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070082 Reset();
83 }
84
85 StateFeedbackPlant(StateFeedbackPlant &&other)
Austin Schuhc5fceb82017-02-25 16:24:12 -080086 : index_(other.index_) {
Brian Silverman0a151c92014-05-02 15:28:44 -070087 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -070088 X_.swap(other.X_);
89 Y_.swap(other.Y_);
Brian Silverman0a151c92014-05-02 15:28:44 -070090 }
91
Austin Schuh1a387962015-01-31 16:36:20 -080092 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -070093
Austin Schuhe3490622013-03-13 01:24:30 -070094 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080095 return coefficients().A;
Austin Schuhe3490622013-03-13 01:24:30 -070096 }
97 double A(int i, int j) const { return A()(i, j); }
Austin Schuhc5fceb82017-02-25 16:24:12 -080098 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv() const {
99 return coefficients().A_inv;
100 }
101 double A_inv(int i, int j) const { return A_inv()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700102 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800103 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -0700104 }
105 double B(int i, int j) const { return B()(i, j); }
106 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800107 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -0700108 }
109 double C(int i, int j) const { return C()(i, j); }
110 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800111 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -0700112 }
113 double D(int i, int j) const { return D()(i, j); }
114 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800115 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700116 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700117 double U_min(int i, int j) const { return U_min()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700118 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800119 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700120 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700121 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700122
123 const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700124 double X(int i, int j) const { return X()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700125 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700126 double Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700127
Brian Silverman0ca790b2014-06-12 21:33:08 -0700128 Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700129 double &mutable_X(int i, int j) { return mutable_X()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700130 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700131 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700132
Austin Schuhb6a6d822016-02-08 00:20:40 -0800133 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuhc5fceb82017-02-25 16:24:12 -0800134 number_of_outputs>
135 &coefficients(int index) const {
136 return *coefficients_[index];
Austin Schuhe3490622013-03-13 01:24:30 -0700137 }
138
Austin Schuhc5fceb82017-02-25 16:24:12 -0800139 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
140 number_of_outputs>
141 &coefficients() const {
142 return *coefficients_[index_];
143 }
144
145 int index() const { return index_; }
146 void set_index(int index) {
147 assert(index >= 0);
148 assert(index < static_cast<int>(coefficients_.size()));
149 index_ = index;
Austin Schuhe3490622013-03-13 01:24:30 -0700150 }
151
152 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700153 X_.setZero();
154 Y_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800155 }
156
Austin Schuh849f0032013-03-03 23:59:53 -0800157 // Assert that U is within the hardware range.
Austin Schuh66c19882017-02-25 13:36:28 -0800158 virtual void CheckU(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400159 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuh66c19882017-02-25 13:36:28 -0800160 if (U(i, 0) > U_max(i, 0) + 0.00001 || U(i, 0) < U_min(i, 0) - 0.00001) {
161 LOG(FATAL, "U out of range\n");
162 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800163 }
164 }
Austin Schuh849f0032013-03-03 23:59:53 -0800165
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800166 // Computes the new X and Y given the control input.
Austin Schuh66c19882017-02-25 13:36:28 -0800167 void Update(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Austin Schuh849f0032013-03-03 23:59:53 -0800168 // Powers outside of the range are more likely controller bugs than things
169 // that the plant should deal with.
Austin Schuh66c19882017-02-25 13:36:28 -0800170 CheckU(U);
171 X_ = A() * X() + B() * U;
172 Y_ = C() * X() + D() * U;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800173 }
174
175 protected:
176 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800177 static const int kNumStates = number_of_states;
178 static const int kNumOutputs = number_of_outputs;
179 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700180
181 private:
Brian Silverman273d8a32014-05-10 22:19:09 -0700182 Eigen::Matrix<double, number_of_states, 1> X_;
183 Eigen::Matrix<double, number_of_outputs, 1> Y_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700184
Austin Schuhb6a6d822016-02-08 00:20:40 -0800185 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh64f17a52017-02-25 14:41:58 -0800186 number_of_states, number_of_inputs, number_of_outputs>>>
187 coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700188
Austin Schuhc5fceb82017-02-25 16:24:12 -0800189 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700190
191 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800192};
193
Austin Schuh9644e1c2013-03-12 00:40:36 -0700194// A Controller is a structure which holds a plant and the K and L matrices.
195// This is designed such that multiple controllers can share one set of state to
196// support gain scheduling easily.
197template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh66c19882017-02-25 13:36:28 -0800198struct StateFeedbackControllerConstants final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700199 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700200
Austin Schuh9644e1c2013-03-12 00:40:36 -0700201 const Eigen::Matrix<double, number_of_states, number_of_outputs> L;
Brian Silverman5808bcb2014-09-14 21:40:43 -0400202 const Eigen::Matrix<double, number_of_inputs, number_of_states> K;
Austin Schuh86093ad2016-02-06 14:29:34 -0800203 const Eigen::Matrix<double, number_of_inputs, number_of_states> Kff;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700204
Austin Schuh66c19882017-02-25 13:36:28 -0800205 StateFeedbackControllerConstants(
Austin Schuh9644e1c2013-03-12 00:40:36 -0700206 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L,
Brian Silverman5808bcb2014-09-14 21:40:43 -0400207 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
Austin Schuhc5fceb82017-02-25 16:24:12 -0800208 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff)
209 : L(L), K(K), Kff(Kff) {}
Austin Schuh9644e1c2013-03-12 00:40:36 -0700210};
211
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800212template <int number_of_states, int number_of_inputs, int number_of_outputs>
213class StateFeedbackLoop {
214 public:
215 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
216
Austin Schuh66c19882017-02-25 13:36:28 -0800217 StateFeedbackLoop(
Austin Schuhc5fceb82017-02-25 16:24:12 -0800218 StateFeedbackPlant<number_of_states, number_of_inputs, number_of_outputs>
219 &&plant,
Austin Schuh66c19882017-02-25 13:36:28 -0800220 ::std::vector<::std::unique_ptr<StateFeedbackControllerConstants<
221 number_of_states, number_of_inputs, number_of_outputs>>> *controllers)
Austin Schuhc5fceb82017-02-25 16:24:12 -0800222 : plant_(::std::move(plant)),
223 controllers_(::std::move(*controllers)),
224 index_(0) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700225 Reset();
226 }
227
Austin Schuhc5fceb82017-02-25 16:24:12 -0800228 StateFeedbackLoop(StateFeedbackLoop &&other)
229 : plant_(::std::move(other.plant_)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700230 X_hat_.swap(other.X_hat_);
231 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800232 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700233 U_.swap(other.U_);
234 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800235 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700236 ::std::swap(controllers_, other.controllers_);
Austin Schuhc5fceb82017-02-25 16:24:12 -0800237 index_ = other.index_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700238 }
239
Austin Schuh1a387962015-01-31 16:36:20 -0800240 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700241
Brian Silverman5808bcb2014-09-14 21:40:43 -0400242 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700243 return controller().K;
244 }
245 double K(int i, int j) const { return K()(i, j); }
Austin Schuh86093ad2016-02-06 14:29:34 -0800246 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff() const {
247 return controller().Kff;
248 }
249 double Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700250 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const {
251 return controller().L;
252 }
253 double L(int i, int j) const { return L()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700254
255 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
256 return X_hat_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700257 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700258 double X_hat(int i, int j) const { return X_hat()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700259 const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700260 double R(int i, int j) const { return R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800261 const Eigen::Matrix<double, number_of_states, 1> &next_R() const {
262 return next_R_;
263 }
264 double next_R(int i, int j) const { return next_R()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700265 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700266 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700267 const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const {
268 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700269 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700270 double U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800271 const Eigen::Matrix<double, number_of_inputs, 1> &ff_U() const {
272 return ff_U_;
273 }
274 double ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700275
Brian Silverman0ca790b2014-06-12 21:33:08 -0700276 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700277 double &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700278 Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700279 double &mutable_R(int i, int j) { return mutable_R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800280 Eigen::Matrix<double, number_of_states, 1> &mutable_next_R() {
281 return next_R_;
282 }
283 double &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700284 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700285 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700286 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700287 return U_uncapped_;
288 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700289 double &mutable_U_uncapped(int i, int j) {
290 return mutable_U_uncapped()(i, j);
291 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800292
Austin Schuhc5fceb82017-02-25 16:24:12 -0800293 const StateFeedbackPlant<number_of_states, number_of_inputs,
294 number_of_outputs>
295 &plant() const {
296 return plant_;
297 }
298
Austin Schuh66c19882017-02-25 13:36:28 -0800299 const StateFeedbackControllerConstants<number_of_states, number_of_inputs,
300 number_of_outputs>
301 &controller() const {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800302 return *controllers_[index_];
Austin Schuh9644e1c2013-03-12 00:40:36 -0700303 }
304
Austin Schuh66c19882017-02-25 13:36:28 -0800305 const StateFeedbackControllerConstants<number_of_states, number_of_inputs,
306 number_of_outputs>
307 &controller(int index) const {
Austin Schuh2054f5f2013-10-27 14:54:10 -0700308 return *controllers_[index];
309 }
310
Austin Schuh9644e1c2013-03-12 00:40:36 -0700311 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700312 X_hat_.setZero();
313 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800314 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700315 U_.setZero();
316 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800317 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800318 }
319
320 // If U is outside the hardware range, limit it before the plant tries to use
321 // it.
322 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400323 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800324 if (U(i, 0) > plant().U_max(i, 0)) {
325 U_(i, 0) = plant().U_max(i, 0);
326 } else if (U(i, 0) < plant().U_min(i, 0)) {
327 U_(i, 0) = plant().U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800328 }
329 }
330 }
331
Austin Schuhf9286cd2014-02-11 00:51:09 -0800332 // Corrects X_hat given the observation in Y.
333 void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800334 X_hat_ +=
335 plant().A_inv() * L() * (Y - plant().C() * X_hat_ - plant().D() * U());
Austin Schuhf9286cd2014-02-11 00:51:09 -0800336 }
337
Austin Schuh3f862bb2016-02-27 14:48:05 -0800338 const Eigen::Matrix<double, number_of_states, 1> error() const {
339 return R() - X_hat();
340 }
341
Austin Schuhb6a6d822016-02-08 00:20:40 -0800342 // Returns the calculated controller power.
343 virtual const Eigen::Matrix<double, number_of_inputs, 1> ControllerOutput() {
344 ff_U_ = FeedForward();
Austin Schuh3f862bb2016-02-27 14:48:05 -0800345 return K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800346 }
347
348 // Calculates the feed forwards power.
349 virtual const Eigen::Matrix<double, number_of_inputs, 1> FeedForward() {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800350 return Kff() * (next_R() - plant().A() * R());
Austin Schuhb6a6d822016-02-08 00:20:40 -0800351 }
352
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800353 // stop_motors is whether or not to output all 0s.
Austin Schuhf9286cd2014-02-11 00:51:09 -0800354 void Update(bool stop_motors) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800355 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700356 U_.setZero();
357 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800358 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800359 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800360 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800361 CapU();
362 }
363
Austin Schuhc2b77742015-11-26 16:18:27 -0800364 UpdateObserver(U_);
Austin Schuh093535c2016-03-05 23:21:00 -0800365
366 UpdateFFReference();
367 }
368
369 // Updates R() after any CapU operations happen on U().
370 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800371 ff_U_ -= U_uncapped() - U();
372 if (!Kff().isZero(0)) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800373 R_ = plant().A() * R() + plant().B() * ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800374 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000375 }
376
Austin Schuhc2b77742015-11-26 16:18:27 -0800377 void UpdateObserver(const Eigen::Matrix<double, number_of_inputs, 1> &new_u) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800378 X_hat_ = plant().A() * X_hat() + plant().B() * new_u;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800379 }
380
Brian Silverman273d8a32014-05-10 22:19:09 -0700381 // Sets the current controller to be index, clamped to be within range.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800382 void set_index(int index) {
Austin Schuhe3490622013-03-13 01:24:30 -0700383 if (index < 0) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800384 index_ = 0;
Austin Schuhe3490622013-03-13 01:24:30 -0700385 } else if (index >= static_cast<int>(controllers_.size())) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800386 index_ = static_cast<int>(controllers_.size()) - 1;
Austin Schuhe3490622013-03-13 01:24:30 -0700387 } else {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800388 index_ = index;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700389 }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800390 plant_.set_index(index);
Austin Schuh9644e1c2013-03-12 00:40:36 -0700391 }
392
Austin Schuhc5fceb82017-02-25 16:24:12 -0800393 int index() const { return index_; }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700394
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800395 protected:
Austin Schuhc5fceb82017-02-25 16:24:12 -0800396 StateFeedbackPlant<number_of_states, number_of_inputs, number_of_outputs>
397 plant_;
398
Austin Schuh66c19882017-02-25 13:36:28 -0800399 ::std::vector<::std::unique_ptr<StateFeedbackControllerConstants<
400 number_of_states, number_of_inputs, number_of_outputs>>>
401 controllers_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700402
Brian Silverman273d8a32014-05-10 22:19:09 -0700403 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800404 static constexpr int kNumStates = number_of_states;
405 static constexpr int kNumOutputs = number_of_outputs;
406 static constexpr int kNumInputs = number_of_inputs;
407
408 // Portion of U which is based on the feed-forwards.
409 Eigen::Matrix<double, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700410
Brian Silverman273d8a32014-05-10 22:19:09 -0700411 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800412 // Internal state estimate.
Brian Silverman273d8a32014-05-10 22:19:09 -0700413 Eigen::Matrix<double, number_of_states, 1> X_hat_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800414 // Current goal (Used by the feed-back controller).
Brian Silverman273d8a32014-05-10 22:19:09 -0700415 Eigen::Matrix<double, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800416 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
417 Eigen::Matrix<double, number_of_states, 1> next_R_;
418 // Computed output after being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700419 Eigen::Matrix<double, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800420 // Computed output before being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700421 Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_;
422
Austin Schuhc5fceb82017-02-25 16:24:12 -0800423 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700424
Brian Silverman0a151c92014-05-02 15:28:44 -0700425 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800426};
427
Brian Silverman273d8a32014-05-10 22:19:09 -0700428#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_