WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
wpi::TimedRobot Class Reference

TimedRobot implements the IterativeRobotBase robot program framework. More...

#include <wpi/framework/TimedRobot.hpp>

Inheritance diagram for wpi::TimedRobot:
wpi::IterativeRobotBase wpi::RobotBase wpi::TimesliceRobot

Public Member Functions

void StartCompetition () override
 Provide an alternate "main loop" via StartCompetition().
void EndCompetition () override
 Ends the main loop in StartCompetition().
 TimedRobot (wpi::units::second_t period=DEFAULT_PERIOD)
 Constructor for TimedRobot.
 TimedRobot (wpi::units::hertz_t frequency)
 Constructor for TimedRobot.
 TimedRobot (TimedRobot &&)=default
TimedRobotoperator= (TimedRobot &&)=default
 ~TimedRobot () override
wpi::units::microsecond_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.
Public Member Functions inherited from wpi::IterativeRobotBase
virtual void DriverStationConnected ()
 Code that needs to know the DS state should go here.
virtual void SimulationInit ()
 Robot-wide simulation initialization code should go here.
virtual void DisabledInit ()
 Initialization code for disabled mode should go here.
virtual void AutonomousInit ()
 Initialization code for autonomous mode should go here.
virtual void TeleopInit ()
 Initialization code for teleop mode should go here.
virtual void UtilityInit ()
 Initialization code for utility mode should go here.
virtual void RobotPeriodic ()
 Periodic code for all modes should go here.
virtual void SimulationPeriodic ()
 Periodic simulation code should go here.
virtual void DisabledPeriodic ()
 Periodic code for disabled mode should go here.
virtual void AutonomousPeriodic ()
 Periodic code for autonomous mode should go here.
virtual void TeleopPeriodic ()
 Periodic code for teleop mode should go here.
virtual void UtilityPeriodic ()
 Periodic code for utility mode should go here.
virtual void DisabledExit ()
 Exit code for disabled mode should go here.
virtual void AutonomousExit ()
 Exit code for autonomous mode should go here.
virtual void TeleopExit ()
 Exit code for teleop mode should go here.
virtual void UtilityExit ()
 Exit code for utility mode should go here.
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.
 IterativeRobotBase (wpi::units::second_t period)
 Constructor for IterativeRobotBase.
 ~IterativeRobotBase () override=default
Public Member Functions inherited from wpi::RobotBase
 RobotBase ()
 Constructor for a generic robot program.
virtual ~RobotBase ()=default

Static Public Attributes

static constexpr auto DEFAULT_PERIOD = 20_ms
 Default loop period.

Protected Attributes

wpi::hal::Handle< HAL_NotifierHandle, HAL_DestroyNotifierm_notifier
std::chrono::microseconds m_startTime
Protected Attributes inherited from wpi::RobotBase
NT_Listener connListenerHandle

Additional Inherited Members

Static Public Member Functions inherited from wpi::RobotBase
static bool IsEnabled ()
 Determine if the Robot is currently enabled.
static bool IsDisabled ()
 Determine if the Robot is currently disabled.
static bool IsAutonomous ()
 Determine if the robot is currently in Autonomous mode.
static bool IsAutonomousEnabled ()
 Determine if the robot is currently in Autonomous mode and enabled.
static bool IsTeleop ()
 Determine if the robot is currently in Operator Control mode.
static bool IsTeleopEnabled ()
 Determine if the robot is current in Operator Control mode and enabled.
static bool IsUtility ()
 Determine if the robot is currently in Utility mode.
static bool IsUtilityEnabled ()
 Determine if the robot is currently in Utility mode and enabled.
static int64_t GetOpModeId ()
 Gets the currently selected operating mode of the driver station.
static std::string GetOpMode ()
 Gets the currently selected operating mode of the driver station.
static std::thread::id GetThreadId ()
 Returns the main thread ID.
static RuntimeType GetRuntimeType ()
 Get the current runtime type.
static constexpr bool IsReal ()
 Get if the robot is real.
static constexpr bool IsSimulation ()
 Get if the robot is a simulation.
Protected Member Functions inherited from wpi::IterativeRobotBase
 IterativeRobotBase (IterativeRobotBase &&)=default
IterativeRobotBaseoperator= (IterativeRobotBase &&)=default
void LoopFunc ()
 Loop function.
Protected Member Functions inherited from wpi::RobotBase
 RobotBase (RobotBase &&)=default
RobotBaseoperator= (RobotBase &&)=default
Static Protected Attributes inherited from wpi::RobotBase
static std::thread::id m_threadId

Detailed Description

TimedRobot implements the IterativeRobotBase robot program framework.

The TimedRobot class is intended to be subclassed by a user creating a robot program.

Periodic() functions from the base class are called on an interval by a Notifier instance.

Constructor & Destructor Documentation

◆ TimedRobot() [1/3]

wpi::TimedRobot::TimedRobot ( wpi::units::second_t period = DEFAULT_PERIOD)
explicit

Constructor for TimedRobot.

Parameters
periodThe period of the robot loop function.

◆ TimedRobot() [2/3]

wpi::TimedRobot::TimedRobot ( wpi::units::hertz_t frequency)
explicit

Constructor for TimedRobot.

Parameters
frequencyThe frequency of the robot loop function.

◆ TimedRobot() [3/3]

wpi::TimedRobot::TimedRobot ( TimedRobot && )
default

◆ ~TimedRobot()

wpi::TimedRobot::~TimedRobot ( )
override

Member Function Documentation

◆ AddPeriodic()

void wpi::TimedRobot::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 TimedRobot's Notifier, so TimedRobot 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.

◆ EndCompetition()

void wpi::TimedRobot::EndCompetition ( )
overridevirtual

Ends the main loop in StartCompetition().

Implements wpi::RobotBase.

◆ GetLoopStartTime()

wpi::units::microsecond_t wpi::TimedRobot::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.GetMonotonicTimestamp(), 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.

◆ operator=()

TimedRobot & wpi::TimedRobot::operator= ( TimedRobot && )
default

◆ StartCompetition()

void wpi::TimedRobot::StartCompetition ( )
overridevirtual

Provide an alternate "main loop" via StartCompetition().

Implements wpi::RobotBase.

Member Data Documentation

◆ DEFAULT_PERIOD

auto wpi::TimedRobot::DEFAULT_PERIOD = 20_ms
staticconstexpr

Default loop period.

◆ m_notifier

wpi::hal::Handle<HAL_NotifierHandle, HAL_DestroyNotifier> wpi::TimedRobot::m_notifier
protected

◆ m_startTime

std::chrono::microseconds wpi::TimedRobot::m_startTime
protected

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