Class Swerve
This subsystem owns and coordinates all components required to control and estimate the state of a swerve drive, including:
- Swerve modules and their IO implementations
- Gyro integration
- High-frequency odometry sampling via
PhoenixOdometryThread - Pose estimation and vision fusion via
RobotState - Acceleration, tilt, and skid limiting via
SwerveRateLimiter
Threading model
Odometry-related sensor signals are updated on a dedicated background thread. Access to these signals and derived state is synchronized using a sharedodometryLock to ensure consistency across the estimator and modules.
Pose estimation
Wheel encoder and gyro data are integrated at high rate to produce odometry updates, which are then fused with delayed vision measurements insideRobotState. The resulting pose estimate is the authoritative source of robot position for
autonomous and field-relative driving.
Driving model
All drive commands ultimately resolve to robot-relativeChassisSpeeds. These speeds are passed through a SwerveRateLimiter before being
discretized and converted to per-module states.
This class exposes convenience commands for robot-relative, field-relative, and user-relative driving, as well as trajectory-style pose targeting and characterization routines.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSwerve(Function<PhoenixOdometryThread, SwerveIO> swerveIo, Function<PhoenixOdometryThread, GyroIO> gyroIo, BiFunction<Integer, PhoenixOdometryThread, SwerveModuleIO> moduleIoFn) Constructs the swerve subsystem and initializes all hardware interfaces, estimator state, and background odometry processing. -
Method Summary
Modifier and TypeMethodDescriptionIdentifies Closest TrenchdoubleReturns the distance between the robot and the closest Trench zone.driveFieldRelative(Supplier<ChassisSpeeds> driveSpeeds) Drives the robot using the estimated global field heading.driveRobotRelative(Supplier<ChassisSpeeds> driveSpeeds) Drives the robot using robot-relative chassis speeds.driveUserRelative(Supplier<ChassisSpeeds> driveSpeeds) Drives the robot using a user-defined field reference heading.Creates a command that immediately commands zero chassis speeds to the drivetrain.Creates a SysId routine for drivetrain feedforward characterization.Returns the current user-relative heading used for driving.Returns the goal location of the robotbooleaninTrench()Returns true if the robot is in the Trench zone.moveThroughTrench(Supplier<ChassisSpeeds> driveSpeeds) Command that moves the robot through the trench, constraining Y movement to stay in the trench.frc.robot.subsystems.swerve.util.MoveToPoseBuilderCreates a command builder for driving the robot to a target global pose.Command that moves the robot to the trench without going throughoverridePose(Supplier<Pose2d> newPose) Immediately sets the robot's pose to a new value, updating both the odometry estimator and the underlying simulation state (if any).voidperiodic()Sets a user-defined field-relative heading offset.Sets a user-defined field-relative heading offset.setFieldRelativeOffset(Supplier<Rotation2d> knownHeading) Sets a user-defined field-relative heading offset.stop()Creates a command that smoothly brings the drivetrain to a complete stop.static final List<Translation2d>returns a list with all of the trench boundsCreates a SysId routine for wheel radius characterization.Methods inherited from class edu.wpi.first.wpilibj2.command.SubsystemBase
addChild, getName, getSubsystem, initSendable, setName, setSubsystemMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface edu.wpi.first.wpilibj2.command.Subsystem
defer, getCurrentCommand, getDefaultCommand, idle, register, removeDefaultCommand, run, runEnd, runOnce, setDefaultCommand, simulationPeriodic, startEnd, startRun
-
Field Details
-
modules
-
state
-
customSkidLimit
public double customSkidLimit
-
-
Constructor Details
-
Swerve
public Swerve(Function<PhoenixOdometryThread, SwerveIO> swerveIo, Function<PhoenixOdometryThread, GyroIO> gyroIo, BiFunction<Integer, PhoenixOdometryThread, SwerveModuleIO> moduleIoFn) Constructs the swerve subsystem and initializes all hardware interfaces, estimator state, and background odometry processing.The provided factories are invoked with a shared
PhoenixOdometryThreadinstance to allow sensors and motors to register high-frequency signals for synchronized sampling.- Parameters:
swerveIo- factory for creating the drivetrain-level IO implementationgyroIo- factory for creating the gyro IO implementationmoduleIoFn- factory for creating per-module IO implementations
-
-
Method Details
-
periodic
public void periodic() -
driveRobotRelative
Drives the robot using robot-relative chassis speeds.Supplied speeds are passed through the
SwerveRateLimiterbefore being applied to the drivetrain.- Parameters:
driveSpeeds- supplier of robot-relative chassis speeds- Returns:
- a command that drives the robot while scheduled
-
driveUserRelative
Drives the robot using a user-defined field reference heading.The supplied field-relative speeds are transformed into robot-relative speeds using the user-controlled heading offset.
- Parameters:
driveSpeeds- supplier of field-relative chassis speeds- Returns:
- a command that drives the robot while scheduled
-
driveFieldRelative
Drives the robot using the estimated global field heading.The supplied field-relative speeds are transformed into robot-relative speeds using the current pose estimate from
RobotState.- Parameters:
driveSpeeds- supplier of field-relative chassis speeds- Returns:
- a command that drives the robot while scheduled
-
overridePose
Immediately sets the robot's pose to a new value, updating both the odometry estimator and the underlying simulation state (if any).This is useful when you need to forcibly override the robot's pose, for example during testing or at the start of autonomous in simulation.
If you only want to update the odometry/estimator without affecting any simulation state, use
RobotState.resetPose(Pose2d)instead.- Parameters:
newPose- a supplier that provides the new robot pose in field coordinates- Returns:
- a command that applies the pose override once when scheduled
-
moveToPose
public frc.robot.subsystems.swerve.util.MoveToPoseBuilder moveToPose()Creates a command builder for driving the robot to a target global pose.The generated command uses a holonomic controller and the current pose estimate to compute chassis speeds, which are rate-limited and applied to the drivetrain.
- Returns:
- a
MoveToPoseBuilderfor configuring pose targets
-
feedforwardCharacterization
Creates a SysId routine for drivetrain feedforward characterization.This routine is used to identify kS and kV parameters for the swerve drive by applying controlled voltage steps and measuring resulting motion.
- Returns:
- a command that runs feedforward characterization
-
wheelRadiusCharacterization
Creates a SysId routine for wheel radius characterization.This routine estimates the effective wheel radius by correlating commanded motion with measured yaw change.
- Returns:
- a command that runs wheel radius characterization
-
setFieldRelativeOffset
Sets a user-defined field-relative heading offset.This offset is used by user-relative driving modes to define "forward" independently of the robot's pose estimate.
- Parameters:
knownHeading- supplier of the desired field heading- Returns:
- a one-shot command that applies the offset
-
setFieldRelativeOffset
Sets a user-defined field-relative heading offset.This offset is used by user-relative driving modes to define "forward" independently of the robot's pose estimate.
- Returns:
- a one-shot command that sets the offset such that the current direction is "forward"
-
resetFieldRelativeOffsetBasedOnPose
Sets a user-defined field-relative heading offset.This offset is used by user-relative driving modes to define "forward" independently of the robot's pose estimate.
- Returns:
- a one-shot command that sets the offset such that it agrees with the estimated pose (+180 degrees when on red alliance)
-
stop
Creates a command that smoothly brings the drivetrain to a complete stop.This command commands zero desired chassis speeds and allows the
SwerveRateLimiterto decelerate the robot within configured acceleration, tilt, and skid constraints rather than stopping abruptly.The command completes once the rate-limited translational speed falls below a small threshold, indicating the robot is effectively stationary. After completion, a final zero-speed command is issued to ensure all modules are explicitly commanded to stop.
This is intended for use when transitioning between driving modes, autonomous steps, or before actions that require the robot to be fully settled.
- Returns:
- a command that decelerates and stops the drivetrain
-
emergencyStop
Creates a command that immediately commands zero chassis speeds to the drivetrain.This method bypasses all rate limiting and deceleration constraints and directly commands the swerve modules to stop in a single control cycle. As a result, it may cause abrupt deceleration, increased state estimation error, and/or loss of traction depending on robot speed and surface conditions.
In most situations,
stop()should be preferred, as it brings the robot to rest in a controlled manner using theSwerveRateLimiter.This command is intended only for exceptional circumstances such as fault handling, disable transitions, or safety-critical interruptions where immediate cessation of motion is required.
- Returns:
- a command that immediately commands zero chassis speeds
-
limitSkidLimit
-
getUserRelativeHeading
Returns the current user-relative heading used for driving.This heading is derived from the gyro yaw and a manually controlled field offset, independent of the pose estimator.
- Returns:
- user-relative field heading
-
closestTrench
Identifies Closest Trench -
distanceFromClosestTrench
public double distanceFromClosestTrench()Returns the distance between the robot and the closest Trench zone. -
inTrench
public boolean inTrench()Returns true if the robot is in the Trench zone. -
trenchSides
returns a list with all of the trench bounds -
goalTrenchPosition
Returns the goal location of the robot -
moveToTrench
Command that moves the robot to the trench without going through -
moveThroughTrench
Command that moves the robot through the trench, constraining Y movement to stay in the trench.- Parameters:
driveSpeeds- supplier of field-relative chassis speeds- Returns:
- a command that drives the robot while scheduled
-