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