WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
DriverStationSim.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 <stdint.h>
8
9#include <memory>
10
15
16namespace wpi::sim {
17
18class OpModeOptions : public std::span<HAL_OpModeOption> {
19 public:
20 OpModeOptions() = default;
21 OpModeOptions(HAL_OpModeOption* options, int32_t len)
22 : span{options, options + len} {}
23 OpModeOptions(const OpModeOptions&) = delete;
24
25 OpModeOptions(OpModeOptions&& oth) : span{oth} {
26 static_cast<span&>(oth) = {};
27 }
28
30
32 if (data()) {
33 HALSIM_FreeOpModeOptionsArray(data(), size());
34 }
35 static_cast<span&>(*this) = oth;
36 static_cast<span&>(oth) = {};
37 return *this;
38 }
39
41 if (data()) {
42 HALSIM_FreeOpModeOptionsArray(data(), size());
43 }
44 }
45};
46
47/**
48 * Class to control a simulated driver station.
49 */
51 public:
52 /**
53 * Register a callback on whether the DS is enabled.
54 *
55 * @param callback the callback that will be called whenever the enabled
56 * state is changed
57 * @param initialNotify if true, the callback will be run on the initial value
58 * @return the CallbackStore object associated with this callback
59 */
60 [[nodiscard]]
61 static std::unique_ptr<CallbackStore> RegisterEnabledCallback(
62 NotifyCallback callback, bool initialNotify);
63
64 /**
65 * Check if the DS is enabled.
66 *
67 * @return true if enabled
68 */
69 static bool GetEnabled();
70
71 /**
72 * Change whether the DS is enabled.
73 *
74 * @param enabled the new value
75 */
76 static void SetEnabled(bool enabled);
77
78 /**
79 * Register a callback on DS robot mode changes.
80 *
81 * @param callback the callback that will be called when robot mode changes
82 * @param initialNotify if true, the callback will be run on the initial value
83 * @return the CallbackStore object associated with this callback
84 */
85 [[nodiscard]]
86 static std::unique_ptr<CallbackStore> RegisterRobotModeCallback(
87 NotifyCallback callback, bool initialNotify);
88
89 /**
90 * Get the robot mode set by the DS.
91 *
92 * @return robot mode
93 */
95
96 /**
97 * Change the robot mode set by the DS.
98 *
99 * @param robotMode the new value
100 */
101 static void SetRobotMode(HAL_RobotMode robotMode);
102
103 /**
104 * Register a callback on the eStop state.
105 *
106 * @param callback the callback that will be called whenever the eStop state
107 * changes
108 * @param initialNotify if true, the callback will be run on the initial value
109 * @return the CallbackStore object associated with this callback
110 */
111 [[nodiscard]]
112 static std::unique_ptr<CallbackStore> RegisterEStopCallback(
113 NotifyCallback callback, bool initialNotify);
114
115 /**
116 * Check if eStop has been activated.
117 *
118 * @return true if eStopped
119 */
120 static bool GetEStop();
121
122 /**
123 * Set whether eStop is active.
124 *
125 * @param eStop true to activate
126 */
127 static void SetEStop(bool eStop);
128
129 /**
130 * Register a callback on whether the FMS is connected.
131 *
132 * @param callback the callback that will be called whenever the FMS
133 * connection changes
134 * @param initialNotify if true, the callback will be run on the initial value
135 * @return the CallbackStore object associated with this callback
136 */
137 [[nodiscard]]
138 static std::unique_ptr<CallbackStore> RegisterFmsAttachedCallback(
139 NotifyCallback callback, bool initialNotify);
140
141 /**
142 * Check if the FMS is connected.
143 *
144 * @return true if FMS is connected
145 */
146 static bool GetFmsAttached();
147
148 /**
149 * Change whether the FMS is connected.
150 *
151 * @param fmsAttached the new value
152 */
153 static void SetFmsAttached(bool fmsAttached);
154
155 /**
156 * Register a callback on whether the DS is connected.
157 *
158 * @param callback the callback that will be called whenever the DS
159 * connection changes
160 * @param initialNotify if true, the callback will be run on the initial value
161 * @return the CallbackStore object associated with this callback
162 */
163 [[nodiscard]]
164 static std::unique_ptr<CallbackStore> RegisterDsAttachedCallback(
165 NotifyCallback callback, bool initialNotify);
166
167 /**
168 * Check if the DS is attached.
169 *
170 * @return true if attached
171 */
172 static bool GetDsAttached();
173
174 /**
175 * Change whether the DS is attached.
176 *
177 * @param dsAttached the new value
178 */
179 static void SetDsAttached(bool dsAttached);
180
181 /**
182 * Register a callback on the alliance station ID.
183 *
184 * @param callback the callback that will be called whenever the alliance
185 * station changes
186 * @param initialNotify if true, the callback will be run on the initial value
187 * @return the CallbackStore object associated with this callback
188 */
189 [[nodiscard]]
190 static std::unique_ptr<CallbackStore> RegisterAllianceStationIdCallback(
191 NotifyCallback callback, bool initialNotify);
192
193 /**
194 * Get the alliance station ID (color + number).
195 *
196 * @return the alliance station color and number
197 */
199
200 /**
201 * Change the alliance station.
202 *
203 * @param allianceStationId the new alliance station
204 */
205 static void SetAllianceStationId(HAL_AllianceStationID allianceStationId);
206
207 /**
208 * Register a callback on match time.
209 *
210 * @param callback the callback that will be called whenever match time
211 * changes
212 * @param initialNotify if true, the callback will be run on the initial value
213 * @return the CallbackStore object associated with this callback
214 */
215 [[nodiscard]]
216 static std::unique_ptr<CallbackStore> RegisterMatchTimeCallback(
217 NotifyCallback callback, bool initialNotify);
218
219 /**
220 * Get the current value of the match timer.
221 *
222 * @return the current match time
223 */
224 static double GetMatchTime();
225
226 /**
227 * Sets the match timer.
228 *
229 * @param matchTime the new match time
230 */
231 static void SetMatchTime(double matchTime);
232
233 /**
234 * Register a callback on DS opmode changes.
235 *
236 * @param callback the callback that will be called when opmode changes
237 * @param initialNotify if true, the callback will be run on the initial value
238 * @return the CallbackStore object associated with this callback
239 */
240 [[nodiscard]]
241 static std::unique_ptr<CallbackStore> RegisterOpModeCallback(
242 NotifyCallback callback, bool initialNotify);
243
244 /**
245 * Get the opmode set by the DS.
246 *
247 * @return opmode
248 */
249 static int64_t GetOpMode();
250
251 /**
252 * Change the opmode set by the DS.
253 *
254 * @param opmode the new value
255 */
256 static void SetOpMode(int64_t opmode);
257
258 /**
259 * Register a callback on opmode options changes.
260 *
261 * @param callback the callback that will be called when the list of opmodes
262 * changes
263 * @param initialNotify if true, the callback will be run on the initial value
264 * @return the CallbackStore object associated with this callback.
265 */
266 [[nodiscard]]
267 static std::unique_ptr<CallbackStore> RegisterOpModeOptionsCallback(
268 OpModeOptionsCallback callback, bool initialNotify);
269
270 /**
271 * Gets the list of opmode options.
272 *
273 * @return opmodes list
274 */
276
277 /**
278 * Updates DriverStation data so that new values are visible to the user
279 * program.
280 */
281 static void NotifyNewData();
282
283 /**
284 * Sets suppression of DriverStation::ReportError and ReportWarning messages.
285 *
286 * @param shouldSend If false then messages will be suppressed.
287 */
288 static void SetSendError(bool shouldSend);
289
290 /**
291 * Sets suppression of DriverStation::SendConsoleLine messages.
292 *
293 * @param shouldSend If false then messages will be suppressed.
294 */
295 static void SetSendConsoleLine(bool shouldSend);
296
297 /**
298 * Gets the joystick outputs.
299 *
300 * @param stick The joystick number
301 * @return The joystick outputs
302 */
303 static int32_t GetJoystickLeds(int stick);
304
305 /**
306 * Gets the joystick rumble.
307 *
308 * @param stick The joystick number
309 * @param rumbleNum Rumble to get (0=left, 1=right, 2=left trigger, 3=right
310 * trigger)
311 * @return The joystick rumble value
312 */
313 static int GetJoystickRumble(int stick, int rumbleNum);
314
315 /**
316 * Sets the state of one joystick button. %Button indexes begin at 0.
317 *
318 * @param stick The joystick number
319 * @param button The button index, beginning at 0
320 * @param state The state of the joystick button
321 */
322 static void SetJoystickButton(int stick, int button, bool state);
323
324 /**
325 * Gets the value of the axis on a joystick.
326 *
327 * @param stick The joystick number
328 * @param axis The analog axis number
329 * @param value The value of the axis on the joystick
330 */
331 static void SetJoystickAxis(int stick, int axis, double value);
332
333 /**
334 * Gets the state of a POV on a joystick.
335 *
336 * @param stick The joystick number
337 * @param pov The POV number
338 * @param value the angle of the POV
339 */
340 static void SetJoystickPOV(int stick, int pov,
342
343 /**
344 * Sets the number of axes for a joystick.
345 *
346 * @param stick The joystick number
347 * @param maximumIndex The number of axes on the indicated joystick
348 */
349 static void SetJoystickAxesMaximumIndex(int stick, int maximumIndex);
350
351 /**
352 * Sets the number of axes for a joystick.
353 *
354 * @param stick The joystick number
355 * @param available The number of axes on the indicated joystick
356 */
357 static void SetJoystickAxesAvailable(int stick, int available);
358
359 /**
360 * Sets the number of POVs for a joystick.
361 *
362 * @param stick The joystick number
363 * @param maximumIndex The number of POVs on the indicated joystick
364 */
365 static void SetJoystickPOVsMaximumIndex(int stick, int maximumIndex);
366
367 /**
368 * Sets the number of POVs for a joystick.
369 *
370 * @param stick The joystick number
371 * @param available The number of POVs on the indicated joystick
372 */
373 static void SetJoystickPOVsAvailable(int stick, int available);
374
375 /**
376 * Sets the number of buttons for a joystick.
377 *
378 * @param stick The joystick number
379 * @param count The number of buttons on the indicated joystick
380 */
381 static void SetJoystickButtonsMaximumIndex(int stick, int count);
382
383 static void SetJoystickButtonsAvailable(int stick, uint64_t available);
384
385 /**
386 * Sets the value of isGamepad for a joystick.
387 *
388 * @param stick The joystick number
389 * @param isGamepad The value of isGamepad
390 */
391 static void SetJoystickIsGamepad(int stick, bool isGamepad);
392
393 /**
394 * Sets the value of type for a joystick.
395 *
396 * @param stick The joystick number
397 * @param type The value of type
398 */
399 static void SetJoystickGamepadType(int stick, int type);
400
401 /**
402 * Sets the name of a joystick.
403 *
404 * @param stick The joystick number
405 * @param name The value of name
406 */
407 static void SetJoystickName(int stick, std::string_view name);
408
409 /**
410 * Sets the supported outputs for a joystick.
411 *
412 * @param stick The joystick number
413 * @param supportedOutputs The supported outputs for the joystick
414 */
415 static void SetJoystickSupportedOutputs(int stick, int supportedOutputs);
416
417 /**
418 * Sets the game specific message.
419 *
420 * @param message the game specific message
421 */
422 static void SetGameData(std::string_view message);
423
424 /**
425 * Sets the event name.
426 *
427 * @param name the event name
428 */
429 static void SetEventName(std::string_view name);
430
431 /**
432 * Sets the match type.
433 *
434 * @param type the match type
435 */
437
438 /**
439 * Sets the match number.
440 *
441 * @param matchNumber the match number
442 */
443 static void SetMatchNumber(int matchNumber);
444
445 /**
446 * Sets the replay number.
447 *
448 * @param replayNumber the replay number
449 */
450 static void SetReplayNumber(int replayNumber);
451
452 /**
453 * Reset all simulation data for the Driver Station.
454 */
455 static void ResetData();
456};
457} // namespace wpi::sim
void HALSIM_FreeOpModeOptionsArray(struct HAL_OpModeOption *arr, size_t length)
@ name
Definition base.h:690
POVDirection
A controller POV direction.
Definition DriverStation.hpp:62
MatchType
The type of robot match that the robot is part of.
Definition DriverStation.hpp:48
Class to control a simulated driver station.
Definition DriverStationSim.hpp:50
static bool GetFmsAttached()
Check if the FMS is connected.
static void ResetData()
Reset all simulation data for the Driver Station.
static int GetJoystickRumble(int stick, int rumbleNum)
Gets the joystick rumble.
static void SetJoystickPOVsMaximumIndex(int stick, int maximumIndex)
Sets the number of POVs for a joystick.
static OpModeOptions GetOpModeOptions()
Gets the list of opmode options.
static void SetAllianceStationId(HAL_AllianceStationID allianceStationId)
Change the alliance station.
static int32_t GetJoystickLeds(int stick)
Gets the joystick outputs.
static void SetJoystickAxesAvailable(int stick, int available)
Sets the number of axes for a joystick.
static void SetJoystickName(int stick, std::string_view name)
Sets the name of a joystick.
static std::unique_ptr< CallbackStore > RegisterEnabledCallback(NotifyCallback callback, bool initialNotify)
Register a callback on whether the DS is enabled.
static std::unique_ptr< CallbackStore > RegisterFmsAttachedCallback(NotifyCallback callback, bool initialNotify)
Register a callback on whether the FMS is connected.
static std::unique_ptr< CallbackStore > RegisterRobotModeCallback(NotifyCallback callback, bool initialNotify)
Register a callback on DS robot mode changes.
static std::unique_ptr< CallbackStore > RegisterOpModeCallback(NotifyCallback callback, bool initialNotify)
Register a callback on DS opmode changes.
static std::unique_ptr< CallbackStore > RegisterAllianceStationIdCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the alliance station ID.
static void SetMatchTime(double matchTime)
Sets the match timer.
static std::unique_ptr< CallbackStore > RegisterOpModeOptionsCallback(OpModeOptionsCallback callback, bool initialNotify)
Register a callback on opmode options changes.
static void SetMatchType(DriverStation::MatchType type)
Sets the match type.
static std::unique_ptr< CallbackStore > RegisterMatchTimeCallback(NotifyCallback callback, bool initialNotify)
Register a callback on match time.
static void SetReplayNumber(int replayNumber)
Sets the replay number.
static void SetJoystickIsGamepad(int stick, bool isGamepad)
Sets the value of isGamepad for a joystick.
static std::unique_ptr< CallbackStore > RegisterDsAttachedCallback(NotifyCallback callback, bool initialNotify)
Register a callback on whether the DS is connected.
static bool GetEnabled()
Check if the DS is enabled.
static void SetMatchNumber(int matchNumber)
Sets the match number.
static void NotifyNewData()
Updates DriverStation data so that new values are visible to the user program.
static double GetMatchTime()
Get the current value of the match timer.
static void SetGameData(std::string_view message)
Sets the game specific message.
static void SetJoystickButton(int stick, int button, bool state)
Sets the state of one joystick button.
static void SetJoystickSupportedOutputs(int stick, int supportedOutputs)
Sets the supported outputs for a joystick.
static int64_t GetOpMode()
Get the opmode set by the DS.
static void SetEStop(bool eStop)
Set whether eStop is active.
static void SetJoystickButtonsMaximumIndex(int stick, int count)
Sets the number of buttons for a joystick.
static void SetJoystickGamepadType(int stick, int type)
Sets the value of type for a joystick.
static void SetEnabled(bool enabled)
Change whether the DS is enabled.
static void SetJoystickAxesMaximumIndex(int stick, int maximumIndex)
Sets the number of axes for a joystick.
static void SetJoystickAxis(int stick, int axis, double value)
Gets the value of the axis on a joystick.
static bool GetDsAttached()
Check if the DS is attached.
static HAL_RobotMode GetRobotMode()
Get the robot mode set by the DS.
static void SetJoystickButtonsAvailable(int stick, uint64_t available)
static void SetJoystickPOVsAvailable(int stick, int available)
Sets the number of POVs for a joystick.
static void SetEventName(std::string_view name)
Sets the event name.
static void SetSendError(bool shouldSend)
Sets suppression of DriverStation::ReportError and ReportWarning messages.
static HAL_AllianceStationID GetAllianceStationId()
Get the alliance station ID (color + number).
static std::unique_ptr< CallbackStore > RegisterEStopCallback(NotifyCallback callback, bool initialNotify)
Register a callback on the eStop state.
static void SetJoystickPOV(int stick, int pov, DriverStation::POVDirection value)
Gets the state of a POV on a joystick.
static void SetRobotMode(HAL_RobotMode robotMode)
Change the robot mode set by the DS.
static void SetFmsAttached(bool fmsAttached)
Change whether the FMS is connected.
static bool GetEStop()
Check if eStop has been activated.
static void SetOpMode(int64_t opmode)
Change the opmode set by the DS.
static void SetDsAttached(bool dsAttached)
Change whether the DS is attached.
static void SetSendConsoleLine(bool shouldSend)
Sets suppression of DriverStation::SendConsoleLine messages.
Definition DriverStationSim.hpp:18
OpModeOptions(OpModeOptions &&oth)
Definition DriverStationSim.hpp:25
OpModeOptions(HAL_OpModeOption *options, int32_t len)
Definition DriverStationSim.hpp:21
~OpModeOptions()
Definition DriverStationSim.hpp:40
OpModeOptions(const OpModeOptions &)=delete
OpModeOptions & operator=(OpModeOptions &&oth)
Definition DriverStationSim.hpp:31
OpModeOptions & operator=(const OpModeOptions &)=delete
HAL_RobotMode
Definition DriverStationTypes.h:55
HAL_AllianceStationID
Definition DriverStationTypes.h:31
Definition CTREPCMSim.hpp:13
std::function< void(std::string_view, const HAL_Value *)> NotifyCallback
Definition CallbackStore.hpp:16
std::function< void(std::string_view, std::span< const HAL_OpModeOption >)> OpModeOptionsCallback
Definition CallbackStore.hpp:19
Definition DriverStationTypes.h:177