blob: b96f1a7e9ebdef7f5e27cc7e0f2cd04e22477e30 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05007
Brian Silverman26e4e522015-12-17 01:56:40 -05008#pragma once
9
Brian Silverman1a675112016-02-20 20:42:49 -050010#include "simulation/gz_msgs/msgs.h"
11
12#ifdef _WIN32
13 // Ensure that Winsock2.h is included before Windows.h, which can get
14 // pulled in by anybody (e.g., Boost).
15 #include <Winsock2.h>
16#endif
17
18#include <gazebo/transport/transport.hh>
Brian Silverman26e4e522015-12-17 01:56:40 -050019#include "SensorBase.h"
20#include "RobotState.h"
Brian Silverman1a675112016-02-20 20:42:49 -050021#include <mutex>
Brian Silverman26e4e522015-12-17 01:56:40 -050022#include <condition_variable>
23
Brian Silverman1a675112016-02-20 20:42:49 -050024struct HALCommonControlData;
Brian Silverman26e4e522015-12-17 01:56:40 -050025class AnalogInput;
26
Brian Silverman1a675112016-02-20 20:42:49 -050027using namespace gazebo;
28
Brian Silverman26e4e522015-12-17 01:56:40 -050029/**
Brian Silverman1a675112016-02-20 20:42:49 -050030 * Provide access to the network communication data to / from the Driver Station.
Brian Silverman26e4e522015-12-17 01:56:40 -050031 */
Brian Silverman1a675112016-02-20 20:42:49 -050032class DriverStation : public SensorBase, public RobotStateInterface
33{
34public:
35 enum Alliance
36 {
37 kRed,
38 kBlue,
39 kInvalid
40 };
Brian Silverman26e4e522015-12-17 01:56:40 -050041
Brian Silverman1a675112016-02-20 20:42:49 -050042 virtual ~DriverStation() = default;
43 static DriverStation &GetInstance();
44 static void ReportError(std::string error);
45 static void ReportWarning(std::string error);
46 static void ReportError(bool is_error, int32_t code, const std::string& error, const std::string& location, const std::string& stack);
Brian Silverman26e4e522015-12-17 01:56:40 -050047
Brian Silverman1a675112016-02-20 20:42:49 -050048 static const uint32_t kBatteryChannel = 7;
49 static const uint32_t kJoystickPorts = 4;
50 static const uint32_t kJoystickAxes = 6;
Brian Silverman26e4e522015-12-17 01:56:40 -050051
Brian Silverman1a675112016-02-20 20:42:49 -050052 float GetStickAxis(uint32_t stick, uint32_t axis);
53 bool GetStickButton(uint32_t stick, uint32_t button);
54 short GetStickButtons(uint32_t stick);
Brian Silverman26e4e522015-12-17 01:56:40 -050055
Brian Silverman1a675112016-02-20 20:42:49 -050056 float GetAnalogIn(uint32_t channel);
57 bool GetDigitalIn(uint32_t channel);
58 void SetDigitalOut(uint32_t channel, bool value);
59 bool GetDigitalOut(uint32_t channel);
Brian Silverman26e4e522015-12-17 01:56:40 -050060
Brian Silverman1a675112016-02-20 20:42:49 -050061 bool IsEnabled() const;
62 bool IsDisabled() const;
63 bool IsAutonomous() const;
64 bool IsOperatorControl() const;
65 bool IsTest() const;
66 bool IsFMSAttached() const;
Brian Silverman26e4e522015-12-17 01:56:40 -050067
Brian Silverman1a675112016-02-20 20:42:49 -050068 uint32_t GetPacketNumber() const;
69 Alliance GetAlliance() const;
70 uint32_t GetLocation() const;
71 void WaitForData();
72 double GetMatchTime() const;
73 float GetBatteryVoltage() const;
74 uint16_t GetTeamNumber() const;
Brian Silverman26e4e522015-12-17 01:56:40 -050075
Brian Silverman26e4e522015-12-17 01:56:40 -050076
Brian Silverman26e4e522015-12-17 01:56:40 -050077
Brian Silverman1a675112016-02-20 20:42:49 -050078 void IncrementUpdateNumber()
79 {
80 m_updateNumber++;
81 }
Brian Silverman26e4e522015-12-17 01:56:40 -050082
Brian Silverman1a675112016-02-20 20:42:49 -050083 /** Only to be used to tell the Driver Station what code you claim to be executing
84 * for diagnostic purposes only
85 * @param entering If true, starting disabled code; if false, leaving disabled code */
86 void InDisabled(bool entering)
87 {
88 m_userInDisabled = entering;
89 }
90 /** Only to be used to tell the Driver Station what code you claim to be executing
91 * for diagnostic purposes only
92 * @param entering If true, starting autonomous code; if false, leaving autonomous code */
93 void InAutonomous(bool entering)
94 {
95 m_userInAutonomous = entering;
96 }
97 /** Only to be used to tell the Driver Station what code you claim to be executing
98 * for diagnostic purposes only
99 * @param entering If true, starting teleop code; if false, leaving teleop code */
100 void InOperatorControl(bool entering)
101 {
102 m_userInTeleop = entering;
103 }
104 /** Only to be used to tell the Driver Station what code you claim to be executing
105 * for diagnostic purposes only
106 * @param entering If true, starting test code; if false, leaving test code */
107 void InTest(bool entering)
108 {
109 m_userInTest = entering;
110 }
Brian Silverman26e4e522015-12-17 01:56:40 -0500111
Brian Silverman1a675112016-02-20 20:42:49 -0500112protected:
113 DriverStation();
Brian Silverman26e4e522015-12-17 01:56:40 -0500114
Brian Silverman1a675112016-02-20 20:42:49 -0500115private:
116 static void InitTask(DriverStation *ds);
117 static DriverStation *m_instance;
118 static uint8_t m_updateNumber;
119 ///< TODO: Get rid of this and use the semaphore signaling
120 static const float kUpdatePeriod;
121
122 void stateCallback(const msgs::ConstDriverStationPtr &msg);
123 void joystickCallback(const msgs::ConstFRCJoystickPtr &msg, int i);
124 void joystickCallback0(const msgs::ConstFRCJoystickPtr &msg);
125 void joystickCallback1(const msgs::ConstFRCJoystickPtr &msg);
126 void joystickCallback2(const msgs::ConstFRCJoystickPtr &msg);
127 void joystickCallback3(const msgs::ConstFRCJoystickPtr &msg);
128 void joystickCallback4(const msgs::ConstFRCJoystickPtr &msg);
129 void joystickCallback5(const msgs::ConstFRCJoystickPtr &msg);
130
131 uint8_t m_digitalOut = 0;
132 std::condition_variable m_waitForDataCond;
133 std::mutex m_waitForDataMutex;
134 mutable std::recursive_mutex m_stateMutex;
135 std::recursive_mutex m_joystickMutex;
136 double m_approxMatchTimeOffset = 0;
137 bool m_userInDisabled = false;
138 bool m_userInAutonomous = false;
139 bool m_userInTeleop = false;
140 bool m_userInTest = false;
141
142 transport::SubscriberPtr stateSub;
143 transport::SubscriberPtr joysticksSub[6];
144 msgs::DriverStationPtr state;
145 msgs::FRCJoystickPtr joysticks[6];
Brian Silverman26e4e522015-12-17 01:56:40 -0500146};