WPILibC++ 2023.4.3-108-ge5452e3
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 /**
23 * Constructs a simulated elevator mechanism.
24 *
25 * @param plant The linear system that represents the elevator.
26 * @param gearbox The type of and number of motors in your
27 * elevator gearbox.
28 * @param gearing The gearing of the elevator (numbers greater
29 * than 1 represent reductions).
30 * @param drumRadius The radius of the drum that your cable is
31 * wrapped around.
32 * @param minHeight The minimum allowed height of the elevator.
33 * @param maxHeight The maximum allowed height of the elevator.
34 * @param simulateGravity Whether gravity should be simulated or not.
35 * @param measurementStdDevs The standard deviation of the measurements.
36 */
37 ElevatorSim(const LinearSystem<2, 1, 1>& plant, const DCMotor& gearbox,
38 double gearing, units::meter_t drumRadius,
39 units::meter_t minHeight, units::meter_t maxHeight,
40 bool simulateGravity,
41 const std::array<double, 1>& measurementStdDevs = {0.0});
42
43 /**
44 * Constructs a simulated elevator mechanism.
45 *
46 * @param gearbox The type of and number of motors in your
47 * elevator gearbox.
48 * @param gearing The gearing of the elevator (numbers greater
49 * than 1 represent reductions).
50 * @param carriageMass The mass of the elevator carriage.
51 * @param drumRadius The radius of the drum that your cable is
52 * wrapped around.
53 * @param minHeight The minimum allowed height of the elevator.
54 * @param maxHeight The maximum allowed height of the elevator.
55 * @param simulateGravity Whether gravity should be simulated or not.
56 * @param measurementStdDevs The standard deviation of the measurements.
57 */
58 ElevatorSim(const DCMotor& gearbox, double gearing,
59 units::kilogram_t carriageMass, units::meter_t drumRadius,
60 units::meter_t minHeight, units::meter_t maxHeight,
61 bool simulateGravity,
62 const std::array<double, 1>& measurementStdDevs = {0.0});
63
64 /**
65 * Returns whether the elevator would hit the lower limit.
66 *
67 * @param elevatorHeight The elevator height.
68 * @return Whether the elevator would hit the lower limit.
69 */
70 bool WouldHitLowerLimit(units::meter_t elevatorHeight) const;
71
72 /**
73 * Returns whether the elevator would hit the upper limit.
74 *
75 * @param elevatorHeight The elevator height.
76 * @return Whether the elevator would hit the upper limit.
77 */
78 bool WouldHitUpperLimit(units::meter_t elevatorHeight) const;
79
80 /**
81 * Returns whether the elevator has hit the lower limit.
82 *
83 * @return Whether the elevator has hit the lower limit.
84 */
85 bool HasHitLowerLimit() const;
86
87 /**
88 * Returns whether the elevator has hit the upper limit.
89 *
90 * @return Whether the elevator has hit the upper limit.
91 */
92 bool HasHitUpperLimit() const;
93
94 /**
95 * Returns the position of the elevator.
96 *
97 * @return The position of the elevator.
98 */
99 units::meter_t GetPosition() const;
100
101 /**
102 * Returns the velocity of the elevator.
103 *
104 * @return The velocity of the elevator.
105 */
106 units::meters_per_second_t GetVelocity() const;
107
108 /**
109 * Returns the elevator current draw.
110 *
111 * @return The elevator current draw.
112 */
113 units::ampere_t GetCurrentDraw() const override;
114
115 /**
116 * Sets the input voltage for the elevator.
117 *
118 * @param voltage The input voltage.
119 */
120 void SetInputVoltage(units::volt_t voltage);
121
122 protected:
123 /**
124 * Updates the state estimate of the elevator.
125 *
126 * @param currentXhat The current state estimate.
127 * @param u The system inputs (voltage).
128 * @param dt The time difference between controller updates.
129 */
130 Vectord<2> UpdateX(const Vectord<2>& currentXhat, const Vectord<1>& u,
131 units::second_t dt) override;
132
133 private:
134 DCMotor m_gearbox;
135 units::meter_t m_drumRadius;
136 units::meter_t m_minHeight;
137 units::meter_t m_maxHeight;
138 double m_gearing;
139 bool m_simulateGravity;
140};
141} // 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
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, double gearing, units::meter_t drumRadius, units::meter_t minHeight, units::meter_t maxHeight, bool simulateGravity, 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.
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.
ElevatorSim(const DCMotor &gearbox, double gearing, units::kilogram_t carriageMass, units::meter_t drumRadius, units::meter_t minHeight, units::meter_t maxHeight, bool simulateGravity, const std::array< double, 1 > &measurementStdDevs={0.0})
Constructs a simulated elevator mechanism.
This class helps simulate linear systems.
Definition: LinearSystemSim.h:31
Definition: BatterySim.h:14
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12