13#include <Eigen/Eigenvalues>
36template <std::same_as<
double>... Ts>
39 Matrixd<
sizeof...(Ts),
sizeof...(Ts)> result;
41 for (
int row = 0; row < result.rows(); ++row) {
42 for (
int col = 0; col < result.cols(); ++col) {
44 result(row, col) = 0.0;
50 [&](
int i,
double tolerance) {
51 if (tolerance == std::numeric_limits<double>::infinity()) {
54 result(i, i) = 1.0 / (tolerance * tolerance);
74template <std::same_as<
double>... Ts>
76 Matrixd<
sizeof...(Ts),
sizeof...(Ts)> result;
78 for (
int row = 0; row < result.rows(); ++row) {
79 for (
int col = 0; col < result.cols(); ++col) {
81 result(row, col) = 0.0;
86 wpi::for_each([&](
int i,
double stdDev) { result(i, i) = stdDev * stdDev; },
109 for (
int row = 0; row < result.rows(); ++row) {
110 for (
int col = 0; col < result.cols(); ++col) {
112 if (costs[row] == std::numeric_limits<double>::infinity()) {
113 result(row, col) = 0.0;
115 result(row, col) = 1.0 / (costs[row] * costs[row]);
118 result(row, col) = 0.0;
140 const std::span<const double> costs);
158 for (
int row = 0; row < result.rows(); ++row) {
159 for (
int col = 0; col < result.cols(); ++col) {
161 result(row, col) = stdDevs[row] * stdDevs[row];
163 result(row, col) = 0.0;
184 const std::span<const double> stdDevs);
186template <std::same_as<
double>... Ts>
188 std::random_device rd;
189 std::mt19937 gen{rd()};
193 [&](
int i,
double stdDev) {
199 std::normal_distribution distr{0.0, stdDev};
200 result(i) = distr(gen);
217 std::random_device rd;
218 std::mt19937 gen{rd()};
221 for (
size_t i = 0; i < stdDevs.size(); ++i) {
224 if (stdDevs[i] == 0.0) {
227 std::normal_distribution distr{0.0, stdDevs[i]};
228 result(i) = distr(gen);
243 const std::span<const double> stdDevs);
255[[deprecated(
"Use Pose2d.ToMatrix() instead.")]]
272[[deprecated(
"Use Pose2d.ToMatrix() instead.")]]
291template <
int States,
int Inputs>
294 Eigen::EigenSolver<Matrixd<States, States>> es{A,
false};
296 for (
int i = 0; i < A.rows(); ++i) {
297 if (std::norm(es.eigenvalues()[i]) < 1) {
301 if constexpr (States != Eigen::Dynamic && Inputs != Eigen::Dynamic) {
302 Eigen::Matrix<std::complex<double>, States, States + Inputs> E;
303 E << es.eigenvalues()[i] * Eigen::Matrix<std::complex<double>, States,
304 States>::Identity() -
308 Eigen::ColPivHouseholderQR<
309 Eigen::Matrix<std::complex<double>, States, States + Inputs>>
311 if (qr.rank() < States) {
315 Eigen::MatrixXcd E{A.rows(), A.rows() + B.cols()};
316 E << es.eigenvalues()[i] *
317 Eigen::MatrixXcd::Identity(A.rows(), A.rows()) -
321 Eigen::ColPivHouseholderQR<Eigen::MatrixXcd> qr{E};
322 if (qr.rank() < A.rows()) {
336 const Eigen::MatrixXd& B);
350template <
int States,
int Outputs>
358 const Eigen::MatrixXd& C);
370[[deprecated(
"Use Pose2d.ToMatrix() instead.")]]
372 return Eigen::Vector3d{
390 for (
int i = 0; i < u.rows(); ++i) {
391 result(i) = std::clamp(u(i), umin(i), umax(i));
398 const Eigen::VectorXd& umin,
399 const Eigen::VectorXd& umax);
412 double maxMagnitude) {
413 double maxValue = u.template lpNorm<Eigen::Infinity>();
415 if (maxValue > maxMagnitude) {
416 return u * maxMagnitude / maxValue;
423 double maxMagnitude);
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.h:28
constexpr const Rotation2d & Rotation() const
Returns the underlying rotation.
Definition Pose2d.h:128
constexpr units::meter_t Y() const
Returns the Y component of the pose's translation.
Definition Pose2d.h:121
constexpr units::meter_t X() const
Returns the X component of the pose's translation.
Definition Pose2d.h:114
constexpr const Translation2d & Translation() const
Returns the underlying translation.
Definition Pose2d.h:107
constexpr double Cos() const
Returns the cosine of the rotation.
Definition Rotation2d.h:222
constexpr double Sin() const
Returns the sine of the rotation.
Definition Rotation2d.h:229
constexpr units::radian_t Radians() const
Returns the radian value of the rotation constrained within [-π, π].
Definition Rotation2d.h:206
constexpr units::meter_t X() const
Returns the X component of the translation.
Definition Translation2d.h:83
constexpr units::meter_t Y() const
Returns the Y component of the translation.
Definition Translation2d.h:90
Definition SystemServer.h:9
WPILIB_DLLEXPORT constexpr Eigen::Vector3d PoseTo3dVector(const Pose2d &pose)
Converts a Pose2d into a vector of [x, y, theta].
Definition StateSpaceUtil.h:256
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
template WPILIB_DLLEXPORT bool IsStabilizable< 2, 1 >(const Matrixd< 2, 2 > &A, const Matrixd< 2, 1 > &B)
Eigen::Vector< double, Size > Vectord
Definition EigenCore.h:12
template WPILIB_DLLEXPORT bool IsStabilizable< Eigen::Dynamic, Eigen::Dynamic >(const Eigen::MatrixXd &A, const Eigen::MatrixXd &B)
bool IsStabilizable(const Matrixd< States, States > &A, const Matrixd< States, Inputs > &B)
Returns true if (A, B) is a stabilizable pair.
Definition StateSpaceUtil.h:292
constexpr Vectord< Inputs > ClampInputMaxMagnitude(const Vectord< Inputs > &u, const Vectord< Inputs > &umin, const Vectord< Inputs > &umax)
Clamps input vector between system's minimum and maximum allowable input.
Definition StateSpaceUtil.h:386
Vectord< sizeof...(Ts)> MakeWhiteNoiseVector(Ts... stdDevs)
Definition StateSpaceUtil.h:187
template WPILIB_DLLEXPORT bool IsDetectable< Eigen::Dynamic, Eigen::Dynamic >(const Eigen::MatrixXd &A, const Eigen::MatrixXd &C)
constexpr Matrixd< sizeof...(Ts), sizeof...(Ts)> MakeCovMatrix(Ts... stdDevs)
Creates a covariance matrix from the given vector for use with Kalman filters.
Definition StateSpaceUtil.h:75
bool IsDetectable(const Matrixd< States, States > &A, const Matrixd< Outputs, States > &C)
Returns true if (A, C) is a detectable pair.
Definition StateSpaceUtil.h:351
template WPILIB_DLLEXPORT bool IsStabilizable< 1, 1 >(const Matrixd< 1, 1 > &A, const Matrixd< 1, 1 > &B)
WPILIB_DLLEXPORT constexpr Eigen::Vector3d PoseToVector(const Pose2d &pose)
Converts a Pose2d into a vector of [x, y, theta].
Definition StateSpaceUtil.h:371
template WPILIB_DLLEXPORT Eigen::VectorXd ClampInputMaxMagnitude< Eigen::Dynamic >(const Eigen::VectorXd &u, const Eigen::VectorXd &umin, const Eigen::VectorXd &umax)
WPILIB_DLLEXPORT constexpr Eigen::Vector4d PoseTo4dVector(const Pose2d &pose)
Converts a Pose2d into a vector of [x, y, std::cos(theta), std::sin(theta)].
Definition StateSpaceUtil.h:273
template WPILIB_DLLEXPORT Eigen::VectorXd DesaturateInputVector< Eigen::Dynamic >(const Eigen::VectorXd &u, double maxMagnitude)
Vectord< Inputs > DesaturateInputVector(const Vectord< Inputs > &u, double maxMagnitude)
Renormalize all inputs if any exceeds the maximum magnitude.
Definition StateSpaceUtil.h:411
constexpr Matrixd< sizeof...(Ts), sizeof...(Ts)> MakeCostMatrix(Ts... tolerances)
Creates a cost matrix from the given vector for use with LQR.
Definition StateSpaceUtil.h:37
constexpr void for_each(F &&f, Ts &&... elems)
Calls f(i, elem) for each element of elems where i is the index of the element in elems and elem is t...
Definition Algorithm.h:29