WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
PeriodicOpMode.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/opmode/OpMode.hpp"
15#include "wpi/units/time.hpp"
16
17namespace wpi {
18
19/**
20 * An opmode structure for periodic operation. This base class implements a loop
21 * that runs one or more functions periodically (on a set time interval aka loop
22 * period). The primary periodic callback function is the abstract Periodic()
23 * function. Additional periodic callbacks with different intervals can be added
24 * using the AddPeriodic() set of functions.
25 *
26 * Lifecycle:
27 *
28 * - Constructed when opmode selected on driver station
29 *
30 * - DisabledPeriodic() called periodically as long as DS is disabled. Note
31 * this is not called on a set time interval (it does not use the same time
32 * interval as Periodic())
33 *
34 * - When DS transitions from disabled to enabled, Start() is called once
35 *
36 * - While DS is enabled, Periodic() is called periodically on the time interval
37 * set by the robot framework, and additional periodic callbacks added via
38 * AddPeriodic() are called periodically on their set time intervals
39 *
40 * - When DS transitions from enabled to disabled, or a different opmode is
41 * selected on the driver station when the DS is enabled, End() is called,
42 * followed by Close(); the object is not reused
43 *
44 * - If a different opmode is selected on the driver station when the DS is
45 * disabled, only Close() is called; the object is not reused
46 */
47class PeriodicOpMode : public OpMode {
48 protected:
49 /**
50 * Constructor.
51 */
53
54 public:
55 ~PeriodicOpMode() override = default;
56
57 /**
58 * Called periodically while the opmode is selected on the DS (robot is
59 * disabled).
60 */
61 void DisabledPeriodic() override {}
62
63 /**
64 * Called a single time when the robot transitions from disabled to enabled.
65 * This is called prior to Periodic() being called.
66 */
67 void Start() override {}
68
69 /**
70 * Called periodically while the robot is enabled. The framework calls this
71 * at OpModeRobot's configured loop period.
72 */
73 void Periodic() override {}
74
75 /**
76 * Called a single time when the robot transitions from enabled to disabled,
77 * or just before Close() is called if a different opmode is selected while
78 * the robot is enabled.
79 */
80 void End() override {}
81
82 /**
83 * Returns the set of additional periodic callbacks registered via
84 * AddPeriodic(). These are executed by the robot framework while the opmode
85 * is enabled, in addition to the primary Periodic() callback.
86 *
87 * @return The vector of additional periodic callbacks.
88 */
89 std::vector<wpi::internal::PeriodicPriorityQueue::Callback> GetCallbacks()
90 override {
91 return m_callbacks;
92 }
93
94 /**
95 * Add a callback to run at a specific period.
96 *
97 * This is scheduled on the same Notifier as Periodic(), so Periodic() and
98 * the callback run synchronously. Interactions between them are thread-safe.
99 *
100 * @param callback The callback to run.
101 * @param period The period at which to run the callback.
102 */
103 void AddPeriodic(std::function<void()> callback,
104 wpi::units::second_t period) {
105 AddPeriodic(std::move(callback), period, period);
106 }
107
108 /**
109 * Add a callback to run at a specific period with a starting time offset.
110 *
111 * This is scheduled on the same Notifier as Periodic(), so Periodic() and
112 * the callback run synchronously. Interactions between them are thread-safe.
113 *
114 * @param callback The callback to run.
115 * @param period The period at which to run the callback.
116 * @param offset The offset from the common starting time. This is useful
117 * for scheduling a callback in a different timeslot relative
118 * to TimedRobot.
119 */
120 void AddPeriodic(std::function<void()> callback, wpi::units::second_t period,
121 wpi::units::second_t offset);
122
123 private:
124 std::vector<wpi::internal::PeriodicPriorityQueue::Callback> m_callbacks;
125 std::chrono::microseconds m_startTime;
126};
127
128} // namespace wpi
Top-level interface for opmode classes.
Definition OpMode.hpp:32
void AddPeriodic(std::function< void()> callback, wpi::units::second_t period)
Add a callback to run at a specific period.
Definition PeriodicOpMode.hpp:103
PeriodicOpMode()
Constructor.
void Start() override
Called a single time when the robot transitions from disabled to enabled.
Definition PeriodicOpMode.hpp:67
void AddPeriodic(std::function< void()> callback, wpi::units::second_t period, wpi::units::second_t offset)
Add a callback to run at a specific period with a starting time offset.
void Periodic() override
Called periodically while the robot is enabled.
Definition PeriodicOpMode.hpp:73
~PeriodicOpMode() override=default
void End() override
Called a single time when the robot transitions from enabled to disabled, or just before Close() is c...
Definition PeriodicOpMode.hpp:80
std::vector< wpi::internal::PeriodicPriorityQueue::Callback > GetCallbacks() override
Returns the set of additional periodic callbacks registered via AddPeriodic().
Definition PeriodicOpMode.hpp:89
void DisabledPeriodic() override
Called periodically while the opmode is selected on the DS (robot is disabled).
Definition PeriodicOpMode.hpp:61
Definition CvSource.hpp:15