Package com.marswars.swerve_lib
Class SwerveMech
java.lang.Object
com.marswars.mechanisms.MechBase
com.marswars.swerve_lib.SwerveMech
- All Implemented Interfaces:
SubsystemIoBase
Mechanism implementation for a four-module swerve drivetrain.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.marswars.mechanisms.MechBase
MechBase.ConstructedMotors -
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionSwerveMech(String logging_prefix, SwerveDriveConfig config) Creates a new swerve mechanism using the provided drivetrain configuration. -
Method Summary
Modifier and TypeMethodDescriptionReturns the measured chassis speeds of the robot.Returns the module states of the swerve drive.Returns the gyro rotation as a Rotation3d containing pitch, yaw, and roll.Returns the raw gyro rotation used for odometry.doubleReturns the gyro yaw rate in radians per second.Returns the swerve drive kinematics instanceReturns the module positions of the swerve drive.Module[]Returns the array of swerve modules in order (FL, FR, BL, BR).Returns the setpoint chassis speeds of the robot.Returns the setpoint module states of the swerve drive.booleanDetects wheel skid by comparing the per-module translational velocities after the shared rotational component is removed.voidlogData()Log data to NetworkTables or other logging systemsvoidreadInputs(double timestamp) Read inputs from hardware or simulationvoidsetChassisRequest(ChassisRequest request) Sets the chassis request for the swerve drive.voidsetChassisRequestParameters(Pose2d pose, Rotation2d operator_forward_direction) Sets the chassis request parameters for the swerve drive.voidsetGyroYaw(Rotation2d yaw) Sets the gyro yaw position.voidStores the current encoder readings as offsetsvoidSets the neutral mode for all swerve modules.voidwriteOutputs(double timestamp) Write outputs to hardware or simulationMethods inherited from class com.marswars.mechanisms.MechBase
configMotors, configMotors, getLoggingKey, getMechName, setMotorCurrentLimit
-
Constructor Details
-
SwerveMech
Creates a new swerve mechanism using the provided drivetrain configuration.- Parameters:
logging_prefix- Logging prefix for telemetryconfig- Drivetrain configuration
-
-
Method Details
-
readInputs
public void readInputs(double timestamp) Read inputs from hardware or simulation- Parameters:
timestamp- the current timestamp
-
writeOutputs
public void writeOutputs(double timestamp) Write outputs to hardware or simulation- Parameters:
timestamp- the current timestamp
-
logData
public void logData()Log data to NetworkTables or other logging systems -
setChassisRequest
Sets the chassis request for the swerve drive.- Parameters:
request-
-
setChassisRequestParameters
Sets the chassis request parameters for the swerve drive.- Parameters:
pose- The current robot pose.operator_forward_direction- The operator's forward direction.
-
setGyroYaw
Sets the gyro yaw position.- Parameters:
yaw- The desired yaw rotation.
-
setModuleOffsets
public void setModuleOffsets()Stores the current encoder readings as offsets -
setNeutralMode
Sets the neutral mode for all swerve modules.- Parameters:
mode- The desired neutral mode
-
getModules
Returns the array of swerve modules in order (FL, FR, BL, BR).- Returns:
- the module array
-
getCurrentChassisSpeeds
Returns the measured chassis speeds of the robot.- Returns:
- ChassisSpeeds object representing the robot's chassis speeds
-
getSetpointChassisSpeeds
Returns the setpoint chassis speeds of the robot.- Returns:
- ChassisSpeeds object representing the robot's setpoint chassis speeds
-
getCurrentModuleStates
Returns the module states of the swerve drive.- Returns:
- SwerveModuleState[] array of module states
-
isSkidding
public boolean isSkidding()Detects wheel skid by comparing the per-module translational velocities after the shared rotational component is removed. If the spread between the fastest and slowest module exceeds the (speed-scaled) skid threshold, at least one wheel is slipping relative to the others.- Returns:
- true if the drivetrain is skidding, false otherwise
-
getSetpointModuleStates
Returns the setpoint module states of the swerve drive.- Returns:
- SwerveModuleState[] array of setpoint module states
-
getModulePositions
Returns the module positions of the swerve drive.- Returns:
- SwerveModulePosition[] array of module positions
-
getGyroYaw
Returns the raw gyro rotation used for odometry.- Returns:
- Rotation2d representing the raw gyro rotation
-
getGyroYawRate
public double getGyroYawRate()Returns the gyro yaw rate in radians per second.- Returns:
- yaw rate in radians per second
-
getGyroRotation
Returns the gyro rotation as a Rotation3d containing pitch, yaw, and roll.- Returns:
- Rotation3d representing the gyro rotation
-
getKinematics
Returns the swerve drive kinematics instance- Returns:
- SwerveDriveKinematics object representing the swerve drive
-