Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 7 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 8 | #pragma once |
| 9 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 10 | #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 Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 19 | #include "SensorBase.h" |
| 20 | #include "RobotState.h" |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 21 | #include <mutex> |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 22 | #include <condition_variable> |
| 23 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 24 | struct HALCommonControlData; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 25 | class AnalogInput; |
| 26 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 27 | using namespace gazebo; |
| 28 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 29 | /** |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 30 | * Provide access to the network communication data to / from the Driver Station. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 31 | */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 32 | class DriverStation : public SensorBase, public RobotStateInterface |
| 33 | { |
| 34 | public: |
| 35 | enum Alliance |
| 36 | { |
| 37 | kRed, |
| 38 | kBlue, |
| 39 | kInvalid |
| 40 | }; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 41 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 42 | 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 Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 47 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 48 | static const uint32_t kBatteryChannel = 7; |
| 49 | static const uint32_t kJoystickPorts = 4; |
| 50 | static const uint32_t kJoystickAxes = 6; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 51 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 52 | float GetStickAxis(uint32_t stick, uint32_t axis); |
| 53 | bool GetStickButton(uint32_t stick, uint32_t button); |
| 54 | short GetStickButtons(uint32_t stick); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 55 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 56 | 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 Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 60 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 61 | bool IsEnabled() const; |
| 62 | bool IsDisabled() const; |
| 63 | bool IsAutonomous() const; |
| 64 | bool IsOperatorControl() const; |
| 65 | bool IsTest() const; |
| 66 | bool IsFMSAttached() const; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 67 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 68 | 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 Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 75 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 76 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 77 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 78 | void IncrementUpdateNumber() |
| 79 | { |
| 80 | m_updateNumber++; |
| 81 | } |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 82 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 83 | /** 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 Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 111 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 112 | protected: |
| 113 | DriverStation(); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 114 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 115 | private: |
| 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 Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 146 | }; |