RIT VEXU Core API
Loading...
Searching...
No Matches
linear_plant_inversion_feedforward.h
1#pragma once
2
3#include "core/math/eigen_interface.h"
4
5#include "core/math/systems/linear_system.h"
6#include "core/math/systems/discretization.h"
7
16template <int STATES, int INPUTS> class LinearPlantInversionFeedforward {
17 public:
25 template <int OUTPUTS>
28
36 LinearPlantInversionFeedforward(const EMat<STATES, STATES> &A, const EMat<STATES, INPUTS> &B, const double &dt)
37 : A_(A), B_(B), m_dt(dt) {
38 auto [Ad, Bd] = discretize_AB(A, B, dt);
39 Ad_ = Ad;
40 Bd_ = Bd;
41 }
42
51 EVec<INPUTS> calculate(const EVec<STATES> &r, const EVec<STATES> &next_r) {
52 /*
53 * ẋ = Ax + Bu
54 * Bu = ẋ - Ax
55 * u = B \ (ẋ - Ax)
56 * u = B \ (next_r - Br)
57 */
58 uff_ = Bd_.householderQr().solve(next_r - (Ad_ * r));
59 r_ = next_r;
60
61 return uff_;
62 }
63
70 EVec<INPUTS> calculate(const EVec<STATES> &next_r) { return calculate(r_, next_r); }
71
85 EVec<INPUTS> calculate(const EVec<STATES> &r, const EVec<STATES> &next_r, const double &dt) {
86 auto [Ad, Bd] = discretize_AB(A_, B_, dt);
87 /*
88 * ẋ = Ax + Bu
89 * Bu = ẋ - Ax
90 * u = B \ (ẋ - Ax)
91 * u = B \ (next_r - Br)
92 */
93 uff_ = Bd.householderQr().solve(next_r - (Ad * r));
94 r_ = next_r;
95
96 return uff_;
97 }
98
109 EVec<INPUTS> calculate(const EVec<STATES> &next_r, const double &dt) { return calculate(r_, next_r, dt); }
110
116 void reset(const EVec<STATES> &initial_state) {
117 r_ = initial_state;
118 uff_.setZero();
119 }
120
122 void reset() {
123 r_.setZero();
124 uff_.setZero();
125 }
126
132 void set_r(const EVec<STATES> &r) { r_ = r; }
133
134 private:
135 // The continuous system matrices
136 EMat<STATES, STATES> A_;
137 EMat<STATES, INPUTS> B_;
138
139 // The discrete system matrices discretized on the nominal timestep
140 EMat<STATES, STATES> Ad_;
141 EMat<STATES, INPUTS> Bd_;
142
143 // The feedforward control input
144 EVec<INPUTS> uff_;
145 // The current reference state
146 EVec<STATES> r_;
147
148 double m_dt;
149};
LinearPlantInversionFeedforward(LinearSystem< STATES, INPUTS, OUTPUTS > &plant, const double &dt)
Definition linear_plant_inversion_feedforward.h:26
EVec< INPUTS > calculate(const EVec< STATES > &r, const EVec< STATES > &next_r)
Definition linear_plant_inversion_feedforward.h:51
EVec< INPUTS > calculate(const EVec< STATES > &next_r, const double &dt)
Definition linear_plant_inversion_feedforward.h:109
void set_r(const EVec< STATES > &r)
Definition linear_plant_inversion_feedforward.h:132
EVec< INPUTS > calculate(const EVec< STATES > &next_r)
Definition linear_plant_inversion_feedforward.h:70
void reset()
Resets the reference to all zeros, and the feedforward to zero.
Definition linear_plant_inversion_feedforward.h:122
LinearPlantInversionFeedforward(const EMat< STATES, STATES > &A, const EMat< STATES, INPUTS > &B, const double &dt)
Definition linear_plant_inversion_feedforward.h:36
EVec< INPUTS > calculate(const EVec< STATES > &r, const EVec< STATES > &next_r, const double &dt)
Definition linear_plant_inversion_feedforward.h:85
void reset(const EVec< STATES > &initial_state)
Definition linear_plant_inversion_feedforward.h:116
Definition linear_system.h:16