Shooter Example

intermediate95 min

Template Project

Use the Command Robot template and add a Shooter subsystem under subsystems.

Overview

Shooters use velocity control: hold a target RPM while load changes when game pieces leave the flywheel. This example sets coast mode, velocity PID, and kV feedforward on one motor.

Goals

The shooter subsystem must:

  • Spin to target RPM (Close, Mid, Far).
  • Maintain speed (Velocity PID).
  • Recover quickly after shots.
  • Report “Ready” status.

Step 1: Create Subsystem Structure

Create Shooter 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.VelocityVoltage;

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

Step 2: Declare Motor and Control Objects

Motor on CAN ID 7. Talon FX: VelocityVoltage request. SPARK MAX: closed-loop controller and encoder.

public class Shooter extends SubsystemBase {
    // Shooter motor on CAN ID 7
    private final TalonFX m_motor = new TalonFX(7);
    // Velocity control request
    private final VelocityVoltage m_request = new VelocityVoltage(0);
}

Step 3: Configure Motor - Idle Mode

Use Coast so the flywheel spins down gradually instead of braking hard.

public Shooter() {
    TalonFXConfiguration config = new TalonFXConfiguration();
    // Coast mode allows flywheel to spin down naturally
    config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
    m_motor.getConfigurator().apply(config);
}

Step 4: Configure Velocity PID Gains

Use a small kP; feedforward does most of the work. kI and kD are often zero on flywheels.

public Shooter() {
    TalonFXConfiguration config = new TalonFXConfiguration();
    config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
    
    // Velocity PID gains (Slot 0)
    config.Slot0.kP = 0.1;  // Proportional gain
    config.Slot0.kI = 0.0;  // Integral gain
    config.Slot0.kD = 0.0;  // Derivative gain
    
    m_motor.getConfigurator().apply(config);
}

Step 5: Configure Velocity Feedforward

Tune kV first so the wheel reaches speed in open loop; add kP for load recovery after shots.

public Shooter() {
    TalonFXConfiguration config = new TalonFXConfiguration();
    config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
    config.Slot0.kP = 0.1;
    config.Slot0.kI = 0.0;
    config.Slot0.kD = 0.0;
    
    // Velocity feedforward (critical for maintaining speed)
    config.Slot0.kV = 0.12;
    
    m_motor.getConfigurator().apply(config);
}

Step 6: Add Method to Set Target Speed

setSpeed commands the velocity setpoint (RPS on Talon FX, RPM on SPARK MAX in this example).

/**
 * Set shooter target velocity
 * @param rps Target velocity in rotations per second
 */
public void setSpeed(double rps) {
    m_motor.setControl(m_request.withVelocity(rps));
}

Step 7: Add Ready Status Check

isReady(target) returns true when measured speed is within a tolerance band—use it before feeding.

/**
 * Check if shooter is at target speed
 * @param target Target velocity in rotations per second
 * @return True if within 2.0 RPS of target
 */
public boolean isReady(double target) {
    double currentVelocity = m_motor.getVelocity().getValue();
    return Math.abs(currentVelocity - target) < 2.0;
}

Step 8: Create Firing Sequence Command

Spin up, wait for isReady, then feed:

// In RobotContainer class - add this method or use in configureBindings()

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import edu.wpi.first.wpilibj2.command.WaitUntilCommand;
import frc.robot.subsystems.Shooter;
import frc.robot.subsystems.Indexer;

/**
 * Create command sequence for shooting
 * @return Sequential command: spin up, wait, feed
 */
public Command getShootSequence() {
    return new SequentialCommandGroup(
        // Step 1: Start spinning up shooter to 4000 RPS
        new InstantCommand(() -> m_shooter.setSpeed(4000), m_shooter),
        // Step 2: Wait until shooter reaches target speed
        new WaitUntilCommand(() -> m_shooter.isReady(4000)),
        // Step 3: Feed note into shooter
        new FeedCommand(m_indexer)
    );
}

Understanding Velocity Control

Feedforward holds nominal speed; PID corrects after disturbances. Position is not the control goal—RPM is.

Full Code - Shooter 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.VelocityVoltage;

public class Shooter extends SubsystemBase {
    // Shooter motor on CAN ID 7
    private final TalonFX m_motor = new TalonFX(7);
    // Velocity control request
    private final VelocityVoltage m_request = new VelocityVoltage(0);
    
    public Shooter() {
        TalonFXConfiguration config = new TalonFXConfiguration();
        // Coast mode allows flywheel to spin down naturally
        config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
        
        // Velocity PID gains (Slot 0)
        config.Slot0.kP = 0.1;  // Proportional gain
        config.Slot0.kI = 0.0;  // Integral gain
        config.Slot0.kD = 0.0;  // Derivative gain
        config.Slot0.kV = 0.12; // Velocity feedforward (critical for maintaining speed)
        
        m_motor.getConfigurator().apply(config);
    }
    
    /**
     * Set shooter target velocity
     * @param rps Target velocity in rotations per second
     */
    public void setSpeed(double rps) {
        m_motor.setControl(m_request.withVelocity(rps));
    }
    
    /**
     * Check if shooter is at target speed
     * @param target Target velocity in rotations per second
     * @return True if within 2.0 RPS of target
     */
    public boolean isReady(double target) {
        double currentVelocity = m_motor.getVelocity().getValue();
        return Math.abs(currentVelocity - target) < 2.0;
    }
}

Full Code - Firing Sequence

// In RobotContainer class - add this method

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import edu.wpi.first.wpilibj2.command.WaitUntilCommand;
import frc.robot.subsystems.Shooter;
import frc.robot.subsystems.Indexer;

// The template provides RobotContainer structure
/**
 * Create command sequence for shooting
 * @return Sequential command: spin up, wait, feed
 */
public Command getShootSequence() {
    return new SequentialCommandGroup(
        // Step 1: Start spinning up shooter to 4000 RPS
        new InstantCommand(() -> m_shooter.setSpeed(4000), m_shooter),
        // Step 2: Wait until shooter reaches target speed
        new WaitUntilCommand(() -> m_shooter.isReady(4000)),
        // Step 3: Feed note into shooter
        new FeedCommand(m_indexer)
    );
}

Tuning Tips

For Velocity Control:

  • Feedforward (kV): Most important! Tune this so it reaches target speed (open loop).
  • Proportional (kP): Add small P to correct errors and recover speed after shots.
  • D/I: Usually not needed for flywheels (inertia provides damping).
  • Coast Mode: Prevents rapid deceleration, saving energy and reducing wear.

State Management

Many teams model Idle → SpinUp → Ready → Fire with commands and isReady() before enabling a feeder.

Practice with WPILib Documentation

See WPILib feedforward control for the theory behind kV tuning.

Resources