Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 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 <condition_variable> |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | #include <thread> |
| 15 | |
| 16 | #include "HAL/DriverStation.h" |
| 17 | #include "HAL/cpp/priority_condition_variable.h" |
| 18 | #include "HAL/cpp/priority_mutex.h" |
| 19 | #include "RobotState.h" |
| 20 | #include "SensorBase.h" |
| 21 | #include "llvm/StringRef.h" |
| 22 | |
| 23 | namespace frc { |
| 24 | |
| 25 | /** |
| 26 | * Provide access to the network communication data to / from the Driver |
| 27 | * Station. |
| 28 | */ |
| 29 | class DriverStation : public SensorBase, public RobotStateInterface { |
| 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 | double GetStickAxis(int stick, int axis); |
| 43 | int GetStickPOV(int stick, int pov); |
| 44 | int GetStickButtons(int stick) const; |
| 45 | bool GetStickButton(int stick, int button); |
| 46 | |
| 47 | int GetStickAxisCount(int stick) const; |
| 48 | int GetStickPOVCount(int stick) const; |
| 49 | int GetStickButtonCount(int stick) const; |
| 50 | |
| 51 | bool GetJoystickIsXbox(int stick) const; |
| 52 | int GetJoystickType(int stick) const; |
| 53 | std::string GetJoystickName(int stick) const; |
| 54 | int GetJoystickAxisType(int stick, int axis) const; |
| 55 | |
| 56 | bool IsEnabled() const override; |
| 57 | bool IsDisabled() const override; |
| 58 | bool IsAutonomous() const override; |
| 59 | bool IsOperatorControl() const override; |
| 60 | bool IsTest() const override; |
| 61 | bool IsDSAttached() const; |
| 62 | bool IsNewControlData() const; |
| 63 | bool IsFMSAttached() const; |
| 64 | bool IsSysActive() const; |
| 65 | bool IsBrownedOut() const; |
| 66 | |
| 67 | Alliance GetAlliance() const; |
| 68 | int GetLocation() const; |
| 69 | void WaitForData(); |
| 70 | bool WaitForData(double timeout); |
| 71 | double GetMatchTime() const; |
| 72 | double GetBatteryVoltage() const; |
| 73 | |
| 74 | /** Only to be used to tell the Driver Station what code you claim to be |
| 75 | * executing for diagnostic purposes only |
| 76 | * @param entering If true, starting disabled code; if false, leaving disabled |
| 77 | * code */ |
| 78 | void InDisabled(bool entering) { m_userInDisabled = entering; } |
| 79 | /** Only to be used to tell the Driver Station what code you claim to be |
| 80 | * executing for diagnostic purposes only |
| 81 | * @param entering If true, starting autonomous code; if false, leaving |
| 82 | * autonomous code */ |
| 83 | void InAutonomous(bool entering) { m_userInAutonomous = entering; } |
| 84 | /** Only to be used to tell the Driver Station what code you claim to be |
| 85 | * executing for diagnostic purposes only |
| 86 | * @param entering If true, starting teleop code; if false, leaving teleop |
| 87 | * code */ |
| 88 | void InOperatorControl(bool entering) { m_userInTeleop = entering; } |
| 89 | /** Only to be used to tell the Driver Station what code you claim to be |
| 90 | * executing for diagnostic purposes only |
| 91 | * @param entering If true, starting test code; if false, leaving test code */ |
| 92 | void InTest(bool entering) { m_userInTest = entering; } |
| 93 | |
| 94 | protected: |
| 95 | void GetData(); |
| 96 | |
| 97 | private: |
| 98 | DriverStation(); |
| 99 | void ReportJoystickUnpluggedError(llvm::StringRef message); |
| 100 | void ReportJoystickUnpluggedWarning(llvm::StringRef message); |
| 101 | void Run(); |
| 102 | void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const; |
| 103 | |
| 104 | // Joystick User Data |
| 105 | std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes; |
| 106 | std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs; |
| 107 | std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons; |
| 108 | std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor; |
| 109 | |
| 110 | // Joystick Cached Data |
| 111 | std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache; |
| 112 | std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache; |
| 113 | std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache; |
| 114 | std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache; |
| 115 | |
| 116 | // Internal Driver Station thread |
| 117 | std::thread m_dsThread; |
| 118 | std::atomic<bool> m_isRunning{false}; |
| 119 | |
| 120 | // WPILib WaitForData control variables |
| 121 | bool m_waitForDataPredicate = false; |
| 122 | std::condition_variable_any m_waitForDataCond; |
| 123 | priority_mutex m_waitForDataMutex; |
| 124 | |
| 125 | mutable std::atomic<bool> m_newControlData{false}; |
| 126 | |
| 127 | mutable priority_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 priority_mutex m_controlWordMutex; |
| 139 | |
| 140 | double m_nextMessageTime = 0; |
| 141 | }; |
| 142 | |
| 143 | } // namespace frc |