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