blob: 891aaa67254c66c92fae0b8c98393659f5a380bf [file] [log] [blame]
Brian Silverman273d8a32014-05-10 22:19:09 -07001#ifndef FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_
2#define FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_
Austin Schuhdc1c84a2013-02-23 16:33:10 -08003
Austin Schuh849f0032013-03-03 23:59:53 -08004#include <assert.h>
Austin Schuhdc1c84a2013-02-23 16:33:10 -08005
Austin Schuhcda86af2014-02-16 16:16:39 -08006#include <iostream>
Austin Schuhc5fceb82017-02-25 16:24:12 -08007#include <memory>
8#include <utility>
9#include <vector>
Austin Schuh3ad5ed82017-02-25 21:36:19 -080010#include <chrono>
Brian Silvermanc571e052013-03-13 17:58:56 -070011
Austin Schuhdc1c84a2013-02-23 16:33:10 -080012#include "Eigen/Dense"
Austin Schuh3ad5ed82017-02-25 21:36:19 -080013#include "unsupported/Eigen/MatrixFunctions"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080014
Austin Schuh3ad5ed82017-02-25 21:36:19 -080015#include "aos/common/controls/control_loop.h"
Austin Schuhcda86af2014-02-16 16:16:39 -080016#include "aos/common/logging/logging.h"
Brian Silverman0a151c92014-05-02 15:28:44 -070017#include "aos/common/macros.h"
18
Austin Schuhe91f14c2017-02-25 19:43:57 -080019template <int number_of_states, int number_of_inputs, int number_of_outputs,
20 typename PlantType, typename ObserverType>
21class StateFeedbackLoop;
22
Brian Silverman5808bcb2014-09-14 21:40:43 -040023// For everything in this file, "inputs" and "outputs" are defined from the
24// perspective of the plant. This means U is an input and Y is an output
25// (because you give the plant U (powers) and it gives you back a Y (sensor
26// values). This is the opposite of what they mean from the perspective of the
27// controller (U is an output because that's what goes to the motors and Y is an
28// input because that's what comes back from the sensors).
29
Austin Schuhdc1c84a2013-02-23 16:33:10 -080030template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh64f17a52017-02-25 14:41:58 -080031struct StateFeedbackPlantCoefficients final {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080032 public:
33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
34
Austin Schuhe3490622013-03-13 01:24:30 -070035 StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other)
Austin Schuh64f17a52017-02-25 14:41:58 -080036 : A(other.A),
Austin Schuhc5fceb82017-02-25 16:24:12 -080037 A_inv(other.A_inv),
Austin Schuh64f17a52017-02-25 14:41:58 -080038 B(other.B),
Austin Schuh64f17a52017-02-25 14:41:58 -080039 C(other.C),
40 D(other.D),
41 U_min(other.U_min),
42 U_max(other.U_max) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080043
Austin Schuhe3490622013-03-13 01:24:30 -070044 StateFeedbackPlantCoefficients(
Austin Schuhdc1c84a2013-02-23 16:33:10 -080045 const Eigen::Matrix<double, number_of_states, number_of_states> &A,
Austin Schuhc5fceb82017-02-25 16:24:12 -080046 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv,
Austin Schuhdc1c84a2013-02-23 16:33:10 -080047 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B,
48 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C,
49 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D,
Brian Silverman5808bcb2014-09-14 21:40:43 -040050 const Eigen::Matrix<double, number_of_inputs, 1> &U_max,
51 const Eigen::Matrix<double, number_of_inputs, 1> &U_min)
Austin Schuh3ad5ed82017-02-25 21:36:19 -080052 : A(A), A_inv(A_inv), B(B), C(C), D(D), U_min(U_min), U_max(U_max) {}
Austin Schuhe3490622013-03-13 01:24:30 -070053
Austin Schuh64f17a52017-02-25 14:41:58 -080054 const Eigen::Matrix<double, number_of_states, number_of_states> A;
Austin Schuhc5fceb82017-02-25 16:24:12 -080055 const Eigen::Matrix<double, number_of_states, number_of_states> A_inv;
Austin Schuh64f17a52017-02-25 14:41:58 -080056 const Eigen::Matrix<double, number_of_states, number_of_inputs> B;
Austin Schuh64f17a52017-02-25 14:41:58 -080057 const Eigen::Matrix<double, number_of_outputs, number_of_states> C;
58 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D;
59 const Eigen::Matrix<double, number_of_inputs, 1> U_min;
60 const Eigen::Matrix<double, number_of_inputs, 1> U_max;
Austin Schuhe3490622013-03-13 01:24:30 -070061};
62
63template <int number_of_states, int number_of_inputs, int number_of_outputs>
64class StateFeedbackPlant {
65 public:
66 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070067
68 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080069 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh66c19882017-02-25 13:36:28 -080070 number_of_states, number_of_inputs, number_of_outputs>>>
71 *coefficients)
Austin Schuhc5fceb82017-02-25 16:24:12 -080072 : coefficients_(::std::move(*coefficients)), index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070073 Reset();
74 }
75
76 StateFeedbackPlant(StateFeedbackPlant &&other)
Austin Schuhc5fceb82017-02-25 16:24:12 -080077 : index_(other.index_) {
Brian Silverman0a151c92014-05-02 15:28:44 -070078 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -070079 X_.swap(other.X_);
80 Y_.swap(other.Y_);
Brian Silverman0a151c92014-05-02 15:28:44 -070081 }
82
Austin Schuh1a387962015-01-31 16:36:20 -080083 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -070084
Austin Schuhe3490622013-03-13 01:24:30 -070085 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080086 return coefficients().A;
Austin Schuhe3490622013-03-13 01:24:30 -070087 }
88 double A(int i, int j) const { return A()(i, j); }
Austin Schuhc5fceb82017-02-25 16:24:12 -080089 const Eigen::Matrix<double, number_of_states, number_of_states> &A_inv() const {
90 return coefficients().A_inv;
91 }
92 double A_inv(int i, int j) const { return A_inv()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -070093 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080094 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -070095 }
96 double B(int i, int j) const { return B()(i, j); }
97 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080098 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -070099 }
100 double C(int i, int j) const { return C()(i, j); }
101 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800102 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -0700103 }
104 double D(int i, int j) const { return D()(i, j); }
105 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800106 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700107 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700108 double U_min(int i, int j) const { return U_min()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700109 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800110 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700111 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700112 double U_max(int i, int j) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700113
114 const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700115 double X(int i, int j) const { return X()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700116 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700117 double Y(int i, int j) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700118
Brian Silverman0ca790b2014-06-12 21:33:08 -0700119 Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700120 double &mutable_X(int i, int j) { return mutable_X()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700121 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700122 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700123
Austin Schuhb6a6d822016-02-08 00:20:40 -0800124 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuhc5fceb82017-02-25 16:24:12 -0800125 number_of_outputs>
126 &coefficients(int index) const {
127 return *coefficients_[index];
Austin Schuhe3490622013-03-13 01:24:30 -0700128 }
129
Austin Schuhc5fceb82017-02-25 16:24:12 -0800130 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
131 number_of_outputs>
132 &coefficients() const {
133 return *coefficients_[index_];
134 }
135
136 int index() const { return index_; }
137 void set_index(int index) {
138 assert(index >= 0);
139 assert(index < static_cast<int>(coefficients_.size()));
140 index_ = index;
Austin Schuhe3490622013-03-13 01:24:30 -0700141 }
142
143 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700144 X_.setZero();
145 Y_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800146 }
147
Austin Schuh849f0032013-03-03 23:59:53 -0800148 // Assert that U is within the hardware range.
Austin Schuh66c19882017-02-25 13:36:28 -0800149 virtual void CheckU(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400150 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuh66c19882017-02-25 13:36:28 -0800151 if (U(i, 0) > U_max(i, 0) + 0.00001 || U(i, 0) < U_min(i, 0) - 0.00001) {
152 LOG(FATAL, "U out of range\n");
153 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800154 }
155 }
Austin Schuh849f0032013-03-03 23:59:53 -0800156
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800157 // Computes the new X and Y given the control input.
Austin Schuh66c19882017-02-25 13:36:28 -0800158 void Update(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Austin Schuh849f0032013-03-03 23:59:53 -0800159 // Powers outside of the range are more likely controller bugs than things
160 // that the plant should deal with.
Austin Schuh66c19882017-02-25 13:36:28 -0800161 CheckU(U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800162 X_ = Update(X(), U);
Austin Schuh01c7b252017-03-05 00:59:31 -0800163 UpdateY(U);
164 }
165
166 // Computes the new Y given the control input.
167 void UpdateY(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
Austin Schuh66c19882017-02-25 13:36:28 -0800168 Y_ = C() * X() + D() * U;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800169 }
170
Austin Schuhe91f14c2017-02-25 19:43:57 -0800171 Eigen::Matrix<double, number_of_states, 1> Update(
172 const Eigen::Matrix<double, number_of_states, 1> X,
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800173 const Eigen::Matrix<double, number_of_inputs, 1> &U) const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800174 return A() * X + B() * U;
175 }
176
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800177 protected:
178 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800179 static const int kNumStates = number_of_states;
180 static const int kNumOutputs = number_of_outputs;
181 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700182
183 private:
Brian Silverman273d8a32014-05-10 22:19:09 -0700184 Eigen::Matrix<double, number_of_states, 1> X_;
185 Eigen::Matrix<double, number_of_outputs, 1> Y_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700186
Austin Schuhb6a6d822016-02-08 00:20:40 -0800187 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh64f17a52017-02-25 14:41:58 -0800188 number_of_states, number_of_inputs, number_of_outputs>>>
189 coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700190
Austin Schuhc5fceb82017-02-25 16:24:12 -0800191 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700192
193 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800194};
195
Austin Schuh32501832017-02-25 18:32:56 -0800196// A container for all the controller coefficients.
Austin Schuh9644e1c2013-03-12 00:40:36 -0700197template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh32501832017-02-25 18:32:56 -0800198struct StateFeedbackControllerCoefficients final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700199 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700200
Brian Silverman5808bcb2014-09-14 21:40:43 -0400201 const Eigen::Matrix<double, number_of_inputs, number_of_states> K;
Austin Schuh86093ad2016-02-06 14:29:34 -0800202 const Eigen::Matrix<double, number_of_inputs, number_of_states> Kff;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700203
Austin Schuh32501832017-02-25 18:32:56 -0800204 StateFeedbackControllerCoefficients(
Brian Silverman5808bcb2014-09-14 21:40:43 -0400205 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K,
Austin Schuhc5fceb82017-02-25 16:24:12 -0800206 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff)
Austin Schuh32501832017-02-25 18:32:56 -0800207 : K(K), Kff(Kff) {}
208};
209
210template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800211struct StateFeedbackHybridPlantCoefficients final {
212 public:
213 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
214
215 StateFeedbackHybridPlantCoefficients(
216 const StateFeedbackHybridPlantCoefficients &other)
217 : A_continuous(other.A_continuous),
218 B_continuous(other.B_continuous),
219 C(other.C),
220 D(other.D),
221 U_min(other.U_min),
222 U_max(other.U_max) {}
223
224 StateFeedbackHybridPlantCoefficients(
225 const Eigen::Matrix<double, number_of_states, number_of_states>
226 &A_continuous,
227 const Eigen::Matrix<double, number_of_states, number_of_inputs>
228 &B_continuous,
229 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C,
230 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D,
231 const Eigen::Matrix<double, number_of_inputs, 1> &U_max,
232 const Eigen::Matrix<double, number_of_inputs, 1> &U_min)
233 : A_continuous(A_continuous),
234 B_continuous(B_continuous),
235 C(C),
236 D(D),
237 U_min(U_min),
238 U_max(U_max) {}
239
240 const Eigen::Matrix<double, number_of_states, number_of_states> A_continuous;
241 const Eigen::Matrix<double, number_of_states, number_of_inputs> B_continuous;
242 const Eigen::Matrix<double, number_of_outputs, number_of_states> C;
243 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> D;
244 const Eigen::Matrix<double, number_of_inputs, 1> U_min;
245 const Eigen::Matrix<double, number_of_inputs, 1> U_max;
246};
247
248template <int number_of_states, int number_of_inputs, int number_of_outputs>
249class StateFeedbackHybridPlant {
250 public:
251 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
252
253 StateFeedbackHybridPlant(
254 ::std::vector<::std::unique_ptr<StateFeedbackHybridPlantCoefficients<
255 number_of_states, number_of_inputs, number_of_outputs>>>
256 *coefficients)
257 : coefficients_(::std::move(*coefficients)), index_(0) {
258 Reset();
259 }
260
261 StateFeedbackHybridPlant(StateFeedbackHybridPlant &&other)
262 : index_(other.index_) {
263 ::std::swap(coefficients_, other.coefficients_);
264 X_.swap(other.X_);
265 Y_.swap(other.Y_);
266 }
267
268 virtual ~StateFeedbackHybridPlant() {}
269
270 const Eigen::Matrix<double, number_of_states, number_of_states> &A() const {
271 return A_;
272 }
273 double A(int i, int j) const { return A()(i, j); }
274 const Eigen::Matrix<double, number_of_states, number_of_inputs> &B() const {
275 return B_;
276 }
277 double B(int i, int j) const { return B()(i, j); }
278 const Eigen::Matrix<double, number_of_outputs, number_of_states> &C() const {
279 return coefficients().C;
280 }
281 double C(int i, int j) const { return C()(i, j); }
282 const Eigen::Matrix<double, number_of_outputs, number_of_inputs> &D() const {
283 return coefficients().D;
284 }
285 double D(int i, int j) const { return D()(i, j); }
286 const Eigen::Matrix<double, number_of_inputs, 1> &U_min() const {
287 return coefficients().U_min;
288 }
289 double U_min(int i, int j) const { return U_min()(i, j); }
290 const Eigen::Matrix<double, number_of_inputs, 1> &U_max() const {
291 return coefficients().U_max;
292 }
293 double U_max(int i, int j) const { return U_max()(i, j); }
294
295 const Eigen::Matrix<double, number_of_states, 1> &X() const { return X_; }
296 double X(int i, int j) const { return X()(i, j); }
297 const Eigen::Matrix<double, number_of_outputs, 1> &Y() const { return Y_; }
298 double Y(int i, int j) const { return Y()(i, j); }
299
300 Eigen::Matrix<double, number_of_states, 1> &mutable_X() { return X_; }
301 double &mutable_X(int i, int j) { return mutable_X()(i, j); }
302 Eigen::Matrix<double, number_of_outputs, 1> &mutable_Y() { return Y_; }
303 double &mutable_Y(int i, int j) { return mutable_Y()(i, j); }
304
305 const StateFeedbackHybridPlantCoefficients<number_of_states, number_of_inputs,
306 number_of_outputs>
307 &coefficients(int index) const {
308 return *coefficients_[index];
309 }
310
311 const StateFeedbackHybridPlantCoefficients<number_of_states, number_of_inputs,
312 number_of_outputs>
313 &coefficients() const {
314 return *coefficients_[index_];
315 }
316
317 int index() const { return index_; }
318 void set_index(int index) {
319 assert(index >= 0);
320 assert(index < static_cast<int>(coefficients_.size()));
321 index_ = index;
322 }
323
324 void Reset() {
325 X_.setZero();
326 Y_.setZero();
327 A_.setZero();
328 B_.setZero();
329 UpdateAB(::aos::controls::kLoopFrequency);
330 }
331
332 // Assert that U is within the hardware range.
333 virtual void CheckU(const Eigen::Matrix<double, number_of_inputs, 1> &U) {
334 for (int i = 0; i < kNumInputs; ++i) {
335 if (U(i, 0) > U_max(i, 0) + 0.00001 || U(i, 0) < U_min(i, 0) - 0.00001) {
336 LOG(FATAL, "U out of range\n");
337 }
338 }
339 }
340
341 // Computes the new X and Y given the control input.
342 void Update(const Eigen::Matrix<double, number_of_inputs, 1> &U,
343 ::std::chrono::nanoseconds dt) {
344 // Powers outside of the range are more likely controller bugs than things
345 // that the plant should deal with.
346 CheckU(U);
347 X_ = Update(X(), U, dt);
348 Y_ = C() * X() + D() * U;
349 }
350
351 Eigen::Matrix<double, number_of_states, 1> Update(
352 const Eigen::Matrix<double, number_of_states, 1> X,
353 const Eigen::Matrix<double, number_of_inputs, 1> &U,
354 ::std::chrono::nanoseconds dt) {
355 UpdateAB(dt);
356 return A() * X + B() * U;
357 }
358
359 protected:
360 // these are accessible from non-templated subclasses
361 static const int kNumStates = number_of_states;
362 static const int kNumOutputs = number_of_outputs;
363 static const int kNumInputs = number_of_inputs;
364
365 private:
366 void UpdateAB(::std::chrono::nanoseconds dt) {
367 Eigen::Matrix<double, number_of_states + number_of_inputs,
368 number_of_states + number_of_inputs>
369 M_state_continuous;
370 M_state_continuous.setZero();
371 M_state_continuous.template block<number_of_states, number_of_states>(0,
372 0) =
373 coefficients().A_continuous *
374 ::std::chrono::duration_cast<::std::chrono::duration<double>>(dt)
375 .count();
376 M_state_continuous.template block<number_of_states, number_of_inputs>(
377 0, number_of_states) =
378 coefficients().B_continuous *
379 ::std::chrono::duration_cast<::std::chrono::duration<double>>(dt)
380 .count();
381
382 Eigen::Matrix<double, number_of_states + number_of_inputs,
383 number_of_states + number_of_inputs>
384 M_state = M_state_continuous.exp();
385 A_ = M_state.template block<number_of_states, number_of_states>(0, 0);
386 B_ = M_state.template block<number_of_states, number_of_inputs>(
387 0, number_of_states);
388 }
389
390 Eigen::Matrix<double, number_of_states, 1> X_;
391 Eigen::Matrix<double, number_of_outputs, 1> Y_;
392
393 Eigen::Matrix<double, number_of_states, number_of_states> A_;
394 Eigen::Matrix<double, number_of_states, number_of_inputs> B_;
395
396
397 ::std::vector<::std::unique_ptr<StateFeedbackHybridPlantCoefficients<
398 number_of_states, number_of_inputs, number_of_outputs>>>
399 coefficients_;
400
401 int index_;
402
403 DISALLOW_COPY_AND_ASSIGN(StateFeedbackHybridPlant);
404};
405
406template <int number_of_states, int number_of_inputs, int number_of_outputs>
Austin Schuh32501832017-02-25 18:32:56 -0800407class StateFeedbackController {
408 public:
409 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
410
411 explicit StateFeedbackController(
412 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
413 number_of_states, number_of_inputs, number_of_outputs>>> *controllers)
414 : coefficients_(::std::move(*controllers)) {}
415
416 StateFeedbackController(StateFeedbackController &&other)
417 : index_(other.index_) {
418 ::std::swap(coefficients_, other.coefficients_);
419 }
420
421 const Eigen::Matrix<double, number_of_inputs, number_of_states> &K() const {
422 return coefficients().K;
423 }
424 double K(int i, int j) const { return K()(i, j); }
425 const Eigen::Matrix<double, number_of_inputs, number_of_states> &Kff() const {
426 return coefficients().Kff;
427 }
428 double Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800429
Austin Schuhe91f14c2017-02-25 19:43:57 -0800430 void Reset() {}
431
Austin Schuh32501832017-02-25 18:32:56 -0800432 // Sets the current controller to be index, clamped to be within range.
433 void set_index(int index) {
434 if (index < 0) {
435 index_ = 0;
436 } else if (index >= static_cast<int>(coefficients_.size())) {
437 index_ = static_cast<int>(coefficients_.size()) - 1;
438 } else {
439 index_ = index;
440 }
441 }
442
443 int index() const { return index_; }
444
445 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
446 number_of_outputs>
447 &coefficients(int index) const {
448 return *coefficients_[index];
449 }
450
451 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
452 number_of_outputs>
453 &coefficients() const {
454 return *coefficients_[index_];
455 }
456
457 private:
458 int index_ = 0;
459 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
460 number_of_states, number_of_inputs, number_of_outputs>>>
461 coefficients_;
462};
463
Austin Schuh32501832017-02-25 18:32:56 -0800464// A container for all the observer coefficients.
465template <int number_of_states, int number_of_inputs, int number_of_outputs>
466struct StateFeedbackObserverCoefficients final {
467 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
468
469 const Eigen::Matrix<double, number_of_states, number_of_outputs> L;
470
471 StateFeedbackObserverCoefficients(
472 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L)
473 : L(L) {}
474};
475
476template <int number_of_states, int number_of_inputs, int number_of_outputs>
477class StateFeedbackObserver {
478 public:
479 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
480
481 explicit StateFeedbackObserver(
482 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
483 number_of_states, number_of_inputs, number_of_outputs>>> *observers)
484 : coefficients_(::std::move(*observers)) {}
485
486 StateFeedbackObserver(StateFeedbackObserver &&other)
Austin Schuhe91f14c2017-02-25 19:43:57 -0800487 : X_hat_(other.X_hat_), index_(other.index_) {
Austin Schuh32501832017-02-25 18:32:56 -0800488 ::std::swap(coefficients_, other.coefficients_);
489 }
490
491 const Eigen::Matrix<double, number_of_states, number_of_outputs> &L() const {
492 return coefficients().L;
493 }
494 double L(int i, int j) const { return L()(i, j); }
495
Austin Schuhe91f14c2017-02-25 19:43:57 -0800496 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
497 return X_hat_;
498 }
499 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
500
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800501 void Reset(
502 StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs,
503 StateFeedbackPlant<number_of_states, number_of_inputs,
504 number_of_outputs>,
505 StateFeedbackObserver> * /*loop*/) {
506 X_hat_.setZero();
507 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800508
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800509 void Predict(
510 StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs,
511 StateFeedbackPlant<number_of_states, number_of_inputs,
512 number_of_outputs>,
513 StateFeedbackObserver> *loop,
514 const Eigen::Matrix<double, number_of_inputs, 1> &new_u,
515 ::std::chrono::nanoseconds /*dt*/) {
516 mutable_X_hat() = loop->plant().Update(X_hat(), new_u);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800517 }
518
519 void Correct(const StateFeedbackLoop<
520 number_of_states, number_of_inputs, number_of_outputs,
521 StateFeedbackPlant<number_of_states, number_of_inputs,
522 number_of_outputs>,
523 StateFeedbackObserver> &loop,
524 const Eigen::Matrix<double, number_of_inputs, 1> &U,
525 const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
526 mutable_X_hat() += loop.plant().A_inv() * L() *
527 (Y - loop.plant().C() * X_hat() - loop.plant().D() * U);
528 }
529
Austin Schuh32501832017-02-25 18:32:56 -0800530 // Sets the current controller to be index, clamped to be within range.
531 void set_index(int index) {
532 if (index < 0) {
533 index_ = 0;
534 } else if (index >= static_cast<int>(coefficients_.size())) {
535 index_ = static_cast<int>(coefficients_.size()) - 1;
536 } else {
537 index_ = index;
538 }
539 }
540
541 int index() const { return index_; }
542
543 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
544 number_of_outputs>
545 &coefficients(int index) const {
546 return *coefficients_[index];
547 }
548
549 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
550 number_of_outputs>
551 &coefficients() const {
552 return *coefficients_[index_];
553 }
554
555 private:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800556 // Internal state estimate.
557 Eigen::Matrix<double, number_of_states, 1> X_hat_;
558
Austin Schuh32501832017-02-25 18:32:56 -0800559 int index_ = 0;
560 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
561 number_of_states, number_of_inputs, number_of_outputs>>>
562 coefficients_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700563};
564
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800565// A container for all the observer coefficients.
566template <int number_of_states, int number_of_inputs, int number_of_outputs>
567struct HybridKalmanCoefficients final {
568 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
569
570 const Eigen::Matrix<double, number_of_states, number_of_states> Q_continuous;
571 const Eigen::Matrix<double, number_of_outputs, number_of_outputs> R_continuous;
572 const Eigen::Matrix<double, number_of_states, number_of_states> P_steady_state;
573
574 HybridKalmanCoefficients(
575 const Eigen::Matrix<double, number_of_states, number_of_states>
576 &Q_continuous,
577 const Eigen::Matrix<double, number_of_outputs, number_of_outputs>
578 &R_continuous,
579 const Eigen::Matrix<double, number_of_states, number_of_states>
580 &P_steady_state)
581 : Q_continuous(Q_continuous),
582 R_continuous(R_continuous),
583 P_steady_state(P_steady_state) {}
584};
585
586template <int number_of_states, int number_of_inputs, int number_of_outputs>
587class HybridKalman {
588 public:
589 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
590
591 explicit HybridKalman(
592 ::std::vector<::std::unique_ptr<HybridKalmanCoefficients<
593 number_of_states, number_of_inputs, number_of_outputs>>> *observers)
594 : coefficients_(::std::move(*observers)) {}
595
596 HybridKalman(HybridKalman &&other)
597 : X_hat_(other.X_hat_), index_(other.index_) {
598 ::std::swap(coefficients_, other.coefficients_);
599 }
600
601 // Getters for Q
602 const Eigen::Matrix<double, number_of_states, number_of_states> &Q() const {
603 return Q_;
604 }
605 double Q(int i, int j) const { return Q()(i, j); }
606 // Getters for R
607 const Eigen::Matrix<double, number_of_outputs, number_of_outputs> &R() const {
608 return R_;
609 }
610 double R(int i, int j) const { return R()(i, j); }
611
612 // Getters for P
613 const Eigen::Matrix<double, number_of_states, number_of_states> &P() const {
614 return P_;
615 }
616 double P(int i, int j) const { return P()(i, j); }
617
618 // Getters for X_hat
619 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
620 return X_hat_;
621 }
622 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
623
624 void Reset(StateFeedbackLoop<
625 number_of_states, number_of_inputs, number_of_outputs,
626 StateFeedbackHybridPlant<number_of_states, number_of_inputs,
627 number_of_outputs>,
628 HybridKalman> *loop) {
629 X_hat_.setZero();
630 P_ = coefficients().P_steady_state;
631 UpdateQR(loop, ::aos::controls::kLoopFrequency);
632 }
633
634 void Predict(StateFeedbackLoop<
635 number_of_states, number_of_inputs, number_of_outputs,
636 StateFeedbackHybridPlant<number_of_states, number_of_inputs,
637 number_of_outputs>,
638 HybridKalman> *loop,
639 const Eigen::Matrix<double, number_of_inputs, 1> &new_u,
640 ::std::chrono::nanoseconds dt) {
641 // Trigger the predict step. This will update A() and B() in the plant.
642 mutable_X_hat() = loop->mutable_plant()->Update(X_hat(), new_u, dt);
643
644 UpdateQR(loop, dt);
645 P_ = loop->plant().A() * P_ * loop->plant().A().transpose() + Q_;
646 }
647
648 void Correct(const StateFeedbackLoop<
649 number_of_states, number_of_inputs, number_of_outputs,
650 StateFeedbackHybridPlant<number_of_states, number_of_inputs,
651 number_of_outputs>,
652 HybridKalman> &loop,
653 const Eigen::Matrix<double, number_of_inputs, 1> &U,
654 const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
655 Eigen::Matrix<double, number_of_outputs, 1> Y_bar =
656 Y - (loop.plant().C() * X_hat_ + loop.plant().D() * U);
657 Eigen::Matrix<double, number_of_outputs, number_of_outputs> S =
658 loop.plant().C() * P_ * loop.plant().C().transpose() + R_;
659 Eigen::Matrix<double, number_of_states, number_of_outputs> KalmanGain;
660 KalmanGain = (S.transpose().ldlt().solve(
661 (P() * loop.plant().C().transpose()).transpose()))
662 .transpose();
663 X_hat_ = X_hat_ + KalmanGain * Y_bar;
664 P_ = (loop.plant().coefficients().A_continuous.Identity() -
665 KalmanGain * loop.plant().C()) *
666 P();
667 }
668
669 // Sets the current controller to be index, clamped to be within range.
670 void set_index(int index) {
671 if (index < 0) {
672 index_ = 0;
673 } else if (index >= static_cast<int>(coefficients_.size())) {
674 index_ = static_cast<int>(coefficients_.size()) - 1;
675 } else {
676 index_ = index;
677 }
678 }
679
680 int index() const { return index_; }
681
682 const HybridKalmanCoefficients<number_of_states, number_of_inputs,
683 number_of_outputs>
684 &coefficients(int index) const {
685 return *coefficients_[index];
686 }
687
688 const HybridKalmanCoefficients<number_of_states, number_of_inputs,
689 number_of_outputs>
690 &coefficients() const {
691 return *coefficients_[index_];
692 }
693
694 private:
695 void UpdateQR(StateFeedbackLoop<
696 number_of_states, number_of_inputs, number_of_outputs,
697 StateFeedbackHybridPlant<number_of_states, number_of_inputs,
698 number_of_outputs>,
699 HybridKalman> *loop,
700 ::std::chrono::nanoseconds dt) {
701 // Now, compute the discrete time Q and R coefficients.
702 Eigen::Matrix<double, number_of_states, number_of_states> Qtemp =
703 (coefficients().Q_continuous +
704 coefficients().Q_continuous.transpose()) /
705 2.0;
706 Eigen::Matrix<double, number_of_outputs, number_of_outputs> Rtemp =
707 (coefficients().R_continuous +
708 coefficients().R_continuous.transpose()) /
709 2.0;
710
711 Eigen::Matrix<double, 2 * number_of_states, 2 * number_of_states> M_gain;
712 M_gain.setZero();
713 // Set up the matrix M = [[-A, Q], [0, A.T]]
714 M_gain.template block<number_of_states, number_of_states>(0, 0) =
715 -loop->plant().coefficients().A_continuous;
716 M_gain.template block<number_of_states, number_of_states>(
717 0, number_of_states) = Qtemp;
718 M_gain.template block<number_of_states, number_of_states>(
719 number_of_states, number_of_states) =
720 loop->plant().coefficients().A_continuous.transpose();
721
722 Eigen::Matrix<double, 2 * number_of_states, 2 *number_of_states> phi =
723 (M_gain *
724 ::std::chrono::duration_cast<::std::chrono::duration<double>>(dt)
725 .count())
726 .exp();
727
728 // Phi12 = phi[0:number_of_states, number_of_states:2*number_of_states]
729 // Phi22 = phi[number_of_states:2*number_of_states,
730 // number_of_states:2*number_of_states]
731 Eigen::Matrix<double, number_of_states, number_of_states> phi12 =
732 phi.block(0, number_of_states, number_of_states, number_of_states);
733 Eigen::Matrix<double, number_of_states, number_of_states> phi22 = phi.block(
734 number_of_states, number_of_states, number_of_states, number_of_states);
735
736 Q_ = phi22.transpose() * phi12;
737 Q_ = (Q_ + Q_.transpose()) / 2.0;
738 R_ = Rtemp /
739 ::std::chrono::duration_cast<::std::chrono::duration<double>>(dt)
740 .count();
741 }
742
743 // Internal state estimate.
744 Eigen::Matrix<double, number_of_states, 1> X_hat_;
745 // Internal covariance estimate.
746 Eigen::Matrix<double, number_of_states, number_of_states> P_;
747
748 // Discretized Q and R for the kalman filter.
749 Eigen::Matrix<double, number_of_states, number_of_states> Q_;
750 Eigen::Matrix<double, number_of_outputs, number_of_outputs> R_;
751
752 int index_ = 0;
753 ::std::vector<::std::unique_ptr<HybridKalmanCoefficients<
754 number_of_states, number_of_inputs, number_of_outputs>>>
755 coefficients_;
756};
757
Austin Schuhe91f14c2017-02-25 19:43:57 -0800758template <int number_of_states, int number_of_inputs, int number_of_outputs,
759 typename PlantType = StateFeedbackPlant<
760 number_of_states, number_of_inputs, number_of_outputs>,
761 typename ObserverType = StateFeedbackObserver<
762 number_of_states, number_of_inputs, number_of_outputs>>
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800763class StateFeedbackLoop {
764 public:
765 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
766
Austin Schuh32501832017-02-25 18:32:56 -0800767 explicit StateFeedbackLoop(
Austin Schuhe91f14c2017-02-25 19:43:57 -0800768 PlantType &&plant,
Austin Schuh32501832017-02-25 18:32:56 -0800769 StateFeedbackController<number_of_states, number_of_inputs,
770 number_of_outputs> &&controller,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800771 ObserverType &&observer)
Austin Schuhc5fceb82017-02-25 16:24:12 -0800772 : plant_(::std::move(plant)),
Austin Schuh32501832017-02-25 18:32:56 -0800773 controller_(::std::move(controller)),
774 observer_(::std::move(observer)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700775 Reset();
776 }
777
Austin Schuhc5fceb82017-02-25 16:24:12 -0800778 StateFeedbackLoop(StateFeedbackLoop &&other)
Austin Schuh32501832017-02-25 18:32:56 -0800779 : plant_(::std::move(other.plant_)),
780 controller_(::std::move(other.controller_)),
781 observer_(::std::move(other.observer_)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700782 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800783 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700784 U_.swap(other.U_);
785 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800786 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700787 }
788
Austin Schuh1a387962015-01-31 16:36:20 -0800789 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700790
Brian Silverman273d8a32014-05-10 22:19:09 -0700791 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800792 return observer().X_hat();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700793 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700794 double X_hat(int i, int j) const { return X_hat()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700795 const Eigen::Matrix<double, number_of_states, 1> &R() const { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700796 double R(int i, int j) const { return R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800797 const Eigen::Matrix<double, number_of_states, 1> &next_R() const {
798 return next_R_;
799 }
800 double next_R(int i, int j) const { return next_R()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700801 const Eigen::Matrix<double, number_of_inputs, 1> &U() const { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700802 double U(int i, int j) const { return U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700803 const Eigen::Matrix<double, number_of_inputs, 1> &U_uncapped() const {
804 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700805 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700806 double U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800807 const Eigen::Matrix<double, number_of_inputs, 1> &ff_U() const {
808 return ff_U_;
809 }
810 double ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700811
Austin Schuhe91f14c2017-02-25 19:43:57 -0800812 Eigen::Matrix<double, number_of_states, 1> &mutable_X_hat() {
813 return observer_.mutable_X_hat();
814 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700815 double &mutable_X_hat(int i, int j) { return mutable_X_hat()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700816 Eigen::Matrix<double, number_of_states, 1> &mutable_R() { return R_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700817 double &mutable_R(int i, int j) { return mutable_R()(i, j); }
Austin Schuhb6a6d822016-02-08 00:20:40 -0800818 Eigen::Matrix<double, number_of_states, 1> &mutable_next_R() {
819 return next_R_;
820 }
821 double &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700822 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U() { return U_; }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700823 double &mutable_U(int i, int j) { return mutable_U()(i, j); }
Brian Silverman0ca790b2014-06-12 21:33:08 -0700824 Eigen::Matrix<double, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700825 return U_uncapped_;
826 }
Brian Silvermana21c3a22014-06-12 21:49:15 -0700827 double &mutable_U_uncapped(int i, int j) {
828 return mutable_U_uncapped()(i, j);
829 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800830
Austin Schuhe91f14c2017-02-25 19:43:57 -0800831 const PlantType &plant() const { return plant_; }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800832 PlantType *mutable_plant() { return &plant_; }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800833
Austin Schuh32501832017-02-25 18:32:56 -0800834 const StateFeedbackController<number_of_states, number_of_inputs,
835 number_of_outputs>
Austin Schuh66c19882017-02-25 13:36:28 -0800836 &controller() const {
Austin Schuh32501832017-02-25 18:32:56 -0800837 return controller_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700838 }
839
Austin Schuhe91f14c2017-02-25 19:43:57 -0800840 const ObserverType &observer() const { return observer_; }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700841
Austin Schuh9644e1c2013-03-12 00:40:36 -0700842 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700843 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800844 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700845 U_.setZero();
846 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800847 ff_U_.setZero();
Austin Schuhe91f14c2017-02-25 19:43:57 -0800848
849 plant_.Reset();
850 controller_.Reset();
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800851 observer_.Reset(this);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800852 }
853
854 // If U is outside the hardware range, limit it before the plant tries to use
855 // it.
856 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400857 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800858 if (U(i, 0) > plant().U_max(i, 0)) {
859 U_(i, 0) = plant().U_max(i, 0);
860 } else if (U(i, 0) < plant().U_min(i, 0)) {
861 U_(i, 0) = plant().U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800862 }
863 }
864 }
865
Austin Schuhf9286cd2014-02-11 00:51:09 -0800866 // Corrects X_hat given the observation in Y.
867 void Correct(const Eigen::Matrix<double, number_of_outputs, 1> &Y) {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800868 observer_.Correct(*this, U(), Y);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800869 }
870
Austin Schuh3f862bb2016-02-27 14:48:05 -0800871 const Eigen::Matrix<double, number_of_states, 1> error() const {
872 return R() - X_hat();
873 }
874
Austin Schuhb6a6d822016-02-08 00:20:40 -0800875 // Returns the calculated controller power.
876 virtual const Eigen::Matrix<double, number_of_inputs, 1> ControllerOutput() {
Austin Schuh32501832017-02-25 18:32:56 -0800877 // TODO(austin): Should this live in StateSpaceController?
Austin Schuhb6a6d822016-02-08 00:20:40 -0800878 ff_U_ = FeedForward();
Austin Schuh32501832017-02-25 18:32:56 -0800879 return controller().K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800880 }
881
882 // Calculates the feed forwards power.
883 virtual const Eigen::Matrix<double, number_of_inputs, 1> FeedForward() {
Austin Schuh32501832017-02-25 18:32:56 -0800884 // TODO(austin): Should this live in StateSpaceController?
885 return controller().Kff() * (next_R() - plant().A() * R());
Austin Schuhb6a6d822016-02-08 00:20:40 -0800886 }
887
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800888 // stop_motors is whether or not to output all 0s.
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800889 void Update(bool stop_motors,
890 ::std::chrono::nanoseconds dt = ::std::chrono::milliseconds(5)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800891 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700892 U_.setZero();
893 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800894 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800895 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800896 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800897 CapU();
898 }
899
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800900 UpdateObserver(U_, dt);
Austin Schuh093535c2016-03-05 23:21:00 -0800901
902 UpdateFFReference();
903 }
904
905 // Updates R() after any CapU operations happen on U().
906 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800907 ff_U_ -= U_uncapped() - U();
Austin Schuh32501832017-02-25 18:32:56 -0800908 if (!controller().Kff().isZero(0)) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800909 R_ = plant().A() * R() + plant().B() * ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800910 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000911 }
912
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800913 void UpdateObserver(const Eigen::Matrix<double, number_of_inputs, 1> &new_u,
914 ::std::chrono::nanoseconds dt) {
915 observer_.Predict(this, new_u, dt);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800916 }
917
Austin Schuh32501832017-02-25 18:32:56 -0800918 // Sets the current controller to be index.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800919 void set_index(int index) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800920 plant_.set_index(index);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800921 controller_.set_index(index);
922 observer_.set_index(index);
Austin Schuh9644e1c2013-03-12 00:40:36 -0700923 }
924
Austin Schuh32501832017-02-25 18:32:56 -0800925 int index() const { return plant_.index(); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700926
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800927 protected:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800928 PlantType plant_;
Austin Schuhc5fceb82017-02-25 16:24:12 -0800929
Austin Schuh32501832017-02-25 18:32:56 -0800930 StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs>
931 controller_;
932
Austin Schuhe91f14c2017-02-25 19:43:57 -0800933 ObserverType observer_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700934
Brian Silverman273d8a32014-05-10 22:19:09 -0700935 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800936 static constexpr int kNumStates = number_of_states;
937 static constexpr int kNumOutputs = number_of_outputs;
938 static constexpr int kNumInputs = number_of_inputs;
939
940 // Portion of U which is based on the feed-forwards.
941 Eigen::Matrix<double, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700942
Brian Silverman273d8a32014-05-10 22:19:09 -0700943 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800944 // Current goal (Used by the feed-back controller).
Brian Silverman273d8a32014-05-10 22:19:09 -0700945 Eigen::Matrix<double, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800946 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
947 Eigen::Matrix<double, number_of_states, 1> next_R_;
948 // Computed output after being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700949 Eigen::Matrix<double, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800950 // Computed output before being capped.
Brian Silverman273d8a32014-05-10 22:19:09 -0700951 Eigen::Matrix<double, number_of_inputs, 1> U_uncapped_;
952
Brian Silverman0a151c92014-05-02 15:28:44 -0700953 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800954};
955
Brian Silverman273d8a32014-05-10 22:19:09 -0700956#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_