blob: 81783cca8b6d9b44a5600c3c40b3754928cf73e8 [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
Tyler Chatow6738c362019-02-16 14:12:30 -08006#include <chrono>
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"
Austin Schuh3ad5ed82017-02-25 21:36:19 -080012#include "unsupported/Eigen/MatrixFunctions"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080013
Brian Silverman6260c092018-01-14 15:21:36 -080014#if defined(__linux__)
John Park33858a32018-09-28 23:05:48 -070015#include "aos/logging/logging.h"
Brian Silverman6260c092018-01-14 15:21:36 -080016#endif
John Park33858a32018-09-28 23:05:48 -070017#include "aos/macros.h"
Brian Silverman0a151c92014-05-02 15:28:44 -070018
Austin Schuhe91f14c2017-02-25 19:43:57 -080019template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -080020 typename PlantType, typename ObserverType, typename Scalar>
Austin Schuhe91f14c2017-02-25 19:43:57 -080021class 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 Schuh20388b62017-11-23 22:40:46 -080030template <int number_of_states, int number_of_inputs, int number_of_outputs,
31 typename Scalar = double>
Austin Schuh64f17a52017-02-25 14:41:58 -080032struct StateFeedbackPlantCoefficients final {
Austin Schuhdc1c84a2013-02-23 16:33:10 -080033 public:
34 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
35
Austin Schuhe3490622013-03-13 01:24:30 -070036 StateFeedbackPlantCoefficients(const StateFeedbackPlantCoefficients &other)
Austin Schuh64f17a52017-02-25 14:41:58 -080037 : A(other.A),
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 Schuh20388b62017-11-23 22:40:46 -080045 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A,
Austin Schuh20388b62017-11-23 22:40:46 -080046 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B,
47 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C,
48 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D,
49 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max,
50 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min)
Sabina Davis3922dfa2018-02-10 23:10:05 -080051 : A(A), B(B), C(C), D(D), U_min(U_min), U_max(U_max) {}
Austin Schuhe3490622013-03-13 01:24:30 -070052
Austin Schuh20388b62017-11-23 22:40:46 -080053 const Eigen::Matrix<Scalar, number_of_states, number_of_states> A;
Austin Schuh20388b62017-11-23 22:40:46 -080054 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> B;
55 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> C;
56 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> D;
57 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_min;
58 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_max;
Austin Schuhe3490622013-03-13 01:24:30 -070059};
60
Austin Schuh20388b62017-11-23 22:40:46 -080061template <int number_of_states, int number_of_inputs, int number_of_outputs,
62 typename Scalar = double>
Austin Schuhe3490622013-03-13 01:24:30 -070063class StateFeedbackPlant {
64 public:
65 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070066
67 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080068 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -080069 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh66c19882017-02-25 13:36:28 -080070 *coefficients)
Austin Schuhc5fceb82017-02-25 16:24:12 -080071 : coefficients_(::std::move(*coefficients)), index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070072 Reset();
73 }
74
Tyler Chatow6738c362019-02-16 14:12:30 -080075 StateFeedbackPlant(StateFeedbackPlant &&other) : index_(other.index_) {
Brian Silverman0a151c92014-05-02 15:28:44 -070076 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -070077 X_.swap(other.X_);
78 Y_.swap(other.Y_);
Brian Silverman0a151c92014-05-02 15:28:44 -070079 }
80
Austin Schuh1a387962015-01-31 16:36:20 -080081 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -070082
Austin Schuh20388b62017-11-23 22:40:46 -080083 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080084 return coefficients().A;
Austin Schuhe3490622013-03-13 01:24:30 -070085 }
Austin Schuh20388b62017-11-23 22:40:46 -080086 Scalar A(int i, int j) const { return A()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -080087 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080088 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -070089 }
Austin Schuh20388b62017-11-23 22:40:46 -080090 Scalar B(int i, int j) const { return B()(i, j); }
91 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080092 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -070093 }
Austin Schuh20388b62017-11-23 22:40:46 -080094 Scalar C(int i, int j) const { return C()(i, j); }
95 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080096 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -070097 }
Austin Schuh20388b62017-11-23 22:40:46 -080098 Scalar D(int i, int j) const { return D()(i, j); }
99 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800100 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700101 }
Austin Schuh20388b62017-11-23 22:40:46 -0800102 Scalar U_min(int i, int j) const { return U_min()(i, j); }
103 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800104 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700105 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800106 Scalar U_max(int i, int j = 0) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700107
Austin Schuh20388b62017-11-23 22:40:46 -0800108 const Eigen::Matrix<Scalar, number_of_states, 1> &X() const { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800109 Scalar X(int i, int j = 0) const { return X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800110 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y() const { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800111 Scalar Y(int i, int j = 0) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700112
Austin Schuh20388b62017-11-23 22:40:46 -0800113 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X() { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800114 Scalar &mutable_X(int i, int j = 0) { return mutable_X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800115 Eigen::Matrix<Scalar, number_of_outputs, 1> &mutable_Y() { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800116 Scalar &mutable_Y(int i, int j = 0) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700117
James Kuszmaul109ed8d2019-02-17 21:41:04 -0800118 size_t coefficients_size() const { return coefficients_.size(); }
119
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 Schuhf257f3c2019-10-27 21:00:43 -0700150 AOS_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;
James Kuszmaul4d752d52019-02-09 17:27:55 -0800279 const Eigen::Matrix<Scalar, number_of_states, number_of_states> Q;
280 const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> R;
Austin Schuh32501832017-02-25 18:32:56 -0800281
282 StateFeedbackObserverCoefficients(
Tyler Chatow6738c362019-02-16 14:12:30 -0800283 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs>
284 &KalmanGain,
James Kuszmaul4d752d52019-02-09 17:27:55 -0800285 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &Q,
286 const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> &R)
287 : KalmanGain(KalmanGain), Q(Q), R(R) {}
Austin Schuh32501832017-02-25 18:32:56 -0800288};
289
Austin Schuh20388b62017-11-23 22:40:46 -0800290template <int number_of_states, int number_of_inputs, int number_of_outputs,
291 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800292class StateFeedbackObserver {
293 public:
294 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
295
296 explicit StateFeedbackObserver(
297 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Tyler Chatow6738c362019-02-16 14:12:30 -0800298 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
299 *observers)
Austin Schuh32501832017-02-25 18:32:56 -0800300 : coefficients_(::std::move(*observers)) {}
301
302 StateFeedbackObserver(StateFeedbackObserver &&other)
Austin Schuhe91f14c2017-02-25 19:43:57 -0800303 : X_hat_(other.X_hat_), index_(other.index_) {
Austin Schuh32501832017-02-25 18:32:56 -0800304 ::std::swap(coefficients_, other.coefficients_);
305 }
306
Tyler Chatow6738c362019-02-16 14:12:30 -0800307 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &KalmanGain()
308 const {
Sabina Davis3922dfa2018-02-10 23:10:05 -0800309 return coefficients().KalmanGain;
Austin Schuh32501832017-02-25 18:32:56 -0800310 }
Sabina Davis3922dfa2018-02-10 23:10:05 -0800311 Scalar KalmanGain(int i, int j) const { return KalmanGain()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800312
Austin Schuh20388b62017-11-23 22:40:46 -0800313 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800314 return X_hat_;
315 }
Austin Schuh20388b62017-11-23 22:40:46 -0800316 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800317
Austin Schuh6501d232017-11-23 20:35:27 -0800318 void Reset(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800319 number_of_outputs, Scalar> * /*loop*/) {
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800320 X_hat_.setZero();
321 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800322
Austin Schuh6501d232017-11-23 20:35:27 -0800323 void Predict(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> &new_u,
Austin Schuh6501d232017-11-23 20:35:27 -0800326 ::std::chrono::nanoseconds /*dt*/) {
327 mutable_X_hat() = plant->Update(X_hat(), new_u);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800328 }
329
Austin Schuh6501d232017-11-23 20:35:27 -0800330 void Correct(const StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800331 number_of_outputs, Scalar> &plant,
332 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U,
333 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Tyler Chatow6738c362019-02-16 14:12:30 -0800334 mutable_X_hat() += KalmanGain() * (Y - plant.C() * X_hat() - plant.D() * U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800335 }
336
Austin Schuh32501832017-02-25 18:32:56 -0800337 // Sets the current controller to be index, clamped to be within range.
338 void set_index(int index) {
339 if (index < 0) {
340 index_ = 0;
341 } else if (index >= static_cast<int>(coefficients_.size())) {
342 index_ = static_cast<int>(coefficients_.size()) - 1;
343 } else {
344 index_ = index;
345 }
346 }
347
348 int index() const { return index_; }
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(int index) const {
353 return *coefficients_[index];
354 }
355
356 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800357 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800358 &coefficients() const {
359 return *coefficients_[index_];
360 }
361
362 private:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800363 // Internal state estimate.
Austin Schuh20388b62017-11-23 22:40:46 -0800364 Eigen::Matrix<Scalar, number_of_states, 1> X_hat_;
Austin Schuhe91f14c2017-02-25 19:43:57 -0800365
Austin Schuh32501832017-02-25 18:32:56 -0800366 int index_ = 0;
367 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800368 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800369 coefficients_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700370};
371
Austin Schuhe91f14c2017-02-25 19:43:57 -0800372template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800373 typename Scalar = double,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800374 typename PlantType = StateFeedbackPlant<
Austin Schuh20388b62017-11-23 22:40:46 -0800375 number_of_states, number_of_inputs, number_of_outputs, Scalar>,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800376 typename ObserverType = StateFeedbackObserver<
Austin Schuh20388b62017-11-23 22:40:46 -0800377 number_of_states, number_of_inputs, number_of_outputs, Scalar>>
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800378class StateFeedbackLoop {
379 public:
380 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
381
Austin Schuh32501832017-02-25 18:32:56 -0800382 explicit StateFeedbackLoop(
Austin Schuhe91f14c2017-02-25 19:43:57 -0800383 PlantType &&plant,
Austin Schuh32501832017-02-25 18:32:56 -0800384 StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800385 number_of_outputs, Scalar> &&controller,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800386 ObserverType &&observer)
Austin Schuhc5fceb82017-02-25 16:24:12 -0800387 : plant_(::std::move(plant)),
Austin Schuh32501832017-02-25 18:32:56 -0800388 controller_(::std::move(controller)),
389 observer_(::std::move(observer)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700390 Reset();
391 }
392
Austin Schuhc5fceb82017-02-25 16:24:12 -0800393 StateFeedbackLoop(StateFeedbackLoop &&other)
Austin Schuh32501832017-02-25 18:32:56 -0800394 : plant_(::std::move(other.plant_)),
395 controller_(::std::move(other.controller_)),
396 observer_(::std::move(other.observer_)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700397 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800398 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700399 U_.swap(other.U_);
400 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800401 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700402 }
403
Austin Schuh1a387962015-01-31 16:36:20 -0800404 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700405
Austin Schuh20388b62017-11-23 22:40:46 -0800406 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800407 return observer().X_hat();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700408 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800409 Scalar X_hat(int i, int j = 0) const { return X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800410 const Eigen::Matrix<Scalar, number_of_states, 1> &R() const { return R_; }
411 Scalar R(int i, int j) const { return R()(i, j); }
412 const Eigen::Matrix<Scalar, number_of_states, 1> &next_R() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800413 return next_R_;
414 }
Austin Schuh20388b62017-11-23 22:40:46 -0800415 Scalar next_R(int i, int j) const { return next_R()(i, j); }
416 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U() const { return U_; }
417 Scalar U(int i, int j) const { return U()(i, j); }
418 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_uncapped() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700419 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700420 }
Austin Schuh20388b62017-11-23 22:40:46 -0800421 Scalar U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
422 const Eigen::Matrix<Scalar, number_of_inputs, 1> &ff_U() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800423 return ff_U_;
424 }
Austin Schuh20388b62017-11-23 22:40:46 -0800425 Scalar ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700426
Austin Schuh20388b62017-11-23 22:40:46 -0800427 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800428 return observer_.mutable_X_hat();
429 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800430 Scalar &mutable_X_hat(int i, int j = 0) { return mutable_X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800431 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_R() { return R_; }
432 Scalar &mutable_R(int i, int j) { return mutable_R()(i, j); }
433 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_next_R() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800434 return next_R_;
435 }
Austin Schuh20388b62017-11-23 22:40:46 -0800436 Scalar &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
437 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U() { return U_; }
438 Scalar &mutable_U(int i, int j) { return mutable_U()(i, j); }
439 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700440 return U_uncapped_;
441 }
Austin Schuh20388b62017-11-23 22:40:46 -0800442 Scalar &mutable_U_uncapped(int i, int j) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700443 return mutable_U_uncapped()(i, j);
444 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800445
Austin Schuhe91f14c2017-02-25 19:43:57 -0800446 const PlantType &plant() const { return plant_; }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800447 PlantType *mutable_plant() { return &plant_; }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800448
Austin Schuh32501832017-02-25 18:32:56 -0800449 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800450 number_of_outputs, Scalar>
Austin Schuh66c19882017-02-25 13:36:28 -0800451 &controller() const {
Austin Schuh32501832017-02-25 18:32:56 -0800452 return controller_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700453 }
454
Austin Schuhe91f14c2017-02-25 19:43:57 -0800455 const ObserverType &observer() const { return observer_; }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700456
Austin Schuh9644e1c2013-03-12 00:40:36 -0700457 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700458 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800459 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700460 U_.setZero();
461 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800462 ff_U_.setZero();
Austin Schuhe91f14c2017-02-25 19:43:57 -0800463
464 plant_.Reset();
465 controller_.Reset();
Austin Schuh6501d232017-11-23 20:35:27 -0800466 observer_.Reset(this->mutable_plant());
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800467 }
468
469 // If U is outside the hardware range, limit it before the plant tries to use
470 // it.
471 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400472 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800473 if (U(i, 0) > plant().U_max(i, 0)) {
474 U_(i, 0) = plant().U_max(i, 0);
475 } else if (U(i, 0) < plant().U_min(i, 0)) {
476 U_(i, 0) = plant().U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800477 }
478 }
479 }
480
Austin Schuhf9286cd2014-02-11 00:51:09 -0800481 // Corrects X_hat given the observation in Y.
Austin Schuh20388b62017-11-23 22:40:46 -0800482 void Correct(const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Austin Schuh6501d232017-11-23 20:35:27 -0800483 observer_.Correct(this->plant(), U(), Y);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800484 }
485
Austin Schuh20388b62017-11-23 22:40:46 -0800486 const Eigen::Matrix<Scalar, number_of_states, 1> error() const {
Austin Schuh3f862bb2016-02-27 14:48:05 -0800487 return R() - X_hat();
488 }
489
Austin Schuhb6a6d822016-02-08 00:20:40 -0800490 // Returns the calculated controller power.
Austin Schuh20388b62017-11-23 22:40:46 -0800491 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> ControllerOutput() {
Austin Schuh32501832017-02-25 18:32:56 -0800492 // TODO(austin): Should this live in StateSpaceController?
Austin Schuhb6a6d822016-02-08 00:20:40 -0800493 ff_U_ = FeedForward();
Austin Schuh32501832017-02-25 18:32:56 -0800494 return controller().K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800495 }
496
497 // Calculates the feed forwards power.
Austin Schuh20388b62017-11-23 22:40:46 -0800498 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> FeedForward() {
Austin Schuh32501832017-02-25 18:32:56 -0800499 // TODO(austin): Should this live in StateSpaceController?
500 return controller().Kff() * (next_R() - plant().A() * R());
Austin Schuhb6a6d822016-02-08 00:20:40 -0800501 }
502
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800503 // stop_motors is whether or not to output all 0s.
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800504 void Update(bool stop_motors,
505 ::std::chrono::nanoseconds dt = ::std::chrono::milliseconds(5)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800506 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700507 U_.setZero();
508 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800509 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800510 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800511 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800512 CapU();
513 }
514
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800515 UpdateObserver(U_, dt);
Austin Schuh093535c2016-03-05 23:21:00 -0800516
517 UpdateFFReference();
518 }
519
520 // Updates R() after any CapU operations happen on U().
521 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800522 ff_U_ -= U_uncapped() - U();
Austin Schuh32501832017-02-25 18:32:56 -0800523 if (!controller().Kff().isZero(0)) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800524 R_ = plant().A() * R() + plant().B() * ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800525 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000526 }
527
Austin Schuh20388b62017-11-23 22:40:46 -0800528 void UpdateObserver(const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800529 ::std::chrono::nanoseconds dt) {
Austin Schuh6501d232017-11-23 20:35:27 -0800530 observer_.Predict(this->mutable_plant(), new_u, dt);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800531 }
532
Austin Schuh32501832017-02-25 18:32:56 -0800533 // Sets the current controller to be index.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800534 void set_index(int index) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800535 plant_.set_index(index);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800536 controller_.set_index(index);
537 observer_.set_index(index);
Austin Schuh9644e1c2013-03-12 00:40:36 -0700538 }
539
Austin Schuh32501832017-02-25 18:32:56 -0800540 int index() const { return plant_.index(); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700541
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800542 protected:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800543 PlantType plant_;
Austin Schuhc5fceb82017-02-25 16:24:12 -0800544
Austin Schuh20388b62017-11-23 22:40:46 -0800545 StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs,
546 Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800547 controller_;
548
Austin Schuhe91f14c2017-02-25 19:43:57 -0800549 ObserverType observer_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700550
Brian Silverman273d8a32014-05-10 22:19:09 -0700551 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800552 static constexpr int kNumStates = number_of_states;
553 static constexpr int kNumOutputs = number_of_outputs;
554 static constexpr int kNumInputs = number_of_inputs;
555
556 // Portion of U which is based on the feed-forwards.
Austin Schuh20388b62017-11-23 22:40:46 -0800557 Eigen::Matrix<Scalar, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700558
Brian Silverman273d8a32014-05-10 22:19:09 -0700559 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800560 // Current goal (Used by the feed-back controller).
Austin Schuh20388b62017-11-23 22:40:46 -0800561 Eigen::Matrix<Scalar, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800562 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
Austin Schuh20388b62017-11-23 22:40:46 -0800563 Eigen::Matrix<Scalar, number_of_states, 1> next_R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800564 // Computed output after being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800565 Eigen::Matrix<Scalar, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800566 // Computed output before being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800567 Eigen::Matrix<Scalar, number_of_inputs, 1> U_uncapped_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700568
Brian Silverman0a151c92014-05-02 15:28:44 -0700569 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800570};
571
Brian Silverman273d8a32014-05-10 22:19:09 -0700572#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_