WPILibC++ 2024.3.2
ElevatorSim.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/length.h>
10#include <units/mass.h>
11#include <units/velocity.h>
12
15
16namespace frc::sim {
17/**
18 * Represents a simulated elevator mechanism.
19 */
20class ElevatorSim : public LinearSystemSim<2, 1, 1> {
21 public:
22 template <typename Distance>
25
26 template <typename Distance>
30
31 /**
32 * Constructs a simulated elevator mechanism.
33 *
34 * @param plant The linear system that represents the elevator.
35 * This system can be created with
36 * LinearSystemId::ElevatorSystem().
37 * @param gearbox The type of and number of motors in your
38 * elevator gearbox.
39 * @param minHeight The minimum allowed height of the elevator.
40 * @param maxHeight The maximum allowed height of the elevator.
41 * @param simulateGravity Whether gravity should be simulated or not.
42 * @param startingHeight The starting height of the elevator.
43 * @param measurementStdDevs The standard deviation of the measurements.
44 */
45 ElevatorSim(const LinearSystem<2, 1, 1>& plant, const DCMotor& gearbox,
46 units::meter_t minHeight, units::meter_t maxHeight,
47 bool simulateGravity, units::meter_t startingHeight,
48 const std::array<double, 1>& measurementStdDevs = {0.0});
49
50 /**
51 * Constructs a simulated elevator mechanism.
52 *
53 * @param gearbox The type of and number of motors in your
54 * elevator gearbox.
55 * @param gearing The gearing of the elevator (numbers greater
56 * than 1 represent reductions).
57 * @param carriageMass The mass of the elevator carriage.
58 * @param drumRadius The radius of the drum that your cable is
59 * wrapped around.
60 * @param minHeight The minimum allowed height of the elevator.
61 * @param maxHeight The maximum allowed height of the elevator.
62 * @param simulateGravity Whether gravity should be simulated or not.
63 * @param startingHeight The starting height of the elevator.
64 * @param measurementStdDevs The standard deviation of the measurements.
65 */
66 ElevatorSim(const DCMotor& gearbox, double gearing,
67 units::kilogram_t carriageMass, units::meter_t drumRadius,
68 units::meter_t minHeight, units::meter_t maxHeight,
69 bool simulateGravity, units::meter_t startingHeight,
70 const std::array<double, 1>& measurementStdDevs = {0.0});
71
72 /**
73 * Constructs a simulated elevator mechanism.
74 *
75 * @param kV The velocity gain.
76 * @param kA The acceleration gain.
77 * @param gearbox The type of and number of motors in your
78 * elevator gearbox.
79 * @param minHeight The minimum allowed height of the elevator.
80 * @param maxHeight The maximum allowed height of the elevator.
81 * @param simulateGravity Whether gravity should be simulated or not.
82 * @param startingHeight The starting height of the elevator.
83 * @param measurementStdDevs The standard deviation of the measurements.
84 */
85 template <typename Distance>
86 requires std::same_as<units::meter, Distance> ||
87 std::same_as<units::radian, Distance>
88 ElevatorSim(decltype(1_V / Velocity_t<Distance>(1)) kV,
89 decltype(1_V / Acceleration_t<Distance>(1)) kA,
90 const DCMotor& gearbox, units::meter_t minHeight,
91 units::meter_t maxHeight, bool simulateGravity,
92 units::meter_t startingHeight,
93 const std::array<double, 1>& measurementStdDevs = {0.0});
95
96 /**
97 * Sets the elevator's state. The new position will be limited between the
98 * minimum and maximum allowed heights.
99 * @param position The new position
100 * @param velocity The new velocity
101 */
102 void SetState(units::meter_t position, units::meters_per_second_t velocity);
103
104 /**
105 * Returns whether the elevator would hit the lower limit.
106 *
107 * @param elevatorHeight The elevator height.
108 * @return Whether the elevator would hit the lower limit.
109 */
110 bool WouldHitLowerLimit(units::meter_t elevatorHeight) const;
111
112 /**
113 * Returns whether the elevator would hit the upper limit.
114 *
115 * @param elevatorHeight The elevator height.
116 * @return Whether the elevator would hit the upper limit.
117 */
118 bool WouldHitUpperLimit(units::meter_t elevatorHeight) const;
119
120 /**
121 * Returns whether the elevator has hit the lower limit.
122 *
123 * @return Whether the elevator has hit the lower limit.
124 */
125 bool HasHitLowerLimit() const;
126
127 /**
128 * Returns whether the elevator has hit the upper limit.
129 *
130 * @return Whether the elevator has hit the upper limit.
131 */
132 bool HasHitUpperLimit() const;
133
134 /**
135 * Returns the position of the elevator.
136 *
137 * @return The position of the elevator.
138 */
139 units::meter_t GetPosition() const;
140
141 /**
142 * Returns the velocity of the elevator.
143 *
144 * @return The velocity of the elevator.
145 */
146 units::meters_per_second_t GetVelocity() const;
147
148 /**
149 * Returns the elevator current draw.
150 *
151 * @return The elevator current draw.
152 */
153 units::ampere_t GetCurrentDraw() const override;
154
155 /**
156 * Sets the input voltage for the elevator.
157 *
158 * @param voltage The input voltage.
159 */
160 void SetInputVoltage(units::volt_t voltage);
161
162 protected:
163 /**
164 * Updates the state estimate of the elevator.
165 *
166 * @param currentXhat The current state estimate.
167 * @param u The system inputs (voltage).
168 * @param dt The time difference between controller updates.
169 */
170 Vectord<2> UpdateX(const Vectord<2>& currentXhat, const Vectord<1>& u,
171 units::second_t dt) override;
172
173 private:
174 DCMotor m_gearbox;
175 units::meter_t m_minHeight;
176 units::meter_t m_maxHeight;
177 bool m_simulateGravity;
178};
179} // namespace frc::sim
Holds the constants for a DC motor.
Definition: DCMotor.h:20
A plant defined using state-space notation.
Definition: LinearSystem.h:31
Represents a simulated elevator mechanism.
Definition: ElevatorSim.h:20
ElevatorSim(decltype(1_V/Velocity_t< Distance >(1)) kV, decltype(1_V/Acceleration_t< Distance >(1)) kA, const DCMotor &gearbox, units::meter_t minHeight, units::meter_t maxHeight, bool simulateGravity, units::meter_t startingHeight, const std::array< double, 1 > &measurementStdDevs={0.0})
Constructs a simulated elevator mechanism.
bool HasHitUpperLimit() const
Returns whether the elevator has hit the upper limit.
units::meters_per_second_t GetVelocity() const
Returns the velocity of the elevator.
void SetInputVoltage(units::volt_t voltage)
Sets the input voltage for the elevator.
ElevatorSim(const LinearSystem< 2, 1, 1 > &plant, const DCMotor &gearbox, units::meter_t minHeight, units::meter_t maxHeight, bool simulateGravity, units::meter_t startingHeight, const std::array< double, 1 > &measurementStdDevs={0.0})
Constructs a simulated elevator mechanism.
units::ampere_t GetCurrentDraw() const override
Returns the elevator current draw.
bool WouldHitUpperLimit(units::meter_t elevatorHeight) const
Returns whether the elevator would hit the upper limit.
ElevatorSim(const DCMotor &gearbox, double gearing, units::kilogram_t carriageMass, units::meter_t drumRadius, units::meter_t minHeight, units::meter_t maxHeight, bool simulateGravity, units::meter_t startingHeight, const std::array< double, 1 > &measurementStdDevs={0.0})
Constructs a simulated elevator mechanism.
bool HasHitLowerLimit() const
Returns whether the elevator has hit the lower limit.
Vectord< 2 > UpdateX(const Vectord< 2 > &currentXhat, const Vectord< 1 > &u, units::second_t dt) override
Updates the state estimate of the elevator.
units::meter_t GetPosition() const
Returns the position of the elevator.
bool WouldHitLowerLimit(units::meter_t elevatorHeight) const
Returns whether the elevator would hit the lower limit.
void SetState(units::meter_t position, units::meters_per_second_t velocity)
Sets the elevator's state.
This class helps simulate linear systems.
Definition: LinearSystemSim.h:31
void SetState(const Vectord< States > &state)
Sets the system state.
Definition: LinearSystemSim.h:107
Container for values which represent quantities of a given unit.
Definition: base.h:1926
typename units::detail::inverse_impl< U >::type inverse
represents the inverse unit type of class U.
Definition: base.h:1134
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1434
Definition: XboxControllerSim.h:13
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12