Elevator Example

intermediate95 min

Template Project

Use the Command Robot template and add an Elevator subsystem under subsystems.

Overview

Elevators need position control: move to a height and hold against gravity. This example configures brake mode, PID, gravity feedforward, and motion profiling on one motor.

Goals

The elevator subsystem must:

  • Move to setpoints (Low, Mid, High).
  • Hold position (Brake Mode + PID).
  • Use Motion Profiling (Smooth acceleration).
  • Compensate for gravity (Feedforward).

Step 1: Create Subsystem Structure

Create Elevator extending SubsystemBase.

package frc.robot.subsystems;

import edu.wpi.first.wpilibj2.command.SubsystemBase;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.signals.NeutralModeValue;
import com.ctre.phoenix6.controls.MotionMagicVoltage;

public class Elevator extends SubsystemBase {
    // We'll add fields here
}

Step 2: Declare Motor and Control Objects

Motor on CAN ID 6. Talon FX: reusable MotionMagicVoltage. SPARK MAX: closed-loop controller reference.

public class Elevator extends SubsystemBase {
    // Elevator motor on CAN ID 6
    private final TalonFX m_motor = new TalonFX(6);
    
    // Motion Magic® control request (trapezoidal motion profile)
    private final MotionMagicVoltage m_request = new MotionMagicVoltage(0);
}

Step 3: Configure Motor - Basic Settings

Set Brake idle mode so the carriage holds when not commanded.

public Elevator() {
    TalonFXConfiguration config = new TalonFXConfiguration();
    // Brake mode to hold position against gravity
    config.MotorOutput.NeutralMode = NeutralModeValue.Brake;
    m_motor.getConfigurator().apply(config);
}

Step 4: Configure PID Gains

Set kP and kD for position; kI is usually 0 on elevators. Tune these with the motion profile, not in isolation.

public Elevator() {
    TalonFXConfiguration config = new TalonFXConfiguration();
    config.MotorOutput.NeutralMode = NeutralModeValue.Brake;
    
    // PID gains for position control (Slot 0)
    config.Slot0.kP = 0.5;  // Proportional gain
    config.Slot0.kI = 0.0;  // Integral gain
    config.Slot0.kD = 0.05; // Derivative gain
    
    m_motor.getConfigurator().apply(config);
}

Step 5: Add Feedforward Constants

kG offsets weight; kS offsets static friction at the start of motion.

public Elevator() {
    TalonFXConfiguration config = new TalonFXConfiguration();
    config.MotorOutput.NeutralMode = NeutralModeValue.Brake;
    config.Slot0.kP = 0.5;
    config.Slot0.kI = 0.0;
    config.Slot0.kD = 0.05;
    
    // Feedforward constants
    config.Slot0.kG = 0.12; // Gravity feedforward (compensates for weight)
    config.Slot0.kS = 0.05; // Static friction feedforward
    
    m_motor.getConfigurator().apply(config);
}

Step 6: Configure Motion Profile Constraints

Cap cruise velocity and acceleration so the profile stays smooth and within mechanical limits.

public Elevator() {
    TalonFXConfiguration config = new TalonFXConfiguration();
    config.MotorOutput.NeutralMode = NeutralModeValue.Brake;
    config.Slot0.kP = 0.5;
    config.Slot0.kI = 0.0;
    config.Slot0.kD = 0.05;
    config.Slot0.kG = 0.12;
    config.Slot0.kS = 0.05;
    
    // Motion profile constraints
    config.MotionMagic.MotionMagicCruiseVelocity = 10; // Max velocity (rotations/sec)
    config.MotionMagic.MotionMagicAcceleration = 20;   // Max acceleration (rotations/sec²)
    
    m_motor.getConfigurator().apply(config);
}

Step 7: Add Method to Set Target Height

setHeight(rotations) sends the closed-loop setpoint; the profile and PID do the rest.

/**
 * Set elevator to target height
 * @param rotations Target position in rotations
 */
public void setHeight(double rotations) {
    m_motor.setControl(m_request.withPosition(rotations));
}

Step 8: Create Commands in RobotContainer

Bind buttons with InstantCommand + setHeight():

// In RobotContainer class - update the configureBindings() method

import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.subsystems.Elevator;

// The template provides RobotContainer with configureBindings()
private void configureBindings() {
    // ... other button bindings
    
    // Button A: Move to low position (0 rotations)
    m_controller.a().onTrue(new InstantCommand(() -> m_elevator.setHeight(0.0), m_elevator));
    // Button B: Move to high position (10 rotations)
    m_controller.b().onTrue(new InstantCommand(() -> m_elevator.setHeight(10.0), m_elevator));
}

Understanding the Complete System

setHeight() starts a trapezoidal profile; PID plus feedforward track that profile and hold at the setpoint.

Full Code - Elevator Subsystem

package frc.robot.subsystems;

import edu.wpi.first.wpilibj2.command.SubsystemBase;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.signals.NeutralModeValue;
import com.ctre.phoenix6.controls.MotionMagicVoltage;

public class Elevator extends SubsystemBase {
    // Elevator motor on CAN ID 6
    private final TalonFX m_motor = new TalonFX(6);
    
    // Motion Magic® control request (trapezoidal motion profile)
    private final MotionMagicVoltage m_request = new MotionMagicVoltage(0);

    public Elevator() {
        TalonFXConfiguration config = new TalonFXConfiguration();
        // Brake mode to hold position against gravity
        config.MotorOutput.NeutralMode = NeutralModeValue.Brake;
        
        // PID gains for position control (Slot 0)
        config.Slot0.kP = 0.5;  // Proportional gain
        config.Slot0.kI = 0.0;  // Integral gain
        config.Slot0.kD = 0.05; // Derivative gain
        config.Slot0.kG = 0.12; // Gravity feedforward (compensates for weight)
        config.Slot0.kS = 0.05; // Static friction feedforward
        
        // Motion profile constraints
        config.MotionMagic.MotionMagicCruiseVelocity = 10; // Max velocity (rotations/sec)
        config.MotionMagic.MotionMagicAcceleration = 20;   // Max acceleration (rotations/sec²)
        
        m_motor.getConfigurator().apply(config);
    }
    
    /**
     * Set elevator to target height
     * @param rotations Target position in rotations
     */
    public void setHeight(double rotations) {
        m_motor.setControl(m_request.withPosition(rotations));
    }
}

Full Code - RobotContainer

// In RobotContainer class - update the configureBindings() method

import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.subsystems.Elevator;

// The template provides RobotContainer structure
private void configureBindings() {
    // ... other button bindings
    
    // Button A: Move to low position (0 rotations)
    m_controller.a().onTrue(new InstantCommand(() -> m_elevator.setHeight(0.0), m_elevator));
    // Button B: Move to high position (10 rotations)
    m_controller.b().onTrue(new InstantCommand(() -> m_elevator.setHeight(10.0), m_elevator));
}

Tuning Tips

Order of operations:

  • Gravity (kG): Increase until elevator holds position or falls very slowly (open loop).
  • Proportional (kP): Increase until it reaches target reasonably well.
  • Derivative (kD): Add to stop oscillation/overshoot.
  • Profile: Adjust Max Velocity/Acceleration for speed vs smoothness.

Practice with WPILib Documentation

WPILib’s elevator tuning practice walks through the same tuning order with interactive examples.

Resources