WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
LinearPlantInversionFeedforward.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <Eigen/QR>
8
12#include "wpi/units/time.hpp"
13
14namespace wpi::math {
15
16/**
17 * Constructs a plant inversion model-based feedforward from a LinearSystem.
18 *
19 * The feedforward is calculated as <strong> u_ff = B<sup>+</sup> (r_k+1 - A
20 * r_k) </strong>, where <strong> B<sup>+</sup> </strong> is the pseudoinverse
21 * of B.
22 *
23 * For more on the underlying math, read
24 * https://file.tavsys.net/control/controls-engineering-in-frc.pdf.
25 *
26 * @tparam States Number of states.
27 * @tparam Inputs Number of inputs.
28 */
29template <int States, int Inputs>
31 public:
34
35 /**
36 * Constructs a feedforward with the given plant.
37 *
38 * @tparam Outputs Number of outputs.
39 * @param plant The plant being controlled.
40 * @param dt Discretization timestep.
41 */
42 template <int Outputs>
45 wpi::units::second_t dt)
46 : LinearPlantInversionFeedforward(plant.A(), plant.B(), dt) {}
47
48 /**
49 * Constructs a feedforward with the given coefficients.
50 *
51 * @param A Continuous system matrix of the plant being controlled.
52 * @param B Continuous input matrix of the plant being controlled.
53 * @param dt Discretization timestep.
54 */
57 wpi::units::second_t dt)
58 : m_dt(dt) {
59 DiscretizeAB<States, Inputs>(A, B, dt, &m_A, &m_B);
60 Reset();
61 }
62
63 /**
64 * Returns the previously calculated feedforward as an input vector.
65 *
66 * @return The calculated feedforward.
67 */
68 const InputVector& Uff() const { return m_uff; }
69
70 /**
71 * Returns an element of the previously calculated feedforward.
72 *
73 * @param i Row of uff.
74 *
75 * @return The row of the calculated feedforward.
76 */
77 double Uff(int i) const { return m_uff(i); }
78
79 /**
80 * Returns the current reference vector r.
81 *
82 * @return The current reference vector.
83 */
84 const StateVector& R() const { return m_r; }
85
86 /**
87 * Returns an element of the reference vector r.
88 *
89 * @param i Row of r.
90 *
91 * @return The row of the current reference vector.
92 */
93 double R(int i) const { return m_r(i); }
94
95 /**
96 * Resets the feedforward with a specified initial state vector.
97 *
98 * @param initialState The initial state vector.
99 */
100 void Reset(const StateVector& initialState) {
101 m_r = initialState;
102 m_uff.setZero();
103 }
104
105 /**
106 * Resets the feedforward with a zero initial state vector.
107 */
108 void Reset() {
109 m_r.setZero();
110 m_uff.setZero();
111 }
112
113 /**
114 * Calculate the feedforward with only the desired
115 * future reference. This uses the internally stored "current"
116 * reference.
117 *
118 * If this method is used the initial state of the system is the one set using
119 * Reset(const StateVector&). If the initial state is not
120 * set it defaults to a zero vector.
121 *
122 * @param nextR The reference state of the future timestep (k + dt).
123 *
124 * @return The calculated feedforward.
125 */
127 return Calculate(m_r, nextR);
128 }
129
130 /**
131 * Calculate the feedforward with current and future reference vectors.
132 *
133 * @param r The reference state of the current timestep (k).
134 * @param nextR The reference state of the future timestep (k + dt).
135 *
136 * @return The calculated feedforward.
137 */
139 // rₖ₊₁ = Arₖ + Buₖ
140 // Buₖ = rₖ₊₁ − Arₖ
141 // uₖ = B⁺(rₖ₊₁ − Arₖ)
142 m_uff = m_B.householderQr().solve(nextR - (m_A * r));
143 m_r = nextR;
144 return m_uff;
145 }
146
147 private:
150
151 wpi::units::second_t m_dt;
152
153 // Current reference
154 StateVector m_r;
155
156 // Computed feedforward
157 InputVector m_uff;
158};
159
160} // namespace wpi::math
Vectord< Inputs > InputVector
Definition LinearPlantInversionFeedforward.hpp:33
void Reset(const StateVector &initialState)
Resets the feedforward with a specified initial state vector.
Definition LinearPlantInversionFeedforward.hpp:100
InputVector Calculate(const StateVector &nextR)
Calculate the feedforward with only the desired future reference.
Definition LinearPlantInversionFeedforward.hpp:126
Vectord< States > StateVector
Definition LinearPlantInversionFeedforward.hpp:32
double R(int i) const
Returns an element of the reference vector r.
Definition LinearPlantInversionFeedforward.hpp:93
double Uff(int i) const
Returns an element of the previously calculated feedforward.
Definition LinearPlantInversionFeedforward.hpp:77
const InputVector & Uff() const
Returns the previously calculated feedforward as an input vector.
Definition LinearPlantInversionFeedforward.hpp:68
void Reset()
Resets the feedforward with a zero initial state vector.
Definition LinearPlantInversionFeedforward.hpp:108
LinearPlantInversionFeedforward(const Matrixd< States, States > &A, const Matrixd< States, Inputs > &B, wpi::units::second_t dt)
Constructs a feedforward with the given coefficients.
Definition LinearPlantInversionFeedforward.hpp:55
InputVector Calculate(const StateVector &r, const StateVector &nextR)
Calculate the feedforward with current and future reference vectors.
Definition LinearPlantInversionFeedforward.hpp:138
const StateVector & R() const
Returns the current reference vector r.
Definition LinearPlantInversionFeedforward.hpp:84
LinearPlantInversionFeedforward(const LinearSystem< States, Inputs, Outputs > &plant, wpi::units::second_t dt)
Constructs a feedforward with the given plant.
Definition LinearPlantInversionFeedforward.hpp:43
A plant defined using state-space notation.
Definition LinearSystem.hpp:35
Definition LinearSystem.hpp:20
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.hpp:21
void DiscretizeAB(const Matrixd< States, States > &contA, const Matrixd< States, Inputs > &contB, wpi::units::second_t dt, Matrixd< States, States > *discA, Matrixd< States, Inputs > *discB)
Discretizes the given continuous A and B matrices.
Definition Discretization.hpp:41
Eigen::Vector< double, Size > Vectord
Definition EigenCore.hpp:12