WPILibC++ 2024.3.2
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 = ClampInput(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) {
98 m_u(row, 0) = value;
100 }
101
102 /**
103 * Sets the system state.
104 *
105 * @param state The new state.
106 */
108
109 /**
110 * Returns the current drawn by this simulated system. Override this method to
111 * add a custom current calculation.
112 *
113 * @return The current drawn by this simulated mechanism.
114 */
115 virtual units::ampere_t GetCurrentDraw() const { return 0_A; }
116
117 protected:
118 /**
119 * Updates the state estimate of the system.
120 *
121 * @param currentXhat The current state estimate.
122 * @param u The system inputs (usually voltage).
123 * @param dt The time difference between controller updates.
124 */
125 virtual Vectord<States> UpdateX(const Vectord<States>& currentXhat,
126 const Vectord<Inputs>& u,
127 units::second_t dt) {
128 return m_plant.CalculateX(currentXhat, u, dt);
129 }
130
131 /**
132 * Clamp the input vector such that no element exceeds the given voltage. If
133 * any does, the relative magnitudes of the input will be maintained.
134 *
135 * @param u The input vector.
136 * @return The normalized input.
137 */
139 return frc::DesaturateInputVector<Inputs>(
141 }
142
143 /// The plant that represents the linear system.
145
146 /// State vector.
148
149 /// Input vector.
151
152 /// Output vector.
154
155 /// The standard deviations of measurements, used for adding noise to the
156 /// measurements.
157 std::array<double, Outputs> m_measurementStdDevs;
158};
159} // namespace frc::sim
A plant defined using state-space notation.
Definition: LinearSystem.h:31
static double GetInputVoltage()
Get the input voltage to the robot controller.
This class helps simulate linear systems.
Definition: LinearSystemSim.h:31
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:157
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:150
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:147
LinearSystem< States, Inputs, Outputs > m_plant
The plant that represents the linear system.
Definition: LinearSystemSim.h:144
void SetState(const Vectord< States > &state)
Sets the system state.
Definition: LinearSystemSim.h:107
Vectord< Inputs > ClampInput(Vectord< Inputs > u)
Clamp the input vector such that no element exceeds the given voltage.
Definition: LinearSystemSim.h:138
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:125
Vectord< Outputs > m_y
Output vector.
Definition: LinearSystemSim.h:153
virtual ~LinearSystemSim()=default
void SetInput(int row, double value)
Definition: LinearSystemSim.h:97
virtual units::ampere_t GetCurrentDraw() const
Returns the current drawn by this simulated system.
Definition: LinearSystemSim.h:115
state
Definition: core.h:2271
Definition: XboxControllerSim.h:13
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12