WPILibC++ 2024.3.2
DriverStation.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 <optional>
8#include <string>
9
10#include <units/time.h>
11#include <wpi/Synchronization.h>
12
13namespace wpi::log {
14class DataLog;
15} // namespace wpi::log
16
17namespace frc {
18
19/**
20 * Provide access to the network communication data to / from the Driver
21 * Station.
22 */
23class DriverStation final {
24 public:
25 /**
26 * The robot alliance that the robot is a part of.
27 */
28 enum Alliance {
29 /// Red alliance.
31 /// Blue alliance.
32 kBlue
33 };
34
35 /**
36 * The type of robot match that the robot is part of.
37 */
38 enum MatchType {
39 /// None.
41 /// Practice.
43 /// Qualification.
45 /// Elimination.
47 };
48
49 /// Number of Joystick ports.
50 static constexpr int kJoystickPorts = 6;
51
52 /**
53 * The state of one joystick button. %Button indexes begin at 1.
54 *
55 * @param stick The joystick to read.
56 * @param button The button index, beginning at 1.
57 * @return The state of the joystick button.
58 */
59 static bool GetStickButton(int stick, int button);
60
61 /**
62 * Whether one joystick button was pressed since the last check. %Button
63 * indexes begin at 1.
64 *
65 * @param stick The joystick to read.
66 * @param button The button index, beginning at 1.
67 * @return Whether the joystick button was pressed since the last check.
68 */
69 static bool GetStickButtonPressed(int stick, int button);
70
71 /**
72 * Whether one joystick button was released since the last check. %Button
73 * indexes begin at 1.
74 *
75 * @param stick The joystick to read.
76 * @param button The button index, beginning at 1.
77 * @return Whether the joystick button was released since the last check.
78 */
79 static bool GetStickButtonReleased(int stick, int button);
80
81 /**
82 * Get the value of the axis on a joystick.
83 *
84 * This depends on the mapping of the joystick connected to the specified
85 * port.
86 *
87 * @param stick The joystick to read.
88 * @param axis The analog axis value to read from the joystick.
89 * @return The value of the axis on the joystick.
90 */
91 static double GetStickAxis(int stick, int axis);
92
93 /**
94 * Get the state of a POV on the joystick.
95 *
96 * @return the angle of the POV in degrees, or -1 if the POV is not pressed.
97 */
98 static int GetStickPOV(int stick, int pov);
99
100 /**
101 * The state of the buttons on the joystick.
102 *
103 * @param stick The joystick to read.
104 * @return The state of the buttons on the joystick.
105 */
106 static int GetStickButtons(int stick);
107
108 /**
109 * Returns the number of axes on a given joystick port.
110 *
111 * @param stick The joystick port number
112 * @return The number of axes on the indicated joystick
113 */
114 static int GetStickAxisCount(int stick);
115
116 /**
117 * Returns the number of POVs on a given joystick port.
118 *
119 * @param stick The joystick port number
120 * @return The number of POVs on the indicated joystick
121 */
122 static int GetStickPOVCount(int stick);
123
124 /**
125 * Returns the number of buttons on a given joystick port.
126 *
127 * @param stick The joystick port number
128 * @return The number of buttons on the indicated joystick
129 */
130 static int GetStickButtonCount(int stick);
131
132 /**
133 * Returns a boolean indicating if the controller is an xbox controller.
134 *
135 * @param stick The joystick port number
136 * @return A boolean that is true if the controller is an xbox controller.
137 */
138 static bool GetJoystickIsXbox(int stick);
139
140 /**
141 * Returns the type of joystick at a given port.
142 *
143 * @param stick The joystick port number
144 * @return The HID type of joystick at the given port
145 */
146 static int GetJoystickType(int stick);
147
148 /**
149 * Returns the name of the joystick at the given port.
150 *
151 * @param stick The joystick port number
152 * @return The name of the joystick at the given port
153 */
154 static std::string GetJoystickName(int stick);
155
156 /**
157 * Returns the types of Axes on a given joystick port.
158 *
159 * @param stick The joystick port number and the target axis
160 * @param axis The analog axis value to read from the joystick.
161 * @return What type of axis the axis is reporting to be
162 */
163 static int GetJoystickAxisType(int stick, int axis);
164
165 /**
166 * Returns if a joystick is connected to the Driver Station.
167 *
168 * This makes a best effort guess by looking at the reported number of axis,
169 * buttons, and POVs attached.
170 *
171 * @param stick The joystick port number
172 * @return true if a joystick is connected
173 */
174 static bool IsJoystickConnected(int stick);
175
176 /**
177 * Check if the DS has enabled the robot.
178 *
179 * @return True if the robot is enabled and the DS is connected
180 */
181 static bool IsEnabled();
182
183 /**
184 * Check if the robot is disabled.
185 *
186 * @return True if the robot is explicitly disabled or the DS is not connected
187 */
188 static bool IsDisabled();
189
190 /**
191 * Check if the robot is e-stopped.
192 *
193 * @return True if the robot is e-stopped
194 */
195 static bool IsEStopped();
196
197 /**
198 * Check if the DS is commanding autonomous mode.
199 *
200 * @return True if the robot is being commanded to be in autonomous mode
201 */
202 static bool IsAutonomous();
203
204 /**
205 * Check if the DS is commanding autonomous mode and if it has enabled the
206 * robot.
207 *
208 * @return True if the robot is being commanded to be in autonomous mode and
209 * enabled.
210 */
211 static bool IsAutonomousEnabled();
212
213 /**
214 * Check if the DS is commanding teleop mode.
215 *
216 * @return True if the robot is being commanded to be in teleop mode
217 */
218 static bool IsTeleop();
219
220 /**
221 * Check if the DS is commanding teleop mode and if it has enabled the robot.
222 *
223 * @return True if the robot is being commanded to be in teleop mode and
224 * enabled.
225 */
226 static bool IsTeleopEnabled();
227
228 /**
229 * Check if the DS is commanding test mode.
230 *
231 * @return True if the robot is being commanded to be in test mode
232 */
233 static bool IsTest();
234
235 /**
236 * Check if the DS is commanding Test mode and if it has enabled the robot.
237 *
238 * @return True if the robot is being commanded to be in Test mode and
239 * enabled.
240 */
241 static bool IsTestEnabled();
242
243 /**
244 * Check if the DS is attached.
245 *
246 * @return True if the DS is connected to the robot
247 */
248 static bool IsDSAttached();
249
250 /**
251 * Is the driver station attached to a Field Management System?
252 *
253 * @return True if the robot is competing on a field being controlled by a
254 * Field Management System
255 */
256 static bool IsFMSAttached();
257
258 /**
259 * Returns the game specific message provided by the FMS.
260 *
261 * If the FMS is not connected, it is set from the game data setting on the
262 * driver station.
263 *
264 * @return A string containing the game specific message.
265 */
266 static std::string GetGameSpecificMessage();
267
268 /**
269 * Returns the name of the competition event provided by the FMS.
270 *
271 * @return A string containing the event name
272 */
273 static std::string GetEventName();
274
275 /**
276 * Returns the type of match being played provided by the FMS.
277 *
278 * @return The match type enum (kNone, kPractice, kQualification,
279 * kElimination)
280 */
282
283 /**
284 * Returns the match number provided by the FMS.
285 *
286 * @return The number of the match
287 */
288 static int GetMatchNumber();
289
290 /**
291 * Returns the number of times the current match has been replayed from the
292 * FMS.
293 *
294 * @return The number of replays
295 */
296 static int GetReplayNumber();
297
298 /**
299 * Get the current alliance from the FMS.
300 *
301 * If the FMS is not connected, it is set from the team alliance setting on
302 * the driver station.
303 *
304 * @return The alliance (red or blue) or an empty optional if the alliance is
305 * invalid
306 */
307 static std::optional<Alliance> GetAlliance();
308
309 /**
310 * Return the driver station location from the FMS.
311 *
312 * If the FMS is not connected, it is set from the team alliance setting on
313 * the driver station.
314 *
315 * This could return 1, 2, or 3.
316 *
317 * @return The location of the driver station (1-3, 0 for invalid)
318 */
319 static std::optional<int> GetLocation();
320
321 /**
322 * Wait for a DS connection.
323 *
324 * @param timeout timeout in seconds. 0 for infinite.
325 * @return true if connected, false if timeout
326 */
327 static bool WaitForDsConnection(units::second_t timeout);
328
329 /**
330 * Return the approximate match time. The FMS does not send an official match
331 * time to the robots, but does send an approximate match time. The value will
332 * count down the time remaining in the current period (auto or teleop).
333 * Warning: This is not an official time (so it cannot be used to dispute ref
334 * calls or guarantee that a function will trigger before the match ends).
335 *
336 * <p>When connected to the real field, this number only changes in full
337 * integer increments, and always counts down.
338 *
339 * <p>When the DS is in practice mode, this number is a floating point number,
340 * and counts down.
341 *
342 * <p>When the DS is in teleop or autonomous mode, this number is a floating
343 * point number, and counts up.
344 *
345 * <p>Simulation matches DS behavior without an FMS connected.
346 *
347 * @return Time remaining in current match period (auto or teleop) in seconds
348 */
349 static units::second_t GetMatchTime();
350
351 /**
352 * Read the battery voltage.
353 *
354 * @return The battery voltage in Volts.
355 */
356 static double GetBatteryVoltage();
357
358 /**
359 * Copy data from the DS task for the user. If no new data exists, it will
360 * just be returned, otherwise the data will be copied from the DS polling
361 * loop.
362 */
363 static void RefreshData();
364
365 /**
366 * Registers the given handle for DS data refresh notifications.
367 *
368 * @param handle The event handle.
369 */
371
372 /**
373 * Unregisters the given handle from DS data refresh notifications.
374 *
375 * @param handle The event handle.
376 */
378
379 /**
380 * Allows the user to specify whether they want joystick connection warnings
381 * to be printed to the console. This setting is ignored when the FMS is
382 * connected -- warnings will always be on in that scenario.
383 *
384 * @param silence Whether warning messages should be silenced.
385 */
386 static void SilenceJoystickConnectionWarning(bool silence);
387
388 /**
389 * Returns whether joystick connection warnings are silenced. This will
390 * always return false when connected to the FMS.
391 *
392 * @return Whether joystick connection warnings are silenced.
393 */
395
396 /**
397 * Starts logging DriverStation data to data log. Repeated calls are ignored.
398 *
399 * @param log data log
400 * @param logJoysticks if true, log joystick data
401 */
402 static void StartDataLog(wpi::log::DataLog& log, bool logJoysticks = true);
403
404 private:
405 DriverStation() = default;
406};
407
408} // namespace frc
WPI_Handle WPI_EventHandle
An event handle.
Definition: Synchronization.h:25
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:23
static std::string GetGameSpecificMessage()
Returns the game specific message provided by the FMS.
static int GetReplayNumber()
Returns the number of times the current match has been replayed from the FMS.
static int GetStickButtons(int stick)
The state of the buttons on the joystick.
static bool IsDSAttached()
Check if the DS is attached.
static std::string GetEventName()
Returns the name of the competition event provided by the FMS.
static double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
static std::string GetJoystickName(int stick)
Returns the name of the joystick at the given port.
static bool IsTeleopEnabled()
Check if the DS is commanding teleop mode and if it has enabled the robot.
static bool IsTest()
Check if the DS is commanding test mode.
static bool IsDisabled()
Check if the robot is disabled.
Alliance
The robot alliance that the robot is a part of.
Definition: DriverStation.h:28
@ kBlue
Blue alliance.
Definition: DriverStation.h:32
@ kRed
Red alliance.
Definition: DriverStation.h:30
static MatchType GetMatchType()
Returns the type of match being played provided by the FMS.
static int GetStickAxisCount(int stick)
Returns the number of axes on a given joystick port.
static bool IsAutonomousEnabled()
Check if the DS is commanding autonomous mode and if it has enabled the robot.
static units::second_t GetMatchTime()
Return the approximate match time.
static void RemoveRefreshedDataEventHandle(WPI_EventHandle handle)
Unregisters the given handle from DS data refresh notifications.
static bool IsJoystickConnected(int stick)
Returns if a joystick is connected to the Driver Station.
static int GetJoystickType(int stick)
Returns the type of joystick at a given port.
static std::optional< Alliance > GetAlliance()
Get the current alliance from the FMS.
static bool IsTeleop()
Check if the DS is commanding teleop mode.
static bool GetJoystickIsXbox(int stick)
Returns a boolean indicating if the controller is an xbox controller.
static bool IsEnabled()
Check if the DS has enabled the robot.
MatchType
The type of robot match that the robot is part of.
Definition: DriverStation.h:38
@ kQualification
Qualification.
Definition: DriverStation.h:44
@ kPractice
Practice.
Definition: DriverStation.h:42
@ kElimination
Elimination.
Definition: DriverStation.h:46
@ kNone
None.
Definition: DriverStation.h:40
static bool GetStickButtonReleased(int stick, int button)
Whether one joystick button was released since the last check.
static bool IsJoystickConnectionWarningSilenced()
Returns whether joystick connection warnings are silenced.
static bool IsAutonomous()
Check if the DS is commanding autonomous mode.
static bool WaitForDsConnection(units::second_t timeout)
Wait for a DS connection.
static int GetJoystickAxisType(int stick, int axis)
Returns the types of Axes on a given joystick port.
static bool GetStickButton(int stick, int button)
The state of one joystick button.
static void StartDataLog(wpi::log::DataLog &log, bool logJoysticks=true)
Starts logging DriverStation data to data log.
static std::optional< int > GetLocation()
Return the driver station location from the FMS.
static int GetStickPOV(int stick, int pov)
Get the state of a POV on the joystick.
static void RefreshData()
Copy data from the DS task for the user.
static double GetBatteryVoltage()
Read the battery voltage.
static bool IsTestEnabled()
Check if the DS is commanding Test mode and if it has enabled the robot.
static void SilenceJoystickConnectionWarning(bool silence)
Allows the user to specify whether they want joystick connection warnings to be printed to the consol...
static bool IsEStopped()
Check if the robot is e-stopped.
static int GetStickButtonCount(int stick)
Returns the number of buttons on a given joystick port.
static int GetStickPOVCount(int stick)
Returns the number of POVs on a given joystick port.
static constexpr int kJoystickPorts
Number of Joystick ports.
Definition: DriverStation.h:50
static void ProvideRefreshedDataEventHandle(WPI_EventHandle handle)
Registers the given handle for DS data refresh notifications.
static bool IsFMSAttached()
Is the driver station attached to a Field Management System?
static int GetMatchNumber()
Returns the match number provided by the FMS.
static bool GetStickButtonPressed(int stick, int button)
Whether one joystick button was pressed since the last check.
A data log.
Definition: DataLog.h:90
dimensionless::scalar_t log(const ScalarUnit x) noexcept
Compute natural logarithm.
Definition: math.h:349
Definition: AprilTagPoseEstimator.h:15
Definition: ntcore_cpp.h:31