Class OpModeRobot

java.lang.Object
org.wpilib.framework.RobotBase
org.wpilib.framework.OpModeRobot
All Implemented Interfaces:
AutoCloseable

public abstract class OpModeRobot extends RobotBase
OpModeRobot implements the opmode-based robot program framework.

The OpModeRobot class is intended to be subclassed by a user creating a robot program.

Classes annotated with Autonomous, Teleop, and Utility in the same package or subpackages as the user's subclass are automatically registered as autonomous, teleop, and utility opmodes respectively.

Opmodes are constructed when selected on the driver station. While selected and disabled, OpMode.disabledPeriodic() is called. When enabled, OpMode.start() is called once and OpMode.periodic() runs at the rate from getPeriod(). On disable or mode switch while enabled, OpMode.end() is called asynchronously and the opmode is then closed and discarded. When no opmode is selected, nonePeriodic() is called. driverStationConnected() is called once when the DS first connects.

  • Field Details

  • Constructor Details

    • OpModeRobot

      public OpModeRobot()
      Constructor with default period.
    • OpModeRobot

      public OpModeRobot(double period)
      Constructor with specified period.
      Parameters:
      period - the period at which to run the robot and opmode periodic callbacks.
  • Method Details

    • addOpModeFactory

      public void addOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor)
      Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      factory - factory function to create the opmode
      mode - robot mode
      name - name of the operating mode
      group - group of the operating mode
      description - description of the operating mode
      textColor - text color, or null for default
      backgroundColor - background color, or null for default
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addOpModeFactory

      public void addOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name, String group, String description)
      Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      factory - factory function to create the opmode
      mode - robot mode
      name - name of the operating mode
      group - group of the operating mode
      description - description of the operating mode
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addOpModeFactory

      public void addOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name, String group)
      Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      factory - factory function to create the opmode
      mode - robot mode
      name - name of the operating mode
      group - group of the operating mode
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addOpModeFactory

      public void addOpModeFactory(Supplier<OpMode> factory, RobotMode mode, String name)
      Adds an opmode using a factory function that creates the opmode. It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      factory - factory function to create the opmode
      mode - robot mode
      name - name of the operating mode
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addOpMode

      public void addOpMode(Class<? extends OpMode> cls, RobotMode mode, String name, String group, String description, Color textColor, Color backgroundColor)
      Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      cls - class to add
      mode - robot mode
      name - name of the operating mode
      group - group of the operating mode
      description - description of the operating mode
      textColor - text color, or null for default
      backgroundColor - background color, or null for default
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addOpMode

      public void addOpMode(Class<? extends OpMode> cls, RobotMode mode, String name, String group, String description)
      Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      cls - class to add
      mode - robot mode
      name - name of the operating mode
      group - group of the operating mode
      description - description of the operating mode
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addOpMode

      public void addOpMode(Class<? extends OpMode> cls, RobotMode mode, String name, String group)
      Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      cls - class to add
      mode - robot mode
      name - name of the operating mode
      group - group of the operating mode
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addOpMode

      public void addOpMode(Class<? extends OpMode> cls, RobotMode mode, String name)
      Adds an opmode for an opmode class. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (the latter is preferred; if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      cls - class to add
      mode - robot mode
      name - name of the operating mode
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addAnnotatedOpMode

      public void addAnnotatedOpMode(Class<? extends OpMode> cls)
      Adds an opmode for an opmode class annotated with Autonomous, Teleop, or Utility. The class must be a public, non-abstract subclass of OpMode with a public constructor that either takes no arguments or accepts a single argument assignable from this robot class type (if multiple match, the most specific parameter type is used). It's necessary to call publishOpModes() to make the added mode visible to the driver station.
      Parameters:
      cls - class to add
      Throws:
      IllegalArgumentException - if class does not meet criteria
    • addAnnotatedOpModeClasses

      Scans for classes in the specified package and all nested packages that are annotated with Autonomous, Teleop, or Utility and registers them. It's necessary to call publishOpModes() to make the added modes visible to the driver station.
      Parameters:
      pkg - package to scan
    • removeOpMode

      public void removeOpMode(RobotMode mode, String name)
      Removes an operating mode option. It's necessary to call publishOpModes() to make the removed mode no longer visible to the driver station.
      Parameters:
      mode - robot mode
      name - name of the operating mode
    • publishOpModes

      public void publishOpModes()
      Publishes the operating mode options to the driver station.
    • clearOpModes

      public void clearOpModes()
      Clears all operating mode options and publishes an empty list to the driver station.
    • addPeriodic

      public void addPeriodic(Runnable callback, double period)
      Add a callback to run at a specific period.
      Parameters:
      callback - The callback to run.
      period - The period at which to run the callback.
    • getPeriod

      public double getPeriod()
      Get the period at which robot and opmode periodic callbacks are run.
      Returns:
      The period at which robot and opmode periodic callbacks are run.
    • driverStationConnected

      public void driverStationConnected()
      Code that needs to know the DS state should go here.

      Users should override this method for initialization that needs to occur after the DS is connected, such as needing the alliance information.

    • robotPeriodic

      public void robotPeriodic()
      Function called periodically every loop, regardless of enabled state or OpMode selection.
    • simulationInit

      public void simulationInit()
      Function called once during robot initialization in simulation.
    • simulationPeriodic

      public void simulationPeriodic()
      Function called periodically in simulation.
    • disabledInit

      public void disabledInit()
      Function called once when the robot becomes disabled.
    • disabledPeriodic

      public void disabledPeriodic()
      Function called periodically while the robot is disabled.
    • disabledExit

      public void disabledExit()
      Function called once when the robot exits disabled state.
    • nonePeriodic

      public void nonePeriodic()
      Function called periodically anytime when no opmode is selected, including when the Driver Station is disconnected.
    • getLoopStartTime

      public long getLoopStartTime()
      Return the system clock time in microseconds for the start of the current periodic loop. This is in the same time base as Timer.getMonotonicTimestamp(), but is stable through a loop. It is updated at the beginning of every periodic callback (including the normal periodic loop).
      Returns:
      Robot running time in microseconds, as of the start of the current periodic function.
    • startCompetition

      public final void startCompetition()
      Provide an alternate "main loop" via startCompetition().
      Specified by:
      startCompetition in class RobotBase
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Overrides:
      close in class RobotBase
    • endCompetition

      public final void endCompetition()
      Ends the main loop in startCompetition().
      Specified by:
      endCompetition in class RobotBase
    • printWatchdogEpochs

      public void printWatchdogEpochs()
      Prints list of epochs added so far and their times.