Class PeriodicOpMode
- All Implemented Interfaces:
AutoCloseable, OpMode
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 Summary
Constructors -
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.Returns a set of custom periodic callbacks to be executed while the opmode is enabled.
-
Constructor Details
-
PeriodicOpMode
protected PeriodicOpMode()Constructor for PeriodicOpMode.
-
-
Method Details
-
getCallbacks
Description copied from interface:OpModeReturns 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.Registration timing: The callbacks returned by this method are registered with the framework immediately when the OpMode is constructed and begin executing as soon as they are registered. To restrict execution to only when the robot is enabled, callbacks should include an if (RobotState.isEnabled()) check at the beginning.
- Specified by:
getCallbacksin interfaceOpMode- 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
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
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.
-