WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
TimesliceRobot.hpp
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
10#include "wpi/units/time.hpp"
11
12namespace wpi {
13
14/**
15 * TimesliceRobot extends the TimedRobot robot program framework to provide
16 * timeslice scheduling of periodic functions.
17 *
18 * The TimesliceRobot class is intended to be subclassed by a user creating a
19 * robot program.
20 *
21 * This class schedules robot operations serially in a timeslice format.
22 * TimedRobot's periodic functions are the first in the timeslice table with 0
23 * ms offset and 20 ms period. You can schedule additional controller periodic
24 * functions at a shorter period (5 ms by default). You give each one a
25 * timeslice duration, then they're run sequentially. The main benefit of this
26 * approach is consistent starting times for each controller periodic, which can
27 * make odometry and estimators more accurate and controller outputs change more
28 * consistently.
29 *
30 * Here's an example of measured subsystem durations and their timeslice
31 * allocations:
32 *
33 * <table>
34 * <tr>
35 * <td><b>Subsystem</b></td>
36 * <td><b>Duration (ms)</b></td>
37 * <td><b>Allocation (ms)</b></td>
38 * </tr>
39 * <tr>
40 * <td><b>Total</b></td>
41 * <td>5.0</td>
42 * <td>5.0</td>
43 * </tr>
44 * <tr>
45 * <td>TimedRobot</td>
46 * <td>?</td>
47 * <td>2.0</td>
48 * </tr>
49 * <tr>
50 * <td>Drivetrain</td>
51 * <td>1.32</td>
52 * <td>1.5</td>
53 * </tr>
54 * <tr>
55 * <td>Flywheel</td>
56 * <td>0.6</td>
57 * <td>0.7</td>
58 * </tr>
59 * <tr>
60 * <td>Turret</td>
61 * <td>0.6</td>
62 * <td>0.8</td>
63 * </tr>
64 * <tr>
65 * <td><b>Free</b></td>
66 * <td>0.0</td>
67 * <td>N/A</td>
68 * </tr>
69 * </table>
70 *
71 * Since TimedRobot periodic functions only run every 20ms, that leaves a 2 ms
72 * empty spot in the allocation table for three of the four 5 ms cycles
73 * comprising 20 ms. That's OK because the OS needs time to do other things.
74 *
75 * If the robot periodic functions and the controller periodic functions have a
76 * lot of scheduling jitter that cause them to occasionally overlap with later
77 * timeslices, consider giving the main robot thread a real-time priority using
78 * wpi::SetCurrentThreadPriority(int). An RT priority of 15 is a reasonable
79 * 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(wpi::units::second_t robotPeriodicAllocation,
97 wpi::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, wpi::units::second_t allocation);
114
115 private:
116 wpi::units::second_t m_nextOffset;
117 wpi::units::second_t m_controllerPeriod;
118};
119
120} // namespace wpi
TimedRobot(wpi::units::second_t period=DEFAULT_PERIOD)
Constructor for TimedRobot.
TimesliceRobot(wpi::units::second_t robotPeriodicAllocation, wpi::units::second_t controllerPeriod)
Constructor for TimesliceRobot.
void Schedule(std::function< void()> func, wpi::units::second_t allocation)
Schedule a periodic function with the constructor's controller period and the given allocation.
Definition CvSource.hpp:15