WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
ImplicitModelFollower.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
11
12namespace wpi::math {
13
14/**
15 * Contains the controller coefficients and logic for an implicit model
16 * follower.
17 *
18 * Implicit model following lets us design a feedback controller that erases the
19 * dynamics of our system and makes it behave like some other system. This can
20 * be used to make a drivetrain more controllable during teleop driving by
21 * making it behave like a slower or more benign drivetrain.
22 *
23 * For more on the underlying math, read appendix B.3 in
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 controller with the given coefficients and plant.
37 *
38 * @param plant The plant being controlled.
39 * @param plantRef The plant whose dynamics should be followed.
40 */
41 template <int Outputs>
44 : ImplicitModelFollower<States, Inputs>(plant.A(), plant.B(),
45 plantRef.A(), plantRef.B()) {}
46
47 /**
48 * Constructs a controller with the given coefficients and plant.
49 *
50 * @param A Continuous system matrix of the plant being controlled.
51 * @param B Continuous input matrix of the plant being controlled.
52 * @param Aref Continuous system matrix whose dynamics should be followed.
53 * @param Bref Continuous input matrix whose dynamics should be followed.
54 */
57 const Matrixd<States, States>& Aref,
58 const Matrixd<States, Inputs>& Bref) {
59 // Find u_imf that makes real model match reference model.
60 //
61 // dx/dt = Ax + Bu_imf
62 // dz/dt = A_ref z + B_ref u
63 //
64 // Let x = z.
65 //
66 // dx/dt = dz/dt
67 // Ax + Bu_imf = Aref x + B_ref u
68 // Bu_imf = A_ref x - Ax + B_ref u
69 // Bu_imf = (A_ref - A)x + B_ref u
70 // u_imf = B⁻¹((A_ref - A)x + Bref u)
71 // u_imf = -B⁻¹(A - A_ref)x + B⁻¹B_ref u
72
73 // The first term makes the open-loop poles that of the reference
74 // system, and the second term makes the input behave like that of the
75 // reference system.
76 m_A = -B.householderQr().solve(A - Aref);
77 m_B = B.householderQr().solve(Bref);
78
79 Reset();
80 }
81
82 /**
83 * Returns the control input vector u.
84 *
85 * @return The control input.
86 */
87 const InputVector& U() const { return m_u; }
88
89 /**
90 * Returns an element of the control input vector u.
91 *
92 * @param i Row of u.
93 *
94 * @return The row of the control input vector.
95 */
96 double U(int i) const { return m_u(i); }
97
98 /**
99 * Resets the controller.
100 */
101 void Reset() { m_u.setZero(); }
102
103 /**
104 * Returns the next output of the controller.
105 *
106 * @param x The current state x.
107 * @param u The current input for the original model.
108 */
110 m_u = m_A * x + m_B * u;
111 return m_u;
112 }
113
114 private:
115 // Computed controller output
116 InputVector m_u;
117
118 // State space conversion gain
120
121 // Input space conversion gain
123};
124
125} // namespace wpi::math
InputVector Calculate(const StateVector &x, const InputVector &u)
Returns the next output of the controller.
Definition ImplicitModelFollower.hpp:109
void Reset()
Resets the controller.
Definition ImplicitModelFollower.hpp:101
ImplicitModelFollower(const Matrixd< States, States > &A, const Matrixd< States, Inputs > &B, const Matrixd< States, States > &Aref, const Matrixd< States, Inputs > &Bref)
Constructs a controller with the given coefficients and plant.
Definition ImplicitModelFollower.hpp:55
const InputVector & U() const
Returns the control input vector u.
Definition ImplicitModelFollower.hpp:87
double U(int i) const
Returns an element of the control input vector u.
Definition ImplicitModelFollower.hpp:96
Vectord< Inputs > InputVector
Definition ImplicitModelFollower.hpp:33
ImplicitModelFollower(const LinearSystem< States, Inputs, Outputs > &plant, const LinearSystem< States, Inputs, Outputs > &plantRef)
Constructs a controller with the given coefficients and plant.
Definition ImplicitModelFollower.hpp:42
Vectord< States > StateVector
Definition ImplicitModelFollower.hpp:32
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
Eigen::Vector< double, Size > Vectord
Definition EigenCore.hpp:12