For the complete documentation index, see llms.txt. This page is also available as Markdown.
Elevator
Learn how to program an Elevator.
Two motors on one shaft or gearbox? Add the second as a follower with withFollowers. Two motors that aren't on the same shaft or gearbox, such as one on each side of the mechanism? Make the second a loosely coupled follower with withLooselyCoupledFollowers: the mechanism still works as this tutorial shows. Followers explains both.
Intro
At the end of this tutorial you will have an Elevator that works in both real life and simulation with the same code!
Simulation
Real Life
Details
This Elevator uses the following hardware and controls:
A SparkMax driving one NEO controls the Elevator
A 12:1 gearbox on the Elevator
#25 chain on a 22 tooth sprocket lifts the carriage
Holding the bottom face button (A on an Xbox controller) takes the Elevator to 0.5 m
Holding the right face button (B on an Xbox controller) takes the Elevator to 1 m
Holding the left face button (X on an Xbox controller) drives the Elevator up
Holding the top face button (Y on an Xbox controller) drives the Elevator down
With no button held, the Elevator goes to 0 m
The steps
The first four steps, one page each. Then you tune it and run it on the robot.
Every step has a tab for Java with Commands v2, Java with Commands v3 and C++.
The arm's robot step, with the elevator differences listed at the end of step 5.
Complete Example
The following files show a complete, working Elevator from the YAMS simple_robot-2027 Commands v2 example. It uses an exponential profile instead of the trapezoidal profile in this tutorial.
// Copyright (c) 2026 Yet Another Software Suite
// SPDX-License-Identifier: LGPL-3.0-or-later
package first.robot.subsystems;
import static org.wpilib.units.Units.Amps;
import static org.wpilib.units.Units.Inches;
import static org.wpilib.units.Units.Meters;
import static org.wpilib.units.Units.Pounds;
import static org.wpilib.units.Units.Rotations;
import static org.wpilib.units.Units.Volts;
import com.ctre.phoenix6.hardware.TalonFX;
import com.revrobotics.util.CANPorts;
import com.revrobotics.spark.SparkLowLevel;
import com.revrobotics.spark.SparkMax;
import org.wpilib.math.controller.ElevatorFeedforward;
import org.wpilib.math.geometry.Translation3d;
import org.wpilib.math.system.DCMotor;
import org.wpilib.units.measure.Distance;
import org.wpilib.units.measure.Mass;
import org.wpilib.command2.Command;
import org.wpilib.command2.SubsystemBase;
import org.wpilib.command2.button.Trigger;
import yams.commands2.config.SmartMotorControllerConfig;
import yams.commands2.mechanisms.Elevator;
import yams.core.gearing.GearBox;
import yams.core.gearing.MechanismGearing;
import yams.core.math.ExponentialProfilePIDController;
import yams.core.mechanisms.config.ElevatorConfig;
import yams.core.mechanisms.config.MechanismPositionConfig;
import yams.core.motorcontrollers.SmartMotorController;
import yams.core.motorcontrollers.enums.ControlMode;
import yams.core.motorcontrollers.enums.MotorMode;
import yams.core.motorcontrollers.local.SparkWrapper;
import yams.core.motorcontrollers.remote.TalonFXWrapper;
import yams.core.telemetry.enums.TelemetryVerbosity;
public class ElevatorSubsystem extends SubsystemBase
{
private final Distance chainPitch = Inches.of(0.25);
private final int toothCount = 22;
private final Distance circumference = chainPitch.times(toothCount);
private final Distance radius = circumference.div(2 * Math.PI);
private final Mass weight = Pounds.of(16);
private final DCMotor motors = DCMotor.getNEO(1);
private final MechanismGearing gearing = new MechanismGearing(GearBox.fromReductionStages(3, 4));
private final SparkMax elevatorMotor = new SparkMax(CANPorts.fromBusId(1), 2, SparkLowLevel.MotorType.kBrushless);
// private final SmartMotorControllerTelemetryConfig motorTelemetryConfig = new SmartMotorControllerTelemetryConfig()
// .withMechanismPosition()
// .withRotorPosition()
// .withMechanismLowerLimit()
// .withMechanismUpperLimit(); // Specific telemetry verbosity
private final SmartMotorControllerConfig motorConfig = new SmartMotorControllerConfig(this)
.withMechanismCircumference(circumference)
// .withFollowers(Pair.of(new SparkMax(3, SparkLowLevel.MotorType.kBrushless), true))
.withClosedLoopController(30, 0, 0)
.withExponentialProfile(ExponentialProfilePIDController
.createElevatorConstraints(Volts.of(12),
motors,
weight,
radius,
gearing))
// .withClosedLoopController(4, 0, 0, MetersPerSecond.of(0.5), MetersPerSecondPerSecond.of(0.5)) // Trapezoidal Profile PID Controller
.withSoftLimits(Meters.of(0), Meters.of(2))
.withStartingPosition(Meters.of(0)) // Required to simulate the elevator
.withGearing(gearing)
// .withExternalEncoder(armMotor.getAbsoluteEncoder()) // External Encoder if you need one, really shouldn't be used for Elevators
.withZeroPower(MotorMode.BRAKE)
.withTelemetry("ElevatorMotor", TelemetryVerbosity.HIGH)
// .withSpecificTelemetry("ElevatorMotor", motorTelemetryConfig) // Specific Telemetry
.withStatorCurrentLimit(Amps.of(40))
// .withVoltageCompensation(Volts.of(12)) // Voltage compensation isn't available on all controllers
.withMotorInverted(false)
// .withClosedLoopRampRate(Seconds.of(0.25)) // Closed Loop Ramp Rate not necessary
// .withOpenLoopRampRate(Seconds.of(0.25)) // Open Loop Ramp Rate not necessary
.withFeedforward(new ElevatorFeedforward(0, 0.1, 0, 0))
.withControlMode(ControlMode.CLOSED_LOOP);
private final SmartMotorController motor = new SparkWrapper(elevatorMotor,
motors,
motorConfig);
private final MechanismPositionConfig m_robotToMechanism = new MechanismPositionConfig()
.withMaxRobotHeight(Meters.of(1.5))
.withMaxRobotLength(Meters.of(0.75))
.withRelativePosition(new Translation3d(Meters.of(-0.25), Meters.of(0), Meters.of(0.5)));
private ElevatorConfig m_config = new ElevatorConfig()
.withHardLimits(Meters.of(0), Meters.of(3))
.withCarriageWeight(weight) // Required to simulate the elevator
.withTelemetry("Elevator", TelemetryVerbosity.HIGH)
.withMechanismPositionConfig(m_robotToMechanism);
private final Elevator m_elevator = new Elevator(m_config, motor);
public ElevatorSubsystem()
{
new Trigger(()->m_elevator.getHeight().lte(Meters.of(0.1)))
.and(()->motor.getMechanismPositionSetpoint().orElse(Rotations.of(1)).isEquivalent(Rotations.of(0)))
.whileTrue(m_elevator.set(0));
}
public void periodic()
{
m_elevator.updateTelemetry();
}
public void simulationPeriodic()
{
m_elevator.simIterate();
}
public Command elevCmd(double dutycycle)
{
return m_elevator.set(dutycycle);
}
public Command setHeight(Distance height)
{
return m_elevator.setHeight(height);
}
}
// Copyright (c) 2026 Yet Another Software Suite
// SPDX-License-Identifier: LGPL-3.0-or-later
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package first.robot;
import static org.wpilib.units.Units.Degrees;
import static org.wpilib.units.Units.Meters;
import static org.wpilib.units.Units.RPM;
import org.wpilib.driverstation.internal.DriverStationBackend;
import org.wpilib.command2.Command;
import org.wpilib.command2.Commands;
import org.wpilib.command2.button.CommandNiDsXboxController;
import first.robot.subsystems.ArmSubsystem;
import first.robot.subsystems.ElevatorSubsystem;
import first.robot.subsystems.ShooterSubsystem;
import first.robot.subsystems.SwerveSubsystem;
public class RobotContainer {
private final SwerveSubsystem drive = new SwerveSubsystem();
private final ArmSubsystem arm = new ArmSubsystem();
private final ElevatorSubsystem elevator = new ElevatorSubsystem();
private final ShooterSubsystem shooter = new ShooterSubsystem();
private final CommandNiDsXboxController xboxController = new CommandNiDsXboxController(0);
public RobotContainer() {
DriverStationBackend.silenceJoystickConnectionAlert(true);
drive.setDefaultCommand(drive.driveWithJoystick(xboxController));
arm.setDefaultCommand(arm.setAngle(Degrees.of(0)));
elevator.setDefaultCommand(elevator.setHeight(Meters.of(0)));
shooter.setDefaultCommand(shooter.setVelocity(RPM.of(0)));
configureBindings();
}
private void configureBindings() {
xboxController.getHID().button(1).whileTrue(arm.setAngle(Degrees.of(30)));
xboxController.getHID().button(2).whileTrue(arm.setAngle(Degrees.of(80)));
xboxController.getHID().button(3).whileTrue(elevator.setHeight(Meters.of(0.5)));
xboxController.getHID().button(4).whileTrue(elevator.setHeight(Meters.of(1.5)));
xboxController.getHID().button(5).whileTrue(shooter.setVelocity(RPM.of(3500)));
xboxController.getHID().button(6).whileTrue(shooter.setVelocity(RPM.of(2000)));
}
public Command getAutonomousCommand() {
return Commands.print("No autonomous command configured");
}
}