WPILibC++ 2023.4.3
TimesliceRobot.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8
9#include <units/time.h>
10
11#include "frc/TimedRobot.h"
12
13namespace frc {
14
15/**
16 * TimesliceRobot extends the TimedRobot robot program framework to provide
17 * timeslice scheduling of periodic functions.
18 *
19 * The TimesliceRobot class is intended to be subclassed by a user creating a
20 * robot program.
21 *
22 * This class schedules robot operations serially in a timeslice format.
23 * TimedRobot's periodic functions are the first in the timeslice table with 0
24 * ms offset and 20 ms period. You can schedule additional controller periodic
25 * functions at a shorter period (5 ms by default). You give each one a
26 * timeslice duration, then they're run sequentially. The main benefit of this
27 * approach is consistent starting times for each controller periodic, which can
28 * make odometry and estimators more accurate and controller outputs change more
29 * consistently.
30 *
31 * Here's an example of measured subsystem durations and their timeslice
32 * allocations:
33 *
34 * <table>
35 * <tr>
36 * <td><b>Subsystem</b></td>
37 * <td><b>Duration (ms)</b></td>
38 * <td><b>Allocation (ms)</b></td>
39 * </tr>
40 * <tr>
41 * <td><b>Total</b></td>
42 * <td>5.0</td>
43 * <td>5.0</td>
44 * </tr>
45 * <tr>
46 * <td>TimedRobot</td>
47 * <td>?</td>
48 * <td>2.0</td>
49 * </tr>
50 * <tr>
51 * <td>Drivetrain</td>
52 * <td>1.32</td>
53 * <td>1.5</td>
54 * </tr>
55 * <tr>
56 * <td>Flywheel</td>
57 * <td>0.6</td>
58 * <td>0.7</td>
59 * </tr>
60 * <tr>
61 * <td>Turret</td>
62 * <td>0.6</td>
63 * <td>0.8</td>
64 * </tr>
65 * <tr>
66 * <td><b>Free</b></td>
67 * <td>0.0</td>
68 * <td>N/A</td>
69 * </tr>
70 * </table>
71 *
72 * Since TimedRobot periodic functions only run every 20ms, that leaves a 2 ms
73 * empty spot in the allocation table for three of the four 5 ms cycles
74 * comprising 20 ms. That's OK because the OS needs time to do other things.
75 *
76 * If the robot periodic functions and the controller periodic functions have a
77 * lot of scheduling jitter that cause them to occasionally overlap with later
78 * timeslices, consider giving the main robot thread a real-time priority using
79 * frc::SetCurrentThreadPriority(). An RT priority of 15 is a reasonable choice.
80 *
81 * If you do enable RT though, <i>make sure your periodic functions do not
82 * block</i>. If they do, the operating system will lock up, and you'll have to
83 * boot the roboRIO into safe mode and delete the robot program to recover.
84 */
85class TimesliceRobot : public TimedRobot {
86 public:
87 /**
88 * Constructor for TimesliceRobot.
89 *
90 * @param robotPeriodicAllocation The allocation to give the TimesliceRobot
91 * periodic functions.
92 * @param controllerPeriod The controller period. The sum of all scheduler
93 * allocations should be less than or equal to this
94 * value.
95 */
96 explicit TimesliceRobot(units::second_t robotPeriodicAllocation,
97 units::second_t controllerPeriod);
98
99 /**
100 * Schedule a periodic function with the constructor's controller period and
101 * the given allocation. The function's runtime allocation will be placed
102 * after the end of the previous one's.
103 *
104 * If a call to this function makes the allocations exceed the controller
105 * period, an exception will be thrown since that means the TimesliceRobot
106 * periodic functions and the given function will have conflicting
107 * timeslices.
108 *
109 * @param func Function to schedule.
110 * @param allocation The function's runtime allocation out of the controller
111 * period.
112 */
113 void Schedule(std::function<void()> func, units::second_t allocation);
114
115 private:
116 units::second_t m_nextOffset;
117 units::second_t m_controllerPeriod;
118};
119
120} // namespace frc
TimedRobot implements the IterativeRobotBase robot program framework.
Definition: TimedRobot.h:30
TimesliceRobot extends the TimedRobot robot program framework to provide timeslice scheduling of peri...
Definition: TimesliceRobot.h:85
TimesliceRobot(units::second_t robotPeriodicAllocation, units::second_t controllerPeriod)
Constructor for TimesliceRobot.
void Schedule(std::function< void()> func, units::second_t allocation)
Schedule a periodic function with the constructor's controller period and the given allocation.
Definition: AprilTagFieldLayout.h:22