Skip to content
learnFRC.org
programmingexample

Swerve Drive Subsystem (CTRE Phoenix 6)

Production-grade swerve drivetrain subsystem with field-oriented drive, heading lock, telemetry, and PathPlanner auto support.

This production subsystem implementation uses the CTRE Phoenix 6 Swerve API, driving 4 independent modules equipped with Kraken X60 or Falcon 500 motors, CANcoders, and a Pigeon 2.0 IMU.

package frc.robot.subsystems;
import com.ctre.phoenix6.hardware.Pigeon2;
import com.ctre.phoenix6.swerve.SwerveDrivetrain;
import com.ctre.phoenix6.swerve.SwerveDrivetrainConstants;
import com.ctre.phoenix6.swerve.SwerveModuleConstants;
import com.ctre.phoenix6.swerve.SwerveRequest;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.units.measure.Angle;
import edu.wpi.first.units.measure.AngularVelocity;
import edu.wpi.first.units.measure.LinearVelocity;
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 CommandSwerveDrivetrain extends SwerveDrivetrain implements SubsystemBase {
// Reusable Swerve Requests to minimize garbage collector allocations
private final SwerveRequest.FieldCentric m_fieldCentric = new SwerveRequest.FieldCentric();
private final SwerveRequest.FieldCentricFacingAngle m_facingAngle = new SwerveRequest.FieldCentricFacingAngle();
private final SwerveRequest.SwerveDriveBrake m_brake = new SwerveRequest.SwerveDriveBrake();
public CommandSwerveDrivetrain(
SwerveDrivetrainConstants driveConstants,
SwerveModuleConstants<?>... modules) {
super(driveConstants, modules);
}
@Override
public void periodic() {
// Publish high-speed odometry telemetry
Pose2d pose = getState().Pose;
SmartDashboard.putNumber("Robot/PoseX_m", pose.getX());
SmartDashboard.putNumber("Robot/PoseY_m", pose.getY());
SmartDashboard.putNumber("Robot/Heading_deg", pose.getRotation().getDegrees());
}
/**
* Standard Field-Oriented Teleop Drive Command factory.
*/
public Command applyRequest(java.util.function.Supplier<SwerveRequest> requestSupplier) {
return run(() -> setControl(requestSupplier.get()));
}
/**
* Snap-to-Heading Command: drives field-oriented while holding a target angle with closed-loop PID.
*/
public Command driveFacingAngleCommand(
LinearVelocity vx, LinearVelocity vy, Angle targetAngle) {
return run(() -> setControl(
m_facingAngle
.withVelocityX(vx.in(MetersPerSecond))
.withVelocityY(vy.in(MetersPerSecond))
.withTargetDirection(new Rotation2d(targetAngle.in(Radians)))
));
}
/**
* X-Brake Command: points wheels inward in an X-pattern to resist pushing defense.
*/
public Command xBrakeCommand() {
return run(() -> setControl(m_brake));
}
}
Current Limits in Generated Constants

Ensure your TunerConstants.java configures SlipCurrent and stator limits on drive motors (typically 60A to 80A) to prevent wheel slip on carpet while accelerating.