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