Class SwerveDriveSimulation

java.lang.Object
org.dyn4j.collision.AbstractCollisionBody<org.dyn4j.dynamics.BodyFixture>
org.dyn4j.dynamics.AbstractPhysicsBody
org.dyn4j.dynamics.Body
org.ironmaple.simulation.drivesims.AbstractDriveTrainSimulation
org.ironmaple.simulation.drivesims.SwerveDriveSimulation
All Implemented Interfaces:
org.dyn4j.collision.CollisionBody<org.dyn4j.dynamics.BodyFixture>, org.dyn4j.DataContainer, org.dyn4j.dynamics.PhysicsBody, org.dyn4j.geometry.Rotatable, org.dyn4j.geometry.Shiftable, org.dyn4j.geometry.Transformable, org.dyn4j.geometry.Translatable, org.dyn4j.Ownable

public class SwerveDriveSimulation extends AbstractDriveTrainSimulation

Simulates a Swerve Drivetrain.

Check Online Documentation

1. Purpose

This class simulates a swerve drivetrain composed of more than two SwerveModuleSimulation modules.

It provides a realistic modeling of drivetrain physics, replicating wheel grip and motor propulsion for an actual swerve drive.

2. Simulation Dynamics

3. Odometry Simulation

To simulate odometry, follow these steps:

Refer to the ModuleIOSim.java example project for more details.

Vision Simulation

