WPILibC++ 2024.3.2
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 <functional>
8#include <utility>
9#include <vector>
10
11#include <hal/Types.h>
12#include <units/math.h>
13#include <units/time.h>
14#include <wpi/priority_queue.h>
15
17#include "frc/Timer.h"
18
19namespace frc {
20
21/**
22 * TimedRobot implements the IterativeRobotBase robot program framework.
23 *
24 * The TimedRobot class is intended to be subclassed by a user creating a
25 * robot program.
26 *
27 * Periodic() functions from the base class are called on an interval by a
28 * Notifier instance.
29 */
31 public:
32 /// Default loop period.
33 static constexpr auto kDefaultPeriod = 20_ms;
34
35 /**
36 * Provide an alternate "main loop" via StartCompetition().
37 */
38 void StartCompetition() override;
39
40 /**
41 * Ends the main loop in StartCompetition().
42 */
43 void EndCompetition() override;
44
45 /**
46 * Constructor for TimedRobot.
47 *
48 * @param period Period.
49 */
50 explicit TimedRobot(units::second_t period = kDefaultPeriod);
51
52 ~TimedRobot() override;
53
54 TimedRobot(TimedRobot&&) = default;
56
57 /**
58 * Add a callback to run at a specific period with a starting time offset.
59 *
60 * This is scheduled on TimedRobot's Notifier, so TimedRobot and the callback
61 * run synchronously. Interactions between them are thread-safe.
62 *
63 * @param callback The callback to run.
64 * @param period The period at which to run the callback.
65 * @param offset The offset from the common starting time. This is useful
66 * for scheduling a callback in a different timeslot relative
67 * to TimedRobot.
68 */
69 void AddPeriodic(std::function<void()> callback, units::second_t period,
70 units::second_t offset = 0_s);
71
72 private:
73 class Callback {
74 public:
75 std::function<void()> func;
76 units::second_t period;
77 units::second_t expirationTime;
78
79 /**
80 * Construct a callback container.
81 *
82 * @param func The callback to run.
83 * @param startTime The common starting point for all callback scheduling.
84 * @param period The period at which to run the callback.
85 * @param offset The offset from the common starting time.
86 */
87 Callback(std::function<void()> func, units::second_t startTime,
88 units::second_t period, units::second_t offset)
89 : func{std::move(func)},
90 period{period},
91 expirationTime{startTime + offset +
92 units::math::floor(
93 (Timer::GetFPGATimestamp() - startTime) / period) *
94 period +
95 period} {}
96
97 bool operator>(const Callback& rhs) const {
98 return expirationTime > rhs.expirationTime;
99 }
100 };
101
103 units::second_t m_startTime;
104
106 m_callbacks;
107};
108
109} // namespace frc
IterativeRobotBase implements a specific type of robot program framework, extending the RobotBase cla...
Definition: IterativeRobotBase.h:57
TimedRobot implements the IterativeRobotBase robot program framework.
Definition: TimedRobot.h:30
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.
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:33
void StartCompetition() override
Provide an alternate "main loop" via StartCompetition().
A timer class.
Definition: Timer.h:36
This class is the same as std::priority_queue with two changes:
Definition: priority_queue.h:27
UnitType floor(const UnitType x) noexcept
Round down value.
Definition: math.h:542
Definition: AprilTagPoseEstimator.h:15
Definition: array.h:89
Unit Conversion Library namespace.
Definition: data.h:31
constexpr bool operator>(const UNIT_LIB_DEFAULT_TYPE lhs, const Units &rhs) noexcept
Definition: base.h:2740