Skip to content
learnFRC.org
programmingexample

Linear Elevator Subsystem (Motion Magic)

Production-tested cascade / continuous elevator subsystem using CTRE Motion Magic, gravity feedforward, and software travel limits.

This production subsystem operates a multi-stage continuous or cascade elevator powered by a Talon FX (Kraken X60 / Falcon 500). It implements onboard Motion Magic with gravity feedforward (kGkG) and strict software travel limits.

package frc.robot.subsystems;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.controls.MotionMagicVoltage;
import com.ctre.phoenix6.controls.VoltageOut;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.signals.GravityTypeValue;
import com.ctre.phoenix6.signals.InvertedValue;
import com.ctre.phoenix6.signals.NeutralModeValue;
import edu.wpi.first.units.measure.Distance;
import edu.wpi.first.units.measure.Voltage;
import static edu.wpi.first.units.Units.*;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
public class ElevatorSubsystem extends SubsystemBase {
private final TalonFX m_motor = new TalonFX(12, "rio");
// Gear ratio and drum spool kinematics
// Example: 25:1 reduction on 1.75 inch pitch diameter drum spool
private static final double ROTATIONS_PER_METER = 25.0 / (Math.PI * 0.04445);
// Reusable control requests
private final MotionMagicVoltage m_motionMagic = new MotionMagicVoltage(0).withSlot(0);
private final VoltageOut m_voltageOut = new VoltageOut(0);
public ElevatorSubsystem() {
configureHardware();
setDefaultCommand(holdPositionCommand());
}
private void configureHardware() {
TalonFXConfiguration cfg = new TalonFXConfiguration();
// 1. Motor Inversion & Brake Mode
cfg.MotorOutput.Inverted = InvertedValue.CounterClockwise_Positive;
cfg.MotorOutput.NeutralMode = NeutralModeValue.Brake;
// 2. Active Safety Limits
cfg.CurrentLimits.StatorCurrentLimitEnable = true;
cfg.CurrentLimits.StatorCurrentLimit = 40.0; // 40A stator limit
cfg.CurrentLimits.SupplyCurrentLimitEnable = true;
cfg.CurrentLimits.SupplyCurrentLimit = 35.0; // 35A breaker limit
// Forward & Reverse Soft Limits (in motor rotations)
cfg.SoftwareLimitSwitch.ForwardSoftLimitEnable = true;
cfg.SoftwareLimitSwitch.ForwardSoftLimitThreshold = 1.2 * ROTATIONS_PER_METER; // 1.2m max height
cfg.SoftwareLimitSwitch.ReverseSoftLimitEnable = true;
cfg.SoftwareLimitSwitch.ReverseSoftLimitThreshold = 0.02 * ROTATIONS_PER_METER; // Just above physical bumper
// 3. Slot 0 Gains (Closed Loop + Gravity Feedforward)
cfg.Slot0.GravityType = GravityTypeValue.Elevator_Static;
cfg.Slot0.kG = 0.42; // 0.42V constant gravity holding voltage
cfg.Slot0.kS = 0.22; // 0.22V friction voltage
cfg.Slot0.kV = 0.11; // Velocity feedforward
cfg.Slot0.kP = 4.50; // Proportional gain
cfg.Slot0.kD = 0.18; // Derivative damping
// 4. Motion Magic Kinematic Constraints
cfg.MotionMagic.MotionMagicCruiseVelocity = 1.8 * ROTATIONS_PER_METER; // 1.8 m/s cruise
cfg.MotionMagic.MotionMagicAcceleration = 3.6 * ROTATIONS_PER_METER; // 3.6 m/s² accel
cfg.MotionMagic.MotionMagicJerk = 36.0 * ROTATIONS_PER_METER; // S-curve jerk
m_motor.getConfigurator().apply(cfg);
m_motor.setPosition(0); // Zero at mechanical bottom
}
@Override
public void periodic() {
double currentHeightMeters = m_motor.getPosition().getValueAsDouble() / ROTATIONS_PER_METER;
SmartDashboard.putNumber("Elevator/Height_m", currentHeightMeters);
SmartDashboard.putNumber("Elevator/StatorCurrent_A", m_motor.getStatorCurrent().getValueAsDouble());
}
/**
* Commands the elevator to move smoothly to a target height using Motion Magic.
*/
public Command moveToHeightCommand(Distance targetHeight) {
double targetRotations = targetHeight.in(Meters) * ROTATIONS_PER_METER;
return run(() -> m_motor.setControl(m_motionMagic.withPosition(targetRotations)))
.until(() -> isAtHeight(targetHeight))
.withName("MoveToHeight");
}
public boolean isAtHeight(Distance targetHeight) {
double currentMeters = m_motor.getPosition().getValueAsDouble() / ROTATIONS_PER_METER;
return Math.abs(currentMeters - targetHeight.in(Meters)) < 0.025; // 2.5 cm tolerance
}
/**
* Default command: continuously holds current position with gravity feedforward.
*/
public Command holdPositionCommand() {
return run(() -> {
// Maintains closed-loop hold on current target
m_motor.setControl(m_motionMagic);
}).withName("HoldPosition");
}
}
Never Disable Soft Limits Without Limit Switches

If you disable soft limits during bench testing, ensure a mechanical deadstop or emergency stop button is within arm’s reach.