blob: c9218dde3ec265a23cabc4df0d8d64a070ce728b [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
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 <condition_variable>
11#include <mutex>
12#include <string>
13
14#include <gazebo/transport/transport.hh>
15
16#include "RobotState.h"
17#include "SensorBase.h"
18#include "llvm/StringRef.h"
19#include "simulation/gz_msgs/msgs.h"
20
21#ifdef _WIN32
22// Ensure that Winsock2.h is included before Windows.h, which can get
23// pulled in by anybody (e.g., Boost).
24#include <Winsock2.h>
25#endif
26
27namespace frc {
28
29struct HALCommonControlData;
30class AnalogInput;
31
32/**
33 * Provide access to the network communication data to / from the Driver
34 * Station.
35 */
36class DriverStation : public SensorBase, public RobotStateInterface {
37 public:
38 enum Alliance { kRed, kBlue, kInvalid };
39
40 virtual ~DriverStation() = default;
41 static DriverStation& GetInstance();
42 static void ReportError(llvm::StringRef error);
43 static void ReportWarning(llvm::StringRef error);
44 static void ReportError(bool is_error, int code, llvm::StringRef error,
45 llvm::StringRef location, llvm::StringRef stack);
46
47 static const int kBatteryChannel = 7;
48 static const int kJoystickPorts = 4;
49 static const int kJoystickAxes = 6;
50
51 double GetStickAxis(int stick, int axis);
52 bool GetStickButton(int stick, int button);
53 int16_t GetStickButtons(int stick);
54
55 double GetAnalogIn(int channel);
56 bool GetDigitalIn(int channel);
57 void SetDigitalOut(int channel, bool value);
58 bool GetDigitalOut(int channel);
59
60 bool IsEnabled() const;
61 bool IsDisabled() const;
62 bool IsAutonomous() const;
63 bool IsOperatorControl() const;
64 bool IsTest() const;
65 bool IsFMSAttached() const;
66
67 int GetPacketNumber() const;
68 Alliance GetAlliance() const;
69 int GetLocation() const;
70 void WaitForData();
71 bool WaitForData(double timeout);
72 double GetMatchTime() const;
73 double GetBatteryVoltage() const;
74 uint16_t GetTeamNumber() const;
75
76 void IncrementUpdateNumber() { m_updateNumber++; }
77
78 /**
79 * Only to be used to tell the Driver Station what code you claim to be
80 * executing for diagnostic purposes only.
81 *
82 * @param entering If true, starting disabled code; if false, leaving
83 * disabled code
84 */
85 void InDisabled(bool entering) { m_userInDisabled = entering; }
86 /**
87 * Only to be used to tell the Driver Station what code you claim to be
88 * executing for diagnostic purposes only.
89 *
90 * @param entering If true, starting autonomous code; if false, leaving
91 * autonomous code
92 */
93 void InAutonomous(bool entering) { m_userInAutonomous = entering; }
94 /**
95 * Only to be used to tell the Driver Station what code you claim to be
96 * executing for diagnostic purposes only.
97 *
98 * @param entering If true, starting teleop code; if false, leaving teleop
99 * code
100 */
101 void InOperatorControl(bool entering) { m_userInTeleop = entering; }
102 /**
103 * Only to be used to tell the Driver Station what code you claim to be
104 * executing for diagnostic purposes only.
105 *
106 * @param entering If true, starting test code; if false, leaving test code
107 */
108 void InTest(bool entering) { m_userInTest = entering; }
109
110 protected:
111 DriverStation();
112
113 private:
114 static void InitTask(DriverStation* ds);
115 static DriverStation* m_instance;
116 static int m_updateNumber;
117 ///< TODO: Get rid of this and use the semaphore signaling
118 static const double kUpdatePeriod;
119
120 void stateCallback(const gazebo::msgs::ConstDriverStationPtr& msg);
121 void joystickCallback(const gazebo::msgs::ConstFRCJoystickPtr& msg, int i);
122 void joystickCallback0(const gazebo::msgs::ConstFRCJoystickPtr& msg);
123 void joystickCallback1(const gazebo::msgs::ConstFRCJoystickPtr& msg);
124 void joystickCallback2(const gazebo::msgs::ConstFRCJoystickPtr& msg);
125 void joystickCallback3(const gazebo::msgs::ConstFRCJoystickPtr& msg);
126 void joystickCallback4(const gazebo::msgs::ConstFRCJoystickPtr& msg);
127 void joystickCallback5(const gazebo::msgs::ConstFRCJoystickPtr& msg);
128
129 int m_digitalOut = 0;
130 std::condition_variable m_waitForDataCond;
131 std::mutex m_waitForDataMutex;
132 bool m_updatedControlLoopData = false;
133 mutable std::recursive_mutex m_stateMutex;
134 std::recursive_mutex m_joystickMutex;
135 double m_approxMatchTimeOffset = 0;
136 bool m_userInDisabled = false;
137 bool m_userInAutonomous = false;
138 bool m_userInTeleop = false;
139 bool m_userInTest = false;
140
141 gazebo::transport::SubscriberPtr stateSub;
142 gazebo::transport::SubscriberPtr joysticksSub[6];
143 gazebo::msgs::DriverStationPtr state;
144 gazebo::msgs::FRCJoystickPtr joysticks[6];
145};
146
147} // namespace frc