WPILibC++ 2027.0.0-alpha-4
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(). An RT priority of 15 is a reasonable choice.
79 *
80 * If you do enable RT though, <i>make sure your periodic functions do not
81 * block</i>. If they do, the operating system will lock up, and you'll have to
82 * boot the roboRIO into safe mode and delete the robot program to recover.
83 */
84class TimesliceRobot : public TimedRobot {
85 public:
86 /**
87 * Constructor for TimesliceRobot.
88 *
89 * @param robotPeriodicAllocation The allocation to give the TimesliceRobot
90 * periodic functions.
91 * @param controllerPeriod The controller period. The sum of all scheduler
92 * allocations should be less than or equal to this
93 * value.
94 */
95 explicit TimesliceRobot(wpi::units::second_t robotPeriodicAllocation,
96 wpi::units::second_t controllerPeriod);
97
98 /**
99 * Schedule a periodic function with the constructor's controller period and
100 * the given allocation. The function's runtime allocation will be placed
101 * after the end of the previous one's.
102 *
103 * If a call to this function makes the allocations exceed the controller
104 * period, an exception will be thrown since that means the TimesliceRobot
105 * periodic functions and the given function will have conflicting
106 * timeslices.
107 *
108 * @param func Function to schedule.
109 * @param allocation The function's runtime allocation out of the controller
110 * period.
111 */
112 void Schedule(std::function<void()> func, wpi::units::second_t allocation);
113
114 private:
115 wpi::units::second_t m_nextOffset;
116 wpi::units::second_t m_controllerPeriod;
117};
118
119} // namespace wpi
TimedRobot(wpi::units::second_t period=kDefaultPeriod)
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