WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
OpMode.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 <vector>
8
10
11namespace wpi {
12
13/**
14 * Top-level interface for opmode classes. Users should generally extend one of
15 * the abstract implementations of this interface (e.g. PeriodicOpMode) rather
16 * than directly implementing this interface.
17 *
18 * <p><b>Lifecycle</b>:
19 * <ul>
20 * <li>constructed when opmode selected on driver station
21 * <li>DisabledPeriodic() called periodically as long as DS is disabled
22 * <li>when DS transitions from disabled to enabled, Start() is called once
23 * <li>while DS is enabled, Periodic() is called periodically and additional
24 * periodic callbacks added via GetCallbacks() are called periodically
25 * <li>when DS transitions from enabled to disabled, or a different opmode is
26 * selected while enabled, End() is called, followed by Close(); the
27 * object is not reused
28 * <li>if a different opmode is selected while disabled, only Close() is
29 * called; the object is not reused
30 * </ul>
31 */
32class OpMode {
33 public:
34 /**
35 * The object is destroyed when the opmode is no longer selected on the DS or
36 * after an enabled run ends. The object will not be reused after the
37 * destructor is called.
38 */
39 virtual ~OpMode() = default;
40
41 /**
42 * This function is called periodically while the opmode is selected on the
43 * DS (robot is disabled). Code that should only run once when the opmode is
44 * selected should go in the opmode constructor.
45 */
46 virtual void DisabledPeriodic() {}
47
48 /** Called once when this opmode transitions to enabled. */
49 virtual void Start() {}
50
51 /**
52 * This function is called periodically while the opmode is enabled.
53 */
54 virtual void Periodic() {}
55
56 /**
57 * This function is called when the robot disables or switches opmodes while
58 * this opmode is enabled. Implementations should stop blocking work
59 * promptly.
60 */
61 virtual void End() {}
62
63 /**
64 * Returns a vector of custom periodic callbacks to be executed while the
65 * opmode is enabled.
66 *
67 * <p>This method allows opmodes to register arbitrary periodic callbacks
68 * with custom execution intervals. The callbacks are executed by the robot
69 * framework at their scheduled times, in addition to the primary Periodic()
70 * callback.
71 *
72 * @return A vector of custom callbacks to execute, or an empty vector if no
73 * custom callbacks are needed. The default implementation returns an
74 * empty vector.
75 */
76 virtual std::vector<wpi::internal::PeriodicPriorityQueue::Callback>
78 return {};
79 }
80};
81
82} // namespace wpi
Top-level interface for opmode classes.
Definition OpMode.hpp:32
virtual ~OpMode()=default
The object is destroyed when the opmode is no longer selected on the DS or after an enabled run ends.
virtual void End()
This function is called when the robot disables or switches opmodes while this opmode is enabled.
Definition OpMode.hpp:61
virtual void DisabledPeriodic()
This function is called periodically while the opmode is selected on the DS (robot is disabled).
Definition OpMode.hpp:46
virtual void Periodic()
This function is called periodically while the opmode is enabled.
Definition OpMode.hpp:54
virtual void Start()
Called once when this opmode transitions to enabled.
Definition OpMode.hpp:49
virtual std::vector< wpi::internal::PeriodicPriorityQueue::Callback > GetCallbacks()
Returns a vector of custom periodic callbacks to be executed while the opmode is enabled.
Definition OpMode.hpp:77
Definition CvSource.hpp:15