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