WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
wpi::PeriodicOpMode Class Referenceabstract

An opmode structure for periodic operation. More...

#include <wpi/opmode/PeriodicOpMode.hpp>

Inheritance diagram for wpi::PeriodicOpMode:
wpi::OpMode

Public Member Functions

 ~PeriodicOpMode () override
void DisabledPeriodic () override
 Called periodically while the opmode is selected on the DS (robot is disabled).
virtual void Start ()
 Called a single time when the robot transitions from disabled to enabled.
virtual void Periodic ()=0
 Called periodically while the robot is enabled.
virtual void End ()
 Called a single time when the robot transitions from enabled to disabled, or just before the destructor is called if a different opmode is selected while the robot is enabled.
int64_t GetLoopStartTime () const
 Return the system clock time in microseconds for the start of the current periodic loop.
void AddPeriodic (std::function< void()> callback, wpi::units::second_t period, wpi::units::second_t offset=0_s)
 Add a callback to run at a specific period with a starting time offset.
wpi::units::second_t GetPeriod () const
 Gets time period between calls to Periodic() functions.
void PrintWatchdogEpochs ()
 Prints list of epochs added so far and their times.
void OpModeRun (int64_t opModeId) final
 This function is called when the opmode starts (robot is enabled).
void OpModeStop () final
 This function is called asynchronously when the robot is disabled, to request the opmode return from OpModeRun().
Public Member Functions inherited from wpi::OpMode
virtual ~OpMode ()=default
 The object is destroyed when the opmode is no longer selected on the DS or after OpModeRun() returns.

Static Public Attributes

static constexpr auto kDefaultPeriod = 20_ms
 Default loop period.

Protected Member Functions

 PeriodicOpMode (wpi::units::second_t period=kDefaultPeriod)
 Constructor.
void LoopFunc ()
 Loop function.

Detailed Description

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 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 the object being destroyed; the object is not reused
  • If a different opmode is selected on the driver station when the DS is disabled, the object is destroyed (without End() being called); the object is not reused

Constructor & Destructor Documentation

◆ PeriodicOpMode()

wpi::PeriodicOpMode::PeriodicOpMode ( wpi::units::second_t period = kDefaultPeriod)
explicitprotected

Constructor.

Periodic opmodes may specify the period used for the Periodic() function.

Parameters
periodperiod for callbacks to the Periodic() function

◆ ~PeriodicOpMode()

wpi::PeriodicOpMode::~PeriodicOpMode ( )
override

Member Function Documentation

◆ AddPeriodic()

void wpi::PeriodicOpMode::AddPeriodic ( std::function< void()> callback,
wpi::units::second_t period,
wpi::units::second_t offset = 0_s )

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
callbackThe callback to run.
periodThe period at which to run the callback.
offsetThe offset from the common starting time. This is useful for scheduling a callback in a different timeslot relative to TimedRobot.

◆ DisabledPeriodic()

void wpi::PeriodicOpMode::DisabledPeriodic ( )
inlineoverridevirtual

Called periodically while the opmode is selected on the DS (robot is disabled).

Reimplemented from wpi::OpMode.

◆ End()

virtual void wpi::PeriodicOpMode::End ( )
inlinevirtual

Called a single time when the robot transitions from enabled to disabled, or just before the destructor is called if a different opmode is selected while the robot is enabled.

◆ GetLoopStartTime()

int64_t wpi::PeriodicOpMode::GetLoopStartTime ( ) const
inline

Return the system clock time in microseconds 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.

◆ GetPeriod()

wpi::units::second_t wpi::PeriodicOpMode::GetPeriod ( ) const
inline

Gets time period between calls to Periodic() functions.

◆ LoopFunc()

void wpi::PeriodicOpMode::LoopFunc ( )
protected

Loop function.

◆ OpModeRun()

void wpi::PeriodicOpMode::OpModeRun ( int64_t opModeId)
finalvirtual

This function is called when the opmode starts (robot is enabled).

Parameters
opModeIdopmode unique ID

Implements wpi::OpMode.

◆ OpModeStop()

void wpi::PeriodicOpMode::OpModeStop ( )
finalvirtual

This function is called asynchronously when the robot is disabled, to request the opmode return from OpModeRun().

Implements wpi::OpMode.

◆ Periodic()

virtual void wpi::PeriodicOpMode::Periodic ( )
pure virtual

Called periodically while the robot is enabled.

◆ PrintWatchdogEpochs()

void wpi::PeriodicOpMode::PrintWatchdogEpochs ( )

Prints list of epochs added so far and their times.

◆ Start()

virtual void wpi::PeriodicOpMode::Start ( )
inlinevirtual

Called a single time when the robot transitions from disabled to enabled.

This is called prior to Periodic() being called.

Member Data Documentation

◆ kDefaultPeriod

auto wpi::PeriodicOpMode::kDefaultPeriod = 20_ms
staticconstexpr

Default loop period.


The documentation for this class was generated from the following file: