WPILibC++ 2027.0.0-alpha-5
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 <functional>
8
10#include "wpi/hal/Notifier.hpp"
11#include "wpi/hal/Types.hpp"
13#include "wpi/units/frequency.hpp"
14#include "wpi/units/time.hpp"
15
16namespace wpi {
17
18/**
19 * TimedRobot implements the IterativeRobotBase robot program framework.
20 *
21 * The TimedRobot class is intended to be subclassed by a user creating a
22 * robot program.
23 *
24 * Periodic() functions from the base class are called on an interval by a
25 * Notifier instance.
26 */
28 public:
29 /// Default loop period.
30 static constexpr auto DEFAULT_PERIOD = 20_ms;
31
32 /**
33 * Provide an alternate "main loop" via StartCompetition().
34 */
35 void StartCompetition() override;
36
37 /**
38 * Ends the main loop in StartCompetition().
39 */
40 void EndCompetition() override;
41
42 /**
43 * Constructor for TimedRobot.
44 *
45 * @param period The period of the robot loop function.
46 */
47 explicit TimedRobot(wpi::units::second_t period = DEFAULT_PERIOD);
48
49 /**
50 * Constructor for TimedRobot.
51 *
52 * @param frequency The frequency of the robot loop function.
53 */
54 explicit TimedRobot(wpi::units::hertz_t frequency);
55
56 TimedRobot(TimedRobot&&) = default;
58
59 ~TimedRobot() override;
60
61 /**
62 * Return the system clock time in microseconds for the start of the current
63 * periodic loop. This is in the same time base as
64 * Timer.GetMonotonicTimestamp(), but is stable through a loop. It is updated
65 * at the beginning of every periodic callback (including the normal periodic
66 * loop).
67 *
68 * @return Robot running time in microseconds, as of the start of the current
69 * periodic function.
70 */
71 wpi::units::microsecond_t GetLoopStartTime() const {
72 return m_callbacks.GetLoopStartTime();
73 }
74
75 /**
76 * Add a callback to run at a specific period with a starting time offset.
77 *
78 * This is scheduled on TimedRobot's Notifier, so TimedRobot and the callback
79 * run synchronously. Interactions between them are thread-safe.
80 *
81 * @param callback The callback to run.
82 * @param period The period at which to run the callback.
83 * @param offset The offset from the common starting time. This is useful
84 * for scheduling a callback in a different timeslot relative
85 * to TimedRobot.
86 */
87 void AddPeriodic(std::function<void()> callback, wpi::units::second_t period,
88 wpi::units::second_t offset = 0_s);
89
90 protected:
92 std::chrono::microseconds m_startTime;
93
94 private:
96};
97
98} // namespace wpi
IterativeRobotBase(wpi::units::second_t period)
Constructor for IterativeRobotBase.
static constexpr auto DEFAULT_PERIOD
Default loop period.
Definition TimedRobot.hpp:30
TimedRobot(wpi::units::hertz_t frequency)
Constructor for TimedRobot.
TimedRobot(TimedRobot &&)=default
void StartCompetition() override
Provide an alternate "main loop" via StartCompetition().
TimedRobot & operator=(TimedRobot &&)=default
wpi::hal::Handle< HAL_NotifierHandle, HAL_DestroyNotifier > m_notifier
Definition TimedRobot.hpp:91
wpi::units::microsecond_t GetLoopStartTime() const
Return the system clock time in microseconds for the start of the current periodic loop.
Definition TimedRobot.hpp:71
TimedRobot(wpi::units::second_t period=DEFAULT_PERIOD)
Constructor for TimedRobot.
~TimedRobot() override
void EndCompetition() override
Ends the main loop in StartCompetition().
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.
std::chrono::microseconds m_startTime
Definition TimedRobot.hpp:92
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
A priority queue for scheduling periodic callbacks based on their next execution time.
Definition PeriodicPriorityQueue.hpp:30
Definition CvSource.hpp:15