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 <llvm/Twine.h>
18 #include <support/deprecated.h>
19 #include <support/mutex.h>
20 
21 #include "ErrorBase.h"
22 #include "RobotState.h"
23 
24 namespace frc {
25 
26 struct MatchInfoData;
27 
33  public:
34  enum Alliance { kRed, kBlue, kInvalid };
35  enum MatchType { kNone, kPractice, kQualification, kElimination };
36 
37  ~DriverStation() override;
38  static DriverStation& GetInstance();
39  static void ReportError(const llvm::Twine& error);
40  static void ReportWarning(const llvm::Twine& error);
41  static void ReportError(bool isError, int code, const llvm::Twine& error,
42  const llvm::Twine& location,
43  const llvm::Twine& stack);
44 
45  static constexpr int kJoystickPorts = 6;
46 
47  bool GetStickButton(int stick, int button);
48  bool GetStickButtonPressed(int stick, int button);
49  bool GetStickButtonReleased(int stick, int button);
50 
51  double GetStickAxis(int stick, int axis);
52  int GetStickPOV(int stick, int pov);
53  int GetStickButtons(int stick) const;
54 
55  int GetStickAxisCount(int stick) const;
56  int GetStickPOVCount(int stick) const;
57  int GetStickButtonCount(int stick) const;
58 
59  bool GetJoystickIsXbox(int stick) const;
60  int GetJoystickType(int stick) const;
61  std::string GetJoystickName(int stick) const;
62  int GetJoystickAxisType(int stick, int axis) const;
63 
64  bool IsEnabled() const override;
65  bool IsDisabled() const override;
66  bool IsAutonomous() const override;
67  bool IsOperatorControl() const override;
68  bool IsTest() const override;
69  bool IsDSAttached() const;
70  bool IsNewControlData() const;
71  bool IsFMSAttached() const;
72  WPI_DEPRECATED("Use RobotController static class method")
73  bool IsSysActive() const;
74  WPI_DEPRECATED("Use RobotController static class method")
75  bool IsBrownedOut() const;
76 
77  std::string GetGameSpecificMessage() const;
78  std::string GetEventName() const;
79  MatchType GetMatchType() const;
80  int GetMatchNumber() const;
81  int GetReplayNumber() const;
82 
83  Alliance GetAlliance() const;
84  int GetLocation() const;
85  void WaitForData();
86  bool WaitForData(double timeout);
87  double GetMatchTime() const;
88  double GetBatteryVoltage() const;
89 
97  void InDisabled(bool entering) { m_userInDisabled = entering; }
98 
106  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
107 
115  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
116 
123  void InTest(bool entering) { m_userInTest = entering; }
124 
125  protected:
126  void GetData();
127 
128  private:
129  DriverStation();
130 
131  void ReportJoystickUnpluggedError(const llvm::Twine& message);
132  void ReportJoystickUnpluggedWarning(const llvm::Twine& message);
133  void Run();
134  void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const;
135 
136  // Joystick User Data
137  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
138  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
139  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
140  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
141  std::unique_ptr<MatchInfoData> m_matchInfo;
142 
143  // Joystick Cached Data
144  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
145  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
146  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
147  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
148  std::unique_ptr<MatchInfoData> m_matchInfoCache;
149 
150  // Joystick button rising/falling edge flags
151  std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
152  std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
153 
154  // Internal Driver Station thread
155  std::thread m_dsThread;
156  std::atomic<bool> m_isRunning{false};
157 
158  mutable wpi::mutex m_cacheDataMutex;
159 
160  // Robot state status variables
161  bool m_userInDisabled = false;
162  bool m_userInAutonomous = false;
163  bool m_userInTeleop = false;
164  bool m_userInTest = false;
165 
166  // Control word variables
167  mutable HAL_ControlWord m_controlWordCache;
168  mutable std::chrono::steady_clock::time_point m_lastControlWordUpdate;
169  mutable wpi::mutex m_controlWordMutex;
170 
171  double m_nextMessageTime = 0;
172 };
173 
174 } // namespace frc
Definition: RobotController.cpp:14
int GetStickPOV(int stick, int pov)
Get the state of a POV on the joystick.
Definition: DriverStation.cpp:233
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:115
int GetJoystickType(int stick) const
Returns the type of joystick at a given port.
Definition: DriverStation.cpp:334
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
int GetStickAxisCount(int stick) const
Returns the number of axes on a given joystick port.
Definition: DriverStation.cpp:274
void GetData()
Copy data from the DS task for the user.
Definition: DriverStation.cpp:634
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:32
int GetStickPOVCount(int stick) const
Returns the number of POVs on a given joystick port.
Definition: DriverStation.cpp:289
int GetStickButtons(int stick) const
The state of the buttons on the joystick.
Definition: DriverStation.cpp:259
bool IsAutonomous() const override
Check if the DS is commanding autonomous mode.
Definition: DriverStation.cpp:400
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:97
bool GetStickButtonReleased(int stick, int button)
Whether one joystick button was released since the last check.
Definition: DriverStation.cpp:169
static DriverStation & GetInstance()
Return a pointer to the singleton DriverStation.
Definition: DriverStation.cpp:49
static void ReportError(const llvm::Twine &error)
Report an error to the DriverStation messages window.
Definition: DriverStation.cpp:59
bool IsEnabled() const override
Check if the DS has enabled the robot.
Definition: DriverStation.cpp:378
double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
Definition: DriverStation.cpp:207
Definition: DriverStation.cpp:24
void WaitForData()
Wait until a new packet comes from the driver station.
Definition: DriverStation.cpp:573
double GetBatteryVoltage() const
Read the battery voltage.
Definition: DriverStation.cpp:620
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
Definition: DriverStation.cpp:448
bool IsBrownedOut() const
Check if the system is browned out.
Definition: DriverStation.cpp:484
bool IsTest() const override
Check if the DS is commanding test mode.
Definition: DriverStation.cpp:422
bool GetJoystickIsXbox(int stick) const
Returns a boolean indicating if the controller is an xbox controller.
Definition: DriverStation.cpp:319
bool GetStickButton(int stick, int button)
The state of one joystick button.
Definition: DriverStation.cpp:101
static void ReportWarning(const llvm::Twine &error)
Report a warning to the DriverStation messages window.
Definition: DriverStation.cpp:70
Definition: RobotState.h:14
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
Definition: DriverStation.cpp:523
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:304
bool GetStickButtonPressed(int stick, int button)
Whether one joystick button was pressed since the last check.
Definition: DriverStation.cpp:132
bool IsDSAttached() const
Check if the DS is attached.
Definition: DriverStation.cpp:433
bool IsDisabled() const override
Check if the robot is disabled.
Definition: DriverStation.cpp:389
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:123
int GetLocation() const
Return the driver station location on the field.
Definition: DriverStation.cpp:547
std::string GetJoystickName(int stick) const
Returns the name of the joystick at the given port.
Definition: DriverStation.cpp:349
int GetJoystickAxisType(int stick, int axis) const
Returns the types of Axes on a given joystick port.
Definition: DriverStation.cpp:364
bool IsSysActive() const
Check if the FPGA outputs are enabled.
Definition: DriverStation.cpp:471
double GetMatchTime() const
Return the approximate match time.
Definition: DriverStation.cpp:610
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:106
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
Definition: DriverStation.cpp:456
bool IsOperatorControl() const override
Check if the DS is commanding teleop mode.
Definition: DriverStation.cpp:411