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/condition_variable.h>
19 #include <support/deprecated.h>
20 #include <support/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  static DriverStation& GetInstance();
41  static void ReportError(const llvm::Twine& error);
42  static void ReportWarning(const llvm::Twine& error);
43  static void ReportError(bool isError, int code, const llvm::Twine& error,
44  const llvm::Twine& location,
45  const llvm::Twine& stack);
46 
47  static constexpr int kJoystickPorts = 6;
48 
49  bool GetStickButton(int stick, int button);
50  bool GetStickButtonPressed(int stick, int button);
51  bool GetStickButtonReleased(int stick, int button);
52 
53  double GetStickAxis(int stick, int axis);
54  int GetStickPOV(int stick, int pov);
55  int GetStickButtons(int stick) const;
56 
57  int GetStickAxisCount(int stick) const;
58  int GetStickPOVCount(int stick) const;
59  int GetStickButtonCount(int stick) const;
60 
61  bool GetJoystickIsXbox(int stick) const;
62  int GetJoystickType(int stick) const;
63  std::string GetJoystickName(int stick) const;
64  int GetJoystickAxisType(int stick, int axis) const;
65 
66  bool IsEnabled() const override;
67  bool IsDisabled() const override;
68  bool IsAutonomous() const override;
69  bool IsOperatorControl() const override;
70  bool IsTest() const override;
71  bool IsDSAttached() const;
72  bool IsNewControlData() const;
73  bool IsFMSAttached() const;
74  WPI_DEPRECATED("Use RobotController static class method")
75  bool IsSysActive() const;
76  WPI_DEPRECATED("Use RobotController static class method")
77  bool IsBrownedOut() const;
78 
79  std::string GetGameSpecificMessage() const;
80  std::string GetEventName() const;
81  MatchType GetMatchType() const;
82  int GetMatchNumber() const;
83  int GetReplayNumber() const;
84 
85  Alliance GetAlliance() const;
86  int GetLocation() const;
87  void WaitForData();
88  bool WaitForData(double timeout);
89  double GetMatchTime() const;
90  double GetBatteryVoltage() const;
91 
99  void InDisabled(bool entering) { m_userInDisabled = entering; }
100 
108  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
109 
117  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
118 
125  void InTest(bool entering) { m_userInTest = entering; }
126 
127  protected:
128  void GetData();
129 
130  private:
131  DriverStation();
132 
133  void ReportJoystickUnpluggedError(const llvm::Twine& message);
134  void ReportJoystickUnpluggedWarning(const llvm::Twine& message);
135  void Run();
136  void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const;
137  void SendMatchData();
138 
139  // Joystick User Data
140  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
141  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
142  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
143  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
144  std::unique_ptr<MatchInfoData> m_matchInfo;
145 
146  // Joystick Cached Data
147  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
148  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
149  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
150  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
151  std::unique_ptr<MatchInfoData> m_matchInfoCache;
152 
153  std::unique_ptr<MatchDataSender> m_matchDataSender;
154 
155  // Joystick button rising/falling edge flags
156  std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
157  std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
158 
159  // Internal Driver Station thread
160  std::thread m_dsThread;
161  std::atomic<bool> m_isRunning{false};
162 
163  wpi::mutex m_waitForDataMutex;
164  wpi::condition_variable m_waitForDataCond;
165  int m_waitForDataCounter;
166 
167  mutable wpi::mutex m_cacheDataMutex;
168 
169  // Robot state status variables
170  bool m_userInDisabled = false;
171  bool m_userInAutonomous = false;
172  bool m_userInTeleop = false;
173  bool m_userInTest = false;
174 
175  // Control word variables
176  mutable HAL_ControlWord m_controlWordCache;
177  mutable std::chrono::steady_clock::time_point m_lastControlWordUpdate;
178  mutable wpi::mutex m_controlWordMutex;
179 
180  double m_nextMessageTime = 0;
181 };
182 
183 } // namespace frc
Definition: RobotController.cpp:14
int GetStickPOV(int stick, int pov)
Get the state of a POV on the joystick.
Definition: DriverStation.cpp:273
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:117
int GetJoystickType(int stick) const
Returns the type of joystick at a given port.
Definition: DriverStation.cpp:374
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:314
void GetData()
Copy data from the DS task for the user.
Definition: DriverStation.cpp:748
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:329
int GetStickButtons(int stick) const
The state of the buttons on the joystick.
Definition: DriverStation.cpp:299
bool IsAutonomous() const override
Check if the DS is commanding autonomous mode.
Definition: DriverStation.cpp:440
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:99
bool GetStickButtonReleased(int stick, int button)
Whether one joystick button was released since the last check.
Definition: DriverStation.cpp:209
static DriverStation & GetInstance()
Return a pointer to the singleton DriverStation.
Definition: DriverStation.cpp:89
static void ReportError(const llvm::Twine &error)
Report an error to the DriverStation messages window.
Definition: DriverStation.cpp:99
bool IsEnabled() const override
Check if the DS has enabled the robot.
Definition: DriverStation.cpp:418
double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
Definition: DriverStation.cpp:247
Definition: DriverStation.cpp:28
void WaitForData()
Wait until a new packet comes from the driver station.
Definition: DriverStation.cpp:613
double GetBatteryVoltage() const
Read the battery voltage.
Definition: DriverStation.cpp:675
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
Definition: DriverStation.cpp:488
bool IsBrownedOut() const
Check if the system is browned out.
Definition: DriverStation.cpp:524
bool IsTest() const override
Check if the DS is commanding test mode.
Definition: DriverStation.cpp:462
bool GetJoystickIsXbox(int stick) const
Returns a boolean indicating if the controller is an xbox controller.
Definition: DriverStation.cpp:359
bool GetStickButton(int stick, int button)
The state of one joystick button.
Definition: DriverStation.cpp:141
static void ReportWarning(const llvm::Twine &error)
Report a warning to the DriverStation messages window.
Definition: DriverStation.cpp:110
Definition: RobotState.h:14
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
Definition: DriverStation.cpp:563
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:344
bool GetStickButtonPressed(int stick, int button)
Whether one joystick button was pressed since the last check.
Definition: DriverStation.cpp:172
bool IsDSAttached() const
Check if the DS is attached.
Definition: DriverStation.cpp:473
bool IsDisabled() const override
Check if the robot is disabled.
Definition: DriverStation.cpp:429
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:125
int GetLocation() const
Return the driver station location on the field.
Definition: DriverStation.cpp:587
std::string GetJoystickName(int stick) const
Returns the name of the joystick at the given port.
Definition: DriverStation.cpp:389
Definition: DriverStation.cpp:36
int GetJoystickAxisType(int stick, int axis) const
Returns the types of Axes on a given joystick port.
Definition: DriverStation.cpp:404
bool IsSysActive() const
Check if the FPGA outputs are enabled.
Definition: DriverStation.cpp:511
double GetMatchTime() const
Return the approximate match time.
Definition: DriverStation.cpp:665
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:108
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
Definition: DriverStation.cpp:496
bool IsOperatorControl() const override
Check if the DS is commanding teleop mode.
Definition: DriverStation.cpp:451