WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
DriverStation.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. 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 <atomic>
11 #include <memory>
12 #include <string>
13 #include <thread>
14 
15 #include "HAL/DriverStation.h"
16 #include "HAL/cpp/priority_mutex.h"
17 #include "RobotState.h"
18 #include "SensorBase.h"
19 #include "llvm/StringRef.h"
20 
21 namespace frc {
22 
28  public:
29  enum Alliance { kRed, kBlue, kInvalid };
30 
31  virtual ~DriverStation();
32  static DriverStation& GetInstance();
33  static void ReportError(llvm::StringRef error);
34  static void ReportWarning(llvm::StringRef error);
35  static void ReportError(bool is_error, int code, llvm::StringRef error,
36  llvm::StringRef location, llvm::StringRef stack);
37 
38  static const int kJoystickPorts = 6;
39 
40  double GetStickAxis(int stick, int axis);
41  int GetStickPOV(int stick, int pov);
42  int GetStickButtons(int stick) const;
43  bool GetStickButton(int stick, int button);
44 
45  int GetStickAxisCount(int stick) const;
46  int GetStickPOVCount(int stick) const;
47  int GetStickButtonCount(int stick) const;
48 
49  bool GetJoystickIsXbox(int stick) const;
50  int GetJoystickType(int stick) const;
51  std::string GetJoystickName(int stick) const;
52  int GetJoystickAxisType(int stick, int axis) const;
53 
54  bool IsEnabled() const override;
55  bool IsDisabled() const override;
56  bool IsAutonomous() const override;
57  bool IsOperatorControl() const override;
58  bool IsTest() const override;
59  bool IsDSAttached() const;
60  bool IsNewControlData() const;
61  bool IsFMSAttached() const;
62  bool IsSysActive() const;
63  bool IsBrownedOut() const;
64 
65  Alliance GetAlliance() const;
66  int GetLocation() const;
67  void WaitForData();
68  bool WaitForData(double timeout);
69  double GetMatchTime() const;
70  double GetBatteryVoltage() const;
71 
76  void InDisabled(bool entering) { m_userInDisabled = entering; }
81  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
86  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
90  void InTest(bool entering) { m_userInTest = entering; }
91 
92  protected:
93  void GetData();
94 
95  private:
96  DriverStation();
97  void ReportJoystickUnpluggedError(llvm::StringRef message);
98  void ReportJoystickUnpluggedWarning(llvm::StringRef message);
99  void Run();
100  void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const;
101 
102  // Joystick User Data
103  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
104  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
105  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
106  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
107 
108  // Joystick Cached Data
109  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
110  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
111  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
112  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
113 
114  // Internal Driver Station thread
115  std::thread m_dsThread;
116  std::atomic<bool> m_isRunning{false};
117 
118  mutable hal::priority_mutex m_joystickDataMutex;
119 
120  // Robot state status variables
121  bool m_userInDisabled = false;
122  bool m_userInAutonomous = false;
123  bool m_userInTeleop = false;
124  bool m_userInTest = false;
125 
126  // Control word variables
127  mutable HAL_ControlWord m_controlWordCache;
128  mutable std::chrono::steady_clock::time_point m_lastControlWordUpdate;
129  mutable hal::priority_mutex m_controlWordMutex;
130 
131  double m_nextMessageTime = 0;
132 };
133 
134 } // namespace frc
static void ReportError(llvm::StringRef error)
Report an error to the DriverStation messages window.
Definition: DriverStation.cpp:49
int GetStickPOV(int stick, int pov)
Get the state of a POV on the joystick.
Definition: DriverStation.cpp:114
Base class for all sensors.
Definition: SensorBase.h:20
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:86
int GetJoystickType(int stick) const
Returns the type of joystick at a given port.
Definition: DriverStation.cpp:245
int GetStickAxisCount(int stick) const
Returns the number of axes on a given joystick port.
Definition: DriverStation.cpp:185
void GetData()
Copy data from the DS task for the user.
Definition: DriverStation.cpp:518
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:27
int GetStickPOVCount(int stick) const
Returns the number of POVs on a given joystick port.
Definition: DriverStation.cpp:200
int GetStickButtons(int stick) const
The state of the buttons on the joystick.
Definition: DriverStation.cpp:140
bool IsAutonomous() const override
Check if the DS is commanding autonomous mode.
Definition: DriverStation.cpp:311
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:76
static DriverStation & GetInstance()
Return a pointer to the singleton DriverStation.
Definition: DriverStation.cpp:39
bool IsEnabled() const override
Check if the DS has enabled the robot.
Definition: DriverStation.cpp:289
double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
Definition: DriverStation.cpp:88
void WaitForData()
Wait until a new packet comes from the driver station.
Definition: DriverStation.cpp:457
double GetBatteryVoltage() const
Read the battery voltage.
Definition: DriverStation.cpp:504
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
Definition: DriverStation.cpp:359
bool IsBrownedOut() const
Check if the system is browned out.
Definition: DriverStation.cpp:393
bool IsTest() const override
Check if the DS is commanding test mode.
Definition: DriverStation.cpp:333
bool GetJoystickIsXbox(int stick) const
Returns a boolean indicating if the controller is an xbox controller.
Definition: DriverStation.cpp:230
bool GetStickButton(int stick, int button)
The state of one joystick button.
Definition: DriverStation.cpp:156
Definition: RobotState.h:14
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
Definition: DriverStation.cpp:407
int GetStickButtonCount(int stick) const
Returns the number of buttons on a given joystick port.
Definition: DriverStation.cpp:215
bool IsDSAttached() const
Check if the DS is attached.
Definition: DriverStation.cpp:344
bool IsDisabled() const override
Check if the robot is disabled.
Definition: DriverStation.cpp:300
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:90
Definition: priority_mutex.h:57
int GetLocation() const
Return the driver station location on the field.
Definition: DriverStation.cpp:431
std::string GetJoystickName(int stick) const
Returns the name of the joystick at the given port.
Definition: DriverStation.cpp:260
static void ReportWarning(llvm::StringRef error)
Report a warning to the DriverStation messages window.
Definition: DriverStation.cpp:59
Definition: DriverStation.h:31
int GetJoystickAxisType(int stick, int axis) const
Returns the types of Axes on a given joystick port.
Definition: DriverStation.cpp:275
bool IsSysActive() const
Check if the FPGA outputs are enabled.
Definition: DriverStation.cpp:381
double GetMatchTime() const
Return the approximate match time.
Definition: DriverStation.cpp:494
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:81
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
Definition: DriverStation.cpp:367
bool IsOperatorControl() const override
Check if the DS is commanding teleop mode.
Definition: DriverStation.cpp:322