Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/DriverStation.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <stdint.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 9 | #include <array> |
| 10 | #include <atomic> |
| 11 | #include <chrono> |
| 12 | #include <string> |
| 13 | #include <string_view> |
| 14 | #include <thread> |
| 15 | #include <type_traits> |
| 16 | |
| 17 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | #include <hal/DriverStation.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 19 | #include <hal/DriverStationTypes.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 20 | #include <hal/HALBase.h> |
| 21 | #include <hal/Power.h> |
| 22 | #include <networktables/NetworkTable.h> |
| 23 | #include <networktables/NetworkTableEntry.h> |
| 24 | #include <networktables/NetworkTableInstance.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 25 | #include <wpi/condition_variable.h> |
| 26 | #include <wpi/mutex.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 28 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | #include "frc/MotorSafety.h" |
| 30 | #include "frc/Timer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace frc; |
| 33 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 34 | namespace { |
| 35 | // A simple class which caches the previous value written to an NT entry |
| 36 | // Used to prevent redundant, repeated writes of the same value |
| 37 | template <class T> |
| 38 | class MatchDataSenderEntry { |
| 39 | public: |
| 40 | MatchDataSenderEntry(const std::shared_ptr<nt::NetworkTable>& table, |
| 41 | std::string_view key, const T& initialVal) { |
| 42 | static_assert(std::is_same_v<T, bool> || std::is_same_v<T, double> || |
| 43 | std::is_same_v<T, std::string>, |
| 44 | "Invalid type for MatchDataSenderEntry - must be " |
| 45 | "to bool, double or std::string"); |
| 46 | |
| 47 | ntEntry = table->GetEntry(key); |
| 48 | if constexpr (std::is_same_v<T, bool>) { |
| 49 | ntEntry.ForceSetBoolean(initialVal); |
| 50 | } else if constexpr (std::is_same_v<T, double>) { |
| 51 | ntEntry.ForceSetDouble(initialVal); |
| 52 | } else if constexpr (std::is_same_v<T, std::string>) { |
| 53 | ntEntry.ForceSetString(initialVal); |
| 54 | } |
| 55 | prevVal = initialVal; |
| 56 | } |
| 57 | |
| 58 | void Set(const T& val) { |
| 59 | if (val != prevVal) { |
| 60 | SetValue(val); |
| 61 | prevVal = val; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | nt::NetworkTableEntry ntEntry; |
| 67 | T prevVal; |
| 68 | |
| 69 | void SetValue(bool val) { ntEntry.SetBoolean(val); } |
| 70 | void SetValue(double val) { ntEntry.SetDouble(val); } |
| 71 | void SetValue(std::string_view val) { ntEntry.SetString(val); } |
| 72 | }; |
| 73 | |
| 74 | struct MatchDataSender { |
| 75 | std::shared_ptr<nt::NetworkTable> table = |
| 76 | nt::NetworkTableInstance::GetDefault().GetTable("FMSInfo"); |
| 77 | MatchDataSenderEntry<std::string> typeMetaData{table, ".type", "FMSInfo"}; |
| 78 | MatchDataSenderEntry<std::string> gameSpecificMessage{ |
| 79 | table, "GameSpecificMessage", ""}; |
| 80 | MatchDataSenderEntry<std::string> eventName{table, "EventName", ""}; |
| 81 | MatchDataSenderEntry<double> matchNumber{table, "MatchNumber", 0.0}; |
| 82 | MatchDataSenderEntry<double> replayNumber{table, "ReplayNumber", 0.0}; |
| 83 | MatchDataSenderEntry<double> matchType{table, "MatchType", 0.0}; |
| 84 | MatchDataSenderEntry<bool> alliance{table, "IsRedAlliance", true}; |
| 85 | MatchDataSenderEntry<double> station{table, "StationNumber", 1.0}; |
| 86 | MatchDataSenderEntry<double> controlWord{table, "FMSControlData", 0.0}; |
| 87 | }; |
| 88 | |
| 89 | struct Instance { |
| 90 | Instance(); |
| 91 | ~Instance(); |
| 92 | |
| 93 | MatchDataSender matchDataSender; |
| 94 | |
| 95 | // Joystick button rising/falling edge flags |
| 96 | wpi::mutex buttonEdgeMutex; |
| 97 | std::array<HAL_JoystickButtons, DriverStation::kJoystickPorts> |
| 98 | previousButtonStates; |
| 99 | std::array<uint32_t, DriverStation::kJoystickPorts> joystickButtonsPressed; |
| 100 | std::array<uint32_t, DriverStation::kJoystickPorts> joystickButtonsReleased; |
| 101 | |
| 102 | // Internal Driver Station thread |
| 103 | std::thread dsThread; |
| 104 | std::atomic<bool> isRunning{false}; |
| 105 | |
| 106 | mutable wpi::mutex waitForDataMutex; |
| 107 | wpi::condition_variable waitForDataCond; |
| 108 | int waitForDataCounter = 0; |
| 109 | |
| 110 | bool silenceJoystickWarning = false; |
| 111 | |
| 112 | // Robot state status variables |
| 113 | bool userInDisabled = false; |
| 114 | bool userInAutonomous = false; |
| 115 | bool userInTeleop = false; |
| 116 | bool userInTest = false; |
| 117 | |
| 118 | units::second_t nextMessageTime = 0_s; |
| 119 | }; |
| 120 | } // namespace |
| 121 | |
| 122 | static constexpr auto kJoystickUnpluggedMessageInterval = 1_s; |
| 123 | |
| 124 | static Instance& GetInstance() { |
| 125 | static Instance instance; |
| 126 | return instance; |
| 127 | } |
| 128 | |
| 129 | static void Run(); |
| 130 | static void SendMatchData(); |
| 131 | |
| 132 | /** |
| 133 | * Reports errors related to unplugged joysticks. |
| 134 | * |
| 135 | * Throttles the errors so that they don't overwhelm the DS. |
| 136 | */ |
| 137 | static void ReportJoystickUnpluggedErrorV(fmt::string_view format, |
| 138 | fmt::format_args args); |
| 139 | |
| 140 | template <typename S, typename... Args> |
| 141 | static inline void ReportJoystickUnpluggedError(const S& format, |
| 142 | Args&&... args) { |
| 143 | ReportJoystickUnpluggedErrorV( |
| 144 | format, fmt::make_args_checked<Args...>(format, args...)); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Reports errors related to unplugged joysticks. |
| 149 | * |
| 150 | * Throttles the errors so that they don't overwhelm the DS. |
| 151 | */ |
| 152 | static void ReportJoystickUnpluggedWarningV(fmt::string_view format, |
| 153 | fmt::format_args args); |
| 154 | |
| 155 | template <typename S, typename... Args> |
| 156 | static inline void ReportJoystickUnpluggedWarning(const S& format, |
| 157 | Args&&... args) { |
| 158 | ReportJoystickUnpluggedWarningV( |
| 159 | format, fmt::make_args_checked<Args...>(format, args...)); |
| 160 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 161 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 162 | static int& GetDSLastCount() { |
| 163 | // There is a rollover error condition here. At Packet# = n * (uintmax), this |
| 164 | // will return false when instead it should return true. However, this at a |
| 165 | // 20ms rate occurs once every 2.7 years of DS connected runtime, so not |
| 166 | // worth the cycles to check. |
| 167 | thread_local int lastCount{0}; |
| 168 | return lastCount; |
| 169 | } |
| 170 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 171 | Instance::Instance() { |
| 172 | HAL_Initialize(500, 0); |
| 173 | |
| 174 | // All joysticks should default to having zero axes, povs and buttons, so |
| 175 | // uninitialized memory doesn't get sent to motor controllers. |
| 176 | for (unsigned int i = 0; i < DriverStation::kJoystickPorts; i++) { |
| 177 | joystickButtonsPressed[i] = 0; |
| 178 | joystickButtonsReleased[i] = 0; |
| 179 | previousButtonStates[i].count = 0; |
| 180 | previousButtonStates[i].buttons = 0; |
| 181 | } |
| 182 | |
| 183 | dsThread = std::thread(&Run); |
| 184 | } |
| 185 | |
| 186 | Instance::~Instance() { |
| 187 | isRunning = false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 188 | // Trigger a DS mutex release in case there is no driver station running. |
| 189 | HAL_ReleaseDSMutex(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 190 | dsThread.join(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | DriverStation& DriverStation::GetInstance() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 194 | ::GetInstance(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 195 | static DriverStation instance; |
| 196 | return instance; |
| 197 | } |
| 198 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 199 | bool DriverStation::GetStickButton(int stick, int button) { |
| 200 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 201 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | if (button <= 0) { |
| 205 | ReportJoystickUnpluggedError( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 206 | "Joystick Button {} index out of range; indexes begin at 1", button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
| 210 | HAL_JoystickButtons buttons; |
| 211 | HAL_GetJoystickButtons(stick, &buttons); |
| 212 | |
| 213 | if (button > buttons.count) { |
| 214 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 215 | "Joystick Button {} missing (max {}), check if all controllers are " |
| 216 | "plugged in", |
| 217 | button, buttons.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
| 221 | return buttons.buttons & 1 << (button - 1); |
| 222 | } |
| 223 | |
| 224 | bool DriverStation::GetStickButtonPressed(int stick, int button) { |
| 225 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 226 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | if (button <= 0) { |
| 230 | ReportJoystickUnpluggedError( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 231 | "Joystick Button {} index out of range; indexes begin at 1", button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | |
| 235 | HAL_JoystickButtons buttons; |
| 236 | HAL_GetJoystickButtons(stick, &buttons); |
| 237 | |
| 238 | if (button > buttons.count) { |
| 239 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 240 | "Joystick Button {} missing (max {}), check if all controllers are " |
| 241 | "plugged in", |
| 242 | button, buttons.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 243 | return false; |
| 244 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 245 | auto& inst = ::GetInstance(); |
| 246 | std::unique_lock lock(inst.buttonEdgeMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 247 | // If button was pressed, clear flag and return true |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 248 | if (inst.joystickButtonsPressed[stick] & 1 << (button - 1)) { |
| 249 | inst.joystickButtonsPressed[stick] &= ~(1 << (button - 1)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 250 | return true; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 251 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 252 | return false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool DriverStation::GetStickButtonReleased(int stick, int button) { |
| 256 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 257 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | if (button <= 0) { |
| 261 | ReportJoystickUnpluggedError( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 262 | "Joystick Button {} index out of range; indexes begin at 1", button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | |
| 266 | HAL_JoystickButtons buttons; |
| 267 | HAL_GetJoystickButtons(stick, &buttons); |
| 268 | |
| 269 | if (button > buttons.count) { |
| 270 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 271 | "Joystick Button {} missing (max {}), check if all controllers are " |
| 272 | "plugged in", |
| 273 | button, buttons.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 274 | return false; |
| 275 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 276 | auto& inst = ::GetInstance(); |
| 277 | std::unique_lock lock(inst.buttonEdgeMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 278 | // If button was released, clear flag and return true |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 279 | if (inst.joystickButtonsReleased[stick] & 1 << (button - 1)) { |
| 280 | inst.joystickButtonsReleased[stick] &= ~(1 << (button - 1)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 281 | return true; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 282 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 283 | return false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | double DriverStation::GetStickAxis(int stick, int axis) { |
| 287 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 288 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 289 | return 0.0; |
| 290 | } |
| 291 | if (axis < 0 || axis >= HAL_kMaxJoystickAxes) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 292 | FRC_ReportError(warn::BadJoystickAxis, "axis {} out of range", axis); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 293 | return 0.0; |
| 294 | } |
| 295 | |
| 296 | HAL_JoystickAxes axes; |
| 297 | HAL_GetJoystickAxes(stick, &axes); |
| 298 | |
| 299 | if (axis >= axes.count) { |
| 300 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 301 | "Joystick Axis {} missing (max {}), check if all controllers are " |
| 302 | "plugged in", |
| 303 | axis, axes.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 304 | return 0.0; |
| 305 | } |
| 306 | |
| 307 | return axes.axes[axis]; |
| 308 | } |
| 309 | |
| 310 | int DriverStation::GetStickPOV(int stick, int pov) { |
| 311 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 312 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 313 | return -1; |
| 314 | } |
| 315 | if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 316 | FRC_ReportError(warn::BadJoystickAxis, "POV {} out of range", pov); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | HAL_JoystickPOVs povs; |
| 321 | HAL_GetJoystickPOVs(stick, &povs); |
| 322 | |
| 323 | if (pov >= povs.count) { |
| 324 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 325 | "Joystick POV {} missing (max {}), check if all controllers are " |
| 326 | "plugged in", |
| 327 | pov, povs.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | return povs.povs[pov]; |
| 332 | } |
| 333 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 334 | int DriverStation::GetStickButtons(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 335 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 336 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | HAL_JoystickButtons buttons; |
| 341 | HAL_GetJoystickButtons(stick, &buttons); |
| 342 | |
| 343 | return buttons.buttons; |
| 344 | } |
| 345 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 346 | int DriverStation::GetStickAxisCount(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 347 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 348 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | HAL_JoystickAxes axes; |
| 353 | HAL_GetJoystickAxes(stick, &axes); |
| 354 | |
| 355 | return axes.count; |
| 356 | } |
| 357 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 358 | int DriverStation::GetStickPOVCount(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 359 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 360 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | HAL_JoystickPOVs povs; |
| 365 | HAL_GetJoystickPOVs(stick, &povs); |
| 366 | |
| 367 | return povs.count; |
| 368 | } |
| 369 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 370 | int DriverStation::GetStickButtonCount(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 371 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 372 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | HAL_JoystickButtons buttons; |
| 377 | HAL_GetJoystickButtons(stick, &buttons); |
| 378 | |
| 379 | return buttons.count; |
| 380 | } |
| 381 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 382 | bool DriverStation::GetJoystickIsXbox(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 383 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 384 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
| 388 | HAL_JoystickDescriptor descriptor; |
| 389 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 390 | |
| 391 | return static_cast<bool>(descriptor.isXbox); |
| 392 | } |
| 393 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 394 | int DriverStation::GetJoystickType(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 395 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 396 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | HAL_JoystickDescriptor descriptor; |
| 401 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 402 | |
| 403 | return static_cast<int>(descriptor.type); |
| 404 | } |
| 405 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 406 | std::string DriverStation::GetJoystickName(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 407 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 408 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | HAL_JoystickDescriptor descriptor; |
| 412 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 413 | |
| 414 | return descriptor.name; |
| 415 | } |
| 416 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 417 | int DriverStation::GetJoystickAxisType(int stick, int axis) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 418 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 419 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | HAL_JoystickDescriptor descriptor; |
| 424 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 425 | |
| 426 | return static_cast<bool>(descriptor.axisTypes); |
| 427 | } |
| 428 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 429 | bool DriverStation::IsJoystickConnected(int stick) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 430 | return GetStickAxisCount(stick) > 0 || GetStickButtonCount(stick) > 0 || |
| 431 | GetStickPOVCount(stick) > 0; |
| 432 | } |
| 433 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 434 | bool DriverStation::IsEnabled() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 435 | HAL_ControlWord controlWord; |
| 436 | HAL_GetControlWord(&controlWord); |
| 437 | return controlWord.enabled && controlWord.dsAttached; |
| 438 | } |
| 439 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 440 | bool DriverStation::IsDisabled() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 441 | HAL_ControlWord controlWord; |
| 442 | HAL_GetControlWord(&controlWord); |
| 443 | return !(controlWord.enabled && controlWord.dsAttached); |
| 444 | } |
| 445 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 446 | bool DriverStation::IsEStopped() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 447 | HAL_ControlWord controlWord; |
| 448 | HAL_GetControlWord(&controlWord); |
| 449 | return controlWord.eStop; |
| 450 | } |
| 451 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 452 | bool DriverStation::IsAutonomous() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 453 | HAL_ControlWord controlWord; |
| 454 | HAL_GetControlWord(&controlWord); |
| 455 | return controlWord.autonomous; |
| 456 | } |
| 457 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 458 | bool DriverStation::IsAutonomousEnabled() { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 459 | HAL_ControlWord controlWord; |
| 460 | HAL_GetControlWord(&controlWord); |
| 461 | return controlWord.autonomous && controlWord.enabled; |
| 462 | } |
| 463 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 464 | bool DriverStation::IsOperatorControl() { |
| 465 | return IsTeleop(); |
| 466 | } |
| 467 | |
| 468 | bool DriverStation::IsTeleop() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 469 | HAL_ControlWord controlWord; |
| 470 | HAL_GetControlWord(&controlWord); |
| 471 | return !(controlWord.autonomous || controlWord.test); |
| 472 | } |
| 473 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 474 | bool DriverStation::IsOperatorControlEnabled() { |
| 475 | return IsTeleopEnabled(); |
| 476 | } |
| 477 | |
| 478 | bool DriverStation::IsTeleopEnabled() { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 479 | HAL_ControlWord controlWord; |
| 480 | HAL_GetControlWord(&controlWord); |
| 481 | return !controlWord.autonomous && !controlWord.test && controlWord.enabled; |
| 482 | } |
| 483 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 484 | bool DriverStation::IsTest() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 485 | HAL_ControlWord controlWord; |
| 486 | HAL_GetControlWord(&controlWord); |
| 487 | return controlWord.test; |
| 488 | } |
| 489 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 490 | bool DriverStation::IsDSAttached() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 491 | HAL_ControlWord controlWord; |
| 492 | HAL_GetControlWord(&controlWord); |
| 493 | return controlWord.dsAttached; |
| 494 | } |
| 495 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 496 | bool DriverStation::IsNewControlData() { |
| 497 | auto& inst = ::GetInstance(); |
| 498 | std::unique_lock lock(inst.waitForDataMutex); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 499 | int& lastCount = GetDSLastCount(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 500 | int currentCount = inst.waitForDataCounter; |
| 501 | if (lastCount == currentCount) { |
| 502 | return false; |
| 503 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 504 | lastCount = currentCount; |
| 505 | return true; |
| 506 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 507 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 508 | bool DriverStation::IsFMSAttached() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 509 | HAL_ControlWord controlWord; |
| 510 | HAL_GetControlWord(&controlWord); |
| 511 | return controlWord.fmsAttached; |
| 512 | } |
| 513 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 514 | std::string DriverStation::GetGameSpecificMessage() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 515 | HAL_MatchInfo info; |
| 516 | HAL_GetMatchInfo(&info); |
| 517 | return std::string(reinterpret_cast<char*>(info.gameSpecificMessage), |
| 518 | info.gameSpecificMessageSize); |
| 519 | } |
| 520 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 521 | std::string DriverStation::GetEventName() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 522 | HAL_MatchInfo info; |
| 523 | HAL_GetMatchInfo(&info); |
| 524 | return info.eventName; |
| 525 | } |
| 526 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 527 | DriverStation::MatchType DriverStation::GetMatchType() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 528 | HAL_MatchInfo info; |
| 529 | HAL_GetMatchInfo(&info); |
| 530 | return static_cast<DriverStation::MatchType>(info.matchType); |
| 531 | } |
| 532 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 533 | int DriverStation::GetMatchNumber() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 534 | HAL_MatchInfo info; |
| 535 | HAL_GetMatchInfo(&info); |
| 536 | return info.matchNumber; |
| 537 | } |
| 538 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 539 | int DriverStation::GetReplayNumber() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 540 | HAL_MatchInfo info; |
| 541 | HAL_GetMatchInfo(&info); |
| 542 | return info.replayNumber; |
| 543 | } |
| 544 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 545 | DriverStation::Alliance DriverStation::GetAlliance() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 546 | int32_t status = 0; |
| 547 | auto allianceStationID = HAL_GetAllianceStation(&status); |
| 548 | switch (allianceStationID) { |
| 549 | case HAL_AllianceStationID_kRed1: |
| 550 | case HAL_AllianceStationID_kRed2: |
| 551 | case HAL_AllianceStationID_kRed3: |
| 552 | return kRed; |
| 553 | case HAL_AllianceStationID_kBlue1: |
| 554 | case HAL_AllianceStationID_kBlue2: |
| 555 | case HAL_AllianceStationID_kBlue3: |
| 556 | return kBlue; |
| 557 | default: |
| 558 | return kInvalid; |
| 559 | } |
| 560 | } |
| 561 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 562 | int DriverStation::GetLocation() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 563 | int32_t status = 0; |
| 564 | auto allianceStationID = HAL_GetAllianceStation(&status); |
| 565 | switch (allianceStationID) { |
| 566 | case HAL_AllianceStationID_kRed1: |
| 567 | case HAL_AllianceStationID_kBlue1: |
| 568 | return 1; |
| 569 | case HAL_AllianceStationID_kRed2: |
| 570 | case HAL_AllianceStationID_kBlue2: |
| 571 | return 2; |
| 572 | case HAL_AllianceStationID_kRed3: |
| 573 | case HAL_AllianceStationID_kBlue3: |
| 574 | return 3; |
| 575 | default: |
| 576 | return 0; |
| 577 | } |
| 578 | } |
| 579 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 580 | void DriverStation::WaitForData() { |
| 581 | WaitForData(0_s); |
| 582 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 583 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 584 | bool DriverStation::WaitForData(units::second_t timeout) { |
| 585 | auto& inst = ::GetInstance(); |
| 586 | auto timeoutTime = std::chrono::steady_clock::now() + |
| 587 | std::chrono::steady_clock::duration{timeout}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 588 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 589 | std::unique_lock lock(inst.waitForDataMutex); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 590 | int& lastCount = GetDSLastCount(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 591 | int currentCount = inst.waitForDataCounter; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 592 | if (lastCount != currentCount) { |
| 593 | lastCount = currentCount; |
| 594 | return true; |
| 595 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 596 | while (inst.waitForDataCounter == currentCount) { |
| 597 | if (timeout > 0_s) { |
| 598 | auto timedOut = inst.waitForDataCond.wait_until(lock, timeoutTime); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 599 | if (timedOut == std::cv_status::timeout) { |
| 600 | return false; |
| 601 | } |
| 602 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 603 | inst.waitForDataCond.wait(lock); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 606 | lastCount = inst.waitForDataCounter; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 607 | return true; |
| 608 | } |
| 609 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 610 | double DriverStation::GetMatchTime() { |
| 611 | int32_t status = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 612 | return HAL_GetMatchTime(&status); |
| 613 | } |
| 614 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 615 | double DriverStation::GetBatteryVoltage() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 616 | int32_t status = 0; |
| 617 | double voltage = HAL_GetVinVoltage(&status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 618 | FRC_CheckErrorStatus(status, "{}", "getVinVoltage"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 619 | |
| 620 | return voltage; |
| 621 | } |
| 622 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 623 | void DriverStation::InDisabled(bool entering) { |
| 624 | ::GetInstance().userInDisabled = entering; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 627 | void DriverStation::InAutonomous(bool entering) { |
| 628 | ::GetInstance().userInAutonomous = entering; |
| 629 | } |
| 630 | |
| 631 | void DriverStation::InOperatorControl(bool entering) { |
| 632 | InTeleop(entering); |
| 633 | } |
| 634 | |
| 635 | void DriverStation::InTeleop(bool entering) { |
| 636 | ::GetInstance().userInTeleop = entering; |
| 637 | } |
| 638 | |
| 639 | void DriverStation::InTest(bool entering) { |
| 640 | ::GetInstance().userInTest = entering; |
| 641 | } |
| 642 | |
| 643 | void DriverStation::WakeupWaitForData() { |
| 644 | auto& inst = ::GetInstance(); |
| 645 | std::scoped_lock waitLock(inst.waitForDataMutex); |
| 646 | // Nofify all threads |
| 647 | inst.waitForDataCounter++; |
| 648 | inst.waitForDataCond.notify_all(); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Copy data from the DS task for the user. |
| 653 | * |
| 654 | * If no new data exists, it will just be returned, otherwise |
| 655 | * the data will be copied from the DS polling loop. |
| 656 | */ |
| 657 | void GetData() { |
| 658 | auto& inst = ::GetInstance(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 659 | { |
| 660 | // Compute the pressed and released buttons |
| 661 | HAL_JoystickButtons currentButtons; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 662 | std::unique_lock lock(inst.buttonEdgeMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 663 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 664 | for (int32_t i = 0; i < DriverStation::kJoystickPorts; i++) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 665 | HAL_GetJoystickButtons(i, ¤tButtons); |
| 666 | |
| 667 | // If buttons weren't pressed and are now, set flags in m_buttonsPressed |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 668 | inst.joystickButtonsPressed[i] |= |
| 669 | ~inst.previousButtonStates[i].buttons & currentButtons.buttons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 670 | |
| 671 | // If buttons were pressed and aren't now, set flags in m_buttonsReleased |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 672 | inst.joystickButtonsReleased[i] |= |
| 673 | inst.previousButtonStates[i].buttons & ~currentButtons.buttons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 674 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 675 | inst.previousButtonStates[i] = currentButtons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 679 | DriverStation::WakeupWaitForData(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 680 | SendMatchData(); |
| 681 | } |
| 682 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 683 | void DriverStation::SilenceJoystickConnectionWarning(bool silence) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 684 | ::GetInstance().silenceJoystickWarning = silence; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 687 | bool DriverStation::IsJoystickConnectionWarningSilenced() { |
| 688 | return !IsFMSAttached() && ::GetInstance().silenceJoystickWarning; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 691 | void ReportJoystickUnpluggedErrorV(fmt::string_view format, |
| 692 | fmt::format_args args) { |
| 693 | auto& inst = GetInstance(); |
| 694 | auto currentTime = Timer::GetFPGATimestamp(); |
| 695 | if (currentTime > inst.nextMessageTime) { |
| 696 | ReportErrorV(err::Error, "", 0, "", format, args); |
| 697 | inst.nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 701 | void ReportJoystickUnpluggedWarningV(fmt::string_view format, |
| 702 | fmt::format_args args) { |
| 703 | auto& inst = GetInstance(); |
| 704 | if (DriverStation::IsFMSAttached() || !inst.silenceJoystickWarning) { |
| 705 | auto currentTime = Timer::GetFPGATimestamp(); |
| 706 | if (currentTime > inst.nextMessageTime) { |
| 707 | ReportErrorV(warn::Warning, "", 0, "", format, args); |
| 708 | inst.nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 709 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 713 | void Run() { |
| 714 | auto& inst = GetInstance(); |
| 715 | inst.isRunning = true; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 716 | int safetyCounter = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 717 | while (inst.isRunning) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 718 | HAL_WaitForDSData(); |
| 719 | GetData(); |
| 720 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 721 | if (DriverStation::IsDisabled()) { |
| 722 | safetyCounter = 0; |
| 723 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 724 | |
| 725 | if (++safetyCounter >= 4) { |
| 726 | MotorSafety::CheckMotors(); |
| 727 | safetyCounter = 0; |
| 728 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 729 | if (inst.userInDisabled) { |
| 730 | HAL_ObserveUserProgramDisabled(); |
| 731 | } |
| 732 | if (inst.userInAutonomous) { |
| 733 | HAL_ObserveUserProgramAutonomous(); |
| 734 | } |
| 735 | if (inst.userInTeleop) { |
| 736 | HAL_ObserveUserProgramTeleop(); |
| 737 | } |
| 738 | if (inst.userInTest) { |
| 739 | HAL_ObserveUserProgramTest(); |
| 740 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 744 | void SendMatchData() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 745 | int32_t status = 0; |
| 746 | HAL_AllianceStationID alliance = HAL_GetAllianceStation(&status); |
| 747 | bool isRedAlliance = false; |
| 748 | int stationNumber = 1; |
| 749 | switch (alliance) { |
| 750 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue1: |
| 751 | isRedAlliance = false; |
| 752 | stationNumber = 1; |
| 753 | break; |
| 754 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue2: |
| 755 | isRedAlliance = false; |
| 756 | stationNumber = 2; |
| 757 | break; |
| 758 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue3: |
| 759 | isRedAlliance = false; |
| 760 | stationNumber = 3; |
| 761 | break; |
| 762 | case HAL_AllianceStationID::HAL_AllianceStationID_kRed1: |
| 763 | isRedAlliance = true; |
| 764 | stationNumber = 1; |
| 765 | break; |
| 766 | case HAL_AllianceStationID::HAL_AllianceStationID_kRed2: |
| 767 | isRedAlliance = true; |
| 768 | stationNumber = 2; |
| 769 | break; |
| 770 | default: |
| 771 | isRedAlliance = true; |
| 772 | stationNumber = 3; |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | HAL_MatchInfo tmpDataStore; |
| 777 | HAL_GetMatchInfo(&tmpDataStore); |
| 778 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 779 | auto& inst = GetInstance(); |
| 780 | inst.matchDataSender.alliance.Set(isRedAlliance); |
| 781 | inst.matchDataSender.station.Set(stationNumber); |
| 782 | inst.matchDataSender.eventName.Set(tmpDataStore.eventName); |
| 783 | inst.matchDataSender.gameSpecificMessage.Set( |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 784 | std::string(reinterpret_cast<char*>(tmpDataStore.gameSpecificMessage), |
| 785 | tmpDataStore.gameSpecificMessageSize)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 786 | inst.matchDataSender.matchNumber.Set(tmpDataStore.matchNumber); |
| 787 | inst.matchDataSender.replayNumber.Set(tmpDataStore.replayNumber); |
| 788 | inst.matchDataSender.matchType.Set(static_cast<int>(tmpDataStore.matchType)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 789 | |
| 790 | HAL_ControlWord ctlWord; |
| 791 | HAL_GetControlWord(&ctlWord); |
| 792 | int32_t wordInt = 0; |
| 793 | std::memcpy(&wordInt, &ctlWord, sizeof(wordInt)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 794 | inst.matchDataSender.controlWord.Set(wordInt); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 795 | } |