WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
OpModeRobot.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 <concepts>
8#include <cstdint>
9#include <functional>
10#include <memory>
11#include <string>
12
15#include "wpi/hal/Notifier.h"
16#include "wpi/hal/Types.hpp"
17#include "wpi/opmode/OpMode.hpp"
18#include "wpi/util/DenseMap.hpp"
19#include "wpi/util/mutex.hpp"
20
21namespace wpi::util {
22class Color;
23} // namespace wpi::util
24
25namespace wpi {
26
28
29namespace detail {
30template <typename T>
31concept OpModeDerived = std::derived_from<T, OpMode>;
32template <typename T>
33concept NoArgOpMode = std::constructible_from<T> && OpModeDerived<T>;
34template <typename T, typename R>
35concept OneArgOpMode = std::constructible_from<T, R&> && OpModeDerived<T>;
36} // namespace detail
37
38/**
39 * Concept indicating a class is derived from OpMode and has either a
40 * no-argument constructor or a constructorthat accepts R&.
41 *
42 * @tparam T opmode class
43 * @tparam R robot class
44 */
45template <typename T, typename R>
48
49/**
50 * OpModeRobotBase is the non-templated base class for OpModeRobot. Users should
51 * generally prefer using OpModeRobot instead of this class.
52 *
53 * Opmodes are constructed when selected on the driver station, and destroyed
54 * when the robot is disabled after being enabled or a different opmode is
55 * selected. When no opmode is selected, NonePeriodic() is called. The
56 * DriverStationConnected() function is called the first time the driver station
57 * connects to the robot.
58 */
59class OpModeRobotBase : public RobotBase {
60 public:
61 using OpModeFactory = std::function<std::unique_ptr<OpMode>()>;
62
63 /**
64 * Provide an alternate "main loop" via StartCompetition().
65 */
66 void StartCompetition() override;
67
68 /**
69 * Ends the main loop in StartCompetition().
70 */
71 void EndCompetition() override;
72
73 /**
74 * Constructor.
75 */
76 OpModeRobotBase() = default;
79
80 /**
81 * Function called exactly once after the DS is connected.
82 *
83 * Code that needs to know the DS state should go here.
84 *
85 * Users should override this method for initialization that needs to occur
86 * after the DS is connected, such as needing the alliance information.
87 */
88 virtual void DriverStationConnected() {}
89
90 /**
91 * Function called periodically anytime when no opmode is selected, including
92 * when the Driver Station is disconnected.
93 */
94 virtual void NonePeriodic() {}
95
96 /**
97 * Adds an operating mode option using a factory function that creates the
98 * opmode. It's necessary to call PublishOpModes() to make the added modes
99 * visible to the driver station.
100 *
101 * @param factory factory function
102 * @param mode robot mode
103 * @param name name of the operating mode
104 * @param group group of the operating mode
105 * @param description description of the operating mode
106 * @param textColor text color
107 * @param backgroundColor background color
108 */
110 std::string_view name, std::string_view group,
111 std::string_view description,
112 const wpi::util::Color& textColor,
113 const wpi::util::Color& backgroundColor);
114
115 /**
116 * Adds an operating mode option using a factory function that creates the
117 * opmode. It's necessary to call PublishOpModes() to make the added modes
118 * visible to the driver station.
119 *
120 * @param factory factory function
121 * @param mode robot mode
122 * @param name name of the operating mode
123 * @param group group of the operating mode
124 * @param description description of the operating mode
125 */
127 std::string_view name, std::string_view group = {},
128 std::string_view description = {});
129
130 /**
131 * Removes an operating mode option. It's necessary to call PublishOpModes()
132 * to make the removed mode no longer visible to the driver station.
133 *
134 * @param mode robot mode
135 * @param name name of the operating mode
136 */
137 void RemoveOpMode(RobotMode mode, std::string_view name);
138
139 /**
140 * Publishes the operating mode options to the driver station.
141 */
143
144 /**
145 * Clears all operating mode options and publishes an empty list to the driver
146 * station.
147 */
149
150 private:
151 struct OpModeData {
152 std::string name;
153 OpModeFactory factory;
154 };
157 wpi::util::mutex m_opModeMutex;
158 std::weak_ptr<OpMode> m_activeOpMode;
159};
160
161/**
162 * OpModeRobot implements the opmode-based robot program framework.
163 *
164 * The OpModeRobot class is intended to be subclassed by a user creating a robot
165 * program. Users must provide their derived class as a template parameter to
166 * this class.
167 *
168 * Opmodes are constructed when selected on the driver station, and destroyed
169 * when the robot is disabled after being enabled or a different opmode is
170 * selected. When no opmode is selected, NonePeriodic() is called. The
171 * DriverStationConnected() function is called the first time the driver station
172 * connects to the robot.
173 *
174 * @tparam Derived derived class
175 */
176template <typename Derived>
178 public:
179 /**
180 * Adds an operating mode option. It's necessary to call PublishOpModes() to
181 * make the added modes visible to the driver station.
182 *
183 * @tparam T opmode class; must be a public, non-abstract subclass of OpMode
184 * with a public constructor that either takes no arguments or accepts a
185 * single argument of this class's type (the latter is preferred).
186 * @param mode robot mode
187 * @param name name of the operating mode
188 * @param group group of the operating mode
189 * @param description description of the operating mode
190 * @param textColor text color
191 * @param backgroundColor background color
192 */
193 template <ConstructibleOpMode<Derived> T>
194 void AddOpMode(RobotMode mode, std::string_view name, std::string_view group,
195 std::string_view description,
196 const wpi::util::Color& textColor,
197 const wpi::util::Color& backgroundColor) {
198 if constexpr (detail::OneArgOpMode<T, Derived>) {
200 [this] { return std::make_unique<T>(*static_cast<Derived*>(this)); },
201 mode, name, group, description, textColor, backgroundColor);
202 } else if constexpr (detail::NoArgOpMode<T>) {
203 AddOpModeFactory([] { return std::make_unique<T>(); }, mode, name, group,
204 description, textColor, backgroundColor);
205 }
206 }
207
208 /**
209 * Adds an operating mode option. It's necessary to call PublishOpModes() to
210 * make the added modes visible to the driver station.
211 *
212 * @tparam T opmode class; must be a public, non-abstract subclass of OpMode
213 * with a public constructor that either takes no arguments or accepts a
214 * single argument of this class's type (the latter is preferred).
215 * @param mode robot mode
216 * @param name name of the operating mode
217 * @param group group of the operating mode
218 * @param description description of the operating mode
219 */
220 template <ConstructibleOpMode<Derived> T>
221 void AddOpMode(RobotMode mode, std::string_view name,
222 std::string_view group = {},
223 std::string_view description = {}) {
224 if constexpr (detail::OneArgOpMode<T, Derived>) {
226 [this] { return std::make_unique<T>(*static_cast<Derived*>(this)); },
227 mode, name, group, description);
228 } else if constexpr (detail::NoArgOpMode<T>) {
229 AddOpModeFactory([] { return std::make_unique<T>(); }, mode, name, group,
230 description);
231 }
232 }
233};
234
235} // namespace wpi
This file defines the DenseMap class.
@ name
Definition base.h:690
void PublishOpModes()
Publishes the operating mode options to the driver station.
void EndCompetition() override
Ends the main loop in StartCompetition().
virtual void NonePeriodic()
Function called periodically anytime when no opmode is selected, including when the Driver Station is...
Definition OpModeRobot.hpp:94
void ClearOpModes()
Clears all operating mode options and publishes an empty list to the driver station.
OpModeRobotBase(OpModeRobotBase &&)=delete
void RemoveOpMode(RobotMode mode, std::string_view name)
Removes an operating mode option.
OpModeRobotBase & operator=(OpModeRobotBase &&)=delete
void AddOpModeFactory(OpModeFactory factory, RobotMode mode, std::string_view name, std::string_view group, std::string_view description, const wpi::util::Color &textColor, const wpi::util::Color &backgroundColor)
Adds an operating mode option using a factory function that creates the opmode.
void AddOpModeFactory(OpModeFactory factory, RobotMode mode, std::string_view name, std::string_view group={}, std::string_view description={})
Adds an operating mode option using a factory function that creates the opmode.
OpModeRobotBase()=default
Constructor.
std::function< std::unique_ptr< OpMode >()> OpModeFactory
Definition OpModeRobot.hpp:61
virtual void DriverStationConnected()
Function called exactly once after the DS is connected.
Definition OpModeRobot.hpp:88
void StartCompetition() override
Provide an alternate "main loop" via StartCompetition().
OpModeRobot implements the opmode-based robot program framework.
Definition OpModeRobot.hpp:177
void AddOpMode(RobotMode mode, std::string_view name, std::string_view group, std::string_view description, const wpi::util::Color &textColor, const wpi::util::Color &backgroundColor)
Adds an operating mode option.
Definition OpModeRobot.hpp:194
void AddOpMode(RobotMode mode, std::string_view name, std::string_view group={}, std::string_view description={})
Adds an operating mode option.
Definition OpModeRobot.hpp:221
RobotBase()
Constructor for a generic robot program.
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
Represents colors that can be used with Addressable LEDs.
Definition Color.hpp:42
Definition DenseMap.hpp:728
Concept indicating a class is derived from OpMode and has either a no-argument constructor or a const...
Definition OpModeRobot.hpp:46
Definition OpModeRobot.hpp:33
Definition OpModeRobot.hpp:35
Definition OpModeRobot.hpp:31
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
RobotMode
The overall robot mode (not including enabled state).
Definition DriverStationTypes.hpp:15
Definition raw_os_ostream.hpp:19
::std::mutex mutex
Definition mutex.hpp:17
Definition CvSource.hpp:15