WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
LinearSystemSim.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 <array>
8
13#include "wpi/units/time.hpp"
14
15namespace wpi::sim {
16/**
17 * This class helps simulate linear systems. To use this class, do the following
18 * in the simulationPeriodic() method.
19 *
20 * Call the SetInput() method with the inputs to your system (generally
21 * voltage). Call the Update() method to update the simulation. Set simulated
22 * sensor readings with the simulated positions in the GetOutput() method.
23 *
24 * @tparam States Number of states of the system.
25 * @tparam Inputs Number of inputs to the system.
26 * @tparam Outputs Number of outputs of the system.
27 */
28template <int States, int Inputs, int Outputs>
30 public:
31 /**
32 * Creates a simulated generic linear system.
33 *
34 * @param system The system to simulate.
35 * @param measurementStdDevs The standard deviations of the measurements.
36 */
39 const std::array<double, Outputs>& measurementStdDevs = {})
40 : m_plant(system), m_measurementStdDevs(measurementStdDevs) {
44 }
45
46 virtual ~LinearSystemSim() = default;
47
48 /**
49 * Updates the simulation.
50 *
51 * @param dt The time between updates.
52 */
53 void Update(wpi::units::second_t dt) {
54 // Update x. By default, this is the linear system dynamics xₖ₊₁ = Axₖ +
55 // Buₖ.
56 m_x = UpdateX(m_x, m_u, dt);
57
58 // yₖ = Cxₖ + Duₖ
59 m_y = m_plant.CalculateY(m_x, m_u);
60
61 // Add noise. If the user did not pass a noise vector to the
62 // constructor, then this method will not do anything because
63 // the standard deviations default to zero.
65 }
66
67 /**
68 * Returns the current output of the plant.
69 *
70 * @return The current output of the plant.
71 */
72 const wpi::math::Vectord<Outputs>& GetOutput() const { return m_y; }
73
74 /**
75 * Returns an element of the current output of the plant.
76 *
77 * @param row The row to return.
78 * @return An element of the current output of the plant.
79 */
80 double GetOutput(int row) const { return m_y(row); }
81
82 /**
83 * Sets the system inputs (usually voltages).
84 *
85 * @param u The system inputs.
86 */
87 void SetInput(const wpi::math::Vectord<Inputs>& u) { m_u = u; }
88
89 /**
90 * Sets the system inputs.
91 *
92 * @param row The row in the input matrix to set.
93 * @param value The value to set the row to.
94 */
95 void SetInput(int row, double value) { m_u(row, 0) = value; }
96
97 /**
98 * Returns the current input of the plant.
99 *
100 * @return The current input of the plant.
101 */
102 const wpi::math::Vectord<Inputs>& GetInput() const { return m_u; }
103
104 /**
105 * Returns an element of the current input of the plant.
106 *
107 * @param row The row to return.
108 * @return An element of the current input of the plant.
109 */
110 double GetInput(int row) const { return m_u(row); }
111
112 /**
113 * Sets the system state.
114 *
115 * @param state The new state.
116 */
118 m_x = state;
119
120 // Update the output to reflect the new state.
121 //
122 // yₖ = Cxₖ + Duₖ
123 m_y = m_plant.CalculateY(m_x, m_u);
124 }
125
126 protected:
127 /**
128 * Updates the state estimate of the system.
129 *
130 * @param currentXhat The current state estimate.
131 * @param u The system inputs (usually voltage).
132 * @param dt The time difference between controller updates.
133 */
135 const wpi::math::Vectord<States>& currentXhat,
136 const wpi::math::Vectord<Inputs>& u, wpi::units::second_t dt) {
137 return m_plant.CalculateX(currentXhat, u, dt);
138 }
139
140 /**
141 * Clamp the input vector such that no element exceeds the given voltage. If
142 * any does, the relative magnitudes of the input will be maintained.
143 *
144 * @param maxInput The maximum magnitude of the input vector after clamping.
145 */
146 void ClampInput(double maxInput) {
148 }
149
150 /// The plant that represents the linear system.
152
153 /// State vector.
155
156 /// Input vector.
158
159 /// Output vector.
161
162 /// The standard deviations of measurements, used for adding noise to the
163 /// measurements.
164 std::array<double, Outputs> m_measurementStdDevs;
165};
166} // namespace wpi::sim
A plant defined using state-space notation.
Definition LinearSystem.hpp:35
const wpi::math::Vectord< Inputs > & GetInput() const
Returns the current input of the plant.
Definition LinearSystemSim.hpp:102
void ClampInput(double maxInput)
Clamp the input vector such that no element exceeds the given voltage.
Definition LinearSystemSim.hpp:146
virtual wpi::math::Vectord< States > UpdateX(const wpi::math::Vectord< States > &currentXhat, const wpi::math::Vectord< Inputs > &u, wpi::units::second_t dt)
Updates the state estimate of the system.
Definition LinearSystemSim.hpp:134
wpi::math::Vectord< Outputs > m_y
Output vector.
Definition LinearSystemSim.hpp:160
void Update(wpi::units::second_t dt)
Updates the simulation.
Definition LinearSystemSim.hpp:53
const wpi::math::Vectord< Outputs > & GetOutput() const
Returns the current output of the plant.
Definition LinearSystemSim.hpp:72
wpi::math::LinearSystem< States, Inputs, Outputs > m_plant
The plant that represents the linear system.
Definition LinearSystemSim.hpp:151
void SetInput(const wpi::math::Vectord< Inputs > &u)
Sets the system inputs (usually voltages).
Definition LinearSystemSim.hpp:87
LinearSystemSim(const wpi::math::LinearSystem< States, Inputs, Outputs > &system, const std::array< double, Outputs > &measurementStdDevs={})
Creates a simulated generic linear system.
Definition LinearSystemSim.hpp:37
double GetInput(int row) const
Returns an element of the current input of the plant.
Definition LinearSystemSim.hpp:110
wpi::math::Vectord< Inputs > m_u
Input vector.
Definition LinearSystemSim.hpp:157
std::array< double, Outputs > m_measurementStdDevs
The standard deviations of measurements, used for adding noise to the measurements.
Definition LinearSystemSim.hpp:164
wpi::math::Vectord< States > m_x
State vector.
Definition LinearSystemSim.hpp:154
void SetState(const wpi::math::Vectord< States > &state)
Sets the system state.
Definition LinearSystemSim.hpp:117
double GetOutput(int row) const
Returns an element of the current output of the plant.
Definition LinearSystemSim.hpp:80
void SetInput(int row, double value)
Sets the system inputs.
Definition LinearSystemSim.hpp:95
virtual ~LinearSystemSim()=default
Eigen::Vector< double, Inputs > DesaturateInputVector(const Eigen::Vector< double, Inputs > &u, double maxMagnitude)
Renormalize all inputs if any exceeds the maximum magnitude.
Definition StateSpaceUtil.hpp:195
Eigen::Vector< double, sizeof...(Ts)> Normal(Ts... stdDevs)
Creates a vector of normally distributed random values with the given standard deviations for each el...
Definition Normal.hpp:28
Eigen::Vector< double, Size > Vectord
Definition EigenCore.hpp:12
Definition CTREPCMSim.hpp:13