WPILibC++  2020.3.2
DriverStation.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 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/DriverStationTypes.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 
40  DriverStation(const DriverStation&) = delete;
41  DriverStation& operator=(const DriverStation&) = delete;
42 
48  static DriverStation& GetInstance();
49 
55  static void ReportError(const wpi::Twine& error);
56 
62  static void ReportWarning(const wpi::Twine& error);
63 
69  static void ReportError(bool isError, int code, const wpi::Twine& error,
70  const wpi::Twine& location, const wpi::Twine& stack);
71 
72  static constexpr int kJoystickPorts = 6;
73 
81  bool GetStickButton(int stick, int button);
82 
91  bool GetStickButtonPressed(int stick, int button);
92 
101  bool GetStickButtonReleased(int stick, int button);
102 
113  double GetStickAxis(int stick, int axis);
114 
120  int GetStickPOV(int stick, int pov);
121 
128  int GetStickButtons(int stick) const;
129 
136  int GetStickAxisCount(int stick) const;
137 
144  int GetStickPOVCount(int stick) const;
145 
152  int GetStickButtonCount(int stick) const;
153 
160  bool GetJoystickIsXbox(int stick) const;
161 
168  int GetJoystickType(int stick) const;
169 
176  std::string GetJoystickName(int stick) const;
177 
184  int GetJoystickAxisType(int stick, int axis) const;
185 
191  bool IsEnabled() const;
192 
198  bool IsDisabled() const;
199 
205  bool IsEStopped() const;
206 
212  bool IsAutonomous() const;
213 
219  bool IsOperatorControl() const;
220 
226  bool IsTest() const;
227 
233  bool IsDSAttached() const;
234 
244  bool IsNewControlData() const;
245 
252  bool IsFMSAttached() const;
253 
259  std::string GetGameSpecificMessage() const;
260 
266  std::string GetEventName() const;
267 
274  MatchType GetMatchType() const;
275 
281  int GetMatchNumber() const;
282 
289  int GetReplayNumber() const;
290 
298  Alliance GetAlliance() const;
299 
307  int GetLocation() const;
308 
317  void WaitForData();
318 
336  bool WaitForData(double timeout);
337 
353  double GetMatchTime() const;
354 
360  double GetBatteryVoltage() const;
361 
369  void InDisabled(bool entering) { m_userInDisabled = entering; }
370 
378  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
379 
387  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
388 
395  void InTest(bool entering) { m_userInTest = entering; }
396 
400  void WakeupWaitForData();
401 
402  protected:
409  void GetData();
410 
411  private:
417  DriverStation();
418 
424  void ReportJoystickUnpluggedError(const wpi::Twine& message);
425 
431  void ReportJoystickUnpluggedWarning(const wpi::Twine& message);
432 
433  void Run();
434 
435  void SendMatchData();
436 
437  std::unique_ptr<MatchDataSender> m_matchDataSender;
438 
439  // Joystick button rising/falling edge flags
440  wpi::mutex m_buttonEdgeMutex;
441  std::array<HAL_JoystickButtons, kJoystickPorts> m_previousButtonStates;
442  std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
443  std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
444 
445  // Internal Driver Station thread
446  std::thread m_dsThread;
447  std::atomic<bool> m_isRunning{false};
448 
449  wpi::mutex m_waitForDataMutex;
450  wpi::condition_variable m_waitForDataCond;
451  int m_waitForDataCounter;
452 
453  // Robot state status variables
454  bool m_userInDisabled = false;
455  bool m_userInAutonomous = false;
456  bool m_userInTeleop = false;
457  bool m_userInTest = false;
458 
459  double m_nextMessageTime = 0;
460 };
461 
462 } // namespace frc
frc::DriverStation::IsDisabled
bool IsDisabled() const
Check if the robot is disabled.
frc::DriverStation::InTest
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:395
frc::DriverStation::IsDSAttached
bool IsDSAttached() const
Check if the DS is attached.
frc::DriverStation::GetData
void GetData()
Copy data from the DS task for the user.
frc::DriverStation::IsFMSAttached
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
frc::DriverStation::GetMatchTime
double GetMatchTime() const
Return the approximate match time.
frc::DriverStation::GetStickAxisCount
int GetStickAxisCount(int stick) const
Returns the number of axes on a given joystick port.
frc::DriverStation::WaitForData
void WaitForData()
Wait until a new packet comes from the driver station.
frc::DriverStation::GetMatchType
MatchType GetMatchType() const
Returns the type of match being played provided by the FMS.
frc::DriverStation::GetMatchNumber
int GetMatchNumber() const
Returns the match number provided by the FMS.
frc::DriverStation::GetStickPOV
int GetStickPOV(int stick, int pov)
Get the state of a POV on the joystick.
frc::DriverStation::GetStickButton
bool GetStickButton(int stick, int button)
The state of one joystick button.
frc::DriverStation::GetStickPOVCount
int GetStickPOVCount(int stick) const
Returns the number of POVs on a given joystick port.
frc::DriverStation::IsEnabled
bool IsEnabled() const
Check if the DS has enabled the robot.
frc::DriverStation::InOperatorControl
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:387
frc::DriverStation::ReportWarning
static void ReportWarning(const wpi::Twine &error)
Report a warning to the DriverStation messages window.
frc::DriverStation::GetStickAxis
double GetStickAxis(int stick, int axis)
Get the value of the axis on a joystick.
frc::DriverStation::IsNewControlData
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
frc::DriverStation::IsAutonomous
bool IsAutonomous() const
Check if the DS is commanding autonomous mode.
frc::DriverStation::GetReplayNumber
int GetReplayNumber() const
Returns the number of times the current match has been replayed from the FMS.
frc::DriverStation::IsTest
bool IsTest() const
Check if the DS is commanding test mode.
frc::DriverStation::GetJoystickType
int GetJoystickType(int stick) const
Returns the type of joystick at a given port.
frc::DriverStation::IsEStopped
bool IsEStopped() const
Check if the robot is e-stopped.
frc::DriverStation::GetStickButtonPressed
bool GetStickButtonPressed(int stick, int button)
Whether one joystick button was pressed since the last check.
frc::DriverStation::GetStickButtonReleased
bool GetStickButtonReleased(int stick, int button)
Whether one joystick button was released since the last check.
frc::DriverStation::GetLocation
int GetLocation() const
Return the driver station location on the field.
frc::DriverStation::GetStickButtons
int GetStickButtons(int stick) const
The state of the buttons on the joystick.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::DriverStation::GetInstance
static DriverStation & GetInstance()
Return a reference to the singleton DriverStation.
frc::DriverStation::WakeupWaitForData
void WakeupWaitForData()
Forces WaitForData() to return immediately.
frc::DriverStation::GetBatteryVoltage
double GetBatteryVoltage() const
Read the battery voltage.
frc::DriverStation::InDisabled
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:369
frc::DriverStation::ReportError
static void ReportError(const wpi::Twine &error)
Report an error to the DriverStation messages window.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::DriverStation::GetEventName
std::string GetEventName() const
Returns the name of the competition event provided by the FMS.
frc::DriverStation::GetJoystickName
std::string GetJoystickName(int stick) const
Returns the name of the joystick at the given port.
frc::DriverStation::IsOperatorControl
bool IsOperatorControl() const
Check if the DS is commanding teleop mode.
frc::DriverStation::GetAlliance
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
frc::DriverStation::InAutonomous
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:378
frc::DriverStation
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:33
frc::DriverStation::GetJoystickIsXbox
bool GetJoystickIsXbox(int stick) const
Returns a boolean indicating if the controller is an xbox controller.
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
frc::DriverStation::GetGameSpecificMessage
std::string GetGameSpecificMessage() const
Returns the game specific message provided by the FMS.
frc::DriverStation::GetStickButtonCount
int GetStickButtonCount(int stick) const
Returns the number of buttons on a given joystick port.
frc::DriverStation::GetJoystickAxisType
int GetJoystickAxisType(int stick, int axis) const
Returns the types of Axes on a given joystick port.