blob: fe224cc578a9589f5ff9d5da2e9d47fdddbc514d [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
Brian Silverman6260c092018-01-14 15:21:36 -080015#if defined(__linux__)
Austin Schuhcda86af2014-02-16 16:16:39 -080016#include "aos/common/logging/logging.h"
Brian Silverman6260c092018-01-14 15:21:36 -080017#endif
Brian Silverman0a151c92014-05-02 15:28:44 -070018#include "aos/common/macros.h"
19
Austin Schuhe91f14c2017-02-25 19:43:57 -080020template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -080021 typename PlantType, typename ObserverType, typename Scalar>
Austin Schuhe91f14c2017-02-25 19:43:57 -080022class StateFeedbackLoop;
23
Brian Silverman5808bcb2014-09-14 21:40:43 -040024// For everything in this file, "inputs" and "outputs" are defined from the
25// perspective of the plant. This means U is an input and Y is an output
26// (because you give the plant U (powers) and it gives you back a Y (sensor
27// values). This is the opposite of what they mean from the perspective of the
28// controller (U is an output because that's what goes to the motors and Y is an
29// input because that's what comes back from the sensors).
30
Austin Schuh20388b62017-11-23 22:40:46 -080031template <int number_of_states, int number_of_inputs, int number_of_outputs,
32 typename Scalar = double>
Austin Schuh64f17a52017-02-25 14:41:58 -080033struct StateFeedbackPlantCoefficients final {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080034 public:
35 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
36
Austin Schuhe3490622013-03-13 01:24:30 -070037 StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other)
Austin Schuh64f17a52017-02-25 14:41:58 -080038 : A(other.A),
Austin Schuh64f17a52017-02-25 14:41:58 -080039 B(other.B),
Austin Schuh64f17a52017-02-25 14:41:58 -080040 C(other.C),
41 D(other.D),
42 U_min(other.U_min),
43 U_max(other.U_max) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080044
Austin Schuhe3490622013-03-13 01:24:30 -070045 StateFeedbackPlantCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -080046 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A,
Austin Schuh20388b62017-11-23 22:40:46 -080047 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B,
48 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C,
49 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D,
50 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max,
51 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min)
Sabina Davis3922dfa2018-02-10 23:10:05 -080052 : A(A), B(B), C(C), D(D), U_min(U_min), U_max(U_max) {}
Austin Schuhe3490622013-03-13 01:24:30 -070053
Austin Schuh20388b62017-11-23 22:40:46 -080054 const Eigen::Matrix<Scalar, number_of_states, number_of_states> A;
Austin Schuh20388b62017-11-23 22:40:46 -080055 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> B;
56 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> C;
57 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> D;
58 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_min;
59 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_max;
Austin Schuhe3490622013-03-13 01:24:30 -070060};
61
Austin Schuh20388b62017-11-23 22:40:46 -080062template <int number_of_states, int number_of_inputs, int number_of_outputs,
63 typename Scalar = double>
Austin Schuhe3490622013-03-13 01:24:30 -070064class 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 Schuh20388b62017-11-23 22:40:46 -080070 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh66c19882017-02-25 13:36:28 -080071 *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 Schuh20388b62017-11-23 22:40:46 -080085 const Eigen::Matrix<Scalar, 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 }
Austin Schuh20388b62017-11-23 22:40:46 -080088 Scalar A(int i, int j) const { return A()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -080089 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080090 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -070091 }
Austin Schuh20388b62017-11-23 22:40:46 -080092 Scalar B(int i, int j) const { return B()(i, j); }
93 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080094 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -070095 }
Austin Schuh20388b62017-11-23 22:40:46 -080096 Scalar C(int i, int j) const { return C()(i, j); }
97 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080098 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -070099 }
Austin Schuh20388b62017-11-23 22:40:46 -0800100 Scalar D(int i, int j) const { return D()(i, j); }
101 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800102 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700103 }
Austin Schuh20388b62017-11-23 22:40:46 -0800104 Scalar U_min(int i, int j) const { return U_min()(i, j); }
105 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800106 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700107 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800108 Scalar U_max(int i, int j = 0) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700109
Austin Schuh20388b62017-11-23 22:40:46 -0800110 const Eigen::Matrix<Scalar, number_of_states, 1> &X() const { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800111 Scalar X(int i, int j = 0) const { return X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800112 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y() const { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800113 Scalar Y(int i, int j = 0) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700114
Austin Schuh20388b62017-11-23 22:40:46 -0800115 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X() { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800116 Scalar &mutable_X(int i, int j = 0) { return mutable_X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800117 Eigen::Matrix<Scalar, number_of_outputs, 1> &mutable_Y() { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800118 Scalar &mutable_Y(int i, int j = 0) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700119
Austin Schuhb6a6d822016-02-08 00:20:40 -0800120 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800121 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800122 &coefficients(int index) const {
123 return *coefficients_[index];
Austin Schuhe3490622013-03-13 01:24:30 -0700124 }
125
Austin Schuhc5fceb82017-02-25 16:24:12 -0800126 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800127 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800128 &coefficients() const {
129 return *coefficients_[index_];
130 }
131
132 int index() const { return index_; }
133 void set_index(int index) {
134 assert(index >= 0);
135 assert(index < static_cast<int>(coefficients_.size()));
136 index_ = index;
Austin Schuhe3490622013-03-13 01:24:30 -0700137 }
138
139 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700140 X_.setZero();
141 Y_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800142 }
143
Austin Schuh849f0032013-03-03 23:59:53 -0800144 // Assert that U is within the hardware range.
Austin Schuh20388b62017-11-23 22:40:46 -0800145 virtual void CheckU(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400146 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuh20388b62017-11-23 22:40:46 -0800147 if (U(i, 0) > U_max(i, 0) + static_cast<Scalar>(0.00001) ||
148 U(i, 0) < U_min(i, 0) - static_cast<Scalar>(0.00001)) {
Brian Silverman6260c092018-01-14 15:21:36 -0800149#if defined(__linux__)
Austin Schuh66c19882017-02-25 13:36:28 -0800150 LOG(FATAL, "U out of range\n");
Brian Silverman6260c092018-01-14 15:21:36 -0800151#else
152 abort();
153#endif
Austin Schuh66c19882017-02-25 13:36:28 -0800154 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800155 }
156 }
Austin Schuh849f0032013-03-03 23:59:53 -0800157
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800158 // Computes the new X and Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800159 void Update(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh849f0032013-03-03 23:59:53 -0800160 // Powers outside of the range are more likely controller bugs than things
161 // that the plant should deal with.
Austin Schuh66c19882017-02-25 13:36:28 -0800162 CheckU(U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800163 X_ = Update(X(), U);
Austin Schuh01c7b252017-03-05 00:59:31 -0800164 UpdateY(U);
165 }
166
167 // Computes the new Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800168 void UpdateY(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh66c19882017-02-25 13:36:28 -0800169 Y_ = C() * X() + D() * U;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800170 }
171
Austin Schuh20388b62017-11-23 22:40:46 -0800172 Eigen::Matrix<Scalar, number_of_states, 1> Update(
173 const Eigen::Matrix<Scalar, number_of_states, 1> X,
174 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800175 return A() * X + B() * U;
176 }
177
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800178 protected:
179 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800180 static const int kNumStates = number_of_states;
181 static const int kNumOutputs = number_of_outputs;
182 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700183
184 private:
Austin Schuh20388b62017-11-23 22:40:46 -0800185 Eigen::Matrix<Scalar, number_of_states, 1> X_;
186 Eigen::Matrix<Scalar, number_of_outputs, 1> Y_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700187
Austin Schuhb6a6d822016-02-08 00:20:40 -0800188 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800189 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh64f17a52017-02-25 14:41:58 -0800190 coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700191
Austin Schuhc5fceb82017-02-25 16:24:12 -0800192 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700193
194 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800195};
196
Austin Schuh32501832017-02-25 18:32:56 -0800197// A container for all the controller coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800198template <int number_of_states, int number_of_inputs, int number_of_outputs,
199 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800200struct StateFeedbackControllerCoefficients final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700201 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700202
Austin Schuh20388b62017-11-23 22:40:46 -0800203 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> K;
204 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> Kff;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700205
Austin Schuh32501832017-02-25 18:32:56 -0800206 StateFeedbackControllerCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -0800207 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K,
208 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff)
Austin Schuh32501832017-02-25 18:32:56 -0800209 : K(K), Kff(Kff) {}
210};
211
Austin Schuh20388b62017-11-23 22:40:46 -0800212template <int number_of_states, int number_of_inputs, int number_of_outputs,
213 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800214class StateFeedbackController {
215 public:
216 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
217
218 explicit StateFeedbackController(
219 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800220 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
221 *controllers)
Austin Schuh32501832017-02-25 18:32:56 -0800222 : coefficients_(::std::move(*controllers)) {}
223
224 StateFeedbackController(StateFeedbackController &&other)
225 : index_(other.index_) {
226 ::std::swap(coefficients_, other.coefficients_);
227 }
228
Austin Schuh20388b62017-11-23 22:40:46 -0800229 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K() const {
Austin Schuh32501832017-02-25 18:32:56 -0800230 return coefficients().K;
231 }
Austin Schuh20388b62017-11-23 22:40:46 -0800232 Scalar K(int i, int j) const { return K()(i, j); }
233 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff() const {
Austin Schuh32501832017-02-25 18:32:56 -0800234 return coefficients().Kff;
235 }
Austin Schuh20388b62017-11-23 22:40:46 -0800236 Scalar Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800237
Austin Schuhe91f14c2017-02-25 19:43:57 -0800238 void Reset() {}
239
Austin Schuh32501832017-02-25 18:32:56 -0800240 // Sets the current controller to be index, clamped to be within range.
241 void set_index(int index) {
242 if (index < 0) {
243 index_ = 0;
244 } else if (index >= static_cast<int>(coefficients_.size())) {
245 index_ = static_cast<int>(coefficients_.size()) - 1;
246 } else {
247 index_ = index;
248 }
249 }
250
251 int index() const { return index_; }
252
253 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800254 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800255 &coefficients(int index) const {
256 return *coefficients_[index];
257 }
258
259 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800260 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800261 &coefficients() const {
262 return *coefficients_[index_];
263 }
264
265 private:
266 int index_ = 0;
267 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800268 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800269 coefficients_;
270};
271
Austin Schuh32501832017-02-25 18:32:56 -0800272// A container for all the observer coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800273template <int number_of_states, int number_of_inputs, int number_of_outputs,
274 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800275struct StateFeedbackObserverCoefficients final {
276 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
277
Sabina Davis3922dfa2018-02-10 23:10:05 -0800278 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> KalmanGain;
Austin Schuh32501832017-02-25 18:32:56 -0800279
280 StateFeedbackObserverCoefficients(
Sabina Davis3922dfa2018-02-10 23:10:05 -0800281 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &KalmanGain)
282 : KalmanGain(KalmanGain) {}
Austin Schuh32501832017-02-25 18:32:56 -0800283};
284
Austin Schuh20388b62017-11-23 22:40:46 -0800285template <int number_of_states, int number_of_inputs, int number_of_outputs,
286 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800287class StateFeedbackObserver {
288 public:
289 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
290
291 explicit StateFeedbackObserver(
292 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800293 number_of_states, number_of_inputs, number_of_outputs, Scalar>>> *observers)
Austin Schuh32501832017-02-25 18:32:56 -0800294 : coefficients_(::std::move(*observers)) {}
295
296 StateFeedbackObserver(StateFeedbackObserver &&other)
Austin Schuhe91f14c2017-02-25 19:43:57 -0800297 : X_hat_(other.X_hat_), index_(other.index_) {
Austin Schuh32501832017-02-25 18:32:56 -0800298 ::std::swap(coefficients_, other.coefficients_);
299 }
300
Sabina Davis3922dfa2018-02-10 23:10:05 -0800301 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &KalmanGain() const {
302 return coefficients().KalmanGain;
Austin Schuh32501832017-02-25 18:32:56 -0800303 }
Sabina Davis3922dfa2018-02-10 23:10:05 -0800304 Scalar KalmanGain(int i, int j) const { return KalmanGain()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800305
Austin Schuh20388b62017-11-23 22:40:46 -0800306 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800307 return X_hat_;
308 }
Austin Schuh20388b62017-11-23 22:40:46 -0800309 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800310
Austin Schuh6501d232017-11-23 20:35:27 -0800311 void Reset(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800312 number_of_outputs, Scalar> * /*loop*/) {
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800313 X_hat_.setZero();
314 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800315
Austin Schuh6501d232017-11-23 20:35:27 -0800316 void Predict(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800317 number_of_outputs, Scalar> *plant,
318 const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh6501d232017-11-23 20:35:27 -0800319 ::std::chrono::nanoseconds /*dt*/) {
320 mutable_X_hat() = plant->Update(X_hat(), new_u);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800321 }
322
Austin Schuh6501d232017-11-23 20:35:27 -0800323 void Correct(const StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800324 number_of_outputs, Scalar> &plant,
325 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U,
326 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Austin Schuh6501d232017-11-23 20:35:27 -0800327 mutable_X_hat() +=
Sabina Davis3922dfa2018-02-10 23:10:05 -0800328 KalmanGain() * (Y - plant.C() * X_hat() - plant.D() * U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800329 }
330
Austin Schuh32501832017-02-25 18:32:56 -0800331 // Sets the current controller to be index, clamped to be within range.
332 void set_index(int index) {
333 if (index < 0) {
334 index_ = 0;
335 } else if (index >= static_cast<int>(coefficients_.size())) {
336 index_ = static_cast<int>(coefficients_.size()) - 1;
337 } else {
338 index_ = index;
339 }
340 }
341
342 int index() const { return index_; }
343
344 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800345 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800346 &coefficients(int index) const {
347 return *coefficients_[index];
348 }
349
350 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800351 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800352 &coefficients() const {
353 return *coefficients_[index_];
354 }
355
356 private:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800357 // Internal state estimate.
Austin Schuh20388b62017-11-23 22:40:46 -0800358 Eigen::Matrix<Scalar, number_of_states, 1> X_hat_;
Austin Schuhe91f14c2017-02-25 19:43:57 -0800359
Austin Schuh32501832017-02-25 18:32:56 -0800360 int index_ = 0;
361 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800362 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800363 coefficients_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700364};
365
Austin Schuhe91f14c2017-02-25 19:43:57 -0800366template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800367 typename Scalar = double,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800368 typename PlantType = StateFeedbackPlant<
Austin Schuh20388b62017-11-23 22:40:46 -0800369 number_of_states, number_of_inputs, number_of_outputs, Scalar>,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800370 typename ObserverType = StateFeedbackObserver<
Austin Schuh20388b62017-11-23 22:40:46 -0800371 number_of_states, number_of_inputs, number_of_outputs, Scalar>>
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800372class StateFeedbackLoop {
373 public:
374 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
375
Austin Schuh32501832017-02-25 18:32:56 -0800376 explicit StateFeedbackLoop(
Austin Schuhe91f14c2017-02-25 19:43:57 -0800377 PlantType &&plant,
Austin Schuh32501832017-02-25 18:32:56 -0800378 StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800379 number_of_outputs, Scalar> &&controller,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800380 ObserverType &&observer)
Austin Schuhc5fceb82017-02-25 16:24:12 -0800381 : plant_(::std::move(plant)),
Austin Schuh32501832017-02-25 18:32:56 -0800382 controller_(::std::move(controller)),
383 observer_(::std::move(observer)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700384 Reset();
385 }
386
Austin Schuhc5fceb82017-02-25 16:24:12 -0800387 StateFeedbackLoop(StateFeedbackLoop &&other)
Austin Schuh32501832017-02-25 18:32:56 -0800388 : plant_(::std::move(other.plant_)),
389 controller_(::std::move(other.controller_)),
390 observer_(::std::move(other.observer_)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700391 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800392 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700393 U_.swap(other.U_);
394 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800395 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700396 }
397
Austin Schuh1a387962015-01-31 16:36:20 -0800398 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700399
Austin Schuh20388b62017-11-23 22:40:46 -0800400 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800401 return observer().X_hat();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700402 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800403 Scalar X_hat(int i, int j = 0) const { return X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800404 const Eigen::Matrix<Scalar, number_of_states, 1> &R() const { return R_; }
405 Scalar R(int i, int j) const { return R()(i, j); }
406 const Eigen::Matrix<Scalar, number_of_states, 1> &next_R() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800407 return next_R_;
408 }
Austin Schuh20388b62017-11-23 22:40:46 -0800409 Scalar next_R(int i, int j) const { return next_R()(i, j); }
410 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U() const { return U_; }
411 Scalar U(int i, int j) const { return U()(i, j); }
412 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_uncapped() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700413 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700414 }
Austin Schuh20388b62017-11-23 22:40:46 -0800415 Scalar U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
416 const Eigen::Matrix<Scalar, number_of_inputs, 1> &ff_U() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800417 return ff_U_;
418 }
Austin Schuh20388b62017-11-23 22:40:46 -0800419 Scalar ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700420
Austin Schuh20388b62017-11-23 22:40:46 -0800421 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800422 return observer_.mutable_X_hat();
423 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800424 Scalar &mutable_X_hat(int i, int j = 0) { return mutable_X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800425 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_R() { return R_; }
426 Scalar &mutable_R(int i, int j) { return mutable_R()(i, j); }
427 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_next_R() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800428 return next_R_;
429 }
Austin Schuh20388b62017-11-23 22:40:46 -0800430 Scalar &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
431 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U() { return U_; }
432 Scalar &mutable_U(int i, int j) { return mutable_U()(i, j); }
433 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700434 return U_uncapped_;
435 }
Austin Schuh20388b62017-11-23 22:40:46 -0800436 Scalar &mutable_U_uncapped(int i, int j) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700437 return mutable_U_uncapped()(i, j);
438 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800439
Austin Schuhe91f14c2017-02-25 19:43:57 -0800440 const PlantType &plant() const { return plant_; }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800441 PlantType *mutable_plant() { return &plant_; }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800442
Austin Schuh32501832017-02-25 18:32:56 -0800443 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800444 number_of_outputs, Scalar>
Austin Schuh66c19882017-02-25 13:36:28 -0800445 &controller() const {
Austin Schuh32501832017-02-25 18:32:56 -0800446 return controller_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700447 }
448
Austin Schuhe91f14c2017-02-25 19:43:57 -0800449 const ObserverType &observer() const { return observer_; }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700450
Austin Schuh9644e1c2013-03-12 00:40:36 -0700451 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700452 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800453 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700454 U_.setZero();
455 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800456 ff_U_.setZero();
Austin Schuhe91f14c2017-02-25 19:43:57 -0800457
458 plant_.Reset();
459 controller_.Reset();
Austin Schuh6501d232017-11-23 20:35:27 -0800460 observer_.Reset(this->mutable_plant());
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800461 }
462
463 // If U is outside the hardware range, limit it before the plant tries to use
464 // it.
465 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400466 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800467 if (U(i, 0) > plant().U_max(i, 0)) {
468 U_(i, 0) = plant().U_max(i, 0);
469 } else if (U(i, 0) < plant().U_min(i, 0)) {
470 U_(i, 0) = plant().U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800471 }
472 }
473 }
474
Austin Schuhf9286cd2014-02-11 00:51:09 -0800475 // Corrects X_hat given the observation in Y.
Austin Schuh20388b62017-11-23 22:40:46 -0800476 void Correct(const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Austin Schuh6501d232017-11-23 20:35:27 -0800477 observer_.Correct(this->plant(), U(), Y);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800478 }
479
Austin Schuh20388b62017-11-23 22:40:46 -0800480 const Eigen::Matrix<Scalar, number_of_states, 1> error() const {
Austin Schuh3f862bb2016-02-27 14:48:05 -0800481 return R() - X_hat();
482 }
483
Austin Schuhb6a6d822016-02-08 00:20:40 -0800484 // Returns the calculated controller power.
Austin Schuh20388b62017-11-23 22:40:46 -0800485 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> ControllerOutput() {
Austin Schuh32501832017-02-25 18:32:56 -0800486 // TODO(austin): Should this live in StateSpaceController?
Austin Schuhb6a6d822016-02-08 00:20:40 -0800487 ff_U_ = FeedForward();
Austin Schuh32501832017-02-25 18:32:56 -0800488 return controller().K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800489 }
490
491 // Calculates the feed forwards power.
Austin Schuh20388b62017-11-23 22:40:46 -0800492 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> FeedForward() {
Austin Schuh32501832017-02-25 18:32:56 -0800493 // TODO(austin): Should this live in StateSpaceController?
494 return controller().Kff() * (next_R() - plant().A() * R());
Austin Schuhb6a6d822016-02-08 00:20:40 -0800495 }
496
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800497 // stop_motors is whether or not to output all 0s.
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800498 void Update(bool stop_motors,
499 ::std::chrono::nanoseconds dt = ::std::chrono::milliseconds(5)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800500 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700501 U_.setZero();
502 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800503 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800504 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800505 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800506 CapU();
507 }
508
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800509 UpdateObserver(U_, dt);
Austin Schuh093535c2016-03-05 23:21:00 -0800510
511 UpdateFFReference();
512 }
513
514 // Updates R() after any CapU operations happen on U().
515 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800516 ff_U_ -= U_uncapped() - U();
Austin Schuh32501832017-02-25 18:32:56 -0800517 if (!controller().Kff().isZero(0)) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800518 R_ = plant().A() * R() + plant().B() * ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800519 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000520 }
521
Austin Schuh20388b62017-11-23 22:40:46 -0800522 void UpdateObserver(const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800523 ::std::chrono::nanoseconds dt) {
Austin Schuh6501d232017-11-23 20:35:27 -0800524 observer_.Predict(this->mutable_plant(), new_u, dt);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800525 }
526
Austin Schuh32501832017-02-25 18:32:56 -0800527 // Sets the current controller to be index.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800528 void set_index(int index) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800529 plant_.set_index(index);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800530 controller_.set_index(index);
531 observer_.set_index(index);
Austin Schuh9644e1c2013-03-12 00:40:36 -0700532 }
533
Austin Schuh32501832017-02-25 18:32:56 -0800534 int index() const { return plant_.index(); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700535
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800536 protected:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800537 PlantType plant_;
Austin Schuhc5fceb82017-02-25 16:24:12 -0800538
Austin Schuh20388b62017-11-23 22:40:46 -0800539 StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs,
540 Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800541 controller_;
542
Austin Schuhe91f14c2017-02-25 19:43:57 -0800543 ObserverType observer_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700544
Brian Silverman273d8a32014-05-10 22:19:09 -0700545 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800546 static constexpr int kNumStates = number_of_states;
547 static constexpr int kNumOutputs = number_of_outputs;
548 static constexpr int kNumInputs = number_of_inputs;
549
550 // Portion of U which is based on the feed-forwards.
Austin Schuh20388b62017-11-23 22:40:46 -0800551 Eigen::Matrix<Scalar, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700552
Brian Silverman273d8a32014-05-10 22:19:09 -0700553 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800554 // Current goal (Used by the feed-back controller).
Austin Schuh20388b62017-11-23 22:40:46 -0800555 Eigen::Matrix<Scalar, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800556 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
Austin Schuh20388b62017-11-23 22:40:46 -0800557 Eigen::Matrix<Scalar, number_of_states, 1> next_R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800558 // Computed output after being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800559 Eigen::Matrix<Scalar, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800560 // Computed output before being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800561 Eigen::Matrix<Scalar, number_of_inputs, 1> U_uncapped_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700562
Brian Silverman0a151c92014-05-02 15:28:44 -0700563 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800564};
565
Brian Silverman273d8a32014-05-10 22:19:09 -0700566#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_