WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
DriverStation.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2016. 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 "SensorBase.h"
11 #include "RobotState.h"
12 #include "Task.h"
13 #include "HAL/HAL.hpp"
14 #include "HAL/cpp/Semaphore.hpp"
15 #include "HAL/cpp/priority_mutex.h"
16 #include "HAL/cpp/priority_condition_variable.h"
17 #include <condition_variable>
18 #include <atomic>
19 
20 struct HALControlWord;
21 class AnalogInput;
22 
28  public:
29  enum Alliance { kRed, kBlue, kInvalid };
30 
31  virtual ~DriverStation();
32  static DriverStation &GetInstance();
33  static void ReportError(std::string error);
34  static void ReportWarning(std::string error);
35  static void ReportError(bool is_error, int32_t code, const std::string& error,
36  const std::string& location,
37  const std::string& stack);
38 
39  static const uint32_t kJoystickPorts = 6;
40 
41  float GetStickAxis(uint32_t stick, uint32_t axis);
42  int GetStickPOV(uint32_t stick, uint32_t pov);
43  uint32_t GetStickButtons(uint32_t stick) const;
44  bool GetStickButton(uint32_t stick, uint8_t button);
45 
46  int GetStickAxisCount(uint32_t stick) const;
47  int GetStickPOVCount(uint32_t stick) const;
48  int GetStickButtonCount(uint32_t stick) const;
49 
50  bool GetJoystickIsXbox(uint32_t stick) const;
51  int GetJoystickType(uint32_t stick) const;
52  std::string GetJoystickName(uint32_t stick) const;
53  int GetJoystickAxisType(uint32_t stick, uint8_t axis) const;
54 
55  bool IsEnabled() const override;
56  bool IsDisabled() const override;
57  bool IsAutonomous() const override;
58  bool IsOperatorControl() const override;
59  bool IsTest() const override;
60  bool IsDSAttached() const;
61  bool IsNewControlData() const;
62  bool IsFMSAttached() const;
63  bool IsSysActive() const;
64  bool IsSysBrownedOut() const;
65 
66  Alliance GetAlliance() const;
67  uint32_t GetLocation() const;
68  void WaitForData();
69  double GetMatchTime() const;
70  float GetBatteryVoltage() const;
71 
77  void InDisabled(bool entering) { m_userInDisabled = entering; }
83  void InAutonomous(bool entering) { m_userInAutonomous = entering; }
89  void InOperatorControl(bool entering) { m_userInTeleop = entering; }
94  void InTest(bool entering) { m_userInTest = entering; }
95 
96  protected:
97  DriverStation();
98 
99  void GetData();
100 
101  private:
102  static DriverStation *m_instance;
103  void ReportJoystickUnpluggedError(std::string message);
104  void ReportJoystickUnpluggedWarning(std::string message);
105  void Run();
106 
107  HALJoystickAxes m_joystickAxes[kJoystickPorts];
108  HALJoystickPOVs m_joystickPOVs[kJoystickPorts];
109  HALJoystickButtons m_joystickButtons[kJoystickPorts];
110  HALJoystickDescriptor m_joystickDescriptor[kJoystickPorts];
111  Task m_task;
112  std::atomic<bool> m_isRunning{false};
113  mutable Semaphore m_newControlData{Semaphore::kEmpty};
114  mutable priority_condition_variable m_packetDataAvailableCond;
115  priority_mutex m_packetDataAvailableMutex;
116  std::condition_variable_any m_waitForDataCond;
117  priority_mutex m_waitForDataMutex;
118  bool m_userInDisabled = false;
119  bool m_userInAutonomous = false;
120  bool m_userInTeleop = false;
121  bool m_userInTest = false;
122  double m_nextMessageTime = 0;
123 };
Definition: HAL.hpp:181
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:77
bool IsNewControlData() const
Has a new control packet from the driver station arrived since the last time this function was called...
Definition: DriverStation.cpp:444
Definition: HAL.hpp:191
static void ReportWarning(std::string error)
Report a warning to the DriverStation messages window.
Definition: DriverStation.cpp:546
bool IsSysActive() const
Check if the FPGA outputs are enabled.
Definition: DriverStation.cpp:419
Definition: Semaphore.hpp:8
int GetStickPOVCount(uint32_t stick) const
Returns the number of POVs on a given joystick port.
Definition: DriverStation.cpp:226
float GetStickAxis(uint32_t stick, uint32_t axis)
Get the value of the axis on a joystick.
Definition: DriverStation.cpp:260
int GetStickPOV(uint32_t stick, uint32_t pov)
Get the state of a POV on the joystick.
Definition: DriverStation.cpp:289
uint32_t GetLocation() const
Return the driver station location on the field This could return 1, 2, or 3.
Definition: DriverStation.cpp:486
bool IsSysBrownedOut() const
Check if the system is browned out.
Definition: DriverStation.cpp:430
static void ReportError(std::string error)
Report an error to the DriverStation messages window.
Definition: DriverStation.cpp:538
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:83
int GetStickAxisCount(uint32_t stick) const
Returns the number of axes on a given joystick port.
Definition: DriverStation.cpp:154
int GetJoystickAxisType(uint32_t stick, uint8_t axis) const
Returns the types of Axes on a given joystick port.
Definition: DriverStation.cpp:212
Base class for all sensors.
Definition: SensorBase.h:20
bool IsEnabled() const override
Check if the DS has enabled the robot.
Definition: DriverStation.cpp:352
bool IsOperatorControl() const override
Check if the DS is commanding teleop mode.
Definition: DriverStation.cpp:385
Definition: priority_condition_variable.h:23
bool IsDSAttached() const
Check if the DS is attached.
Definition: DriverStation.cpp:406
void GetData()
Copy data from the DS task for the user.
Definition: DriverStation.cpp:100
static DriverStation & GetInstance()
Return a pointer to the singleton DriverStation.
Definition: DriverStation.cpp:90
Definition: RobotState.h:12
Definition: priority_mutex.h:53
Definition: HAL.hpp:176
Definition: HAL.hpp:186
uint32_t GetStickButtons(uint32_t stick) const
The state of the buttons on the joystick.
Definition: DriverStation.cpp:313
bool GetStickButton(uint32_t stick, uint8_t button)
The state of one joystick button.
Definition: DriverStation.cpp:329
bool IsAutonomous() const override
Check if the DS is commanding autonomous mode.
Definition: DriverStation.cpp:374
double GetMatchTime() const
Return the approximate match time The FMS does not send an official match time to the robots...
Definition: DriverStation.cpp:528
bool IsTest() const override
Check if the DS is commanding test mode.
Definition: DriverStation.cpp:396
Analog input class.
Definition: AnalogInput.h:32
int GetJoystickType(uint32_t stick) const
Returns the type of joystick at a given port.
Definition: DriverStation.cpp:184
void WaitForData()
Wait until a new packet comes from the driver station This blocks on a semaphore, so the waiting is e...
Definition: DriverStation.cpp:510
std::string GetJoystickName(uint32_t stick) const
Returns the name of the joystick at the given port.
Definition: DriverStation.cpp:170
Alliance GetAlliance() const
Return the alliance that the driver station says it is on.
Definition: DriverStation.cpp:464
bool GetJoystickIsXbox(uint32_t stick) const
Returns a boolean indicating if the controller is an xbox controller.
Definition: DriverStation.cpp:198
bool IsFMSAttached() const
Is the driver station attached to a Field Management System?
Definition: DriverStation.cpp:453
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:94
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:89
Wrapper class around std::thread that allows changing thread priority.
Definition: Task.h:19
DriverStation()
DriverStation constructor.
Definition: DriverStation.cpp:35
float GetBatteryVoltage() const
Read the battery voltage.
Definition: DriverStation.cpp:116
Definition: HAL.hpp:151
int GetStickButtonCount(uint32_t stick) const
Returns the number of buttons on a given joystick port.
Definition: DriverStation.cpp:242
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:27
bool IsDisabled() const override
Check if the robot is disabled.
Definition: DriverStation.cpp:363