WPILibC++ 2023.4.3-108-ge5452e3
LinearSystem.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
7#include <algorithm>
8#include <functional>
9#include <stdexcept>
10
11#include "frc/EigenCore.h"
12#include "frc/StateSpaceUtil.h"
14#include "units/time.h"
15
16namespace frc {
17
18/**
19 * A plant defined using state-space notation.
20 *
21 * A plant is a mathematical model of a system's dynamics.
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 * @tparam Outputs Number of outputs.
29 */
30template <int States, int Inputs, int Outputs>
32 public:
36
37 /**
38 * Constructs a discrete plant with the given continuous system coefficients.
39 *
40 * @param A System matrix.
41 * @param B Input matrix.
42 * @param C Output matrix.
43 * @param D Feedthrough matrix.
44 * @throws std::domain_error if any matrix element isn't finite.
45 */
50 if (!A.allFinite()) {
51 throw std::domain_error(
52 "Elements of A aren't finite. This is usually due to model "
53 "implementation errors.");
54 }
55 if (!B.allFinite()) {
56 throw std::domain_error(
57 "Elements of B aren't finite. This is usually due to model "
58 "implementation errors.");
59 }
60 if (!C.allFinite()) {
61 throw std::domain_error(
62 "Elements of C aren't finite. This is usually due to model "
63 "implementation errors.");
64 }
65 if (!D.allFinite()) {
66 throw std::domain_error(
67 "Elements of D aren't finite. This is usually due to model "
68 "implementation errors.");
69 }
70
71 m_A = A;
72 m_B = B;
73 m_C = C;
74 m_D = D;
75 }
76
77 LinearSystem(const LinearSystem&) = default;
81
82 /**
83 * Returns the system matrix A.
84 */
85 const Matrixd<States, States>& A() const { return m_A; }
86
87 /**
88 * Returns an element of the system matrix A.
89 *
90 * @param i Row of A.
91 * @param j Column of A.
92 */
93 double A(int i, int j) const { return m_A(i, j); }
94
95 /**
96 * Returns the input matrix B.
97 */
98 const Matrixd<States, Inputs>& B() const { return m_B; }
99
100 /**
101 * Returns an element of the input matrix B.
102 *
103 * @param i Row of B.
104 * @param j Column of B.
105 */
106 double B(int i, int j) const { return m_B(i, j); }
107
108 /**
109 * Returns the output matrix C.
110 */
111 const Matrixd<Outputs, States>& C() const { return m_C; }
112
113 /**
114 * Returns an element of the output matrix C.
115 *
116 * @param i Row of C.
117 * @param j Column of C.
118 */
119 double C(int i, int j) const { return m_C(i, j); }
120
121 /**
122 * Returns the feedthrough matrix D.
123 */
124 const Matrixd<Outputs, Inputs>& D() const { return m_D; }
125
126 /**
127 * Returns an element of the feedthrough matrix D.
128 *
129 * @param i Row of D.
130 * @param j Column of D.
131 */
132 double D(int i, int j) const { return m_D(i, j); }
133
134 /**
135 * Computes the new x given the old x and the control input.
136 *
137 * This is used by state observers directly to run updates based on state
138 * estimate.
139 *
140 * @param x The current state.
141 * @param clampedU The control input.
142 * @param dt Timestep for model update.
143 */
145 units::second_t dt) const {
148 DiscretizeAB<States, Inputs>(m_A, m_B, dt, &discA, &discB);
149
150 return discA * x + discB * clampedU;
151 }
152
153 /**
154 * Computes the new y given the control input.
155 *
156 * This is used by state observers directly to run updates based on state
157 * estimate.
158 *
159 * @param x The current state.
160 * @param clampedU The control input.
161 */
163 const InputVector& clampedU) const {
164 return m_C * x + m_D * clampedU;
165 }
166
167 private:
168 /**
169 * Continuous system matrix.
170 */
172
173 /**
174 * Continuous input matrix.
175 */
177
178 /**
179 * Output matrix.
180 */
182
183 /**
184 * Feedthrough matrix.
185 */
187};
188
189} // namespace frc
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
A plant defined using state-space notation.
Definition: LinearSystem.h:31
const Matrixd< States, Inputs > & B() const
Returns the input matrix B.
Definition: LinearSystem.h:98
LinearSystem & operator=(const LinearSystem &)=default
double D(int i, int j) const
Returns an element of the feedthrough matrix D.
Definition: LinearSystem.h:132
double B(int i, int j) const
Returns an element of the input matrix B.
Definition: LinearSystem.h:106
double A(int i, int j) const
Returns an element of the system matrix A.
Definition: LinearSystem.h:93
StateVector CalculateX(const StateVector &x, const InputVector &clampedU, units::second_t dt) const
Computes the new x given the old x and the control input.
Definition: LinearSystem.h:144
const Matrixd< States, States > & A() const
Returns the system matrix A.
Definition: LinearSystem.h:85
Vectord< Outputs > OutputVector
Definition: LinearSystem.h:35
LinearSystem(LinearSystem &&)=default
LinearSystem & operator=(LinearSystem &&)=default
const Matrixd< Outputs, Inputs > & D() const
Returns the feedthrough matrix D.
Definition: LinearSystem.h:124
OutputVector CalculateY(const StateVector &x, const InputVector &clampedU) const
Computes the new y given the control input.
Definition: LinearSystem.h:162
Vectord< States > StateVector
Definition: LinearSystem.h:33
const Matrixd< Outputs, States > & C() const
Returns the output matrix C.
Definition: LinearSystem.h:111
double C(int i, int j) const
Returns an element of the output matrix C.
Definition: LinearSystem.h:119
LinearSystem(const LinearSystem &)=default
LinearSystem(const Matrixd< States, States > &A, const Matrixd< States, Inputs > &B, const Matrixd< Outputs, States > &C, const Matrixd< Outputs, Inputs > &D)
Constructs a discrete plant with the given continuous system coefficients.
Definition: LinearSystem.h:46
Vectord< Inputs > InputVector
Definition: LinearSystem.h:34
Definition: AprilTagPoseEstimator.h:15
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12