RIT VEXU Core API
Loading...
Searching...
No Matches
linear_plant_inversion_feedforward.h
1#pragma once
2
3#include "math/eigen_interface.h"
4
5#include "math/systems/linear_system.h"
6#include "math/systems/discretization.h"
7
8#include <iostream>
9
18template <int STATES, int INPUTS> class LinearPlantInversionFeedforward {
19 public:
27 template <int OUTPUTS>
30
38 LinearPlantInversionFeedforward(const EMat<STATES, STATES> &A, const EMat<STATES, INPUTS> &B, const double &dt)
39 : A_(A), B_(B), m_dt(dt) {
40 auto [Ad, Bd] = discretize_AB(A, B, dt);
41 Ad_ = Ad;
42 Bd_ = Bd;
43 }
44
53 EVec<INPUTS> calculate(const EVec<STATES> &r, const EVec<STATES> &next_r) {
54 // ẋ = Ax + Bu
55 // Bu = ẋ - Ax
56 // u = B \ (ẋ - Ax)
57 // u = B \ (next_r - Br)
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 uff_ = Bd.householderQr().solve(next_r - (Ad * r));
93 r_ = next_r;
94
95 return uff_;
96 }
97
108 EVec<INPUTS> calculate(const EVec<STATES> &next_r, const double &dt) { return calculate(r_, next_r, dt); }
109
115 void reset(const EVec<STATES> &initial_state) {
116 r_ = initial_state;
117 uff_.setZero();
118 }
119
123 void reset() {
124 r_.setZero();
125 uff_.setZero();
126 }
127
133 void set_r(const EVec<STATES> &r) { r_ = r; }
134
135 private:
136 // The continuous system matrices
137 EMat<STATES, STATES> A_;
138 EMat<STATES, INPUTS> B_;
139
140 // The discrete system matrices discretized on the nominal timestep
141 EMat<STATES, STATES> Ad_;
142 EMat<STATES, INPUTS> Bd_;
143
144 // The feedforward control input
145 EVec<INPUTS> uff_;
146 // The current reference state
147 EVec<STATES> r_;
148
149 double m_dt;
150};
LinearPlantInversionFeedforward(LinearSystem< STATES, INPUTS, OUTPUTS > &plant, const double &dt)
Definition linear_plant_inversion_feedforward.h:28
EVec< INPUTS > calculate(const EVec< STATES > &r, const EVec< STATES > &next_r)
Definition linear_plant_inversion_feedforward.h:53
EVec< INPUTS > calculate(const EVec< STATES > &next_r, const double &dt)
Definition linear_plant_inversion_feedforward.h:108
void set_r(const EVec< STATES > &r)
Definition linear_plant_inversion_feedforward.h:133
EVec< INPUTS > calculate(const EVec< STATES > &next_r)
Definition linear_plant_inversion_feedforward.h:70
void reset()
Definition linear_plant_inversion_feedforward.h:123
LinearPlantInversionFeedforward(const EMat< STATES, STATES > &A, const EMat< STATES, INPUTS > &B, const double &dt)
Definition linear_plant_inversion_feedforward.h:38
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:115
Definition linear_system.h:17