You can obtain the real robot pose from AbstractDriveTrainSimulation.getSimulatedDriveTrainPose() and feed it to the PhotonVision simulation to simulate vision.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final GyroSimulation
     
    protected final SwerveDriveKinematics
     
    protected final Translation2d[]
     

    Fields inherited from class org.ironmaple.simulation.drivesims.AbstractDriveTrainSimulation

    BUMPER_COEFFICIENT_OF_FRICTION, BUMPER_COEFFICIENT_OF_RESTITUTION, config

    Fields inherited from class org.dyn4j.dynamics.AbstractPhysicsBody

    angularDamping, angularVelocity, atRest, atRestDetectionEnabled, atRestTime, bullet, force, forces, gravityScale, linearDamping, linearVelocity, mass, torque, torques

    Fields inherited from class org.dyn4j.collision.AbstractCollisionBody

    enabled, fixtureModificationHandler, fixtures, fixturesUnmodifiable, owner, radius, transform, transform0, userData

    Fields inherited from interface org.dyn4j.collision.CollisionBody

    TYPICAL_FIXTURE_COUNT

    Fields inherited from interface org.dyn4j.dynamics.PhysicsBody

    DEFAULT_ANGULAR_DAMPING, DEFAULT_LINEAR_DAMPING
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a Swerve Drive Simulation.
  • Method Summary

    Modifier and Type
    Method
    Description
    edu.wpi.first.units.measure.Distance
    Obtains the drive base radius of the swerve drive.
     
     
    edu.wpi.first.units.measure.AngularAcceleration
    Obtains the maximum achievable angular acceleration of the chassis.
    edu.wpi.first.units.measure.AngularVelocity
    Obtains the maximum achievable angular velocity of the chassis.
    edu.wpi.first.units.measure.LinearAcceleration
    Obtains the maximum achievable linear acceleration of the chassis.
    edu.wpi.first.units.measure.LinearVelocity
    Obtains the maximum achievable linear velocity of the chassis.
    void
    Updates the Swerve Drive Simulation.

    Methods inherited from class org.dyn4j.dynamics.AbstractPhysicsBody

    accumulate, addFixture, addFixture, addFixture, applyForce, applyForce, applyForce, applyImpulse, applyImpulse, applyImpulse, applyTorque, applyTorque, clearAccumulatedForce, clearAccumulatedTorque, clearForce, clearTorque, computeSweptAABB, computeSweptAABB, createSweptAABB, createSweptAABB, getAccumulatedForce, getAccumulatedTorque, getAngularDamping, getAngularVelocity, getChangeInOrientation, getChangeInPosition, getForce, getGravityScale, getLinearDamping, getLinearVelocity, getLinearVelocity, getLocalCenter, getMass, getTorque, getWorldCenter, integratePosition, integrateVelocity, isAtRest, isAtRestDetectionEnabled, isBullet, isDynamic, isKinematic, isStatic, removeAllFixtures, removeFixture, removeFixture, removeFixture, removeFixtures, setAngularDamping, setAngularVelocity, setAtRest, setAtRestDetectionEnabled, setBullet, setEnabled, setGravityScale, setLinearDamping, setLinearVelocity, setLinearVelocity, setMass, setMass, setMassType, toString, updateAtRestTime, updateMass

    Methods inherited from class org.dyn4j.collision.AbstractCollisionBody

    addFixture, computeAABB, computeAABB, contains, containsFixture, createAABB, createAABB, getFixture, getFixture, getFixtureCount, getFixtureIterator, getFixtureModificationHandler, getFixtures, getFixtures, getLocalPoint, getLocalVector, getOwner, getPreviousTransform, getRotationDiscRadius, getTransform, getUserData, getWorldPoint, getWorldVector, isEnabled, rotate, rotate, rotate, rotate, rotate, rotate, rotateAboutCenter, setFixtureModificationHandler, setOwner, setRotationDiscRadius, setTransform, setUserData, shift, translate, translate, translateToOrigin

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.dyn4j.collision.CollisionBody

    addFixture, addFixture, computeAABB, computeAABB, contains, containsFixture, createAABB, createAABB, getFixture, getFixture, getFixtureCount, getFixtureIterator, getFixtureModificationHandler, getFixtures, getFixtures, getLocalCenter, getLocalPoint, getLocalVector, getPreviousTransform, getRotationDiscRadius, getTransform, getWorldCenter, getWorldPoint, getWorldVector, isEnabled, removeAllFixtures, removeFixture, removeFixture, removeFixture, removeFixtures, rotateAboutCenter, setEnabled, setFixtureModificationHandler, setTransform, translateToOrigin

    Methods inherited from interface org.dyn4j.DataContainer

    getUserData, setUserData

    Methods inherited from interface org.dyn4j.Ownable

    getOwner, setOwner

    Methods inherited from interface org.dyn4j.dynamics.PhysicsBody

    addFixture, addFixture, applyForce, applyForce, applyForce, applyImpulse, applyImpulse, applyImpulse, applyTorque, applyTorque, clearAccumulatedForce, clearAccumulatedTorque, clearForce, clearTorque, computeSweptAABB, computeSweptAABB, createSweptAABB, createSweptAABB, getAccumulatedForce, getAccumulatedTorque, getAngularDamping, getAngularVelocity, getChangeInOrientation, getChangeInPosition, getForce, getGravityScale, getLinearDamping, getLinearVelocity, getLinearVelocity, getMass, getTorque, integratePosition, integrateVelocity, isAtRest, isAtRestDetectionEnabled, isBullet, isDynamic, isKinematic, isStatic, setAngularDamping, setAngularVelocity, setAtRest, setAtRestDetectionEnabled, setBullet, setGravityScale, setLinearDamping, setLinearVelocity, setLinearVelocity, setMass, setMass, setMassType, updateAtRestTime, updateMass

    Methods inherited from interface org.dyn4j.geometry.Rotatable

    rotate, rotate, rotate, rotate, rotate, rotate

    Methods inherited from interface org.dyn4j.geometry.Shiftable

    shift

    Methods inherited from interface org.dyn4j.geometry.Translatable

    translate, translate
  • Field Details

  • Constructor Details

    • SwerveDriveSimulation

      public SwerveDriveSimulation(DriveTrainSimulationConfig config, Pose2d initialPoseOnField)

      Creates a Swerve Drive Simulation.

      This constructor initializes a swerve drive simulation with the given robot mass, bumper dimensions, module simulations, module translations, gyro simulation, and initial pose on the field.

      Parameters:
      config - a DriveTrainSimulationConfig instance containing the configurations of * this drivetrain
      initialPoseOnField - the initial pose of the drivetrain in the simulation world, represented as a Pose2d
  • Method Details

    • simulationSubTick

      public void simulationSubTick()

      Updates the Swerve Drive Simulation.

      This method performs the following actions during each sub-tick of the simulation:

      • Applies the translational friction force to the physics engine.
      • Applies the rotational friction torque to the physics engine.
      • Updates the simulation of each swerve module.
      • Applies the propelling forces of the modules to the physics engine.
      • Updates the gyro simulation of the drivetrain.
      Specified by:
      simulationSubTick in class AbstractDriveTrainSimulation
    • maxLinearVelocity

      public edu.wpi.first.units.measure.LinearVelocity maxLinearVelocity()

      Obtains the maximum achievable linear velocity of the chassis.

      Returns:
      the maximum linear velocity
      See Also:
    • maxLinearAcceleration

      public edu.wpi.first.units.measure.LinearAcceleration maxLinearAcceleration()

      Obtains the maximum achievable linear acceleration of the chassis.

      Returns:
      the maximum linear acceleration'
      See Also:
    • driveBaseRadius

      public edu.wpi.first.units.measure.Distance driveBaseRadius()

      Obtains the drive base radius of the swerve drive.

      Returns:
      the drive base radius.
    • maxAngularVelocity

      public edu.wpi.first.units.measure.AngularVelocity maxAngularVelocity()

      Obtains the maximum achievable angular velocity of the chassis.

      Returns:
      the maximum angular velocity
    • maxAngularAcceleration

      public edu.wpi.first.units.measure.AngularAcceleration maxAngularAcceleration()

      Obtains the maximum achievable angular acceleration of the chassis.

      Returns:
      the maximum angular acceleration
    • getModules

      public SwerveModuleSimulation[] getModules()
    • getGyroSimulation

      public GyroSimulation getGyroSimulation()