Class SwerveModuleSimulation
Simulation for a Single Swerve Module.
Check Online Documentation
This class provides a simulation for a single swerve module in the SwerveDriveSimulation
.
1. Purpose
This class serves as the bridge between your code and the physics engine.
You will apply voltage outputs to the drive/steer motor of the module and obtain their encoder readings in your code, just as how you deal with your physical motors.
2. Perspectives
- Simulates the steering mechanism using a custom brushless motor simulator.
- Simulates the propelling force generated by the driving motor, with a current limit.
- Simulates encoder readings, which can be used to simulate a
SwerveDriveOdometry
.
3. Simulating Odometry
- Retrieve the encoder readings from
getDriveEncoderUnGearedPosition()
} andgetSteerAbsoluteFacing()
. - Use
SwerveDriveOdometry
to estimate the pose of your robot. - 250Hz
Odometry is supported. You can retrive cached encoder readings from every sub-tick through
getCachedDriveEncoderUnGearedPositions()
andgetCachedSteerAbsolutePositions()
.
An example of how to simulate odometry using this class is the ModuleIOSim.java
from the Advanced Swerve Drive with maple-sim
example.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a Swerve Module Simulation. -
Method Summary
Modifier and TypeMethodDescriptionAngle[]
Obtains the Cached Readings of the Drive Encoder's Un-Geared Position.Angle[]
Obtains the Cached Readings of the Drive Encoder's Final Position (Wheel Rotations).Obtains the Cached Readings of the Steer Absolute Positions.Angle[]
Obtains the Cached Readings of the Steer Relative Encoder's Position.Obtains the Position of the Drive Encoder.Obtains the Speed of the Drive Encoder.Obtains the Actual Output Voltage of the Drive Motor.Obtains the Stator current the Drive Motor.Obtains the Amount of Current Supplied to the Drive Motor.Obtains the Final Position of the Wheel.Obtains the Final Speed of the Wheel.protected SwerveModuleState
Obtains the "free spin" state of the moduleObtains the Absolute Angle of the Steer Mechanism.Obtains the Absolute Rotational Velocity of the Steer Mechanism.Obtains the Absolute Facing of the Steer Mechanism.Obtains the Actual Output Voltage of the Steering Motor.Obtains the Stator current the Steer Motor.Obtains the Amount of Current Supplied to the Steer Motor.Obtains the Relative Position of the Steer Encoder.Obtains the Speed of the Steer Relative Encoder (Geared).org.dyn4j.geometry.Vector2
updateSimulationSubTickGetModuleForce
(org.dyn4j.geometry.Vector2 moduleCurrentGroundVelocityWorldRelative, Rotation2d robotFacing, double gravityForceOnModuleNewtons) Updates the Simulation for This Module.<T extends SimulatedMotorController>
TuseDriveMotorController
(T driveMotorController) Sets the motor controller for the drive motor.<T extends SimulatedMotorController>
TuseSteerMotorController
(T steerMotorController) Requests the Steering Motor to Run at a Specified Output.
-
Field Details
-
config
-
-
Constructor Details
-
SwerveModuleSimulation
Constructs a Swerve Module Simulation.
If you are using
SimulatedArena.overrideSimulationTimings(Time, int)
to use custom timings, you must call the method before constructing any swerve module simulations using this constructor.- Parameters:
config
- the configuration
-
-
Method Details
-
getDriveMotorConfigs
-
getSteerMotorConfigs
-
useDriveMotorController
Sets the motor controller for the drive motor.
The configured controller runs control loop on the motor.
- Parameters:
driveMotorController
- the motor controller to control the drive motor
-
useGenericMotorControllerForDrive
-
useSteerMotorController
Requests the Steering Motor to Run at a Specified Output.
Think of it as the
requestOutput()
of your physical steering motor.- Parameters:
steerMotorController
- the motor controller to control the steer motor
-
useGenericControllerForSteer
-
updateSimulationSubTickGetModuleForce
public org.dyn4j.geometry.Vector2 updateSimulationSubTickGetModuleForce(org.dyn4j.geometry.Vector2 moduleCurrentGroundVelocityWorldRelative, Rotation2d robotFacing, double gravityForceOnModuleNewtons) Updates the Simulation for This Module.
Note: Friction forces are not simulated in this method.
- Parameters:
moduleCurrentGroundVelocityWorldRelative
- the current ground velocity of the module, relative to the worldrobotFacing
- the absolute facing of the robot, relative to the worldgravityForceOnModuleNewtons
- the gravitational force acting on this module, in newtons- Returns:
- the propelling force generated by the module, as a
Vector2
object
-
getCurrentState
- Returns:
- the current module state of this simulation module
-
getFreeSpinState
Obtains the "free spin" state of the module
The "free spin" state of a simulated module refers to its state after spinning freely for a long time under the current input voltage
- Returns:
- the free spinning module state
-
getDriveMotorAppliedVoltage
Obtains the Actual Output Voltage of the Drive Motor.
- Returns:
- the actual output voltage of the drive motor
-
getSteerMotorAppliedVoltage
Obtains the Actual Output Voltage of the Steering Motor.
- Returns:
- the actual output voltage of the steering motor
- See Also:
-
getDriveMotorSupplyCurrent
Obtains the Amount of Current Supplied to the Drive Motor.
- Returns:
- the current supplied to the drive motor
-
getDriveMotorStatorCurrent
Obtains the Stator current the Drive Motor.
- Returns:
- the stator current of the drive motor
-
getSteerMotorSupplyCurrent
Obtains the Amount of Current Supplied to the Steer Motor.
- Returns:
- the current supplied to the steer motor
- See Also:
-
getSteerMotorStatorCurrent
Obtains the Stator current the Steer Motor.
- Returns:
- the stator current of the drive motor
- See Also:
-
getDriveEncoderUnGearedPosition
Obtains the Position of the Drive Encoder.
This value represents the un-geared position of the encoder, i.e., the amount of radians the drive motor's encoder has rotated.
- Returns:
- the position of the drive motor's encoder (un-geared)
-
getDriveWheelFinalPosition
Obtains the Final Position of the Wheel.
This method provides the final position of the drive encoder in terms of wheel angle.
- Returns:
- the final position of the drive encoder (wheel rotations)
-
getDriveEncoderUnGearedSpeed
Obtains the Speed of the Drive Encoder.
- Returns:
- the un-geared speed of the drive encoder
-
getDriveWheelFinalSpeed
Obtains the Final Speed of the Wheel.
- Returns:
- the final speed of the drive wheel
-
getSteerRelativeEncoderPosition
Obtains the Relative Position of the Steer Encoder.
- Returns:
- the relative encoder position of the steer motor
- See Also:
-
getSteerRelativeEncoderVelocity
Obtains the Speed of the Steer Relative Encoder (Geared).
- Returns:
- the speed of the steer relative encoder
- See Also:
-
getSteerAbsoluteFacing
Obtains the Absolute Facing of the Steer Mechanism.
- Returns:
- the absolute facing of the steer mechanism, as a
Rotation2d
-
getSteerAbsoluteAngle
Obtains the Absolute Angle of the Steer Mechanism.
- Returns:
- the (continuous) final angle of the steer mechanism, as a
Angle
- See Also:
-
getSteerAbsoluteEncoderSpeed
Obtains the Absolute Rotational Velocity of the Steer Mechanism.
- Returns:
- the absolute angular velocity of the steer mechanism
-
getCachedDriveEncoderUnGearedPositions
Obtains the Cached Readings of the Drive Encoder's Un-Geared Position.
The values of
getDriveEncoderUnGearedPosition()
are cached at each sub-tick and can be retrieved using this method.- Returns:
- an array of cached drive encoder un-geared positions
-
getCachedDriveWheelFinalPositions
Obtains the Cached Readings of the Drive Encoder's Final Position (Wheel Rotations).
The values of
getDriveWheelFinalPosition()
are cached at each sub-tick and are divided by the gear ratio to obtain the final wheel rotations.- Returns:
- an array of cached drive encoder final positions (wheel rotations)
-
getCachedSteerRelativeEncoderPositions
Obtains the Cached Readings of the Steer Relative Encoder's Position.
The values of
getSteerRelativeEncoderPosition()
are cached at each sub-tick and can be retrieved using this method.- Returns:
- an array of cached steer relative encoder positions
-
getCachedSteerAbsolutePositions
Obtains the Cached Readings of the Steer Absolute Positions.
The values of
getSteerAbsoluteFacing()
are cached at each sub-tick and can be retrieved using this method.- Returns:
- an array of cached absolute steer positions, as
Rotation2d
objects
-