Class PeriodicOpMode

java.lang.Object
org.wpilib.opmode.PeriodicOpMode
All Implemented Interfaces:
AutoCloseable, OpMode

public abstract class PeriodicOpMode extends Object implements OpMode
An opmode structure for periodic operation. This base class implements a loop that runs one or more functions periodically (on a set time interval aka loop period). The primary periodic callback function is the abstract periodic() function; the time interval for this callback is 20 ms by default, but may be changed via passing a different time interval to OpModeRobot's constructor. Additional periodic callbacks with different intervals can be added using the addPeriodic() set of functions.

Lifecycle:

  • constructed when opmode selected on driver station
  • disabledPeriodic() called periodically as long as DS is disabled. Note this is not called on a set time interval (it does not use the same time interval as periodic())
  • when DS transitions from disabled to enabled, start() is called once
  • while DS is enabled, periodic() is called periodically on the time interval set by OpModeRobot's constructor, and additional periodic callbacks added via addPeriodic() are called periodically on their set time intervals
  • when DS transitions from enabled to disabled, or a different opmode is selected on the driver station when the DS is enabled, end() is called, followed by close(); the object is not reused
  • if a different opmode is selected on the driver station when the DS is disabled, only close() is called; the object is not reused

All lifecycle callbacks and periodic callbacks run synchronously on the same thread that invokes them. Interactions between opmodes and the robot framework do not require additional synchronization.

  • Constructor Details

    • PeriodicOpMode

      protected PeriodicOpMode()
      Constructor for PeriodicOpMode.
  • Method Details

    • getCallbacks

      Description copied from interface: OpMode
      Returns a set of custom periodic callbacks to be executed while the opmode is enabled.

      This method allows opmodes to register arbitrary periodic callbacks with custom execution intervals. The callbacks are executed by the robot framework at their scheduled times, in addition to the primary OpMode.periodic() callback.

      Specified by:
      getCallbacks in interface OpMode
      Returns:
      A set of custom callbacks to execute, or an empty set if no custom callbacks are needed. The default implementation returns an empty set.
    • addPeriodic

      public final void addPeriodic(Runnable callback, double period)
      Add a callback to run at a specific period.

      This is scheduled on OpModeRobot's Notifier, so OpModeRobot and the callback run synchronously. Interactions between them are thread-safe.

      Parameters:
      callback - The callback to run.
      period - The period at which to run the callback in seconds.
    • addPeriodic

      public final void addPeriodic(Runnable callback, double period, double offset)
      Add a callback to run at a specific period with a starting time offset.

      This is scheduled on TimedRobot's Notifier, so TimedRobot and the callback run synchronously. Interactions between them are thread-safe.

      Parameters:
      callback - The callback to run.
      period - The period at which to run the callback in seconds.
      offset - The offset from the common starting time in seconds. This is useful for scheduling a callback in a different timeslot relative to TimedRobot.