WPILibC++  2018.4.1-20180729184725-1146-g6db5f80
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 "frc/ErrorBase.h"
23 #include "frc/RobotState.h"
24 
25 namespace frc {
26 
27 class MatchDataSender;
28 
33 class DriverStation : public ErrorBase {
34  public:
35  enum Alliance { kRed, kBlue, kInvalid };
36  enum MatchType { kNone, kPractice, kQualification, kElimination };
37 
38  ~DriverStation() override;
39 
45  static DriverStation& GetInstance();
46 
52  static void ReportError(const wpi::Twine& error);
53 
59  static void ReportWarning(const wpi::Twine& error);
60 
66  static void ReportError(bool isError, int code, const wpi::Twine& error,
67  const wpi::Twine& location, const wpi::Twine& stack);
68 
69  static constexpr int kJoystickPorts = 6;
70 
78  bool GetStickButton(int stick, int button);
79 
88  bool GetStickButtonPressed(int stick, int button);
89 
98  bool GetStickButtonReleased(int stick, int button);
99 
110  double GetStickAxis(int stick, int axis);
111 
117  int GetStickPOV(int stick, int pov);
118 
125  int GetStickButtons(int stick) const;
126 
133  int GetStickAxisCount(int stick) const;
134 
141  int GetStickPOVCount(int stick) const;
142 
149  int GetStickButtonCount(int stick) const;
150 
157  bool GetJoystickIsXbox(int stick) const;
158 
165  int GetJoystickType(int stick) const;
166 
173  std::string GetJoystickName(int stick) const;
174 
181  int GetJoystickAxisType(int stick, int axis) const;
182 
188  bool IsEnabled() const;
189 
195  bool IsDisabled() const;
196 
202  bool IsAutonomous() const;
203 
209  bool IsOperatorControl() const;
210 
216  bool IsTest() const;
217 
223  bool IsDSAttached() const;
224 
234  bool IsNewControlData() const;
235 
242  bool IsFMSAttached() const;
243 
253  WPI_DEPRECATED("Use RobotController static class method")
254  bool IsSysActive() const;
255 
262  WPI_DEPRECATED("Use RobotController static class method")
263  bool IsBrownedOut() const;
264 
265  std::string GetGameSpecificMessage() const;
266  std::string GetEventName() const;
267  MatchType GetMatchType() const;
268  int GetMatchNumber() const;
269  int GetReplayNumber() const;
270 
278  Alliance GetAlliance() const;
279 
287  int GetLocation() const;
288 
297  void WaitForData();
298 
316  bool WaitForData(double timeout);
317 
333  double GetMatchTime() const;
334 
340  double GetBatteryVoltage() const;
341 
349  void InDisabled(bool entering) { m_userInDisabled = entering; }
350 
358  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
359 
367  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
368 
375  void InTest(bool entering) { m_userInTest = entering; }
376 
377  protected:
384  void GetData();
385 
386  private:
392  DriverStation();
393 
399  void ReportJoystickUnpluggedError(const wpi::Twine& message);
400 
406  void ReportJoystickUnpluggedWarning(const wpi::Twine& message);
407 
408  void Run();
409 
410  void SendMatchData();
411 
412  std::unique_ptr<MatchDataSender> m_matchDataSender;
413 
414  // Joystick button rising/falling edge flags
415  wpi::mutex m_buttonEdgeMutex;
416  std::array<HAL_JoystickButtons, kJoystickPorts> m_previousButtonStates;
417  std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
418  std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
419 
420  // Internal Driver Station thread
421  std::thread m_dsThread;
422  std::atomic<bool> m_isRunning{false};
423 
424  wpi::mutex m_waitForDataMutex;
425  wpi::condition_variable m_waitForDataCond;
426  int m_waitForDataCounter;
427 
428  // Robot state status variables
429  bool m_userInDisabled = false;
430  bool m_userInAutonomous = false;
431  bool m_userInTeleop = false;
432  bool m_userInTest = false;
433 
434  double m_nextMessageTime = 0;
435 };
436 
437 } // namespace frc
double GetBatteryVoltage() const
Read the battery voltage.
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
int GetStickAxisCount(int stick) const
Returns the number of axes on a given joystick port.
double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
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:367
static DriverStation & GetInstance()
Return a reference to the singleton DriverStation.
bool GetStickButton(int stick, int button)
The state of one joystick button.
bool IsTest() const
Check if the DS is commanding test mode.
bool GetStickButtonReleased(int stick, int button)
Whether one joystick button was released since the last check.
int GetStickPOVCount(int stick) const
Returns the number of POVs on a given joystick port.
bool IsDisabled() const
Check if the robot is disabled.
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:33
static void ReportWarning(const wpi::Twine &error)
Report a warning to the DriverStation messages window.
bool IsDSAttached() const
Check if the DS is attached.
double GetMatchTime() const
Return the approximate match time.
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:349
int GetStickButtonCount(int stick) const
Returns the number of buttons on a given joystick port.
int GetStickPOV(int stick, int pov)
Get the state of a POV on the joystick.
int GetLocation() const
Return the driver station location on the field.
bool IsBrownedOut() const
Check if the system is browned out.
int GetJoystickType(int stick) const
Returns the type of joystick at a given port.
void GetData()
Copy data from the DS task for the user.
bool IsEnabled() const
Check if the DS has enabled the robot.
int GetStickButtons(int stick) const
The state of the buttons on the joystick.
Base class for most objects.
Definition: ErrorBase.h:74
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
bool GetStickButtonPressed(int stick, int button)
Whether one joystick button was pressed since the last check.
std::string GetJoystickName(int stick) const
Returns the name of the joystick at the given port.
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
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:375
void WaitForData()
Wait until a new packet comes from the driver station.
bool IsSysActive() const
Check if the FPGA outputs are enabled.
static void ReportError(const wpi::Twine &error)
Report an error to the DriverStation messages window.
bool IsAutonomous() const
Check if the DS is commanding autonomous mode.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
int GetJoystickAxisType(int stick, int axis) const
Returns the types of Axes on a given joystick port.
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:358
bool IsOperatorControl() const
Check if the DS is commanding teleop mode.
bool GetJoystickIsXbox(int stick) const
Returns a boolean indicating if the controller is an xbox controller.