Class PeriodicOpMode

java.lang.Object
org.wpilib.opmode.PeriodicOpMode
All Implemented Interfaces:
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 the 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 the 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
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default loop period.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor.
    protected
    PeriodicOpMode(double period)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    addPeriodic(Runnable callback, double period)
    Add a callback to run at a specific period.
    final void
    addPeriodic(Runnable callback, double period, double offset)
    Add a callback to run at a specific period with a starting time offset.
    final void
    addPeriodic(Runnable callback, Time period)
    Add a callback to run at a specific period.
    final void
    addPeriodic(Runnable callback, Time period, Time offset)
    Add a callback to run at a specific period with a starting time offset.
    void
    Called when the opmode is de-selected on the DS.
    void
    Called periodically while the opmode is selected on the DS (robot is disabled).
    void
    end()
    Called a single time when the robot transitions from enabled to disabled, or just before close() is called if a different opmode is selected while the robot is enabled.
    long
    Return the system clock time in micrseconds for the start of the current periodic loop.
    double
    Gets time period between calls to Periodic() functions.
    protected void
    Loop function.
    final void
    This function is called when the opmode is no longer selected on the DS or after opModeRun() returns.
    final void
    opModeRun(long opModeId)
    This function is called when the opmode starts (robot is enabled).
    final void
    This function is called asynchronously when the robot is disabled, to request the opmode return from opModeRun().
    abstract void
    Called periodically while the robot is enabled.
    void
    Prints list of epochs added so far and their times.
    void
    Called a single time when the robot transitions from disabled to enabled.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • PeriodicOpMode

      protected PeriodicOpMode()
      Constructor. Periodic opmodes may specify the period used for the periodic() function; the no-argument constructor uses a default period of 20 ms.
    • PeriodicOpMode

      protected PeriodicOpMode(double period)
      Constructor. Periodic opmodes may specify the period used for the periodic() function.
      Parameters:
      period - period (in seconds) for callbacks to the periodic() function
  • Method Details

    • disabledPeriodic

      public void disabledPeriodic()
      Called periodically while the opmode is selected on the DS (robot is disabled).
      Specified by:
      disabledPeriodic in interface OpMode
    • close

      public void close()
      Called when the opmode is de-selected on the DS. The object is not reused even if the same opmode is selected again (a new object will be created).
    • start

      public void start()
      Called a single time when the robot transitions from disabled to enabled. This is called prior to periodic() being called.
    • periodic

      public abstract void periodic()
      Called periodically while the robot is enabled.
    • end

      public void end()
      Called a single time when the robot transitions from enabled to disabled, or just before close() is called if a different opmode is selected while the robot is enabled.
    • getLoopStartTime

      public long getLoopStartTime()
      Return the system clock time in micrseconds for the start of the current periodic loop. This is in the same time base as Timer.getFPGATimestamp(), 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.
    • addPeriodic

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

      This is scheduled on the same Notifier as periodic(), so periodic() 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 the same Notifier as periodic(), so periodic() 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 PeriodicOpMode.
    • addPeriodic

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

      This is scheduled on the same Notifier as periodic(), so periodic() 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.
    • addPeriodic

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

      This is scheduled on the same Notifier as periodic(), so periodic() 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.
      offset - The offset from the common starting time. This is useful for scheduling a callback in a different timeslot relative to PeriodicOpMode.
    • getPeriod

      public double getPeriod()
      Gets time period between calls to Periodic() functions.
      Returns:
      The time period between calls to Periodic() functions.
    • loopFunc

      protected void loopFunc()
      Loop function.
    • opModeRun

      public final void opModeRun(long opModeId)
      Description copied from interface: OpMode
      This function is called when the opmode starts (robot is enabled).
      Specified by:
      opModeRun in interface OpMode
      Parameters:
      opModeId - opmode unique ID
    • opModeStop

      public final void opModeStop()
      Description copied from interface: OpMode
      This function is called asynchronously when the robot is disabled, to request the opmode return from opModeRun().
      Specified by:
      opModeStop in interface OpMode
    • opModeClose

      public final void opModeClose()
      Description copied from interface: OpMode
      This function is called when the opmode is no longer selected on the DS or after opModeRun() returns. The object will not be reused after this is called.
      Specified by:
      opModeClose in interface OpMode
    • printWatchdogEpochs

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