1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <algorithm>
#include <concepts>
#include <stdexcept>
#include <type_traits>
#include <gcem.hpp>
#include <wpi/Algorithm.h>
#include <wpi/SmallVector.h>
#include "frc/EigenCore.h"
#include "frc/system/Discretization.h"
#include "units/time.h"
namespace frc {
/**
* A plant defined using state-space notation.
*
* A plant is a mathematical model of a system's dynamics.
*
* For more on the underlying math, read
* https://file.tavsys.net/control/controls-engineering-in-frc.pdf.
*
* @tparam States Number of states.
* @tparam Inputs Number of inputs.
* @tparam Outputs Number of outputs.
*/
template <int States, int Inputs, int Outputs>
class LinearSystem {
public:
using StateVector = Vectord<States>;
using InputVector = Vectord<Inputs>;
using OutputVector = Vectord<Outputs>;
/**
* Constructs a discrete plant with the given continuous system coefficients.
*
* @param A System matrix.
* @param B Input matrix.
* @param C Output matrix.
* @param D Feedthrough matrix.
* @throws std::domain_error if any matrix element isn't finite.
*/
constexpr LinearSystem(const Matrixd<States, States>& A,
const Matrixd<States, Inputs>& B,
const Matrixd<Outputs, States>& C,
const Matrixd<Outputs, Inputs>& D) {
auto allFinite = [](const auto& mat) {
if (std::is_constant_evaluated()) {
for (int row = 0; row < mat.rows(); ++row) {
for (int col = 0; col < mat.cols(); ++col) {
if (!gcem::internal::is_finite(mat(row, col))) {
return false;
}
}
}
return true;
} else {
return mat.allFinite();
}
};
if (!allFinite(A)) {
throw std::domain_error(
"Elements of A aren't finite. This is usually due to model "
"implementation errors.");
}
if (!allFinite(B)) {
throw std::domain_error(
"Elements of B aren't finite. This is usually due to model "
"implementation errors.");
}
if (!allFinite(C)) {
throw std::domain_error(
"Elements of C aren't finite. This is usually due to model "
"implementation errors.");
}
if (!allFinite(D)) {
throw std::domain_error(
"Elements of D aren't finite. This is usually due to model "
"implementation errors.");
}
m_A = A;
m_B = B;
m_C = C;
m_D = D;
}
constexpr LinearSystem(const LinearSystem&) = default;
constexpr LinearSystem& operator=(const LinearSystem&) = default;
constexpr LinearSystem(LinearSystem&&) = default;
constexpr LinearSystem& operator=(LinearSystem&&) = default;
/**
* Returns the system matrix A.
*/
constexpr const Matrixd<States, States>& A() const { return m_A; }
/**
* Returns an element of the system matrix A.
*
* @param i Row of A.
* @param j Column of A.
*/
constexpr double A(int i, int j) const { return m_A(i, j); }
/**
* Returns the input matrix B.
*/
constexpr const Matrixd<States, Inputs>& B() const { return m_B; }
/**
* Returns an element of the input matrix B.
*
* @param i Row of B.
* @param j Column of B.
*/
constexpr double B(int i, int j) const { return m_B(i, j); }
/**
* Returns the output matrix C.
*/
constexpr const Matrixd<Outputs, States>& C() const { return m_C; }
/**
* Returns an element of the output matrix C.
*
* @param i Row of C.
* @param j Column of C.
*/
constexpr double C(int i, int j) const { return m_C(i, j); }
/**
* Returns the feedthrough matrix D.
*/
constexpr const Matrixd<Outputs, Inputs>& D() const { return m_D; }
/**
* Returns an element of the feedthrough matrix D.
*
* @param i Row of D.
* @param j Column of D.
*/
constexpr double D(int i, int j) const { return m_D(i, j); }
/**
* Computes the new x given the old x and the control input.
*
* This is used by state observers directly to run updates based on state
* estimate.
*
* @param x The current state.
* @param clampedU The control input.
* @param dt Timestep for model update.
*/
StateVector CalculateX(const StateVector& x, const InputVector& clampedU,
units::second_t dt) const {
Matrixd<States, States> discA;
Matrixd<States, Inputs> discB;
DiscretizeAB<States, Inputs>(m_A, m_B, dt, &discA, &discB);
return discA * x + discB * clampedU;
}
/**
* Computes the new y given the control input.
*
* This is used by state observers directly to run updates based on state
* estimate.
*
* @param x The current state.
* @param clampedU The control input.
*/
OutputVector CalculateY(const StateVector& x,
const InputVector& clampedU) const {
return m_C * x + m_D * clampedU;
}
/**
* Returns the LinearSystem with the outputs listed in outputIndices.
*
* <p>This is used by state observers such as the Kalman Filter.
*
* @param outputIndices the list of output indices to include in the sliced
* system.
* @return the sliced LinearSystem with outputs set to row vectors of
* LinearSystem.
* @throws std::domain_error if any outputIndices are outside the range of
* system outputs.
* @throws std::domain_error if number of outputIndices exceeds the system
* outputs.
* @throws std::domain_error if duplication exists in outputIndices.
*/
template <std::same_as<int>... OutputIndices>
LinearSystem<States, Inputs, sizeof...(OutputIndices)> Slice(
OutputIndices... outputIndices) {
static_assert(sizeof...(OutputIndices) <= Outputs,
"More outputs requested than available. This is usually due "
"to model implementation errors.");
wpi::for_each(
[](size_t i, const auto& elem) {
if (elem < 0 || elem >= Outputs) {
throw std::domain_error(
"Slice indices out of range. This is usually due to model "
"implementation errors.");
}
},
outputIndices...);
// Sort and deduplicate output indices
wpi::SmallVector<int> outputIndicesArray{outputIndices...};
std::sort(outputIndicesArray.begin(), outputIndicesArray.end());
auto last =
std::unique(outputIndicesArray.begin(), outputIndicesArray.end());
outputIndicesArray.erase(last, outputIndicesArray.end());
if (outputIndicesArray.size() != sizeof...(outputIndices)) {
throw std::domain_error(
"Duplicate indices exist. This is usually due to model "
"implementation errors.");
}
return LinearSystem<States, Inputs, sizeof...(OutputIndices)>{
m_A, m_B, m_C(outputIndicesArray, Eigen::placeholders::all),
m_D(outputIndicesArray, Eigen::placeholders::all)};
}
private:
/**
* Continuous system matrix.
*/
Matrixd<States, States> m_A;
/**
* Continuous input matrix.
*/
Matrixd<States, Inputs> m_B;
/**
* Output matrix.
*/
Matrixd<Outputs, States> m_C;
/**
* Feedthrough matrix.
*/
Matrixd<Outputs, Inputs> m_D;
};
} // namespace frc