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