Swerve Drive
Overview
Creating a Swerve Module
import static edu.wpi.first.units.Units.*;
import com.ctre.phoenix6.hardware.CANcoder;
import com.revrobotics.spark.SparkLowLevel.MotorType;
import com.revrobotics.spark.SparkMax;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.system.plant.DCMotor;
import yams.gearing.MechanismGearing;
import yams.mechanisms.config.SwerveModuleConfig;
import yams.mechanisms.swerve.SwerveModule;
import yams.motorcontrollers.SmartMotorController;
import yams.motorcontrollers.SmartMotorControllerConfig;
import yams.motorcontrollers.SmartMotorControllerConfig.TelemetryVerbosity;
import yams.motorcontrollers.local.SparkWrapper;
public SwerveModule createModule(
SparkMax driveMotor,
SparkMax azimuthMotor,
CANcoder absoluteEncoder,
String moduleName,
Translation2d location)
{
// Define gearing ratios
MechanismGearing driveGearing = new MechanismGearing(12.75); // L2 SDS MK4i
MechanismGearing azimuthGearing = new MechanismGearing(6.75);
Distance wheelDiameter = Inches.of(4);
// Drive motor configuration
SmartMotorControllerConfig driveCfg = new SmartMotorControllerConfig(this)
.withWheelDiameter(wheelDiameter)
.withClosedLoopController(0.3, 0, 0)
.withGearing(driveGearing)
.withFeedforward(new SimpleMotorFeedforward(0, 12.0 / 4.5, 0.01)) // kS, kV, kA
.withStatorCurrentLimit(Amps.of(40))
.withTelemetry("driveMotor", TelemetryVerbosity.HIGH);
// Azimuth motor configuration
SmartMotorControllerConfig azimuthCfg = new SmartMotorControllerConfig(this)
.withClosedLoopController(1, 0, 0)
.withFeedforward(new SimpleMotorFeedforward(0, 1))
.withGearing(azimuthGearing)
.withStatorCurrentLimit(Amps.of(20))
.withTelemetry("angleMotor", TelemetryVerbosity.HIGH);
// Create SmartMotorControllers
SmartMotorController driveSMC = new SparkWrapper(driveMotor, DCMotor.getNEO(1), driveCfg);
SmartMotorController azimuthSMC = new SparkWrapper(azimuthMotor, DCMotor.getNEO(1), azimuthCfg);
// Create module configuration
SwerveModuleConfig moduleConfig = new SwerveModuleConfig(driveSMC, azimuthSMC)
.withAbsoluteEncoder(absoluteEncoder.getAbsolutePosition().asSupplier())
.withTelemetry(moduleName, TelemetryVerbosity.HIGH)
.withLocation(location)
.withOptimization(true); // Enable state optimization
return new SwerveModule(moduleConfig);
}Module Configuration Options
Method
Description
Creating the SwerveDrive
SwerveDriveConfig Options
Method
Description
Driving the Robot
Using SwerveInputStream
SwerveInputStream Options
Method
Description
Drive Commands
SwerveDrive Methods Reference
Driving
Method
Description
Odometry & Pose
Method
Description
Utility
Method
Description
Periodic Updates
SysId for Characterization
Complete Example
Next Steps
Last updated