WPILibC++  unspecified
DriverStation.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2017 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 <mutex>
14 #include <string>
15 #include <thread>
16 
17 #include <HAL/DriverStation.h>
18 #include <llvm/StringRef.h>
19 
20 #include "RobotState.h"
21 #include "SensorBase.h"
22 
23 namespace frc {
24 
30  public:
31  enum Alliance { kRed, kBlue, kInvalid };
32 
33  virtual ~DriverStation();
34  static DriverStation& GetInstance();
35  static void ReportError(llvm::StringRef error);
36  static void ReportWarning(llvm::StringRef error);
37  static void ReportError(bool is_error, int code, llvm::StringRef error,
38  llvm::StringRef location, llvm::StringRef stack);
39 
40  static const int kJoystickPorts = 6;
41 
42  bool GetStickButton(int stick, int button);
43  bool GetStickButtonPressed(int stick, int button);
44  bool GetStickButtonReleased(int stick, int button);
45 
46  double GetStickAxis(int stick, int axis);
47  int GetStickPOV(int stick, int pov);
48  int GetStickButtons(int stick) const;
49 
50  int GetStickAxisCount(int stick) const;
51  int GetStickPOVCount(int stick) const;
52  int GetStickButtonCount(int stick) const;
53 
54  bool GetJoystickIsXbox(int stick) const;
55  int GetJoystickType(int stick) const;
56  std::string GetJoystickName(int stick) const;
57  int GetJoystickAxisType(int stick, int axis) const;
58 
59  bool IsEnabled() const override;
60  bool IsDisabled() const override;
61  bool IsAutonomous() const override;
62  bool IsOperatorControl() const override;
63  bool IsTest() const override;
64  bool IsDSAttached() const;
65  bool IsNewControlData() const;
66  bool IsFMSAttached() const;
67  bool IsSysActive() const;
68  bool IsBrownedOut() const;
69 
70  Alliance GetAlliance() const;
71  int GetLocation() const;
72  void WaitForData();
73  bool WaitForData(double timeout);
74  double GetMatchTime() const;
75  double GetBatteryVoltage() const;
76 
81  void InDisabled(bool entering) { m_userInDisabled = entering; }
86  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
91  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
95  void InTest(bool entering) { m_userInTest = entering; }
96 
97  protected:
98  void GetData();
99 
100  private:
101  DriverStation();
102  void ReportJoystickUnpluggedError(llvm::StringRef message);
103  void ReportJoystickUnpluggedWarning(llvm::StringRef message);
104  void Run();
105  void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const;
106 
107  // Joystick User Data
108  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
109  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
110  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
111  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
112 
113  // Joystick Cached Data
114  std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
115  std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
116  std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
117  std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
118 
119  // Joystick button rising/falling edge flags
120  std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
121  std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
122 
123  // Internal Driver Station thread
124  std::thread m_dsThread;
125  std::atomic<bool> m_isRunning{false};
126 
127  mutable std::mutex m_joystickDataMutex;
128 
129  // Robot state status variables
130  bool m_userInDisabled = false;
131  bool m_userInAutonomous = false;
132  bool m_userInTeleop = false;
133  bool m_userInTest = false;
134 
135  // Control word variables
136  mutable HAL_ControlWord m_controlWordCache;
137  mutable std::chrono::steady_clock::time_point m_lastControlWordUpdate;
138  mutable std::mutex m_controlWordMutex;
139 
140  double m_nextMessageTime = 0;
141 };
142 
143 } // namespace frc
Definition: Timer.cpp:18
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:218
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:91
int GetJoystickType(int stick) const
Returns the type of joystick at a given port.
Definition: DriverStation.cpp:319
int GetStickAxisCount(int stick) const
Returns the number of axes on a given joystick port.
Definition: DriverStation.cpp:259
void GetData()
Copy data from the DS task for the user.
Definition: DriverStation.cpp:592
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:29
int GetStickPOVCount(int stick) const
Returns the number of POVs on a given joystick port.
Definition: DriverStation.cpp:274
int GetStickButtons(int stick) const
The state of the buttons on the joystick.
Definition: DriverStation.cpp:244
bool IsAutonomous() const override
Check if the DS is commanding autonomous mode.
Definition: DriverStation.cpp:385
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:81
bool GetStickButtonReleased(int stick, int button)
Whether one joystick button was released since the last check.
Definition: DriverStation.cpp:154
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:363
double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
Definition: DriverStation.cpp:192
void WaitForData()
Wait until a new packet comes from the driver station.
Definition: DriverStation.cpp:531
double GetBatteryVoltage() const
Read the battery voltage.
Definition: DriverStation.cpp:578
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
Definition: DriverStation.cpp:433
bool IsBrownedOut() const
Check if the system is browned out.
Definition: DriverStation.cpp:467
bool IsTest() const override
Check if the DS is commanding test mode.
Definition: DriverStation.cpp:407
bool GetJoystickIsXbox(int stick) const
Returns a boolean indicating if the controller is an xbox controller.
Definition: DriverStation.cpp:304
bool GetStickButton(int stick, int button)
The state of one joystick button.
Definition: DriverStation.cpp:86
Definition: RobotState.h:14
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
Definition: DriverStation.cpp:481
int GetStickButtonCount(int stick) const
Returns the number of buttons on a given joystick port.
Definition: DriverStation.cpp:289
bool GetStickButtonPressed(int stick, int button)
Whether one joystick button was pressed since the last check.
Definition: DriverStation.cpp:117
bool IsDSAttached() const
Check if the DS is attached.
Definition: DriverStation.cpp:418
bool IsDisabled() const override
Check if the robot is disabled.
Definition: DriverStation.cpp:374
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:95
int GetLocation() const
Return the driver station location on the field.
Definition: DriverStation.cpp:505
std::string GetJoystickName(int stick) const
Returns the name of the joystick at the given port.
Definition: DriverStation.cpp:334
static void ReportWarning(llvm::StringRef error)
Report a warning to the DriverStation messages window.
Definition: DriverStation.cpp:59
int GetJoystickAxisType(int stick, int axis) const
Returns the types of Axes on a given joystick port.
Definition: DriverStation.cpp:349
bool IsSysActive() const
Check if the FPGA outputs are enabled.
Definition: DriverStation.cpp:455
double GetMatchTime() const
Return the approximate match time.
Definition: DriverStation.cpp:568
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
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:86
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
Definition: DriverStation.cpp:441
bool IsOperatorControl() const override
Check if the DS is commanding teleop mode.
Definition: DriverStation.cpp:396