Intake (Command-Based)

beginner80 min

Template Project

Start from the Command Robot WPILib template (Robot.java, RobotContainer.java, and example subsystems). In VS Code: WPILib: Create a new project → Command Robot.

Overview

This walkthrough builds a one-motor intake as a subsystem, wires it through RobotContainer, and uses default commands so the roller stops when no button is held.

Step 1: Create the Subsystem Class

Create Intake extending SubsystemBase. All intake hardware code lives here.

Step 1a: Subsystem Structure

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.DutyCycleOut;

public class Intake extends SubsystemBase {
    // We'll add fields and methods here
}

Step 1b: Declare Motor Field

Add the motor on CAN ID 5. On Talon FX, add a reusable DutyCycleOut control request.

public class Intake extends SubsystemBase {
    // Intake motor on CAN ID 5
    private final TalonFX m_motor = new TalonFX(5);
    private final DutyCycleOut m_out = new DutyCycleOut(0);
}

Step 1c: Configure Motor in Constructor

In the constructor, set Coast idle mode and enable current limiting.

public Intake() {
    // Configure motor for intake use
    TalonFXConfiguration config = new TalonFXConfiguration();
    // Coast mode allows free spinning when stopped
    config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
    // Enable current limiting to protect motor
    config.CurrentLimits.StatorCurrentLimitEnable = true;
    config.CurrentLimits.StatorCurrentLimit = 30.0;
    m_motor.getConfigurator().apply(config);
}

Step 1d: Add Public Methods

Add run and stop so commands never touch the motor object directly.

/**
 * Run the intake at the specified speed
 * @param speed Motor speed (-1.0 to 1.0, positive = collect)
 */
public void run(double speed) {
    m_motor.setControl(m_out.withOutput(speed));
}

/**
 * Stop the intake motor
 */
public void stop() {
    m_motor.setControl(m_out.withOutput(0.0));
}

Step 2: Set Up RobotContainer

Update the template RobotContainer to construct the intake and bind buttons.

Step 2a: RobotContainer Structure

package frc.robot;

import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.subsystems.Intake;

public class RobotContainer {
    // The template provides ExampleSubsystem - replace with your subsystems
    // We'll add fields here next
}

Step 2b: Create Subsystem and Controller

public class RobotContainer {
    // Create subsystem instance
    private final Intake m_intake = new Intake();
    // Create controller on USB port 1
    private final CommandXboxController m_controller = new CommandXboxController(1);
}

Step 2c: Configure Button Bindings

Set a default command that stops the intake, then bind A (forward while held) and B (reverse while held).

public RobotContainer() {
    // Default command: Stop intake when no other command is running
    m_intake.setDefaultCommand(new RunCommand(() -> m_intake.stop(), m_intake));

    // Button bindings
    // Button A: Run intake forward (collect) while held
    m_controller.a().whileTrue(new RunCommand(() -> m_intake.run(0.6), m_intake));
    // Button B: Run intake reverse (eject) while held
    m_controller.b().whileTrue(new RunCommand(() -> m_intake.run(-0.4), m_intake));
}

Understanding Default Commands

When no other command requires the intake, the default command runs and commands zero—so the motor does not keep the last speed after you release a button.

Full Code - Intake 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.DutyCycleOut;

public class Intake extends SubsystemBase {
    // Intake motor on CAN ID 5
    private final TalonFX m_motor = new TalonFX(5);
    private final DutyCycleOut m_out = new DutyCycleOut(0);

    public Intake() {
        // Configure motor for intake use
        TalonFXConfiguration config = new TalonFXConfiguration();
        // Coast mode allows free spinning when stopped
        config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
        // Enable current limiting to protect motor
        config.CurrentLimits.StatorCurrentLimitEnable = true;
        config.CurrentLimits.StatorCurrentLimit = 30.0;
        m_motor.getConfigurator().apply(config);
    }

    /**
     * Run the intake at the specified speed
     * @param speed Motor speed (-1.0 to 1.0, positive = collect)
     */
    public void run(double speed) {
        m_motor.setControl(m_out.withOutput(speed));
    }

    /**
     * Stop the intake motor
     */
    public void stop() {
        m_motor.setControl(m_out.withOutput(0.0));
    }
}

Full Code - RobotContainer

package frc.robot;

import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.subsystems.Intake;

public class RobotContainer {
    // Create subsystem instance
    private final Intake m_intake = new Intake();
    // The template uses Constants.OperatorConstants.kDriverControllerPort
    // For this example, we'll use port 1 directly
    private final CommandXboxController m_controller = new CommandXboxController(1);

    public RobotContainer() {
        // Configure button bindings (template provides configureBindings() method)
        configureBindings();
        
        // Default command: Stop intake when no other command is running
        m_intake.setDefaultCommand(new RunCommand(() -> m_intake.stop(), m_intake));
    }
    
    private void configureBindings() {
        // Button A: Run intake forward (collect) while held
        m_controller.a().whileTrue(new RunCommand(() -> m_intake.run(0.6), m_intake));
        // Button B: Run intake reverse (eject) while held
        m_controller.b().whileTrue(new RunCommand(() -> m_intake.run(-0.4), m_intake));
    }
}

Benefits

Why this is better:

  • Encapsulation: Hardware details are hidden in the Subsystem.
  • Readability: RobotContainer clearly shows controls.
  • Safety: Default commands ensure motors stop.
  • Flexibility: Easy to reuse the intake in Auto.

Resources