blob: 1ea94d9e3422988ad2d115093ae0ed2090f4503b [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
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cassert>
Tyler Chatow6738c362019-02-16 14:12:30 -08005#include <chrono>
Austin Schuhc5fceb82017-02-25 16:24:12 -08006#include <memory>
7#include <utility>
8#include <vector>
Brian Silvermanc571e052013-03-13 17:58:56 -07009
Austin Schuhdc1c84a2013-02-23 16:33:10 -080010#include "Eigen/Dense"
Austin Schuh3ad5ed82017-02-25 21:36:19 -080011#include "unsupported/Eigen/MatrixFunctions"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080012
Brian Silverman6260c092018-01-14 15:21:36 -080013#if defined(__linux__)
John Park33858a32018-09-28 23:05:48 -070014#include "aos/logging/logging.h"
Austin Schuhb39f4522022-03-27 13:29:42 -070015#include "glog/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),
James Kuszmaul03be1242020-02-21 14:52:04 -080042 U_max(other.U_max),
Austin Schuh64433f12022-02-21 19:40:38 -080043 dt(other.dt),
44 delayed_u(other.delayed_u) {}
Austin Schuhdc1c84a2013-02-23 16:33:10 -080045
Austin Schuhe3490622013-03-13 01:24:30 -070046 StateFeedbackPlantCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -080047 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A,
Austin Schuh20388b62017-11-23 22:40:46 -080048 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B,
49 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C,
50 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D,
51 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max,
James Kuszmaul03be1242020-02-21 14:52:04 -080052 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min,
Austin Schuhb39f4522022-03-27 13:29:42 -070053 const std::chrono::nanoseconds dt, size_t delayed_u)
Austin Schuh64433f12022-02-21 19:40:38 -080054 : A(A),
55 B(B),
56 C(C),
57 D(D),
58 U_min(U_min),
59 U_max(U_max),
60 dt(dt),
61 delayed_u(delayed_u) {}
Austin Schuhe3490622013-03-13 01:24:30 -070062
Austin Schuh20388b62017-11-23 22:40:46 -080063 const Eigen::Matrix<Scalar, number_of_states, number_of_states> A;
Austin Schuh20388b62017-11-23 22:40:46 -080064 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> B;
65 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> C;
66 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> D;
67 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_min;
68 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_max;
James Kuszmaul03be1242020-02-21 14:52:04 -080069 const std::chrono::nanoseconds dt;
Austin Schuh64433f12022-02-21 19:40:38 -080070
71 // If true, this adds a single cycle output delay model to the plant. This is
72 // useful for modeling a control loop cycle where you sample, compute, and
73 // then queue the outputs to be ready to be executed when the next cycle
74 // happens.
Austin Schuhb39f4522022-03-27 13:29:42 -070075 const size_t delayed_u;
Austin Schuhe3490622013-03-13 01:24:30 -070076};
77
Austin Schuh20388b62017-11-23 22:40:46 -080078template <int number_of_states, int number_of_inputs, int number_of_outputs,
79 typename Scalar = double>
Austin Schuhe3490622013-03-13 01:24:30 -070080class StateFeedbackPlant {
81 public:
82 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070083
84 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080085 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -080086 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuhb02bf5b2021-07-31 21:28:21 -070087 &&coefficients)
88 : coefficients_(::std::move(coefficients)), index_(0) {
Austin Schuhb39f4522022-03-27 13:29:42 -070089 if (coefficients_.size() > 1u) {
90 for (size_t i = 1; i < coefficients_.size(); ++i) {
91 if (coefficients_[i]->delayed_u != coefficients_[0]->delayed_u) {
92 abort();
93 }
94 }
95 }
96 last_U_ = Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic>(
97 number_of_inputs,
98 std::max(static_cast<size_t>(1u), coefficients_[0]->delayed_u));
Brian Silverman0a151c92014-05-02 15:28:44 -070099 Reset();
100 }
101
Tyler Chatow6738c362019-02-16 14:12:30 -0800102 StateFeedbackPlant(StateFeedbackPlant &&other) : index_(other.index_) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700103 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700104 X_.swap(other.X_);
105 Y_.swap(other.Y_);
Austin Schuh64433f12022-02-21 19:40:38 -0800106 last_U_.swap(other.last_U_);
Brian Silverman0a151c92014-05-02 15:28:44 -0700107 }
108
Austin Schuh1a387962015-01-31 16:36:20 -0800109 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700110
Austin Schuh20388b62017-11-23 22:40:46 -0800111 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800112 return coefficients().A;
Austin Schuhe3490622013-03-13 01:24:30 -0700113 }
Austin Schuh20388b62017-11-23 22:40:46 -0800114 Scalar A(int i, int j) const { return A()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800115 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800116 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -0700117 }
Austin Schuh20388b62017-11-23 22:40:46 -0800118 Scalar B(int i, int j) const { return B()(i, j); }
119 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800120 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -0700121 }
Austin Schuh20388b62017-11-23 22:40:46 -0800122 Scalar C(int i, int j) const { return C()(i, j); }
123 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800124 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -0700125 }
Austin Schuh20388b62017-11-23 22:40:46 -0800126 Scalar D(int i, int j) const { return D()(i, j); }
127 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800128 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700129 }
Austin Schuh20388b62017-11-23 22:40:46 -0800130 Scalar U_min(int i, int j) const { return U_min()(i, j); }
131 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800132 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700133 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800134 Scalar U_max(int i, int j = 0) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700135
Austin Schuh43b9ae92020-02-29 23:08:38 -0800136 const std::chrono::nanoseconds dt() const { return coefficients().dt; }
137
Austin Schuh20388b62017-11-23 22:40:46 -0800138 const Eigen::Matrix<Scalar, number_of_states, 1> &X() const { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800139 Scalar X(int i, int j = 0) const { return X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800140 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y() const { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800141 Scalar Y(int i, int j = 0) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700142
Austin Schuh20388b62017-11-23 22:40:46 -0800143 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X() { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800144 Scalar &mutable_X(int i, int j = 0) { return mutable_X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800145 Eigen::Matrix<Scalar, number_of_outputs, 1> &mutable_Y() { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800146 Scalar &mutable_Y(int i, int j = 0) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700147
James Kuszmaul109ed8d2019-02-17 21:41:04 -0800148 size_t coefficients_size() const { return coefficients_.size(); }
149
Austin Schuhb6a6d822016-02-08 00:20:40 -0800150 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800151 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800152 &coefficients(int index) const {
153 return *coefficients_[index];
Austin Schuhe3490622013-03-13 01:24:30 -0700154 }
155
Austin Schuhc5fceb82017-02-25 16:24:12 -0800156 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800157 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800158 &coefficients() const {
159 return *coefficients_[index_];
160 }
161
162 int index() const { return index_; }
163 void set_index(int index) {
164 assert(index >= 0);
165 assert(index < static_cast<int>(coefficients_.size()));
166 index_ = index;
Austin Schuhe3490622013-03-13 01:24:30 -0700167 }
168
169 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700170 X_.setZero();
171 Y_.setZero();
Austin Schuh64433f12022-02-21 19:40:38 -0800172 last_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800173 }
174
Austin Schuh849f0032013-03-03 23:59:53 -0800175 // Assert that U is within the hardware range.
Austin Schuh20388b62017-11-23 22:40:46 -0800176 virtual void CheckU(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400177 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuh20388b62017-11-23 22:40:46 -0800178 if (U(i, 0) > U_max(i, 0) + static_cast<Scalar>(0.00001) ||
179 U(i, 0) < U_min(i, 0) - static_cast<Scalar>(0.00001)) {
Brian Silverman6260c092018-01-14 15:21:36 -0800180#if defined(__linux__)
Austin Schuhf257f3c2019-10-27 21:00:43 -0700181 AOS_LOG(FATAL, "U out of range\n");
Brian Silverman6260c092018-01-14 15:21:36 -0800182#else
183 abort();
184#endif
Austin Schuh66c19882017-02-25 13:36:28 -0800185 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800186 }
187 }
Austin Schuh849f0032013-03-03 23:59:53 -0800188
Austin Schuhb39f4522022-03-27 13:29:42 -0700189 const Eigen::Matrix<Scalar, number_of_inputs, 1> last_U(
190 size_t index = 0) const {
191 return last_U_.template block<number_of_inputs, 1>(0, index);
192 }
193
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800194 // Computes the new X and Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800195 void Update(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh849f0032013-03-03 23:59:53 -0800196 // Powers outside of the range are more likely controller bugs than things
197 // that the plant should deal with.
Austin Schuh66c19882017-02-25 13:36:28 -0800198 CheckU(U);
Austin Schuhb39f4522022-03-27 13:29:42 -0700199 if (coefficients().delayed_u > 0) {
200#if defined(__linux__)
201 DCHECK_EQ(static_cast<ssize_t>(coefficients().delayed_u), last_U_.cols());
202#endif
203 X_ = Update(X(), last_U(coefficients().delayed_u - 1));
204 UpdateY(last_U(coefficients().delayed_u - 1));
205 for (int i = coefficients().delayed_u; i > 1; --i) {
206 last_U_.template block<number_of_inputs, 1>(0, i - 1) =
207 last_U_.template block<number_of_inputs, 1>(0, i - 2);
208 }
209 last_U_.template block<number_of_inputs, 1>(0, 0) = U;
Austin Schuh64433f12022-02-21 19:40:38 -0800210 } else {
211 X_ = Update(X(), U);
212 UpdateY(U);
213 }
Austin Schuh01c7b252017-03-05 00:59:31 -0800214 }
215
216 // Computes the new Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800217 void UpdateY(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh66c19882017-02-25 13:36:28 -0800218 Y_ = C() * X() + D() * U;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800219 }
220
Austin Schuh20388b62017-11-23 22:40:46 -0800221 Eigen::Matrix<Scalar, number_of_states, 1> Update(
222 const Eigen::Matrix<Scalar, number_of_states, 1> X,
223 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800224 return A() * X + B() * U;
225 }
226
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800227 protected:
228 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800229 static const int kNumStates = number_of_states;
230 static const int kNumOutputs = number_of_outputs;
231 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700232
233 private:
Austin Schuh20388b62017-11-23 22:40:46 -0800234 Eigen::Matrix<Scalar, number_of_states, 1> X_;
235 Eigen::Matrix<Scalar, number_of_outputs, 1> Y_;
Austin Schuhb39f4522022-03-27 13:29:42 -0700236 Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic> last_U_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700237
Austin Schuhb6a6d822016-02-08 00:20:40 -0800238 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800239 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh64f17a52017-02-25 14:41:58 -0800240 coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700241
Austin Schuhc5fceb82017-02-25 16:24:12 -0800242 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700243
244 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800245};
246
Austin Schuh32501832017-02-25 18:32:56 -0800247// A container for all the controller coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800248template <int number_of_states, int number_of_inputs, int number_of_outputs,
249 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800250struct StateFeedbackControllerCoefficients final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700251 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700252
Austin Schuh20388b62017-11-23 22:40:46 -0800253 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> K;
254 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> Kff;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700255
Austin Schuh32501832017-02-25 18:32:56 -0800256 StateFeedbackControllerCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -0800257 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K,
258 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff)
Austin Schuh32501832017-02-25 18:32:56 -0800259 : K(K), Kff(Kff) {}
260};
261
Austin Schuh20388b62017-11-23 22:40:46 -0800262template <int number_of_states, int number_of_inputs, int number_of_outputs,
263 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800264class StateFeedbackController {
265 public:
266 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
267
268 explicit StateFeedbackController(
269 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800270 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700271 &&controllers)
272 : coefficients_(::std::move(controllers)) {}
Austin Schuh32501832017-02-25 18:32:56 -0800273
274 StateFeedbackController(StateFeedbackController &&other)
275 : index_(other.index_) {
276 ::std::swap(coefficients_, other.coefficients_);
277 }
278
Austin Schuh20388b62017-11-23 22:40:46 -0800279 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K() const {
Austin Schuh32501832017-02-25 18:32:56 -0800280 return coefficients().K;
281 }
Austin Schuh20388b62017-11-23 22:40:46 -0800282 Scalar K(int i, int j) const { return K()(i, j); }
283 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff() const {
Austin Schuh32501832017-02-25 18:32:56 -0800284 return coefficients().Kff;
285 }
Austin Schuh20388b62017-11-23 22:40:46 -0800286 Scalar Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800287
Austin Schuhe91f14c2017-02-25 19:43:57 -0800288 void Reset() {}
289
Austin Schuh32501832017-02-25 18:32:56 -0800290 // Sets the current controller to be index, clamped to be within range.
291 void set_index(int index) {
292 if (index < 0) {
293 index_ = 0;
294 } else if (index >= static_cast<int>(coefficients_.size())) {
295 index_ = static_cast<int>(coefficients_.size()) - 1;
296 } else {
297 index_ = index;
298 }
299 }
300
301 int index() const { return index_; }
302
303 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800304 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800305 &coefficients(int index) const {
306 return *coefficients_[index];
307 }
308
309 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800310 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800311 &coefficients() const {
312 return *coefficients_[index_];
313 }
314
315 private:
316 int index_ = 0;
317 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800318 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800319 coefficients_;
320};
321
Austin Schuh32501832017-02-25 18:32:56 -0800322// A container for all the observer coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800323template <int number_of_states, int number_of_inputs, int number_of_outputs,
324 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800325struct StateFeedbackObserverCoefficients final {
326 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
327
Sabina Davis3922dfa2018-02-10 23:10:05 -0800328 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> KalmanGain;
James Kuszmaul4d752d52019-02-09 17:27:55 -0800329 const Eigen::Matrix<Scalar, number_of_states, number_of_states> Q;
330 const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> R;
Austin Schuh32501832017-02-25 18:32:56 -0800331
Austin Schuh64433f12022-02-21 19:40:38 -0800332 // If true, this adds a single cycle output delay model to the plant. This is
333 // useful for modeling a control loop cycle where you sample, compute, and
334 // then queue the outputs to be ready to be executed when the next cycle
335 // happens.
Austin Schuhb39f4522022-03-27 13:29:42 -0700336 const size_t delayed_u;
Austin Schuh64433f12022-02-21 19:40:38 -0800337
Austin Schuh32501832017-02-25 18:32:56 -0800338 StateFeedbackObserverCoefficients(
Tyler Chatow6738c362019-02-16 14:12:30 -0800339 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs>
340 &KalmanGain,
James Kuszmaul4d752d52019-02-09 17:27:55 -0800341 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &Q,
Austin Schuh64433f12022-02-21 19:40:38 -0800342 const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> &R,
Austin Schuhb39f4522022-03-27 13:29:42 -0700343 size_t delayed_u)
Austin Schuh64433f12022-02-21 19:40:38 -0800344 : KalmanGain(KalmanGain), Q(Q), R(R), delayed_u(delayed_u) {}
Austin Schuh32501832017-02-25 18:32:56 -0800345};
346
Austin Schuh20388b62017-11-23 22:40:46 -0800347template <int number_of_states, int number_of_inputs, int number_of_outputs,
348 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800349class StateFeedbackObserver {
350 public:
351 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
352
353 explicit StateFeedbackObserver(
354 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Tyler Chatow6738c362019-02-16 14:12:30 -0800355 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700356 &&observers)
Austin Schuhb39f4522022-03-27 13:29:42 -0700357 : coefficients_(::std::move(observers)) {
358 last_U_ = Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic>(
359 number_of_inputs, std::max(static_cast<size_t>(1u), coefficients().delayed_u));
360 }
Austin Schuh32501832017-02-25 18:32:56 -0800361
362 StateFeedbackObserver(StateFeedbackObserver &&other)
Austin Schuh64433f12022-02-21 19:40:38 -0800363 : X_hat_(other.X_hat_), last_U_(other.last_U_), index_(other.index_) {
Austin Schuh32501832017-02-25 18:32:56 -0800364 ::std::swap(coefficients_, other.coefficients_);
365 }
366
Tyler Chatow6738c362019-02-16 14:12:30 -0800367 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &KalmanGain()
368 const {
Sabina Davis3922dfa2018-02-10 23:10:05 -0800369 return coefficients().KalmanGain;
Austin Schuh32501832017-02-25 18:32:56 -0800370 }
Sabina Davis3922dfa2018-02-10 23:10:05 -0800371 Scalar KalmanGain(int i, int j) const { return KalmanGain()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800372
Austin Schuh20388b62017-11-23 22:40:46 -0800373 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800374 return X_hat_;
375 }
Austin Schuh20388b62017-11-23 22:40:46 -0800376 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800377
Austin Schuhb39f4522022-03-27 13:29:42 -0700378 const Eigen::Matrix<Scalar, number_of_inputs, 1> last_U(
379 size_t index = 0) const {
380 return last_U_.template block<number_of_inputs, 1>(0, index);
Austin Schuh482a9142022-02-23 16:54:39 -0800381 }
382
Austin Schuh6501d232017-11-23 20:35:27 -0800383 void Reset(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800384 number_of_outputs, Scalar> * /*loop*/) {
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800385 X_hat_.setZero();
Austin Schuh64433f12022-02-21 19:40:38 -0800386 last_U_.setZero();
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800387 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800388
Austin Schuh6501d232017-11-23 20:35:27 -0800389 void Predict(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800390 number_of_outputs, Scalar> *plant,
391 const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh6501d232017-11-23 20:35:27 -0800392 ::std::chrono::nanoseconds /*dt*/) {
Austin Schuhb39f4522022-03-27 13:29:42 -0700393 if (plant->coefficients().delayed_u > 0) {
394 mutable_X_hat() =
395 plant->Update(X_hat(), last_U(coefficients().delayed_u - 1));
396 for (int i = coefficients().delayed_u; i > 1; --i) {
397 last_U_.template block<number_of_inputs, 1>(0, i - 1) =
398 last_U_.template block<number_of_inputs, 1>(0, i - 2);
399 }
400 last_U_.template block<number_of_inputs, 1>(0, 0) = new_u;
Austin Schuh64433f12022-02-21 19:40:38 -0800401 } else {
402 mutable_X_hat() = plant->Update(X_hat(), new_u);
403 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800404 }
405
Austin Schuh6501d232017-11-23 20:35:27 -0800406 void Correct(const StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800407 number_of_outputs, Scalar> &plant,
408 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U,
409 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Tyler Chatow6738c362019-02-16 14:12:30 -0800410 mutable_X_hat() += KalmanGain() * (Y - plant.C() * X_hat() - plant.D() * U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800411 }
412
Austin Schuh32501832017-02-25 18:32:56 -0800413 // Sets the current controller to be index, clamped to be within range.
414 void set_index(int index) {
415 if (index < 0) {
416 index_ = 0;
417 } else if (index >= static_cast<int>(coefficients_.size())) {
418 index_ = static_cast<int>(coefficients_.size()) - 1;
419 } else {
420 index_ = index;
421 }
422 }
423
424 int index() const { return index_; }
425
426 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800427 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800428 &coefficients(int index) const {
429 return *coefficients_[index];
430 }
431
432 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800433 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800434 &coefficients() const {
435 return *coefficients_[index_];
436 }
437
438 private:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800439 // Internal state estimate.
Austin Schuh20388b62017-11-23 22:40:46 -0800440 Eigen::Matrix<Scalar, number_of_states, 1> X_hat_;
Austin Schuhb39f4522022-03-27 13:29:42 -0700441 Eigen::Matrix<Scalar, number_of_inputs, Eigen::Dynamic> last_U_;
Austin Schuhe91f14c2017-02-25 19:43:57 -0800442
Austin Schuh32501832017-02-25 18:32:56 -0800443 int index_ = 0;
444 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800445 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800446 coefficients_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700447};
448
Austin Schuhe91f14c2017-02-25 19:43:57 -0800449template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800450 typename Scalar = double,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800451 typename PlantType = StateFeedbackPlant<
Austin Schuh20388b62017-11-23 22:40:46 -0800452 number_of_states, number_of_inputs, number_of_outputs, Scalar>,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800453 typename ObserverType = StateFeedbackObserver<
Austin Schuh20388b62017-11-23 22:40:46 -0800454 number_of_states, number_of_inputs, number_of_outputs, Scalar>>
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800455class StateFeedbackLoop {
456 public:
457 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
458
Austin Schuh32501832017-02-25 18:32:56 -0800459 explicit StateFeedbackLoop(
Austin Schuhe91f14c2017-02-25 19:43:57 -0800460 PlantType &&plant,
Austin Schuh32501832017-02-25 18:32:56 -0800461 StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800462 number_of_outputs, Scalar> &&controller,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800463 ObserverType &&observer)
Austin Schuhc5fceb82017-02-25 16:24:12 -0800464 : plant_(::std::move(plant)),
Austin Schuh32501832017-02-25 18:32:56 -0800465 controller_(::std::move(controller)),
466 observer_(::std::move(observer)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700467 Reset();
468 }
469
Austin Schuhc5fceb82017-02-25 16:24:12 -0800470 StateFeedbackLoop(StateFeedbackLoop &&other)
Austin Schuh32501832017-02-25 18:32:56 -0800471 : plant_(::std::move(other.plant_)),
472 controller_(::std::move(other.controller_)),
473 observer_(::std::move(other.observer_)) {
Austin Schuh36f8c4e2020-02-29 20:29:41 -0800474 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700475 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800476 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700477 U_.swap(other.U_);
478 U_uncapped_.swap(other.U_uncapped_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700479 }
480
Austin Schuh1a387962015-01-31 16:36:20 -0800481 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700482
Austin Schuh20388b62017-11-23 22:40:46 -0800483 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800484 return observer().X_hat();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700485 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800486 Scalar X_hat(int i, int j = 0) const { return X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800487 const Eigen::Matrix<Scalar, number_of_states, 1> &R() const { return R_; }
Austin Schuh95771d92021-01-23 14:42:25 -0800488 Scalar R(int i, int j = 0) const { return R()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800489 const Eigen::Matrix<Scalar, number_of_states, 1> &next_R() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800490 return next_R_;
491 }
Austin Schuh95771d92021-01-23 14:42:25 -0800492 Scalar next_R(int i, int j = 0) const { return next_R()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800493 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U() const { return U_; }
Austin Schuh95771d92021-01-23 14:42:25 -0800494 Scalar U(int i, int j = 0) const { return U()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800495 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_uncapped() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700496 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700497 }
Austin Schuh95771d92021-01-23 14:42:25 -0800498 Scalar U_uncapped(int i, int j = 0) const { return U_uncapped()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800499 const Eigen::Matrix<Scalar, number_of_inputs, 1> &ff_U() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800500 return ff_U_;
501 }
Austin Schuh95771d92021-01-23 14:42:25 -0800502 Scalar ff_U(int i, int j = 0) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700503
Austin Schuh20388b62017-11-23 22:40:46 -0800504 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800505 return observer_.mutable_X_hat();
506 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800507 Scalar &mutable_X_hat(int i, int j = 0) { return mutable_X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800508 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_R() { return R_; }
Austin Schuh95771d92021-01-23 14:42:25 -0800509 Scalar &mutable_R(int i, int j = 0) { return mutable_R()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800510 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_next_R() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800511 return next_R_;
512 }
Austin Schuh95771d92021-01-23 14:42:25 -0800513 Scalar &mutable_next_R(int i, int j = 0) { return mutable_next_R()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800514 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U() { return U_; }
Austin Schuh95771d92021-01-23 14:42:25 -0800515 Scalar &mutable_U(int i, int j = 0) { return mutable_U()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800516 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700517 return U_uncapped_;
518 }
Austin Schuh95771d92021-01-23 14:42:25 -0800519 Scalar &mutable_U_uncapped(int i, int j = 0) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700520 return mutable_U_uncapped()(i, j);
521 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800522
Austin Schuhe91f14c2017-02-25 19:43:57 -0800523 const PlantType &plant() const { return plant_; }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800524 PlantType *mutable_plant() { return &plant_; }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800525
Austin Schuh32501832017-02-25 18:32:56 -0800526 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800527 number_of_outputs, Scalar>
Austin Schuh66c19882017-02-25 13:36:28 -0800528 &controller() const {
Austin Schuh32501832017-02-25 18:32:56 -0800529 return controller_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700530 }
531
Austin Schuhe91f14c2017-02-25 19:43:57 -0800532 const ObserverType &observer() const { return observer_; }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700533
Austin Schuh9644e1c2013-03-12 00:40:36 -0700534 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700535 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800536 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700537 U_.setZero();
538 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800539 ff_U_.setZero();
Austin Schuhe91f14c2017-02-25 19:43:57 -0800540
541 plant_.Reset();
542 controller_.Reset();
Austin Schuh6501d232017-11-23 20:35:27 -0800543 observer_.Reset(this->mutable_plant());
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800544 }
545
546 // If U is outside the hardware range, limit it before the plant tries to use
547 // it.
548 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400549 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800550 if (U(i, 0) > plant().U_max(i, 0)) {
551 U_(i, 0) = plant().U_max(i, 0);
552 } else if (U(i, 0) < plant().U_min(i, 0)) {
553 U_(i, 0) = plant().U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800554 }
555 }
556 }
557
Austin Schuhf9286cd2014-02-11 00:51:09 -0800558 // Corrects X_hat given the observation in Y.
Austin Schuh20388b62017-11-23 22:40:46 -0800559 void Correct(const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Austin Schuh6501d232017-11-23 20:35:27 -0800560 observer_.Correct(this->plant(), U(), Y);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800561 }
562
Austin Schuh20388b62017-11-23 22:40:46 -0800563 const Eigen::Matrix<Scalar, number_of_states, 1> error() const {
Austin Schuh3f862bb2016-02-27 14:48:05 -0800564 return R() - X_hat();
565 }
566
Austin Schuhb6a6d822016-02-08 00:20:40 -0800567 // Returns the calculated controller power.
Austin Schuh20388b62017-11-23 22:40:46 -0800568 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> ControllerOutput() {
Austin Schuh32501832017-02-25 18:32:56 -0800569 // TODO(austin): Should this live in StateSpaceController?
Austin Schuhb6a6d822016-02-08 00:20:40 -0800570 ff_U_ = FeedForward();
Austin Schuh32501832017-02-25 18:32:56 -0800571 return controller().K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800572 }
573
574 // Calculates the feed forwards power.
Austin Schuh20388b62017-11-23 22:40:46 -0800575 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> FeedForward() {
Austin Schuh32501832017-02-25 18:32:56 -0800576 // TODO(austin): Should this live in StateSpaceController?
577 return controller().Kff() * (next_R() - plant().A() * R());
Austin Schuhb6a6d822016-02-08 00:20:40 -0800578 }
579
Austin Schuh43b9ae92020-02-29 23:08:38 -0800580 void UpdateController(bool stop_motors) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800581 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700582 U_.setZero();
583 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800584 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800585 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800586 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800587 CapU();
588 }
Austin Schuh43b9ae92020-02-29 23:08:38 -0800589 UpdateFFReference();
590 }
591
592 // stop_motors is whether or not to output all 0s.
593 void Update(bool stop_motors,
594 ::std::chrono::nanoseconds dt = ::std::chrono::milliseconds(0)) {
595 UpdateController(stop_motors);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800596
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800597 UpdateObserver(U_, dt);
Austin Schuh093535c2016-03-05 23:21:00 -0800598 }
599
600 // Updates R() after any CapU operations happen on U().
601 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800602 ff_U_ -= U_uncapped() - U();
Austin Schuh32501832017-02-25 18:32:56 -0800603 if (!controller().Kff().isZero(0)) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800604 R_ = plant().A() * R() + plant().B() * ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800605 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000606 }
607
Austin Schuh20388b62017-11-23 22:40:46 -0800608 void UpdateObserver(const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800609 ::std::chrono::nanoseconds dt) {
Austin Schuh6501d232017-11-23 20:35:27 -0800610 observer_.Predict(this->mutable_plant(), new_u, dt);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800611 }
612
Austin Schuh32501832017-02-25 18:32:56 -0800613 // Sets the current controller to be index.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800614 void set_index(int index) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800615 plant_.set_index(index);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800616 controller_.set_index(index);
617 observer_.set_index(index);
Austin Schuh9644e1c2013-03-12 00:40:36 -0700618 }
619
Austin Schuh32501832017-02-25 18:32:56 -0800620 int index() const { return plant_.index(); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700621
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800622 protected:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800623 PlantType plant_;
Austin Schuhc5fceb82017-02-25 16:24:12 -0800624
Austin Schuh20388b62017-11-23 22:40:46 -0800625 StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs,
626 Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800627 controller_;
628
Austin Schuhe91f14c2017-02-25 19:43:57 -0800629 ObserverType observer_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700630
Brian Silverman273d8a32014-05-10 22:19:09 -0700631 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800632 static constexpr int kNumStates = number_of_states;
633 static constexpr int kNumOutputs = number_of_outputs;
634 static constexpr int kNumInputs = number_of_inputs;
635
636 // Portion of U which is based on the feed-forwards.
Austin Schuh20388b62017-11-23 22:40:46 -0800637 Eigen::Matrix<Scalar, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700638
Brian Silverman273d8a32014-05-10 22:19:09 -0700639 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800640 // Current goal (Used by the feed-back controller).
Austin Schuh20388b62017-11-23 22:40:46 -0800641 Eigen::Matrix<Scalar, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800642 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
Austin Schuh20388b62017-11-23 22:40:46 -0800643 Eigen::Matrix<Scalar, number_of_states, 1> next_R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800644 // Computed output after being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800645 Eigen::Matrix<Scalar, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800646 // Computed output before being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800647 Eigen::Matrix<Scalar, number_of_inputs, 1> U_uncapped_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700648
Brian Silverman0a151c92014-05-02 15:28:44 -0700649 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800650};
651
Brian Silverman273d8a32014-05-10 22:19:09 -0700652#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_