Class SelfControlledSwerveDriveSimulation
An easier way to simulate swerve drive.
Check Online Documentation
This class owns and controls a SwerveDriveSimulation
, running closed loops/open loops on the simulated
motors.
It works identically to how the real swerve simulation code.
Note: The order for swerve modules is: front-left, front-right, back-left, back-right.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
-
Constructor Summary
ConstructorDescriptionSelfControlledSwerveDriveSimulation
(SwerveDriveSimulation swerveDriveSimulation) Default Constructor.SelfControlledSwerveDriveSimulation
(SwerveDriveSimulation swerveDriveSimulation, Matrix<N3, N1> stateStdDevs, Matrix<N3, N1> visionMeasurementStdDevs) Constructs an instance with given odometry standard deviations. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addVisionEstimation
(Pose2d robotPoseMeters, double timeStampSeconds) Adds a vision estimation to the pose estimator.void
addVisionEstimation
(Pose2d robotPoseMeters, double timeStampSeconds, Matrix<N3, N1> measurementStdDevs) Adds a vision estimation to the pose estimator.edu.wpi.first.units.measure.Distance
Obtain the ACTUAL robot pose.Get the ACTUAL field-relative chassis speeds of the robot.Get the ACTUAL robot-relative chassis speeds of the robot.Obtains the CACHED module positions measured by the encoders.Obtain theSwerveDriveSimulation
object controlled by this simplified swerve simulation.Obtains the LATEST module positions measured by the encoders.getMeasuredSpeedsFieldRelative
(boolean useGyroForAngularVelocity) Obtain the field-relative chassis speeds measured from the encoders.getMeasuredSpeedsRobotRelative
(boolean useGyroForAngularVelocity) Obtain the robot-relative chassis speeds measured from the encoders.Obtain the MEASURED swerve states.Obtains the robot pose measured by the odometry.Deprecated.This rotation is NOT the actual facing of the robot; it is uncalibrated and is only used for features like rotation lock.Obtain the optimized SETPOINTS of the swerve.edu.wpi.first.units.measure.AngularAcceleration
edu.wpi.first.units.measure.AngularVelocity
edu.wpi.first.units.measure.LinearAcceleration
edu.wpi.first.units.measure.LinearVelocity
void
periodic()
Periodic Method for Simplified Swerve Sim.void
resetOdometry
(Pose2d pose) Resets the odometry to a specified position.void
runChassisSpeeds
(ChassisSpeeds chassisSpeeds, Translation2d centerOfRotationMeters, boolean fieldCentricDrive, boolean discretizeSpeeds) Runs chassis speeds on the simulated swerve drive.void
runSwerveStates
(SwerveModuleState[] setPoints) Runs a raw module states on the chassis.void
setSimulationWorldPose
(Pose2d robotPose) Teleport the robot to a specified location on the simulated field.edu.wpi.first.units.measure.Distance
edu.wpi.first.units.measure.Distance
withCurrentLimits
(edu.wpi.first.units.measure.Current driveCurrentLimit, edu.wpi.first.units.measure.Current steerCurrentLimit) withSteerPID
(PIDController steerController)
-
Constructor Details
-
SelfControlledSwerveDriveSimulation
Default Constructor.
Constructs a simplified swerve simulation with default standard deviations for odometry & vision pose estimates.
Odometry is simulated as high-frequency odometry, assuming a robot period of 0.02 seconds and an odometry frequency of 250 Hz.
- Parameters:
swerveDriveSimulation
- theSwerveDriveSimulation
to control.
-
SelfControlledSwerveDriveSimulation
public SelfControlledSwerveDriveSimulation(SwerveDriveSimulation swerveDriveSimulation, Matrix<N3, N1> stateStdDevs, Matrix<N3, N1> visionMeasurementStdDevs) Constructs an instance with given odometry standard deviations.
Constructs a simplified swerve simulation with specified standard deviations for odometry & vision pose estimates.
- Parameters:
swerveDriveSimulation
- theSwerveDriveSimulation
to control.stateStdDevs
- the standard deviations for odometry encoders.visionMeasurementStdDevs
- the standard deviations for vision pose estimates.
-
-
Method Details
-
periodic
public void periodic()Periodic Method for Simplified Swerve Sim.
Call this method in the
Subsystem.periodic()
of your swerve subsystem.Updates the odometry by fetching cached inputs.
-
getLatestModulePositions
Obtains the LATEST module positions measured by the encoders.
The order for swerve modules is: front-left, front-right, back-left, back-right.
- Returns:
- the module positions
-
getCachedModulePositions
Obtains the CACHED module positions measured by the encoders.
This simulates high-frequency odometry.
The module positions, or the value of
getLatestModulePositions()
are cached in every simulation sub-tick.The array is ordered in a [timeStampIndex][moduleIndex] format.
The order for swerve modules is: front-left, front-right, back-left, back-right.
- Returns:
- the cached module positions
-
getRawGyroAngle
Deprecated.This rotation is NOT the actual facing of the robot; it is uncalibrated and is only used for features like rotation lock.Obtains the raw angle of the gyro.
Note that the simulated gyro also drifts/skids like real gyros, especially if the robot collides.
To obtain the facing of the robot retrieved from the odometry, use
getOdometryEstimatedPose()
; to obtain the actual facing of the robot, usegetActualPoseInSimulationWorld()
.- Returns:
- the raw (uncalibrated) angle of the simulated gyro.
-
getOdometryEstimatedPose
Obtains the robot pose measured by the odometry.
Retrieves the pose estimated by the odometry system (and vision, if applicable).
This method wraps around
PoseEstimator.getEstimatedPosition()
.This represents the estimated position of the robot.
Note that this estimation includes realistic simulations of measurement errors due to skidding and odometry drift.
To obtain the ACTUAL pose of the robot, with no measurement errors, use
getActualPoseInSimulationWorld()
. -
resetOdometry
Resets the odometry to a specified position.
This method wraps around
SwerveDrivePoseEstimator#resetPosition(Rotation2d, SwerveModulePosition[], Pose2d)
.It resets the position of the pose estimator to the given pose.
- Parameters:
pose
- The position on the field where the robot is located.
-
addVisionEstimation
Adds a vision estimation to the pose estimator.
This method wraps around
PoseEstimator.addVisionMeasurement(Pose2d, double)
.Adds a vision measurement to the Kalman Filter, correcting the odometry pose estimate while accounting for measurement noise.
- Parameters:
robotPoseMeters
- The pose of the robot as measured by the vision camera.timeStampSeconds
- The timestamp of the vision measurement, in seconds.
-
addVisionEstimation
public void addVisionEstimation(Pose2d robotPoseMeters, double timeStampSeconds, Matrix<N3, N1> measurementStdDevs) Adds a vision estimation to the pose estimator.
This method wraps around
PoseEstimator.addVisionMeasurement(Pose2d, double, Matrix)
.Adds a vision measurement to the Kalman Filter, correcting the odometry pose estimate while accounting for measurement noise.
- Parameters:
robotPoseMeters
- The pose of the robot as measured by the vision camera.timeStampSeconds
- The timestamp of the vision measurement, in seconds.measurementStdDevs
- Standard deviations of the vision pose measurement (x position in meters, y position in meters, and heading in radians). Increase these values to reduce the trust in the vision pose measurement.
-
runChassisSpeeds
public void runChassisSpeeds(ChassisSpeeds chassisSpeeds, Translation2d centerOfRotationMeters, boolean fieldCentricDrive, boolean discretizeSpeeds) Runs chassis speeds on the simulated swerve drive.
Runs the specified chassis speeds, either robot-centric or field-centric.
- Parameters:
chassisSpeeds
- The speeds to run, in either robot-centric or field-centric coordinates.centerOfRotationMeters
- The center of rotation. For example, if you set the center of rotation at one corner of the robot and provide a chassis speed that has only a dtheta component, the robot will rotate around that corner.fieldCentricDrive
- Whether to execute field-centric drive with the provided speed.discretizeSpeeds
- Whether to applyChassisSpeeds.discretize(double)
to the provided speed.
-
runSwerveStates
Runs a raw module states on the chassis.
Runs the specified module states on the modules.
- Parameters:
setPoints
- an array ofSwerveModuleState
yielding the requested states
-
getMeasuredStates
Obtain the MEASURED swerve states.
The order of the swerve modules is: front-left, front-right, back-left, back-right.
- Returns:
- The actual measured swerve states of the simulated swerve.
-
getSetPointsOptimized
Obtain the optimized SETPOINTS of the swerve.
The setpoints are calculated using
SwerveDriveKinematics.toSwerveModuleStates(ChassisSpeeds)
in the most recent call torunChassisSpeeds(ChassisSpeeds, Translation2d, boolean, boolean)
.The setpoints are optimized using
SwerveModuleState.optimize(SwerveModuleState, Rotation2d)
.The order of the swerve modules is: front-left, front-right, back-left, back-right.
- Returns:
- The optimized setpoints of the swerve, calculated during the last chassis speed run.
-
getMeasuredSpeedsFieldRelative
Obtain the field-relative chassis speeds measured from the encoders.
The speeds are measured from the simulated swerve modules.
- Parameters:
useGyroForAngularVelocity
- Whether to use the gyro for a more accurate angular velocity measurement.- Returns:
- The measured chassis speeds, field-relative.
-
getMeasuredSpeedsRobotRelative
Obtain the robot-relative chassis speeds measured from the encoders.
The speeds are measured from the simulated swerve modules.
- Parameters:
useGyroForAngularVelocity
- Whether to use the gyro for a more accurate angular velocity measurement.- Returns:
- The measured chassis speeds, robot-relative.
-
getDriveTrainSimulation
Obtain the
SwerveDriveSimulation
object controlled by this simplified swerve simulation.- Returns:
- The swerve drive simulation.
-
getActualPoseInSimulationWorld
Obtain the ACTUAL robot pose.
Obtains the ACTUAL robot pose with zero measurement error.
To obtain the pose calculated by odometry (where the robot thinks it is), use
getOdometryEstimatedPose()
. -
getActualSpeedsFieldRelative
Get the ACTUAL field-relative chassis speeds of the robot.
Wraps around
AbstractDriveTrainSimulation.getDriveTrainSimulatedChassisSpeedsFieldRelative()
.- Returns:
- the actual chassis speeds in the simulation world, field-relative
-
getActualSpeedsRobotRelative
Get the ACTUAL robot-relative chassis speeds of the robot.
Wraps around
AbstractDriveTrainSimulation.getDriveTrainSimulatedChassisSpeedsRobotRelative()
.- Returns:
- the actual chassis speeds in the simulation world, robot-relative
-
setSimulationWorldPose
Teleport the robot to a specified location on the simulated field.
This method moves the drivetrain instantly to the specified location on the field, bypassing any obstacles in its path.
Wraps around
AbstractDriveTrainSimulation.setSimulationWorldPose(Pose2d)
.- Parameters:
robotPose
- the pose of the robot to teleport to
-
withSteerPID
-
withCurrentLimits
public SelfControlledSwerveDriveSimulation withCurrentLimits(edu.wpi.first.units.measure.Current driveCurrentLimit, edu.wpi.first.units.measure.Current steerCurrentLimit) -
maxLinearVelocity
public edu.wpi.first.units.measure.LinearVelocity maxLinearVelocity() -
maxLinearAcceleration
public edu.wpi.first.units.measure.LinearAcceleration maxLinearAcceleration() -
trackWidthY
public edu.wpi.first.units.measure.Distance trackWidthY()- See Also:
-
trackLengthX
public edu.wpi.first.units.measure.Distance trackLengthX() -
driveBaseRadius
public edu.wpi.first.units.measure.Distance driveBaseRadius()- See Also:
-
maxAngularVelocity
public edu.wpi.first.units.measure.AngularVelocity maxAngularVelocity() -
maxAngularAcceleration
public edu.wpi.first.units.measure.AngularAcceleration maxAngularAcceleration()
-