blob: f15985cd118ccaac19608698a6cb5710f6c1636e [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6#pragma once
7
8#include "SensorBase.h"
9#include "RobotState.h"
10#include "HAL/HAL.hpp"
11#include "HAL/cpp/Semaphore.hpp"
12#include "HAL/cpp/priority_mutex.h"
13#include "HAL/cpp/priority_condition_variable.h"
14#include <condition_variable>
15
16struct HALControlWord;
17class AnalogInput;
18
19/**
20 * Provide access to the network communication data to / from the Driver
21 * Station.
22 */
23class DriverStation : public SensorBase, public RobotStateInterface {
24 public:
25 enum Alliance { kRed, kBlue, kInvalid };
26
27 virtual ~DriverStation();
28 static DriverStation &GetInstance();
29 static void ReportError(std::string error);
30
31 static const uint32_t kJoystickPorts = 6;
32
33 float GetStickAxis(uint32_t stick, uint32_t axis);
34 int GetStickPOV(uint32_t stick, uint32_t pov);
35 uint32_t GetStickButtons(uint32_t stick) const;
36 bool GetStickButton(uint32_t stick, uint8_t button);
37
38 int GetStickAxisCount(uint32_t stick) const;
39 int GetStickPOVCount(uint32_t stick) const;
40 int GetStickButtonCount(uint32_t stick) const;
41
42 bool GetJoystickIsXbox(uint32_t stick) const;
43 int GetJoystickType(uint32_t stick) const;
44 std::string GetJoystickName(uint32_t stick) const;
45 int GetJoystickAxisType(uint32_t stick, uint8_t axis) const;
46
47 bool IsEnabled() const override;
48 bool IsDisabled() const override;
49 bool IsAutonomous() const override;
50 bool IsOperatorControl() const override;
51 bool IsTest() const override;
52 bool IsDSAttached() const;
53 bool IsNewControlData() const;
54 bool IsFMSAttached() const;
55 bool IsSysActive() const;
56 bool IsSysBrownedOut() const;
57
58 Alliance GetAlliance() const;
59 uint32_t GetLocation() const;
60 void WaitForData();
61 double GetMatchTime() const;
62 float GetBatteryVoltage() const;
63
64 /** Only to be used to tell the Driver Station what code you claim to be
65 * executing
66 * for diagnostic purposes only
67 * @param entering If true, starting disabled code; if false, leaving disabled
68 * code */
69 void InDisabled(bool entering) { m_userInDisabled = entering; }
70 /** Only to be used to tell the Driver Station what code you claim to be
71 * executing
72 * for diagnostic purposes only
73 * @param entering If true, starting autonomous code; if false, leaving
74 * autonomous code */
75 void InAutonomous(bool entering) { m_userInAutonomous = entering; }
76 /** Only to be used to tell the Driver Station what code you claim to be
77 * executing
78 * for diagnostic purposes only
79 * @param entering If true, starting teleop code; if false, leaving teleop
80 * code */
81 void InOperatorControl(bool entering) { m_userInTeleop = entering; }
82 /** Only to be used to tell the Driver Station what code you claim to be
83 * executing
84 * for diagnostic purposes only
85 * @param entering If true, starting test code; if false, leaving test code */
86 void InTest(bool entering) { m_userInTest = entering; }
87
88 protected:
89 DriverStation();
90
91 void GetData();
92
93 private:
94 static DriverStation *m_instance;
95 void ReportJoystickUnpluggedError(std::string message);
96 void Run();
97
98 HALJoystickAxes m_joystickAxes[kJoystickPorts];
99 HALJoystickPOVs m_joystickPOVs[kJoystickPorts];
100 HALJoystickButtons m_joystickButtons[kJoystickPorts];
101 HALJoystickDescriptor m_joystickDescriptor[kJoystickPorts];
102 mutable Semaphore m_newControlData{Semaphore::kEmpty};
103 mutable priority_condition_variable m_packetDataAvailableCond;
104 priority_mutex m_packetDataAvailableMutex;
105 std::condition_variable_any m_waitForDataCond;
106 priority_mutex m_waitForDataMutex;
107 bool m_userInDisabled = false;
108 bool m_userInAutonomous = false;
109 bool m_userInTeleop = false;
110 bool m_userInTest = false;
111 double m_nextMessageTime = 0;
112};