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