WPILibC++ 2023.4.3-108-ge5452e3
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 static constexpr auto kDefaultPeriod = 20_ms;
33
34 /**
35 * Provide an alternate "main loop" via StartCompetition().
36 */
37 void StartCompetition() override;
38
39 /**
40 * Ends the main loop in StartCompetition().
41 */
42 void EndCompetition() override;
43
44 /**
45 * Constructor for TimedRobot.
46 *
47 * @param period Period.
48 */
49 explicit TimedRobot(units::second_t period = kDefaultPeriod);
50
51 ~TimedRobot() override;
52
53 TimedRobot(TimedRobot&&) = default;
55
56 /**
57 * Add a callback to run at a specific period with a starting time offset.
58 *
59 * This is scheduled on TimedRobot's Notifier, so TimedRobot and the callback
60 * run synchronously. Interactions between them are thread-safe.
61 *
62 * @param callback The callback to run.
63 * @param period The period at which to run the callback.
64 * @param offset The offset from the common starting time. This is useful
65 * for scheduling a callback in a different timeslot relative
66 * to TimedRobot.
67 */
68 void AddPeriodic(std::function<void()> callback, units::second_t period,
69 units::second_t offset = 0_s);
70
71 private:
72 class Callback {
73 public:
74 std::function<void()> func;
75 units::second_t period;
76 units::second_t expirationTime;
77
78 /**
79 * Construct a callback container.
80 *
81 * @param func The callback to run.
82 * @param startTime The common starting point for all callback scheduling.
83 * @param period The period at which to run the callback.
84 * @param offset The offset from the common starting time.
85 */
86 Callback(std::function<void()> func, units::second_t startTime,
87 units::second_t period, units::second_t offset)
88 : func{std::move(func)},
89 period{period},
90 expirationTime{startTime + offset +
91 units::math::floor(
92 (Timer::GetFPGATimestamp() - startTime) / period) *
93 period +
94 period} {}
95
96 bool operator>(const Callback& rhs) const {
97 return expirationTime > rhs.expirationTime;
98 }
99 };
100
102 units::second_t m_startTime;
103
105 m_callbacks;
106};
107
108} // 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
Definition: TimedRobot.h:32
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: BFloat16.h:88
Unit Conversion Library namespace.
Definition: magnetic_flux.h:31
constexpr bool operator>(const UNIT_LIB_DEFAULT_TYPE lhs, const Units &rhs) noexcept
Definition: base.h:2751