blob: 83a54f0a4e3101fb73e9873559b8c4c45b5df085 [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),
James Kuszmaul03be1242020-02-21 14:52:04 -080042 U_max(other.U_max),
43 dt(other.dt) {}
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,
James Kuszmaul03be1242020-02-21 14:52:04 -080051 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min,
52 const std::chrono::nanoseconds dt)
53 : A(A), B(B), C(C), D(D), U_min(U_min), U_max(U_max), dt(dt) {}
Austin Schuhe3490622013-03-13 01:24:30 -070054
Austin Schuh20388b62017-11-23 22:40:46 -080055 const Eigen::Matrix<Scalar, number_of_states, number_of_states> A;
Austin Schuh20388b62017-11-23 22:40:46 -080056 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> B;
57 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> C;
58 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> D;
59 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_min;
60 const Eigen::Matrix<Scalar, number_of_inputs, 1> U_max;
James Kuszmaul03be1242020-02-21 14:52:04 -080061 const std::chrono::nanoseconds dt;
Austin Schuhe3490622013-03-13 01:24:30 -070062};
63
Austin Schuh20388b62017-11-23 22:40:46 -080064template <int number_of_states, int number_of_inputs, int number_of_outputs,
65 typename Scalar = double>
Austin Schuhe3490622013-03-13 01:24:30 -070066class StateFeedbackPlant {
67 public:
68 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman0a151c92014-05-02 15:28:44 -070069
70 StateFeedbackPlant(
Austin Schuhb6a6d822016-02-08 00:20:40 -080071 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -080072 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh66c19882017-02-25 13:36:28 -080073 *coefficients)
Austin Schuhc5fceb82017-02-25 16:24:12 -080074 : coefficients_(::std::move(*coefficients)), index_(0) {
Brian Silverman0a151c92014-05-02 15:28:44 -070075 Reset();
76 }
77
Tyler Chatow6738c362019-02-16 14:12:30 -080078 StateFeedbackPlant(StateFeedbackPlant &&other) : index_(other.index_) {
Brian Silverman0a151c92014-05-02 15:28:44 -070079 ::std::swap(coefficients_, other.coefficients_);
Brian Silverman273d8a32014-05-10 22:19:09 -070080 X_.swap(other.X_);
81 Y_.swap(other.Y_);
Brian Silverman0a151c92014-05-02 15:28:44 -070082 }
83
Austin Schuh1a387962015-01-31 16:36:20 -080084 virtual ~StateFeedbackPlant() {}
Brian Silverman0a151c92014-05-02 15:28:44 -070085
Austin Schuh20388b62017-11-23 22:40:46 -080086 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &A() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080087 return coefficients().A;
Austin Schuhe3490622013-03-13 01:24:30 -070088 }
Austin Schuh20388b62017-11-23 22:40:46 -080089 Scalar A(int i, int j) const { return A()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -080090 const Eigen::Matrix<Scalar, number_of_states, number_of_inputs> &B() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080091 return coefficients().B;
Austin Schuhe3490622013-03-13 01:24:30 -070092 }
Austin Schuh20388b62017-11-23 22:40:46 -080093 Scalar B(int i, int j) const { return B()(i, j); }
94 const Eigen::Matrix<Scalar, number_of_outputs, number_of_states> &C() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080095 return coefficients().C;
Austin Schuhe3490622013-03-13 01:24:30 -070096 }
Austin Schuh20388b62017-11-23 22:40:46 -080097 Scalar C(int i, int j) const { return C()(i, j); }
98 const Eigen::Matrix<Scalar, number_of_outputs, number_of_inputs> &D() const {
Austin Schuh64f17a52017-02-25 14:41:58 -080099 return coefficients().D;
Austin Schuhe3490622013-03-13 01:24:30 -0700100 }
Austin Schuh20388b62017-11-23 22:40:46 -0800101 Scalar D(int i, int j) const { return D()(i, j); }
102 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_min() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800103 return coefficients().U_min;
Austin Schuhe3490622013-03-13 01:24:30 -0700104 }
Austin Schuh20388b62017-11-23 22:40:46 -0800105 Scalar U_min(int i, int j) const { return U_min()(i, j); }
106 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_max() const {
Austin Schuh64f17a52017-02-25 14:41:58 -0800107 return coefficients().U_max;
Austin Schuhe3490622013-03-13 01:24:30 -0700108 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800109 Scalar U_max(int i, int j = 0) const { return U_max()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700110
Austin Schuh20388b62017-11-23 22:40:46 -0800111 const Eigen::Matrix<Scalar, number_of_states, 1> &X() const { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800112 Scalar X(int i, int j = 0) const { return X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800113 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y() const { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800114 Scalar Y(int i, int j = 0) const { return Y()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700115
Austin Schuh20388b62017-11-23 22:40:46 -0800116 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X() { return X_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800117 Scalar &mutable_X(int i, int j = 0) { return mutable_X()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800118 Eigen::Matrix<Scalar, number_of_outputs, 1> &mutable_Y() { return Y_; }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800119 Scalar &mutable_Y(int i, int j = 0) { return mutable_Y()(i, j); }
Austin Schuhe3490622013-03-13 01:24:30 -0700120
James Kuszmaul109ed8d2019-02-17 21:41:04 -0800121 size_t coefficients_size() const { return coefficients_.size(); }
122
Austin Schuhb6a6d822016-02-08 00:20:40 -0800123 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800124 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800125 &coefficients(int index) const {
126 return *coefficients_[index];
Austin Schuhe3490622013-03-13 01:24:30 -0700127 }
128
Austin Schuhc5fceb82017-02-25 16:24:12 -0800129 const StateFeedbackPlantCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800130 number_of_outputs, Scalar>
Austin Schuhc5fceb82017-02-25 16:24:12 -0800131 &coefficients() const {
132 return *coefficients_[index_];
133 }
134
135 int index() const { return index_; }
136 void set_index(int index) {
137 assert(index >= 0);
138 assert(index < static_cast<int>(coefficients_.size()));
139 index_ = index;
Austin Schuhe3490622013-03-13 01:24:30 -0700140 }
141
142 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700143 X_.setZero();
144 Y_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800145 }
146
Austin Schuh849f0032013-03-03 23:59:53 -0800147 // Assert that U is within the hardware range.
Austin Schuh20388b62017-11-23 22:40:46 -0800148 virtual void CheckU(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400149 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuh20388b62017-11-23 22:40:46 -0800150 if (U(i, 0) > U_max(i, 0) + static_cast<Scalar>(0.00001) ||
151 U(i, 0) < U_min(i, 0) - static_cast<Scalar>(0.00001)) {
Brian Silverman6260c092018-01-14 15:21:36 -0800152#if defined(__linux__)
Austin Schuhf257f3c2019-10-27 21:00:43 -0700153 AOS_LOG(FATAL, "U out of range\n");
Brian Silverman6260c092018-01-14 15:21:36 -0800154#else
155 abort();
156#endif
Austin Schuh66c19882017-02-25 13:36:28 -0800157 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800158 }
159 }
Austin Schuh849f0032013-03-03 23:59:53 -0800160
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800161 // Computes the new X and Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800162 void Update(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh849f0032013-03-03 23:59:53 -0800163 // Powers outside of the range are more likely controller bugs than things
164 // that the plant should deal with.
Austin Schuh66c19882017-02-25 13:36:28 -0800165 CheckU(U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800166 X_ = Update(X(), U);
Austin Schuh01c7b252017-03-05 00:59:31 -0800167 UpdateY(U);
168 }
169
170 // Computes the new Y given the control input.
Austin Schuh20388b62017-11-23 22:40:46 -0800171 void UpdateY(const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) {
Austin Schuh66c19882017-02-25 13:36:28 -0800172 Y_ = C() * X() + D() * U;
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800173 }
174
Austin Schuh20388b62017-11-23 22:40:46 -0800175 Eigen::Matrix<Scalar, number_of_states, 1> Update(
176 const Eigen::Matrix<Scalar, number_of_states, 1> X,
177 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U) const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800178 return A() * X + B() * U;
179 }
180
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800181 protected:
182 // these are accessible from non-templated subclasses
Austin Schuhb1cdb382013-03-01 22:53:52 -0800183 static const int kNumStates = number_of_states;
184 static const int kNumOutputs = number_of_outputs;
185 static const int kNumInputs = number_of_inputs;
Austin Schuhe3490622013-03-13 01:24:30 -0700186
187 private:
Austin Schuh20388b62017-11-23 22:40:46 -0800188 Eigen::Matrix<Scalar, number_of_states, 1> X_;
189 Eigen::Matrix<Scalar, number_of_outputs, 1> Y_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700190
Austin Schuhb6a6d822016-02-08 00:20:40 -0800191 ::std::vector<::std::unique_ptr<StateFeedbackPlantCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800192 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh64f17a52017-02-25 14:41:58 -0800193 coefficients_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700194
Austin Schuhc5fceb82017-02-25 16:24:12 -0800195 int index_;
Brian Silverman0a151c92014-05-02 15:28:44 -0700196
197 DISALLOW_COPY_AND_ASSIGN(StateFeedbackPlant);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800198};
199
Austin Schuh32501832017-02-25 18:32:56 -0800200// A container for all the controller coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800201template <int number_of_states, int number_of_inputs, int number_of_outputs,
202 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800203struct StateFeedbackControllerCoefficients final {
Austin Schuh9644e1c2013-03-12 00:40:36 -0700204 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
Brian Silverman273d8a32014-05-10 22:19:09 -0700205
Austin Schuh20388b62017-11-23 22:40:46 -0800206 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> K;
207 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> Kff;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700208
Austin Schuh32501832017-02-25 18:32:56 -0800209 StateFeedbackControllerCoefficients(
Austin Schuh20388b62017-11-23 22:40:46 -0800210 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K,
211 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff)
Austin Schuh32501832017-02-25 18:32:56 -0800212 : K(K), Kff(Kff) {}
213};
214
Austin Schuh20388b62017-11-23 22:40:46 -0800215template <int number_of_states, int number_of_inputs, int number_of_outputs,
216 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800217class StateFeedbackController {
218 public:
219 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
220
221 explicit StateFeedbackController(
222 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800223 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
224 *controllers)
Austin Schuh32501832017-02-25 18:32:56 -0800225 : coefficients_(::std::move(*controllers)) {}
226
227 StateFeedbackController(StateFeedbackController &&other)
228 : index_(other.index_) {
229 ::std::swap(coefficients_, other.coefficients_);
230 }
231
Austin Schuh20388b62017-11-23 22:40:46 -0800232 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &K() const {
Austin Schuh32501832017-02-25 18:32:56 -0800233 return coefficients().K;
234 }
Austin Schuh20388b62017-11-23 22:40:46 -0800235 Scalar K(int i, int j) const { return K()(i, j); }
236 const Eigen::Matrix<Scalar, number_of_inputs, number_of_states> &Kff() const {
Austin Schuh32501832017-02-25 18:32:56 -0800237 return coefficients().Kff;
238 }
Austin Schuh20388b62017-11-23 22:40:46 -0800239 Scalar Kff(int i, int j) const { return Kff()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800240
Austin Schuhe91f14c2017-02-25 19:43:57 -0800241 void Reset() {}
242
Austin Schuh32501832017-02-25 18:32:56 -0800243 // Sets the current controller to be index, clamped to be within range.
244 void set_index(int index) {
245 if (index < 0) {
246 index_ = 0;
247 } else if (index >= static_cast<int>(coefficients_.size())) {
248 index_ = static_cast<int>(coefficients_.size()) - 1;
249 } else {
250 index_ = index;
251 }
252 }
253
254 int index() const { return index_; }
255
256 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800257 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800258 &coefficients(int index) const {
259 return *coefficients_[index];
260 }
261
262 const StateFeedbackControllerCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800263 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800264 &coefficients() const {
265 return *coefficients_[index_];
266 }
267
268 private:
269 int index_ = 0;
270 ::std::vector<::std::unique_ptr<StateFeedbackControllerCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800271 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800272 coefficients_;
273};
274
Austin Schuh32501832017-02-25 18:32:56 -0800275// A container for all the observer coefficients.
Austin Schuh20388b62017-11-23 22:40:46 -0800276template <int number_of_states, int number_of_inputs, int number_of_outputs,
277 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800278struct StateFeedbackObserverCoefficients final {
279 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
280
Sabina Davis3922dfa2018-02-10 23:10:05 -0800281 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> KalmanGain;
James Kuszmaul4d752d52019-02-09 17:27:55 -0800282 const Eigen::Matrix<Scalar, number_of_states, number_of_states> Q;
283 const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> R;
Austin Schuh32501832017-02-25 18:32:56 -0800284
285 StateFeedbackObserverCoefficients(
Tyler Chatow6738c362019-02-16 14:12:30 -0800286 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs>
287 &KalmanGain,
James Kuszmaul4d752d52019-02-09 17:27:55 -0800288 const Eigen::Matrix<Scalar, number_of_states, number_of_states> &Q,
289 const Eigen::Matrix<Scalar, number_of_outputs, number_of_outputs> &R)
290 : KalmanGain(KalmanGain), Q(Q), R(R) {}
Austin Schuh32501832017-02-25 18:32:56 -0800291};
292
Austin Schuh20388b62017-11-23 22:40:46 -0800293template <int number_of_states, int number_of_inputs, int number_of_outputs,
294 typename Scalar = double>
Austin Schuh32501832017-02-25 18:32:56 -0800295class StateFeedbackObserver {
296 public:
297 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
298
299 explicit StateFeedbackObserver(
300 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Tyler Chatow6738c362019-02-16 14:12:30 -0800301 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
302 *observers)
Austin Schuh32501832017-02-25 18:32:56 -0800303 : coefficients_(::std::move(*observers)) {}
304
305 StateFeedbackObserver(StateFeedbackObserver &&other)
Austin Schuhe91f14c2017-02-25 19:43:57 -0800306 : X_hat_(other.X_hat_), index_(other.index_) {
Austin Schuh32501832017-02-25 18:32:56 -0800307 ::std::swap(coefficients_, other.coefficients_);
308 }
309
Tyler Chatow6738c362019-02-16 14:12:30 -0800310 const Eigen::Matrix<Scalar, number_of_states, number_of_outputs> &KalmanGain()
311 const {
Sabina Davis3922dfa2018-02-10 23:10:05 -0800312 return coefficients().KalmanGain;
Austin Schuh32501832017-02-25 18:32:56 -0800313 }
Sabina Davis3922dfa2018-02-10 23:10:05 -0800314 Scalar KalmanGain(int i, int j) const { return KalmanGain()(i, j); }
Austin Schuh32501832017-02-25 18:32:56 -0800315
Austin Schuh20388b62017-11-23 22:40:46 -0800316 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800317 return X_hat_;
318 }
Austin Schuh20388b62017-11-23 22:40:46 -0800319 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() { return X_hat_; }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800320
Austin Schuh6501d232017-11-23 20:35:27 -0800321 void Reset(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800322 number_of_outputs, Scalar> * /*loop*/) {
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800323 X_hat_.setZero();
324 }
Austin Schuhe91f14c2017-02-25 19:43:57 -0800325
Austin Schuh6501d232017-11-23 20:35:27 -0800326 void Predict(StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800327 number_of_outputs, Scalar> *plant,
328 const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh6501d232017-11-23 20:35:27 -0800329 ::std::chrono::nanoseconds /*dt*/) {
330 mutable_X_hat() = plant->Update(X_hat(), new_u);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800331 }
332
Austin Schuh6501d232017-11-23 20:35:27 -0800333 void Correct(const StateFeedbackPlant<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800334 number_of_outputs, Scalar> &plant,
335 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U,
336 const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Tyler Chatow6738c362019-02-16 14:12:30 -0800337 mutable_X_hat() += KalmanGain() * (Y - plant.C() * X_hat() - plant.D() * U);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800338 }
339
Austin Schuh32501832017-02-25 18:32:56 -0800340 // Sets the current controller to be index, clamped to be within range.
341 void set_index(int index) {
342 if (index < 0) {
343 index_ = 0;
344 } else if (index >= static_cast<int>(coefficients_.size())) {
345 index_ = static_cast<int>(coefficients_.size()) - 1;
346 } else {
347 index_ = index;
348 }
349 }
350
351 int index() const { return index_; }
352
353 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800354 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800355 &coefficients(int index) const {
356 return *coefficients_[index];
357 }
358
359 const StateFeedbackObserverCoefficients<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800360 number_of_outputs, Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800361 &coefficients() const {
362 return *coefficients_[index_];
363 }
364
365 private:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800366 // Internal state estimate.
Austin Schuh20388b62017-11-23 22:40:46 -0800367 Eigen::Matrix<Scalar, number_of_states, 1> X_hat_;
Austin Schuhe91f14c2017-02-25 19:43:57 -0800368
Austin Schuh32501832017-02-25 18:32:56 -0800369 int index_ = 0;
370 ::std::vector<::std::unique_ptr<StateFeedbackObserverCoefficients<
Austin Schuh20388b62017-11-23 22:40:46 -0800371 number_of_states, number_of_inputs, number_of_outputs, Scalar>>>
Austin Schuh32501832017-02-25 18:32:56 -0800372 coefficients_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700373};
374
Austin Schuhe91f14c2017-02-25 19:43:57 -0800375template <int number_of_states, int number_of_inputs, int number_of_outputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800376 typename Scalar = double,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800377 typename PlantType = StateFeedbackPlant<
Austin Schuh20388b62017-11-23 22:40:46 -0800378 number_of_states, number_of_inputs, number_of_outputs, Scalar>,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800379 typename ObserverType = StateFeedbackObserver<
Austin Schuh20388b62017-11-23 22:40:46 -0800380 number_of_states, number_of_inputs, number_of_outputs, Scalar>>
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800381class StateFeedbackLoop {
382 public:
383 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
384
Austin Schuh32501832017-02-25 18:32:56 -0800385 explicit StateFeedbackLoop(
Austin Schuhe91f14c2017-02-25 19:43:57 -0800386 PlantType &&plant,
Austin Schuh32501832017-02-25 18:32:56 -0800387 StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800388 number_of_outputs, Scalar> &&controller,
Austin Schuhe91f14c2017-02-25 19:43:57 -0800389 ObserverType &&observer)
Austin Schuhc5fceb82017-02-25 16:24:12 -0800390 : plant_(::std::move(plant)),
Austin Schuh32501832017-02-25 18:32:56 -0800391 controller_(::std::move(controller)),
392 observer_(::std::move(observer)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700393 Reset();
394 }
395
Austin Schuhc5fceb82017-02-25 16:24:12 -0800396 StateFeedbackLoop(StateFeedbackLoop &&other)
Austin Schuh32501832017-02-25 18:32:56 -0800397 : plant_(::std::move(other.plant_)),
398 controller_(::std::move(other.controller_)),
399 observer_(::std::move(other.observer_)) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700400 R_.swap(other.R_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800401 next_R_.swap(other.next_R_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700402 U_.swap(other.U_);
403 U_uncapped_.swap(other.U_uncapped_);
Austin Schuhb6a6d822016-02-08 00:20:40 -0800404 ff_U_.swap(other.ff_U_);
Brian Silverman273d8a32014-05-10 22:19:09 -0700405 }
406
Austin Schuh1a387962015-01-31 16:36:20 -0800407 virtual ~StateFeedbackLoop() {}
Brian Silverman0a151c92014-05-02 15:28:44 -0700408
Austin Schuh20388b62017-11-23 22:40:46 -0800409 const Eigen::Matrix<Scalar, number_of_states, 1> &X_hat() const {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800410 return observer().X_hat();
Austin Schuh9644e1c2013-03-12 00:40:36 -0700411 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800412 Scalar X_hat(int i, int j = 0) const { return X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800413 const Eigen::Matrix<Scalar, number_of_states, 1> &R() const { return R_; }
414 Scalar R(int i, int j) const { return R()(i, j); }
415 const Eigen::Matrix<Scalar, number_of_states, 1> &next_R() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800416 return next_R_;
417 }
Austin Schuh20388b62017-11-23 22:40:46 -0800418 Scalar next_R(int i, int j) const { return next_R()(i, j); }
419 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U() const { return U_; }
420 Scalar U(int i, int j) const { return U()(i, j); }
421 const Eigen::Matrix<Scalar, number_of_inputs, 1> &U_uncapped() const {
Brian Silverman273d8a32014-05-10 22:19:09 -0700422 return U_uncapped_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700423 }
Austin Schuh20388b62017-11-23 22:40:46 -0800424 Scalar U_uncapped(int i, int j) const { return U_uncapped()(i, j); }
425 const Eigen::Matrix<Scalar, number_of_inputs, 1> &ff_U() const {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800426 return ff_U_;
427 }
Austin Schuh20388b62017-11-23 22:40:46 -0800428 Scalar ff_U(int i, int j) const { return ff_U()(i, j); }
Brian Silverman273d8a32014-05-10 22:19:09 -0700429
Austin Schuh20388b62017-11-23 22:40:46 -0800430 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_X_hat() {
Austin Schuhe91f14c2017-02-25 19:43:57 -0800431 return observer_.mutable_X_hat();
432 }
Sabina Davis8d20ca82018-02-19 13:17:45 -0800433 Scalar &mutable_X_hat(int i, int j = 0) { return mutable_X_hat()(i, j); }
Austin Schuh20388b62017-11-23 22:40:46 -0800434 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_R() { return R_; }
435 Scalar &mutable_R(int i, int j) { return mutable_R()(i, j); }
436 Eigen::Matrix<Scalar, number_of_states, 1> &mutable_next_R() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800437 return next_R_;
438 }
Austin Schuh20388b62017-11-23 22:40:46 -0800439 Scalar &mutable_next_R(int i, int j) { return mutable_next_R()(i, j); }
440 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U() { return U_; }
441 Scalar &mutable_U(int i, int j) { return mutable_U()(i, j); }
442 Eigen::Matrix<Scalar, number_of_inputs, 1> &mutable_U_uncapped() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700443 return U_uncapped_;
444 }
Austin Schuh20388b62017-11-23 22:40:46 -0800445 Scalar &mutable_U_uncapped(int i, int j) {
Brian Silvermana21c3a22014-06-12 21:49:15 -0700446 return mutable_U_uncapped()(i, j);
447 }
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800448
Austin Schuhe91f14c2017-02-25 19:43:57 -0800449 const PlantType &plant() const { return plant_; }
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800450 PlantType *mutable_plant() { return &plant_; }
Austin Schuhc5fceb82017-02-25 16:24:12 -0800451
Austin Schuh32501832017-02-25 18:32:56 -0800452 const StateFeedbackController<number_of_states, number_of_inputs,
Austin Schuh20388b62017-11-23 22:40:46 -0800453 number_of_outputs, Scalar>
Austin Schuh66c19882017-02-25 13:36:28 -0800454 &controller() const {
Austin Schuh32501832017-02-25 18:32:56 -0800455 return controller_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700456 }
457
Austin Schuhe91f14c2017-02-25 19:43:57 -0800458 const ObserverType &observer() const { return observer_; }
Austin Schuh2054f5f2013-10-27 14:54:10 -0700459
Austin Schuh9644e1c2013-03-12 00:40:36 -0700460 void Reset() {
Brian Silverman273d8a32014-05-10 22:19:09 -0700461 R_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800462 next_R_.setZero();
Brian Silverman273d8a32014-05-10 22:19:09 -0700463 U_.setZero();
464 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800465 ff_U_.setZero();
Austin Schuhe91f14c2017-02-25 19:43:57 -0800466
467 plant_.Reset();
468 controller_.Reset();
Austin Schuh6501d232017-11-23 20:35:27 -0800469 observer_.Reset(this->mutable_plant());
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800470 }
471
472 // If U is outside the hardware range, limit it before the plant tries to use
473 // it.
474 virtual void CapU() {
Brian Silverman5808bcb2014-09-14 21:40:43 -0400475 for (int i = 0; i < kNumInputs; ++i) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800476 if (U(i, 0) > plant().U_max(i, 0)) {
477 U_(i, 0) = plant().U_max(i, 0);
478 } else if (U(i, 0) < plant().U_min(i, 0)) {
479 U_(i, 0) = plant().U_min(i, 0);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800480 }
481 }
482 }
483
Austin Schuhf9286cd2014-02-11 00:51:09 -0800484 // Corrects X_hat given the observation in Y.
Austin Schuh20388b62017-11-23 22:40:46 -0800485 void Correct(const Eigen::Matrix<Scalar, number_of_outputs, 1> &Y) {
Austin Schuh6501d232017-11-23 20:35:27 -0800486 observer_.Correct(this->plant(), U(), Y);
Austin Schuhf9286cd2014-02-11 00:51:09 -0800487 }
488
Austin Schuh20388b62017-11-23 22:40:46 -0800489 const Eigen::Matrix<Scalar, number_of_states, 1> error() const {
Austin Schuh3f862bb2016-02-27 14:48:05 -0800490 return R() - X_hat();
491 }
492
Austin Schuhb6a6d822016-02-08 00:20:40 -0800493 // Returns the calculated controller power.
Austin Schuh20388b62017-11-23 22:40:46 -0800494 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> ControllerOutput() {
Austin Schuh32501832017-02-25 18:32:56 -0800495 // TODO(austin): Should this live in StateSpaceController?
Austin Schuhb6a6d822016-02-08 00:20:40 -0800496 ff_U_ = FeedForward();
Austin Schuh32501832017-02-25 18:32:56 -0800497 return controller().K() * error() + ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800498 }
499
500 // Calculates the feed forwards power.
Austin Schuh20388b62017-11-23 22:40:46 -0800501 virtual const Eigen::Matrix<Scalar, number_of_inputs, 1> FeedForward() {
Austin Schuh32501832017-02-25 18:32:56 -0800502 // TODO(austin): Should this live in StateSpaceController?
503 return controller().Kff() * (next_R() - plant().A() * R());
Austin Schuhb6a6d822016-02-08 00:20:40 -0800504 }
505
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800506 // stop_motors is whether or not to output all 0s.
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800507 void Update(bool stop_motors,
508 ::std::chrono::nanoseconds dt = ::std::chrono::milliseconds(5)) {
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800509 if (stop_motors) {
Brian Silverman273d8a32014-05-10 22:19:09 -0700510 U_.setZero();
511 U_uncapped_.setZero();
Austin Schuhb6a6d822016-02-08 00:20:40 -0800512 ff_U_.setZero();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800513 } else {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800514 U_ = U_uncapped_ = ControllerOutput();
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800515 CapU();
516 }
517
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800518 UpdateObserver(U_, dt);
Austin Schuh093535c2016-03-05 23:21:00 -0800519
520 UpdateFFReference();
521 }
522
523 // Updates R() after any CapU operations happen on U().
524 void UpdateFFReference() {
Austin Schuhb6a6d822016-02-08 00:20:40 -0800525 ff_U_ -= U_uncapped() - U();
Austin Schuh32501832017-02-25 18:32:56 -0800526 if (!controller().Kff().isZero(0)) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800527 R_ = plant().A() * R() + plant().B() * ff_U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800528 }
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000529 }
530
Austin Schuh20388b62017-11-23 22:40:46 -0800531 void UpdateObserver(const Eigen::Matrix<Scalar, number_of_inputs, 1> &new_u,
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800532 ::std::chrono::nanoseconds dt) {
Austin Schuh6501d232017-11-23 20:35:27 -0800533 observer_.Predict(this->mutable_plant(), new_u, dt);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800534 }
535
Austin Schuh32501832017-02-25 18:32:56 -0800536 // Sets the current controller to be index.
Austin Schuhc5fceb82017-02-25 16:24:12 -0800537 void set_index(int index) {
Austin Schuhc5fceb82017-02-25 16:24:12 -0800538 plant_.set_index(index);
Austin Schuhe91f14c2017-02-25 19:43:57 -0800539 controller_.set_index(index);
540 observer_.set_index(index);
Austin Schuh9644e1c2013-03-12 00:40:36 -0700541 }
542
Austin Schuh32501832017-02-25 18:32:56 -0800543 int index() const { return plant_.index(); }
Austin Schuh9644e1c2013-03-12 00:40:36 -0700544
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800545 protected:
Austin Schuhe91f14c2017-02-25 19:43:57 -0800546 PlantType plant_;
Austin Schuhc5fceb82017-02-25 16:24:12 -0800547
Austin Schuh20388b62017-11-23 22:40:46 -0800548 StateFeedbackController<number_of_states, number_of_inputs, number_of_outputs,
549 Scalar>
Austin Schuh32501832017-02-25 18:32:56 -0800550 controller_;
551
Austin Schuhe91f14c2017-02-25 19:43:57 -0800552 ObserverType observer_;
Austin Schuh2054f5f2013-10-27 14:54:10 -0700553
Brian Silverman273d8a32014-05-10 22:19:09 -0700554 // These are accessible from non-templated subclasses.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800555 static constexpr int kNumStates = number_of_states;
556 static constexpr int kNumOutputs = number_of_outputs;
557 static constexpr int kNumInputs = number_of_inputs;
558
559 // Portion of U which is based on the feed-forwards.
Austin Schuh20388b62017-11-23 22:40:46 -0800560 Eigen::Matrix<Scalar, number_of_inputs, 1> ff_U_;
Austin Schuh9644e1c2013-03-12 00:40:36 -0700561
Brian Silverman273d8a32014-05-10 22:19:09 -0700562 private:
Austin Schuhb6a6d822016-02-08 00:20:40 -0800563 // Current goal (Used by the feed-back controller).
Austin Schuh20388b62017-11-23 22:40:46 -0800564 Eigen::Matrix<Scalar, number_of_states, 1> R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800565 // Goal to go to in the next cycle (Used by Feed-Forward controller.)
Austin Schuh20388b62017-11-23 22:40:46 -0800566 Eigen::Matrix<Scalar, number_of_states, 1> next_R_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800567 // Computed output after being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800568 Eigen::Matrix<Scalar, number_of_inputs, 1> U_;
Austin Schuhb6a6d822016-02-08 00:20:40 -0800569 // Computed output before being capped.
Austin Schuh20388b62017-11-23 22:40:46 -0800570 Eigen::Matrix<Scalar, number_of_inputs, 1> U_uncapped_;
Brian Silverman273d8a32014-05-10 22:19:09 -0700571
Brian Silverman0a151c92014-05-02 15:28:44 -0700572 DISALLOW_COPY_AND_ASSIGN(StateFeedbackLoop);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800573};
574
Brian Silverman273d8a32014-05-10 22:19:09 -0700575#endif // FRC971_CONTROL_LOOPS_STATE_FEEDBACK_LOOP_H_