Class SimulatedArena

java.lang.Object
org.ironmaple.simulation.SimulatedArena
Direct Known Subclasses:
Arena2024Crescendo, Arena2025Reefscape, Arena2026Rebuilt, ArenaEvergreen

public abstract class SimulatedArena extends Object

Abstract Simulation World

Check Online Documentation

The heart of the simulator.

This class cannot be instantiated directly; it must be created as a specific season's arena.

The default instance can be obtained using the getInstance() method.

Simulates all interactions within the arena field.

The following objects can be added to the simulation world and will interact with each other:

  • Field Details

    • ALLOW_CREATION_ON_REAL_ROBOT

      public static boolean ALLOW_CREATION_ON_REAL_ROBOT
      Whether to allow the simulation to run a real robot This feature is HIGHLY RECOMMENDED to be turned OFF
    • redScore

      protected int redScore
    • blueScore

      protected int blueScore
    • matchClock

      protected double matchClock
    • redScoringBreakdown

      public Map<String,Double> redScoringBreakdown
    • blueScoringBreakdown

      public Map<String,Double> blueScoringBreakdown
    • redPublishers

      protected Map<String,DoublePublisher> redPublishers
    • bluePublishers

      protected Map<String,DoublePublisher> bluePublishers
    • redTable

      public NetworkTable redTable
    • blueTable

      public NetworkTable blueTable
    • genericInfoTable

      public NetworkTable genericInfoTable
    • matchClockPublisher

      public DoublePublisher matchClockPublisher
    • resetFieldPublisher

      public static BooleanPublisher resetFieldPublisher
    • resetFieldSubscriber

      public static BooleanSubscriber resetFieldSubscriber
    • physicsWorld

      protected final org.dyn4j.world.World<org.dyn4j.dynamics.Body> physicsWorld
    • driveTrainSimulations

      protected final Set<AbstractDriveTrainSimulation> driveTrainSimulations
    • gamePieces

      protected final Set<GamePiece> gamePieces
    • customSimulations

      protected final List<SimulatedArena.Simulatable> customSimulations
  • Constructor Details

    • SimulatedArena

      protected SimulatedArena(SimulatedArena.FieldMap obstaclesMap)

      Constructs a new simulation arena with the specified field map of obstacles.

      This constructor initializes a physics world with zero gravity and adds the provided obstacles to the world.

      It also sets up the collections for drivetrain simulations, game pieces, projectiles, and intake simulations.

      Parameters:
      obstaclesMap - the season-specific field map containing the layout of obstacles for the simulation
  • Method Details

    • getInstance

      public static SimulatedArena getInstance()

      Gets/Creates the Default Simulation World

      Multiple instances of SimulatedArena can exist elsewhere.

      Returns:
      the main simulation arena instance
      Throws:
      IllegalStateException - if the method is call when running on a real robot
    • overrideInstance

      public static void overrideInstance(SimulatedArena newInstance)

      Overrides the Default Simulation World

      Overrides the return value of getInstance()

      This method allows simulating an arena from a different year or a custom field.

      Currently, only the 2024 arena is supported, so avoid calling this method for now.

      Parameters:
      newInstance - the new simulation arena instance to override the current one
    • getSimulationSubTicksIn1Period

      public static int getSimulationSubTicksIn1Period()
    • getSimulationDt

      public static Time getSimulationDt()
    • getScore

      public int getScore(boolean isBlue)

      Returns the score of the specified team.

      Parameters:
      isBlue - The team to return the score of as a bool.
      Returns:
      The score of the specified team.
    • getScore

      public int getScore(DriverStation.Alliance allianceColor)

      Returns the score of the specified team.

      Parameters:
      allianceColor - The team to return the score of as a Alliance enum.
      Returns:
      The score of the specified team.
    • addToScore

      public void addToScore(boolean isBlue, int toAdd)

      Adds to the score of the specified team

      Parameters:
      isBlue - Wether to add to the blue or red team score.
      toAdd - How many points to add.
    • overrideSimulationTimings

      public static void overrideSimulationTimings(Time robotPeriod, int simulationSubTicksPerPeriod)

      Overrides the Timing Configurations of the Simulations.

      If Using Advantage-Kit: DO NOT CHANGE THE DEFAULT TIMINGS

      Changes apply to every instance of SimulatedArena.

      The new configuration will take effect the next time simulationPeriodic() is called on an instance.

      It is recommended to call this method before the first call to simulationPeriodic() of any instance.

      It is also recommended to keep the simulation frequency above 200 Hz for accurate simulation results.

      Parameters:
      robotPeriod - the time between two calls of simulationPeriodic(), usually obtained from IterativeRobotBase.getPeriod()
      simulationSubTicksPerPeriod - the number of Iterations, or simulationSubTick(int) that the simulation runs per each call to simulationPeriodic()
    • addCustomSimulation

      public void addCustomSimulation(SimulatedArena.Simulatable simulatable)

      Registers a custom simulation.

      Parameters:
      simulatable - the custom simulation to register
    • addIntakeSimulation

      protected void addIntakeSimulation(IntakeSimulation intakeSimulation)

      Registers an IntakeSimulation.

      NOTE: This method is automatically called in the constructor of IntakeSimulation, so you don't need to call it manually.

      The intake simulation should be bound to an AbstractDriveTrainSimulation and becomes part of its collision space.

      This method immediately starts the IntakeSimulation.GamePieceContactListener, which listens for contact between the intake and any game piece.

      Parameters:
      intakeSimulation - the intake simulation to be registered
    • addDriveTrainSimulation

      public void addDriveTrainSimulation(AbstractDriveTrainSimulation driveTrainSimulation)

      Registers an AbstractDriveTrainSimulation.

      The collision space of the drive train is immediately added to the simulation world.

      Starting from the next call to simulationPeriodic(), the AbstractDriveTrainSimulation.simulationSubTick() method will be called during each sub-tick of the simulator.

      Parameters:
      driveTrainSimulation - the drivetrain simulation to be registered
    • addGamePiece

      public void addGamePiece(GamePieceOnFieldSimulation gamePiece)

      Registers a GamePieceOnFieldSimulation to the Simulation.

      The collision space of the game piece is immediately added to the simulation world.

      IntakeSimulations will be able to interact with this game piece during the next call to simulationPeriodic().

      Parameters:
      gamePiece - the game piece to be registered in the simulation
    • enableBreakdownPublishing

      public void enableBreakdownPublishing()

      Tells the arena to start publishing the match breakdown data to network tables

    • disableBreakdownPublishing

      public void disableBreakdownPublishing()

      Tells the arena to stop publishing the match breakdown data to network tables

    • publishBreakdown

      protected void publishBreakdown()

      Publishes the match breakdown data to network tables

    • replaceValueInMatchBreakDown

      public void replaceValueInMatchBreakDown(boolean isBlueTeam, String valueKey, Double value)

      replaces or adds a value to the match scoring breakdown published to network tables

      Parameters:
      isBlueTeam - Wether to add to the blue teams match breakdown or the red teams match breakdown
      valueKey - The name of the value to be added
      value - The value to be added
    • setupValueForMatchBreakdown

      public void setupValueForMatchBreakdown(String valueKey)

      Defaults a value to 0 and creates it in the match breakdown. This is useful on startup to make sure all match breakdown values display before they are first updated

      Parameters:
      valueKey - The key to add to match breakdown
    • replaceValueInMatchBreakDown

      public void replaceValueInMatchBreakDown(boolean isBlueTeam, String valueKey, Integer value)

      replaces or adds a value to the match scoring breakdown published to network tables

      Parameters:
      isBlueTeam - Wether to add to the blue teams match breakdown or the red teams match breakdown
      valueKey - The name of the value to be added
      value - The value to be added
    • addValueToMatchBreakdown

      public void addValueToMatchBreakdown(boolean isBlueTeam, String ValueKey, Double toAdd)

      Adds too a value in the scoring breakdown. If value does not already exist in the scoring breakdown it will be defaulted to 0 and then added too

      Parameters:
      isBlueTeam - Wether to add to the blue teams match breakdown or the red teams match breakdown
      ValueKey - The name of the value to be added too
      toAdd - how much to be added to specified value
    • addValueToMatchBreakdown

      public void addValueToMatchBreakdown(boolean isBlueTeam, String valueKey, int toAdd)

      Adds too a value in the scoring breakdown. If value does not already exist in the scoring breakdown it will be defaulted to 0 and then added too

      Parameters:
      isBlueTeam - Wether to add to the blue teams match breakdown or the red teams match breakdown
      valueKey - The name of the value to be added too
      toAdd - how much to be added to specified value
    • addGamePieceProjectile

      public void addGamePieceProjectile(GamePieceProjectile gamePieceProjectile)

      Registers a GamePieceProjectile to the Simulation and Launches It.

      Calls to GamePieceProjectile.launch(), which will launch the game piece immediately.

      Parameters:
      gamePieceProjectile - the projectile to be registered and launched in the simulation
    • removeGamePiece

      public boolean removeGamePiece(GamePieceOnFieldSimulation gamePiece)

      Removes a GamePieceOnFieldSimulation from the Simulation.

      Removes the game piece from the physics world and the simulation's game piece collection.

      Parameters:
      gamePiece - the game piece to be removed from the simulation
      Returns:
      true if this set contained the specified element
    • removePiece

      public boolean removePiece(GamePiece toRemove)
    • removeProjectile

      public boolean removeProjectile(GamePieceProjectile gamePieceLaunched)

      Removes a GamePieceProjectile from the Simulation.

      Removes the game piece projectile from the simulation.

      Parameters:
      gamePieceLaunched - the game piece projectile to be removed from the simulation
      Returns:
      true if this set contained the specified element
    • clearGamePieces

      public void clearGamePieces()

      Removes All GamePieceOnFieldSimulation Objects from the Simulation.

      This method clears all game pieces from the physics world and the simulation's game piece collection.

    • shutDown

      public void shutDown()

      Shuts down the current SimulatedArena and stops all simulation so that simulatable objects may be added to a new arena

    • simulationPeriodic

      public void simulationPeriodic()

      Update the simulation world.

      This method should be called ONCE in IterativeRobotBase.simulationPeriodic() (or LoggedRobot.simulationPeriodic() if using Advantage-Kit)

      If not configured through overrideSimulationTimings(Time, int) , the simulator will iterate through 5 Sub-ticks by default.

      The amount of CPU Time that the Dyn4j engine uses in displayed in SmartDashboard/MapleArenaSimulation/Dyn4jEngineCPUTimeMS, usually performance is not a concern

    • simulationSubTick

      protected void simulationSubTick(int subTickNum)

      Processes a Single Simulation Sub-Tick.

      This method performs the actions for each sub-tick of the simulation, including:

    • gamePiecesOnField

      public Set<GamePieceOnFieldSimulation> gamePiecesOnField()

      Returns a list of all grounded pieces on the field

      Returns:
      all grounded (aka not projectile) pieces on the field as a set of GamePieceOnFieldSimulation objects
    • gamePieceLaunched

      public Set<GamePieceProjectile> gamePieceLaunched()

      Returns a list of all projectile pieces on the field

      Returns:
      all projectile pieces on the field as a set of GamePieceProjectile objects
    • getGamePiecesPosesByType

      public List<Pose3d> getGamePiecesPosesByType(String type)

      Obtains the 3D Poses of a Specific Type of Game Piece.

      This method is used to visualize the positions of game pieces

      Also, if you have a game-piece detection vision system (wow!), this is the how you can simulate it.

      Both GamePieceOnFieldSimulation and GamePieceProjectile of the specified type will be included.

      Parameters:
      type - the type of game piece, as determined by the constructor of GamePieceOnFieldSimulation
      Returns:
      a List of Pose3d objects representing the 3D positions of the game pieces
    • getGamePiecesArrayByType

      public Pose3d[] getGamePiecesArrayByType(String type)

      Obtains the 3D Poses of a Specific Type of Game Piece as an array.

      See Also:
    • getGamePiecesByType

      public List<GamePiece> getGamePiecesByType(String type)

      Returns all game pieces on the field of the specified type as a list

      Parameters:
      type - The string type to be selected.
      Returns:
      The game pieces as a list of GamePiece
    • resetFieldForAuto

      public void resetFieldForAuto()

      Resets the Field for Autonomous Mode.

      This method clears all current game pieces from the field and places new game pieces in their starting positions for the autonomous mode.

    • placeGamePiecesOnField

      public abstract void placeGamePiecesOnField()

      Places Game Pieces on the Field for Autonomous Mode.

      This method sets up the game pieces on the field, typically in their starting positions for autonomous mode.

      It should be implemented differently for each season-specific subclass of SimulatedArena to reflect the unique game piece placements for that season's game.