Class SwerveDriveConfig<T extends SwerveDriveConfig<T>>
java.lang.Object
yams.core.mechanisms.config.SwerveDriveConfig<T>
- Type Parameters:
T- Concrete config type, so chainedwithcalls keep the subclass type.
- Direct Known Subclasses:
SwerveDriveConfig, SwerveDriveConfig
Swerve Drive Configuration.
Holds modules, gyro supplier, and drive controllers. Modules are ordered clockwise from
front-left: FL, FR, BL, BR. See SwerveDriveConfig for the
Subsystem-bound subclass and a full construction example.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate theSwerveDriveConfigfor theSwerveDriveprotectedCopy aSwerveDriveConfig.protectedSwerveDriveConfig(SwerveModule... modules) Create theSwerveDriveConfigfor theSwerveDrive -
Method Summary
Modifier and TypeMethodDescriptionabstract Tclone()Clone theSwerveDriveConfigwithout modules, subsystem, telemetry name, gyro supplier, gyro angular velocity supplier, gyro offset, and gyro inversion.static org.wpilib.math.geometry.Translation2dcubeTranslation(org.wpilib.math.geometry.Translation2d translation) Cube theTranslation2dmagnitude given in Polar coordinates.org.wpilib.math.kinematics.ChassisVelocitiesGet the anti-tipping correction for the robot's attitude now.Optional<org.wpilib.math.geometry.Translation2d> Get the center of rotation.Optional<org.wpilib.units.measure.Time> Get the discretization time for the pose estimation used byoptimizeRobotRelativeChassisSpeeds(ChassisVelocities)org.wpilib.units.measure.AngleGet the gyro offset.org.wpilib.math.geometry.Rotation3dGet the robot's attitude from the gyro: the gyro's attitude rotated into the robot's frame for the axis set withwithGyroHeadingAxis(GyroAxis), with the inversion (withGyroInverted(boolean)) and offset (withGyroOffset(Angle)) applied to its yaw.org.wpilib.math.geometry.Pose2dGet the starting pose of the robot.Optional<org.wpilib.units.measure.AngularVelocity> Get the maximum angular speed of the chassis.Optional<org.wpilib.units.measure.LinearVelocity> Get the maximum speed of the chassis.Optional<org.wpilib.units.measure.LinearVelocity> Get the maximum speed of the modules.Get theSwerveModules for theSwerveDrive.Get the module state optimization override.Optional<org.wpilib.math.controller.PIDController> Get the rotation PID controller used for drive to pose and heading control.Get the user specifiedSwerveDriveTelemetryConfig, if configured viawithTelemetry(String,SwerveDriveTelemetryConfig).Get the telemetry name for theSwerveDrive.Get the telemetry verbosity for theSwerveModule.Optional<org.wpilib.math.controller.PIDController> Get the translation PID controller used for drive to pose.org.wpilib.math.kinematics.ChassisVelocitiesoptimizeRobotRelativeChassisSpeeds(org.wpilib.math.kinematics.ChassisVelocities speeds) Optimize the given chassis speeds, first adding the anti-tipping correction when it is configured withwithAntiTipping(AntiTipping).static org.wpilib.math.geometry.Translation2dscaleTranslation(org.wpilib.math.geometry.Translation2d translation, double scalar) Scale theTranslation2dPolar coordinate magnitude.protected abstract Tself()Get this config as its concrete type, so chainedwithcalls keep the subclass type.booleanUse an external feedback sensor for theSwerveModules.withAntiTipping(org.wpilib.math.controller.AntiTipping antiTipping) Drive the robot back under itself when it starts to tip, with anAntiTippingreading the robot's attitude from the gyro set withwithGyro(Supplier): its correction is added to every robot relative chassis speed.withAntiTipping(org.wpilib.units.measure.LinearVelocity kP, org.wpilib.units.measure.Angle tippingThreshold, org.wpilib.units.measure.LinearVelocity maxCorrectionSpeed) Drive the robot back under itself when it starts to tip, with WPILib'sAntiTipping: once the robot's pitch or roll, from the gyro set withwithGyro(Supplier), is pasttippingThreshold, a correction toward the side it is tipping to,kPtimes the sine of the tilt and at mostmaxCorrectionSpeed, is added to every robot relative chassis speed.withCenterOfRotation(org.wpilib.math.geometry.Translation2d centerOfRotation) Set the center of rotation; 0,0 is the center of the robot.withCenterOfRotation(org.wpilib.units.measure.Distance forward, org.wpilib.units.measure.Distance left) Set the center of rotation; 0,0 is the center of the robot.withDiscretizationTime(org.wpilib.units.measure.Time dt) Set the discretization time for the pose estimation.Set the gyro, as the robot's attitude: its yaw is the robot's heading (or the axis set withwithGyroHeadingAxis(GyroAxis)), and its roll and pitch are what anti-tipping corrects with (seewithAntiTipping(AntiTipping)).withGyroAngularVelocityScaleFactor(double scaleFactor) Set the angular velocity scale factor to improve the accuracy of the pose estimation.withGyroHeadingAxis(GyroAxis axis) Set which axis of the gyro points up through the robot, for a gyro mounted so the robot turning shows up as its roll or pitch instead of its yaw.withGyroInverted(boolean inverted) Set the gyro inverted.withGyroOffset(org.wpilib.units.measure.Angle offset) Set the gyro offset.withGyroVelocity(Supplier<org.wpilib.units.measure.AngularVelocity> angularVelocitySupplier) Angular velocity of the gyro.withMaximumChassisSpeed(org.wpilib.units.measure.LinearVelocity speed, org.wpilib.units.measure.AngularVelocity angularVelocity) Maximum speed of the chassis to desaturate towards.withMaximumModuleSpeed(org.wpilib.units.measure.LinearVelocity speed) Set the maximum speed of the modules to desaturate towards.withModules(SwerveModule... modules) Set theSwerveModules for theSwerveDrive.withModuleStateOptimization(boolean enabled) Enable or disable state optimization on everySwerveModuleof the drive, overriding each module'sSwerveModuleConfig.withOptimization(boolean).withRotationController(org.wpilib.math.controller.PIDController controller) Set the rotation PID controller.withSimDiscretizationTime(org.wpilib.units.measure.Time dt) Set the discretization time for the pose estimation.withSimGyroAngularVelocityScaleFactor(double scaleFactor) Set the angular velocity scale factor to improve the accuracy of the pose estimation.withSimRotationController(org.wpilib.math.controller.PIDController controller) Set the rotation PID controller.withSimTranslationController(org.wpilib.math.controller.PIDController controller) Set the translation PID controller.withStartingPose(org.wpilib.math.geometry.Pose2d pose) Set the starting pose of the robot.withTelemetry(String name, TelemetryVerbosity telemetryVerbosity) Configure telemetry for theSwerveModulemechanism.withTelemetry(String name, SwerveDriveTelemetryConfig telemetryConfig) Configure telemetry for theSwerveDrivewith aSwerveDriveTelemetryConfig.withTranslationController(org.wpilib.math.controller.PIDController controller) Set the translation PID controller.
-
Constructor Details
-
SwerveDriveConfig
Create theSwerveDriveConfigfor theSwerveDrive- Parameters:
modules-SwerveModules for theSwerveDrive- Implementation Note:
- Protected so only
SwerveDriveConfigcan construct this.
-
SwerveDriveConfig
protected SwerveDriveConfig()Create theSwerveDriveConfigfor theSwerveDrive- Implementation Note:
- Must define modules with
withModules(SwerveModule...). Protected so onlySwerveDriveConfigcan construct this.
-
SwerveDriveConfig
Copy aSwerveDriveConfig.- Parameters:
cfg-SwerveDriveConfigto copy.
-
-
Method Details
-
self
Get this config as its concrete type, so chainedwithcalls keep the subclass type.- Returns:
- this
SwerveDriveConfig.
-
clone
Clone theSwerveDriveConfigwithout modules, subsystem, telemetry name, gyro supplier, gyro angular velocity supplier, gyro offset, and gyro inversion.- Overrides:
clonein classObject- Returns:
- New
SwerveDriveConfig
-
withModules
Set theSwerveModules for theSwerveDrive.- Parameters:
modules-SwerveModules for theSwerveDrive.- Returns:
SwerveDriveConfigfor chaining.
-
withSimTranslationController
Set the translation PID controller.- Parameters:
controller-PIDControllerfor the translation, input units are meters.- Returns:
SwerveDriveConfigfor chaining.
-
withSimRotationController
Set the rotation PID controller.- Parameters:
controller-PIDControllerfor the rotation, input units are radians.- Returns:
SwerveDriveConfigfor chaining.
-
withTranslationController
Set the translation PID controller.- Parameters:
controller-PIDControllerfor the translation, input units are meters.- Returns:
SwerveDriveConfigfor chaining.
-
withRotationController
Set the rotation PID controller.- Parameters:
controller-PIDControllerfor the rotation, input units are radians.- Returns:
SwerveDriveConfigfor chaining.
-
withCenterOfRotation
Set the center of rotation; 0,0 is the center of the robot.- Parameters:
centerOfRotation-Translation2dof the center of rotation in Meters, X is forward, Y is left.- Returns:
SwerveDriveConfigfor chaining.
-
withCenterOfRotation
public T withCenterOfRotation(org.wpilib.units.measure.Distance forward, org.wpilib.units.measure.Distance left) Set the center of rotation; 0,0 is the center of the robot.- Parameters:
forward- Forward distance from the center of robot.left- Left distance from the center of robot.- Returns:
SwerveDriveConfigfor chaining.
-
withSimDiscretizationTime
Set the discretization time for the pose estimation.- Parameters:
dt- Discretization time for the pose estimation.- Returns:
SwerveDriveConfigfor chaining.
-
withSimGyroAngularVelocityScaleFactor
Set the angular velocity scale factor to improve the accuracy of the pose estimation.- Parameters:
scaleFactor- Scale factor to apply to the gyro angular velocity, [0, 1].- Returns:
SwerveDriveConfigfor chaining.
-
withDiscretizationTime
Set the discretization time for the pose estimation.- Parameters:
dt- Discretization time for the pose estimation.- Returns:
SwerveDriveConfigfor chaining.
-
withGyroAngularVelocityScaleFactor
Set the angular velocity scale factor to improve the accuracy of the pose estimation.- Parameters:
scaleFactor- Scale factor to apply to the gyro angular velocity, [0, 1].- Returns:
SwerveDriveConfigfor chaining.
-
withGyroVelocity
public T withGyroVelocity(Supplier<org.wpilib.units.measure.AngularVelocity> angularVelocitySupplier) Angular velocity of the gyro.- Parameters:
angularVelocitySupplier-Supplier<org.wpilib.units.measure.AngularVelocity>for the gyro angular velocity.- Returns:
SwerveDriveConfigfor chaining.
-
withGyro
Set the gyro, as the robot's attitude: its yaw is the robot's heading (or the axis set withwithGyroHeadingAxis(GyroAxis)), and its roll and pitch are what anti-tipping corrects with (seewithAntiTipping(AntiTipping)).- Parameters:
gyro-Supplierfor the robot's attitude, such aspigeon::getRotation3dorimu::getRotation3d.- Returns:
SwerveDriveConfigfor chaining.
-
withGyroHeadingAxis
Set which axis of the gyro points up through the robot, for a gyro mounted so the robot turning shows up as its roll or pitch instead of its yaw. Defaults toGyroAxis.YAW. The gyro's attitude is rotated into the robot's frame withGyroAxis.toRobotAttitude(Rotation3d)before the heading and anti-tipping (seewithAntiTipping(AntiTipping)) read it;withGyroInverted(boolean)andwithGyroOffset(Angle)apply to the resulting heading.- Parameters:
axis- Axis of the gyro that points up through the robot.- Returns:
SwerveDriveConfigfor chaining.
-
withAntiTipping
public T withAntiTipping(org.wpilib.units.measure.LinearVelocity kP, org.wpilib.units.measure.Angle tippingThreshold, org.wpilib.units.measure.LinearVelocity maxCorrectionSpeed) Drive the robot back under itself when it starts to tip, with WPILib'sAntiTipping: once the robot's pitch or roll, from the gyro set withwithGyro(Supplier), is pasttippingThreshold, a correction toward the side it is tipping to,kPtimes the sine of the tilt and at mostmaxCorrectionSpeed, is added to every robot relative chassis speed. SeeAntiTippingfor tuning.- Parameters:
kP- Correction speed per sine of the tilt.tippingThreshold- Tilt past which to correct: a few degrees above the most the robot tilts in normal driving, well below where it tips.maxCorrectionSpeed- Fastest the correction may drive.- Returns:
SwerveDriveConfigfor chaining.- Throws:
IllegalArgumentException- ifkPormaxCorrectionSpeedis not positive, ortippingThresholdis not between 0 and 90 degrees.
-
withAntiTipping
Drive the robot back under itself when it starts to tip, with anAntiTippingreading the robot's attitude from the gyro set withwithGyro(Supplier): its correction is added to every robot relative chassis speed.- Parameters:
antiTipping-AntiTippingto correct with.- Returns:
SwerveDriveConfigfor chaining.
-
getAntiTippingCorrection
public org.wpilib.math.kinematics.ChassisVelocities getAntiTippingCorrection()Get the anti-tipping correction for the robot's attitude now.- Returns:
- Robot relative correction, or zero when the robot is not tipping, or anti-tipping or the gyro is not configured.
-
withGyroOffset
Set the gyro offset.- Parameters:
offset- Offset to apply to the gyro.- Returns:
SwerveDriveConfigfor chaining.
-
withGyroInverted
Set the gyro inverted.- Parameters:
inverted- Inverted state of the gyro.- Returns:
SwerveDriveConfigfor chaining.
-
withMaximumChassisSpeed
public T withMaximumChassisSpeed(org.wpilib.units.measure.LinearVelocity speed, org.wpilib.units.measure.AngularVelocity angularVelocity) Maximum speed of the chassis to desaturate towards.- Parameters:
speed- Linear velocity of the Chassis.angularVelocity- Angular velocity of the Chassis.- Returns:
SwerveDriveConfigfor chaining.
-
withMaximumModuleSpeed
Set the maximum speed of the modules to desaturate towards.- Parameters:
speed- Linear velocity of the modules.- Returns:
SwerveDriveConfigfor chaining.
-
withModuleStateOptimization
Enable or disable state optimization on everySwerveModuleof the drive, overriding each module'sSwerveModuleConfig.withOptimization(boolean). Without this override, each module keeps its own setting.Modules already in this config are updated immediately, so calling this on a running drive's config (e.g.
drive.getConfig().withModuleStateOptimization(false)) toggles optimization during operation. Modules added later withwithModules(SwerveModule...)get it when theSwerveDriveis created.- Parameters:
enabled- True to let modules turn around and reverse their drive when that is the shorter way to the requested angle, false to always steer to the requested angle.- Returns:
SwerveDriveConfigfor chaining.
-
withStartingPose
Set the starting pose of the robot.- Parameters:
pose-Pose2dto set the robot to.new Pose2d()- Returns:
SwerveDriveConfigfor chaining.
-
withTelemetry
Configure telemetry for theSwerveModulemechanism.- Parameters:
name- Telemetry NametelemetryVerbosity- Telemetry verbosity to apply.- Returns:
SwerveDriveConfigfor chaining.
-
withTelemetry
Configure telemetry for theSwerveDrivewith aSwerveDriveTelemetryConfig.- Parameters:
name- Telemetry NametelemetryConfig- Config that specifies what to log.- Returns:
SwerveDriveConfigfor chaining.
-
getSwerveDriveTelemetryConfig
Get the user specifiedSwerveDriveTelemetryConfig, if configured viawithTelemetry(String,SwerveDriveTelemetryConfig).- Returns:
SwerveDriveTelemetryConfigif configured.
-
getTelemetryName
Get the telemetry name for theSwerveDrive.- Returns:
- Telemetry name for the
SwerveDrive.
-
getCenterOfRotation
Get the center of rotation.- Returns:
Translation2dof the center of rotation in Meters, X is forward, Y is left.
-
getTelemetryVerbosity
Get the telemetry verbosity for theSwerveModule.- Returns:
TelemetryVerbosityfor theSwerveModule.
-
getModules
Get theSwerveModules for theSwerveDrive.- Returns:
SwerveModules for theSwerveDrive.
-
getGyroRotation3d
public org.wpilib.math.geometry.Rotation3d getGyroRotation3d()Get the robot's attitude from the gyro: the gyro's attitude rotated into the robot's frame for the axis set withwithGyroHeadingAxis(GyroAxis), with the inversion (withGyroInverted(boolean)) and offset (withGyroOffset(Angle)) applied to its yaw. Its yaw is the robot's heading, wrapped to half a rotation each way, and its roll and pitch are the robot's tilt.- Returns:
- The robot's attitude.
- Throws:
IllegalStateException- if no gyro supplier was set withwithGyro(Supplier).
-
getInitialPose
public org.wpilib.math.geometry.Pose2d getInitialPose()Get the starting pose of the robot.- Returns:
Pose2dof the robot.
-
getMaximumChassisLinearVelocity
Get the maximum speed of the chassis.- Returns:
- Maximum speed of the chassis.
-
getMaximumChassisAngularVelocity
Get the maximum angular speed of the chassis.- Returns:
- Maximum angular speed of the chassis.
-
getMaximumModuleLinearVelocity
Get the maximum speed of the modules.- Returns:
- Maximum speed of the modules.
-
getModuleStateOptimization
-
optimizeRobotRelativeChassisSpeeds
public org.wpilib.math.kinematics.ChassisVelocities optimizeRobotRelativeChassisSpeeds(org.wpilib.math.kinematics.ChassisVelocities speeds) Optimize the given chassis speeds, first adding the anti-tipping correction when it is configured withwithAntiTipping(AntiTipping).- Parameters:
speeds-ChassisVelocitiesto optimize.- Returns:
- Optimized
ChassisVelocities. - Throws:
IllegalStateException- if a gyro angular velocity scale factor is configured withwithGyroAngularVelocityScaleFactor(double)but no gyro supplier was set withwithGyro(Supplier).
-
getGyroOffset
public org.wpilib.units.measure.Angle getGyroOffset()Get the gyro offset.- Returns:
- Gyro offset.
-
getTranslationPID
Get the translation PID controller used for drive to pose. In simulation the simulation controller is preferred over the real one.- Returns:
- Translation PID controller, or empty when none is configured.
-
getRotationPID
Get the rotation PID controller used for drive to pose and heading control. In simulation the simulation controller is preferred over the real one.- Returns:
- Rotation PID controller, or empty when none is configured.
-
cubeTranslation
public static org.wpilib.math.geometry.Translation2d cubeTranslation(org.wpilib.math.geometry.Translation2d translation) Cube theTranslation2dmagnitude given in Polar coordinates.- Parameters:
translation-Translation2dto manipulate.- Returns:
- Cubed magnitude from
Translation2d.
-
scaleTranslation
public static org.wpilib.math.geometry.Translation2d scaleTranslation(org.wpilib.math.geometry.Translation2d translation, double scalar) Scale theTranslation2dPolar coordinate magnitude.- Parameters:
translation-Translation2dto use.scalar- Multiplier for the Polar coordinate magnitude to use.- Returns:
Translation2dscaled by given magnitude scalar.
-
getDiscretization
Get the discretization time for the pose estimation used byoptimizeRobotRelativeChassisSpeeds(ChassisVelocities)- Returns:
- Discretization time for the pose estimation.
-
useExternalFeedbackSensor
public boolean useExternalFeedbackSensor()Use an external feedback sensor for theSwerveModules.- Returns:
- External feedback sensor for the
SwerveModules.
-