WPILibC++ 2024.3.2
|
TimesliceRobot extends the TimedRobot robot program framework to provide timeslice scheduling of periodic functions. More...
#include <frc/TimesliceRobot.h>
Public Member Functions | |
TimesliceRobot (units::second_t robotPeriodicAllocation, units::second_t controllerPeriod) | |
Constructor for TimesliceRobot. More... | |
void | Schedule (std::function< void()> func, units::second_t allocation) |
Schedule a periodic function with the constructor's controller period and the given allocation. More... | |
Public Member Functions inherited from frc::TimedRobot | |
void | StartCompetition () override |
Provide an alternate "main loop" via StartCompetition(). More... | |
void | EndCompetition () override |
Ends the main loop in StartCompetition(). More... | |
TimedRobot (units::second_t period=kDefaultPeriod) | |
Constructor for TimedRobot. More... | |
~TimedRobot () override | |
TimedRobot (TimedRobot &&)=default | |
TimedRobot & | operator= (TimedRobot &&)=default |
void | AddPeriodic (std::function< void()> callback, units::second_t period, units::second_t offset=0_s) |
Add a callback to run at a specific period with a starting time offset. More... | |
Public Member Functions inherited from frc::IterativeRobotBase | |
virtual void | RobotInit () |
Robot-wide initialization code should go here. More... | |
virtual void | DriverStationConnected () |
Code that needs to know the DS state should go here. More... | |
virtual void | SimulationInit () |
Robot-wide simulation initialization code should go here. More... | |
virtual void | DisabledInit () |
Initialization code for disabled mode should go here. More... | |
virtual void | AutonomousInit () |
Initialization code for autonomous mode should go here. More... | |
virtual void | TeleopInit () |
Initialization code for teleop mode should go here. More... | |
virtual void | TestInit () |
Initialization code for test mode should go here. More... | |
virtual void | RobotPeriodic () |
Periodic code for all modes should go here. More... | |
virtual void | SimulationPeriodic () |
Periodic simulation code should go here. More... | |
virtual void | DisabledPeriodic () |
Periodic code for disabled mode should go here. More... | |
virtual void | AutonomousPeriodic () |
Periodic code for autonomous mode should go here. More... | |
virtual void | TeleopPeriodic () |
Periodic code for teleop mode should go here. More... | |
virtual void | TestPeriodic () |
Periodic code for test mode should go here. More... | |
virtual void | DisabledExit () |
Exit code for disabled mode should go here. More... | |
virtual void | AutonomousExit () |
Exit code for autonomous mode should go here. More... | |
virtual void | TeleopExit () |
Exit code for teleop mode should go here. More... | |
virtual void | TestExit () |
Exit code for test mode should go here. More... | |
void | SetNetworkTablesFlushEnabled (bool enabled) |
Enables or disables flushing NetworkTables every loop iteration. More... | |
void | EnableLiveWindowInTest (bool testLW) |
Sets whether LiveWindow operation is enabled during test mode. More... | |
bool | IsLiveWindowEnabledInTest () |
Whether LiveWindow operation is enabled during test mode. More... | |
units::second_t | GetPeriod () const |
Gets time period between calls to Periodic() functions. More... | |
IterativeRobotBase (units::second_t period) | |
Constructor for IterativeRobotBase. More... | |
~IterativeRobotBase () override=default | |
Public Member Functions inherited from frc::RobotBase | |
bool | IsEnabled () const |
Determine if the Robot is currently enabled. More... | |
bool | IsDisabled () const |
Determine if the Robot is currently disabled. More... | |
bool | IsAutonomous () const |
Determine if the robot is currently in Autonomous mode. More... | |
bool | IsAutonomousEnabled () const |
Determine if the robot is currently in Autonomous mode and enabled. More... | |
bool | IsTeleop () const |
Determine if the robot is currently in Operator Control mode. More... | |
bool | IsTeleopEnabled () const |
Determine if the robot is current in Operator Control mode and enabled. More... | |
bool | IsTest () const |
Determine if the robot is currently in Test mode. More... | |
bool | IsTestEnabled () const |
Determine if the robot is current in Test mode and enabled. More... | |
virtual void | StartCompetition ()=0 |
Start the main robot code. More... | |
virtual void | EndCompetition ()=0 |
Ends the main loop in StartCompetition(). More... | |
RobotBase () | |
Constructor for a generic robot program. More... | |
virtual | ~RobotBase ()=default |
Additional Inherited Members | |
Static Public Member Functions inherited from frc::RobotBase | |
static std::thread::id | GetThreadId () |
Returns the main thread ID. More... | |
static RuntimeType | GetRuntimeType () |
Get the current runtime type. More... | |
static constexpr bool | IsReal () |
Get if the robot is real. More... | |
static constexpr bool | IsSimulation () |
Get if the robot is a simulation. More... | |
Static Public Attributes inherited from frc::TimedRobot | |
static constexpr auto | kDefaultPeriod = 20_ms |
Default loop period. More... | |
Protected Member Functions inherited from frc::IterativeRobotBase | |
IterativeRobotBase (IterativeRobotBase &&)=default | |
IterativeRobotBase & | operator= (IterativeRobotBase &&)=default |
void | LoopFunc () |
Loop function. More... | |
Protected Member Functions inherited from frc::RobotBase | |
RobotBase (RobotBase &&)=default | |
RobotBase & | operator= (RobotBase &&)=default |
Static Protected Attributes inherited from frc::RobotBase | |
static std::thread::id | m_threadId |
TimesliceRobot extends the TimedRobot robot program framework to provide timeslice scheduling of periodic functions.
The TimesliceRobot class is intended to be subclassed by a user creating a robot program.
This class schedules robot operations serially in a timeslice format. TimedRobot's periodic functions are the first in the timeslice table with 0 ms offset and 20 ms period. You can schedule additional controller periodic functions at a shorter period (5 ms by default). You give each one a timeslice duration, then they're run sequentially. The main benefit of this approach is consistent starting times for each controller periodic, which can make odometry and estimators more accurate and controller outputs change more consistently.
Here's an example of measured subsystem durations and their timeslice allocations:
Subsystem | Duration (ms) | Allocation (ms) |
Total | 5.0 | 5.0 |
TimedRobot | ? | 2.0 |
Drivetrain | 1.32 | 1.5 |
Flywheel | 0.6 | 0.7 |
Turret | 0.6 | 0.8 |
Free | 0.0 | N/A |
Since TimedRobot periodic functions only run every 20ms, that leaves a 2 ms empty spot in the allocation table for three of the four 5 ms cycles comprising 20 ms. That's OK because the OS needs time to do other things.
If the robot periodic functions and the controller periodic functions have a lot of scheduling jitter that cause them to occasionally overlap with later timeslices, consider giving the main robot thread a real-time priority using frc::SetCurrentThreadPriority(). An RT priority of 15 is a reasonable choice.
If you do enable RT though, make sure your periodic functions do not block. If they do, the operating system will lock up, and you'll have to boot the roboRIO into safe mode and delete the robot program to recover.
|
explicit |
Constructor for TimesliceRobot.
robotPeriodicAllocation | The allocation to give the TimesliceRobot periodic functions. |
controllerPeriod | The controller period. The sum of all scheduler allocations should be less than or equal to this value. |
void frc::TimesliceRobot::Schedule | ( | std::function< void()> | func, |
units::second_t | allocation | ||
) |
Schedule a periodic function with the constructor's controller period and the given allocation.
The function's runtime allocation will be placed after the end of the previous one's.
If a call to this function makes the allocations exceed the controller period, an exception will be thrown since that means the TimesliceRobot periodic functions and the given function will have conflicting timeslices.
func | Function to schedule. |
allocation | The function's runtime allocation out of the controller period. |