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