Class PeriodicOpMode
java.lang.Object
org.wpilib.opmode.PeriodicOpMode
- All Implemented Interfaces:
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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor.protectedPeriodicOpMode(double period) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddPeriodic(Runnable callback, double period) Add a callback to run at a specific period.final voidaddPeriodic(Runnable callback, double period, double offset) Add a callback to run at a specific period with a starting time offset.final voidaddPeriodic(Runnable callback, Time period) Add a callback to run at a specific period.final voidaddPeriodic(Runnable callback, Time period, Time offset) Add a callback to run at a specific period with a starting time offset.voidclose()Called when the opmode is de-selected on the DS.voidCalled periodically while the opmode is selected on the DS (robot is disabled).voidend()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.longReturn the system clock time in micrseconds for the start of the current periodic loop.doubleGets time period between calls to Periodic() functions.protected voidloopFunc()Loop function.final voidThis function is called when the opmode is no longer selected on the DS or after opModeRun() returns.final voidopModeRun(long opModeId) This function is called when the opmode starts (robot is enabled).final voidThis function is called asynchronously when the robot is disabled, to request the opmode return from opModeRun().abstract voidperiodic()Called periodically while the robot is enabled.voidPrints list of epochs added so far and their times.voidstart()Called a single time when the robot transitions from disabled to enabled.
-
Field Details
-
kDefaultPeriod
-
-
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
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
Called periodically while the opmode is selected on the DS (robot is disabled).- Specified by:
disabledPeriodicin interfaceOpMode
-
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
Called a single time when the robot transitions from disabled to enabled. This is called prior to periodic() being called. -
periodic
Called periodically while the robot is enabled. -
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
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
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
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
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
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
Gets time period between calls to Periodic() functions.- Returns:
- The time period between calls to Periodic() functions.
-
loopFunc
Loop function. -
opModeRun
-
opModeStop
Description copied from interface:OpModeThis function is called asynchronously when the robot is disabled, to request the opmode return from opModeRun().- Specified by:
opModeStopin interfaceOpMode
-
opModeClose
Description copied from interface:OpModeThis 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:
opModeClosein interfaceOpMode
-
printWatchdogEpochs
Prints list of epochs added so far and their times.
-