How do I set an elevator's target height?
Goal: send your Elevator to a height from a button, an autonomous routine or your own code.
This page assumes you already have an Elevator in a subsystem. If you do not, build one with the Elevator tutorial first; it ends with complete files you can copy.
An elevator works the same way as an arm: How do I set an arm's target angle? explains run, runTo and the setpoint method, the default command and the Commands v3 names. This page covers only what differs.
What differs from an arm
The target is a
Distance(org.wpilib.units.measure.Distance), such asMeters.of(1)orInches.of(30), not anAngle. The tolerance ofrunTois aDistancetoo, such asCentimeters.of(2).The setpoint method is
setMeasurementPositionSetpoint(height), the method for a distance, notsetMechanismPositionSetpoint.YAMS needs the drum or sprocket size to turn meters into motor rotations:
withDrumRadius(...)orwithMechanismCircumference(...)in theSmartMotorControllerConfig. See How do I tell YAMS my drum or sprocket size?The carriage needs
kGat every height, since gravity pulls it down the same wherever it is, where an arm'skGis scaled by the cosine of its angle.
Add the commands and bind them
The Elevator tutorial sets .withSoftLimits(Meters.of(0), Meters.of(2)), so every target here is between 0 and 2 meters.
/** Go to a height and hold it. Never ends on its own. */
public Command run(Distance height) {
return elevator.run(height);
}
/** Go to a height, then end once the carriage is within tolerance. */
public Command runTo(Distance height, Distance tolerance) {
return elevator.runTo(height, tolerance);
}
/** Set the target from your own code. */
public void setHeightSetpoint(Distance height) {
elevator.setMeasurementPositionSetpoint(height);
}m_exampleSubsystem.setDefaultCommand(m_exampleSubsystem.run(Meters.of(0)));
m_driverController.a().whileTrue(m_exampleSubsystem.run(Meters.of(0.5)));
m_driverController.b().whileTrue(m_exampleSubsystem.run(Meters.of(1)));
m_driverController.x().onTrue(m_exampleSubsystem.runTo(Meters.of(1), Centimeters.of(2)));/** Go to a height and hold it. Never ends on its own. */
public Command setHeight(Distance height) {
return elevator.run(height);
}
/** Go to a height, then end once the carriage is within tolerance. */
public Command moveTo(Distance height, Distance tolerance) {
return elevator.runTo(height, tolerance);
}
/** Hold a height at the lowest priority, so any other command can take over. For the default command. */
public Command hold(Distance height) {
return run(coroutine -> coroutine.await(elevator.run(height)))
.withPriority(Command.LOWEST_PRIORITY)
.named("Elevator Hold");
}
/** Set the target from your own code. */
public void setHeightSetpoint(Distance height) {
elevator.setMeasurementPositionSetpoint(height);
}Check it
Start the simulation (or enable the robot) and open NetworkTables > Mechanisms > Elevator > mechanism in your dashboard.
Elevatoris the name you gaveElevatorConfig.withTelemetry.Hold B. The drawn carriage should rise to 1 meter and stay there.
Release B. The default command takes over and the carriage returns to the bottom.
If it did not work
The carriage stops short of the target. The target is outside your soft limits. YAMS clamps the setpoint to the nearest limit and prints a warning in the Driver Station. A
runToto a clamped target never ends.The carriage sags below the target, or slides down when you let go.
kGis still 0. The tutorial's gains are placeholders. FindkGfirst (the voltage that holds the carriage still), thenkP, with the Elevator tutorial's Tune it step.The carriage goes to the wrong height, off by the same factor every time. The drum radius, circumference or cascading stage count is wrong. See How do I tell YAMS my drum or sprocket size?
Something else. See Troubleshooting.
Last updated
Was this helpful?
