WPILibC++  unspecified
DriverStation.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <array>
11 #include <atomic>
12 #include <memory>
13 #include <string>
14 #include <thread>
15 
16 #include <HAL/DriverStation.h>
17 #include <wpi/Twine.h>
18 #include <wpi/condition_variable.h>
19 #include <wpi/deprecated.h>
20 #include <wpi/mutex.h>
21 
22 #include "ErrorBase.h"
23 #include "RobotState.h"
24 
25 namespace frc {
26 
27 struct MatchInfoData;
28 class MatchDataSender;
29 
35  public:
36  enum Alliance { kRed, kBlue, kInvalid };
37  enum MatchType { kNone, kPractice, kQualification, kElimination };
38 
39  ~DriverStation() override;
40 
46  static DriverStation& GetInstance();
47 
53  static void ReportError(const wpi::Twine& error);
54 
60  static void ReportWarning(const wpi::Twine& error);
61 
67  static void ReportError(bool isError, int code, const wpi::Twine& error,
68  const wpi::Twine& location, const wpi::Twine& stack);
69 
70  static constexpr int kJoystickPorts = 6;
71 
79  bool GetStickButton(int stick, int button);
80 
89  bool GetStickButtonPressed(int stick, int button);
90 
99  bool GetStickButtonReleased(int stick, int button);
100 
111  double GetStickAxis(int stick, int axis);
112 
118  int GetStickPOV(int stick, int pov);
119 
126  int GetStickButtons(int stick) const;
127 
134  int GetStickAxisCount(int stick) const;
135 
142  int GetStickPOVCount(int stick) const;
143 
150  int GetStickButtonCount(int stick) const;
151 
158  bool GetJoystickIsXbox(int stick) const;
159 
166  int GetJoystickType(int stick) const;
167 
174  std::string GetJoystickName(int stick) const;
175 
182  int GetJoystickAxisType(int stick, int axis) const;
183 
189  bool IsEnabled() const override;
190 
196  bool IsDisabled() const override;
197 
203  bool IsAutonomous() const override;
204 
210  bool IsOperatorControl() const override;
211 
217  bool IsTest() const override;
218 
224  bool IsDSAttached() const;
225 
235  bool IsNewControlData() const;
236 
243  bool IsFMSAttached() const;
244 
254  WPI_DEPRECATED("Use RobotController static class method")
255  bool IsSysActive() const;
256 
263  WPI_DEPRECATED("Use RobotController static class method")
264  bool IsBrownedOut() const;
265 
266  std::string GetGameSpecificMessage() const;
267  std::string GetEventName() const;
268  MatchType GetMatchType() const;
269  int GetMatchNumber() const;
270  int GetReplayNumber() const;
271 
279  Alliance GetAlliance() const;
280 
288  int GetLocation() const;
289 
298  void WaitForData();
299 
317  bool WaitForData(double timeout);
318 
334  double GetMatchTime() const;
335 
341  double GetBatteryVoltage() const;
342 
350  void InDisabled(bool entering) { m_userInDisabled = entering; }
351 
359  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
360 
368  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
369 
376  void InTest(bool entering) { m_userInTest = entering; }
377 
378  protected:
385  void GetData();
386 
387  private:
393  DriverStation();
394 
400  void ReportJoystickUnpluggedError(const wpi::Twine& message);
401 
407  void ReportJoystickUnpluggedWarning(const wpi::Twine& message);
408 
409  void Run();
410 
420  void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const;
421 
422  void SendMatchData();
423 
424  // Joystick User Data
425  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
426  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
427  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
428  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
429  std::unique_ptr<MatchInfoData> m_matchInfo;
430 
431  // Joystick Cached Data
432  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
433  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
434  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
435  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
436  std::unique_ptr<MatchInfoData> m_matchInfoCache;
437 
438  std::unique_ptr<MatchDataSender> m_matchDataSender;
439 
440  // Joystick button rising/falling edge flags
441  std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
442  std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
443 
444  // Internal Driver Station thread
445  std::thread m_dsThread;
446  std::atomic<bool> m_isRunning{false};
447 
448  wpi::mutex m_waitForDataMutex;
449  wpi::condition_variable m_waitForDataCond;
450  int m_waitForDataCounter;
451 
452  mutable wpi::mutex m_cacheDataMutex;
453 
454  // Robot state status variables
455  bool m_userInDisabled = false;
456  bool m_userInAutonomous = false;
457  bool m_userInTeleop = false;
458  bool m_userInTest = false;
459 
460  // Control word variables
461  mutable HAL_ControlWord m_controlWordCache;
462  mutable std::chrono::steady_clock::time_point m_lastControlWordUpdate;
463  mutable wpi::mutex m_controlWordMutex;
464 
465  double m_nextMessageTime = 0;
466 };
467 
468 } // namespace frc
Definition: Utility.cpp:119
int GetStickPOV(int stick, int pov)
Get the state of a POV on the joystick.
Definition: DriverStation.cpp:217
static void ReportWarning(const wpi::Twine &error)
Report a warning to the DriverStation messages window.
Definition: DriverStation.cpp:95
void InOperatorControl(bool entering)
Only to be used to tell the Driver Station what code you claim to be executing for diagnostic purpose...
Definition: DriverStation.h:368
int GetJoystickType(int stick) const
Returns the type of joystick at a given port.
Definition: DriverStation.cpp:289
int GetStickAxisCount(int stick) const
Returns the number of axes on a given joystick port.
Definition: DriverStation.cpp:249
static void ReportError(const wpi::Twine &error)
Report an error to the DriverStation messages window.
Definition: DriverStation.cpp:89
void GetData()
Copy data from the DS task for the user.
Definition: DriverStation.cpp:470
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:34
int GetStickPOVCount(int stick) const
Returns the number of POVs on a given joystick port.
Definition: DriverStation.cpp:259
int GetStickButtons(int stick) const
The state of the buttons on the joystick.
Definition: DriverStation.cpp:239
bool IsAutonomous() const override
Check if the DS is commanding autonomous mode.
Definition: DriverStation.cpp:330
void InDisabled(bool entering)
Only to be used to tell the Driver Station what code you claim to be executing for diagnostic purpose...
Definition: DriverStation.h:350
bool GetStickButtonReleased(int stick, int button)
Whether one joystick button was released since the last check.
Definition: DriverStation.cpp:166
static DriverStation & GetInstance()
Return a reference to the singleton DriverStation.
Definition: DriverStation.cpp:84
bool IsEnabled() const override
Check if the DS has enabled the robot.
Definition: DriverStation.cpp:318
double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
Definition: DriverStation.cpp:195
void WaitForData()
Wait until a new packet comes from the driver station.
Definition: DriverStation.cpp:436
double GetBatteryVoltage() const
Read the battery voltage.
Definition: DriverStation.cpp:462
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
Definition: DriverStation.cpp:354
bool IsBrownedOut() const
Check if the system is browned out.
Definition: DriverStation.cpp:369
bool IsTest() const override
Check if the DS is commanding test mode.
Definition: DriverStation.cpp:342
bool GetJoystickIsXbox(int stick) const
Returns a boolean indicating if the controller is an xbox controller.
Definition: DriverStation.cpp:279
bool GetStickButton(int stick, int button)
The state of one joystick button.
Definition: DriverStation.cpp:114
Definition: RobotState.h:14
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
Definition: DriverStation.cpp:401
Base class for most objects.
Definition: ErrorBase.h:74
int GetStickButtonCount(int stick) const
Returns the number of buttons on a given joystick port.
Definition: DriverStation.cpp:269
bool GetStickButtonPressed(int stick, int button)
Whether one joystick button was pressed since the last check.
Definition: DriverStation.cpp:137
bool IsDSAttached() const
Check if the DS is attached.
Definition: DriverStation.cpp:348
bool IsDisabled() const override
Check if the robot is disabled.
Definition: DriverStation.cpp:324
void InTest(bool entering)
Only to be used to tell the Driver Station what code you claim to be executing for diagnostic purpose...
Definition: DriverStation.h:376
int GetLocation() const
Return the driver station location on the field.
Definition: DriverStation.cpp:418
std::string GetJoystickName(int stick) const
Returns the name of the joystick at the given port.
Definition: DriverStation.cpp:299
int GetJoystickAxisType(int stick, int axis) const
Returns the types of Axes on a given joystick port.
Definition: DriverStation.cpp:308
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
bool IsSysActive() const
Check if the FPGA outputs are enabled.
Definition: DriverStation.cpp:362
double GetMatchTime() const
Return the approximate match time.
Definition: DriverStation.cpp:457
void InAutonomous(bool entering)
Only to be used to tell the Driver Station what code you claim to be executing for diagnostic purpose...
Definition: DriverStation.h:359
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
Definition: DriverStation.cpp:356
bool IsOperatorControl() const override
Check if the DS is commanding teleop mode.
Definition: DriverStation.cpp:336