WPILibC++ 2026.1.1
Loading...
Searching...
No Matches
TimedRobot.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 <chrono>
8#include <functional>
9#include <utility>
10#include <vector>
11
12#include <hal/Notifier.h>
13#include <hal/Types.h>
14#include <units/frequency.h>
15#include <units/math.h>
16#include <units/time.h>
17#include <wpi/priority_queue.h>
18
20#include "frc/RobotController.h"
21
22namespace frc {
23
24/**
25 * TimedRobot implements the IterativeRobotBase robot program framework.
26 *
27 * The TimedRobot class is intended to be subclassed by a user creating a
28 * robot program.
29 *
30 * Periodic() functions from the base class are called on an interval by a
31 * Notifier instance.
32 */
34 public:
35 /// Default loop period.
36 static constexpr auto kDefaultPeriod = 20_ms;
37
38 /**
39 * Provide an alternate "main loop" via StartCompetition().
40 */
41 void StartCompetition() override;
42
43 /**
44 * Ends the main loop in StartCompetition().
45 */
46 void EndCompetition() override;
47
48 /**
49 * Constructor for TimedRobot.
50 *
51 * @param period The period of the robot loop function.
52 */
53 explicit TimedRobot(units::second_t period = kDefaultPeriod);
54
55 /**
56 * Constructor for TimedRobot.
57 *
58 * @param frequency The frequency of the robot loop function.
59 */
60 explicit TimedRobot(units::hertz_t frequency);
61
62 TimedRobot(TimedRobot&&) = default;
64
65 ~TimedRobot() override;
66
67 /**
68 * Return the system clock time in micrseconds for the start of the current
69 * periodic loop. This is in the same time base as Timer.GetFPGATimestamp(),
70 * but is stable through a loop. It is updated at the beginning of every
71 * periodic callback (including the normal periodic loop).
72 *
73 * @return Robot running time in microseconds, as of the start of the current
74 * periodic function.
75 */
76 uint64_t GetLoopStartTime();
77
78 /**
79 * Add a callback to run at a specific period with a starting time offset.
80 *
81 * This is scheduled on TimedRobot's Notifier, so TimedRobot and the callback
82 * run synchronously. Interactions between them are thread-safe.
83 *
84 * @param callback The callback to run.
85 * @param period The period at which to run the callback.
86 * @param offset The offset from the common starting time. This is useful
87 * for scheduling a callback in a different timeslot relative
88 * to TimedRobot.
89 */
90 void AddPeriodic(std::function<void()> callback, units::second_t period,
91 units::second_t offset = 0_s);
92
93 private:
94 class Callback {
95 public:
96 std::function<void()> func;
97 std::chrono::microseconds period;
98 std::chrono::microseconds expirationTime;
99
100 /**
101 * Construct a callback container.
102 *
103 * @param func The callback to run.
104 * @param startTime The common starting point for all callback scheduling.
105 * @param period The period at which to run the callback.
106 * @param offset The offset from the common starting time.
107 */
108 Callback(std::function<void()> func, std::chrono::microseconds startTime,
109 std::chrono::microseconds period, std::chrono::microseconds offset)
110 : func{std::move(func)},
111 period{period},
112 expirationTime(
113 startTime + offset + period +
114 (std::chrono::microseconds{frc::RobotController::GetFPGATime()} -
115 startTime) /
116 period * period) {}
117
118 bool operator>(const Callback& rhs) const {
119 return expirationTime > rhs.expirationTime;
120 }
121 };
122
124 std::chrono::microseconds m_startTime;
125 uint64_t m_loopStartTimeUs = 0;
126
128 m_callbacks;
129};
130
131} // namespace frc
IterativeRobotBase implements a specific type of robot program framework, extending the RobotBase cla...
Definition IterativeRobotBase.h:57
Definition RobotController.h:33
TimedRobot implements the IterativeRobotBase robot program framework.
Definition TimedRobot.h:33
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.
uint64_t GetLoopStartTime()
Return the system clock time in micrseconds for the start of the current periodic loop.
TimedRobot(units::second_t period=kDefaultPeriod)
Constructor for TimedRobot.
TimedRobot(TimedRobot &&)=default
TimedRobot(units::hertz_t frequency)
Constructor for TimedRobot.
void EndCompetition() override
Ends the main loop in StartCompetition().
TimedRobot & operator=(TimedRobot &&)=default
~TimedRobot() override
static constexpr auto kDefaultPeriod
Default loop period.
Definition TimedRobot.h:36
void StartCompetition() override
Provide an alternate "main loop" via StartCompetition().
A move-only C++ wrapper around a HAL handle.
Definition Types.h:96
This class is the same as std::priority_queue with two changes:
Definition priority_queue.h:27
Definition CAN.h:11
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280