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> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 12 | #include <span> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <string_view> |
| 15 | #include <thread> |
| 16 | #include <type_traits> |
| 17 | |
| 18 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | #include <hal/DriverStation.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 20 | #include <hal/DriverStationTypes.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | #include <hal/HALBase.h> |
| 22 | #include <hal/Power.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 23 | #include <networktables/BooleanTopic.h> |
| 24 | #include <networktables/IntegerTopic.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | #include <networktables/NetworkTable.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 26 | #include <networktables/NetworkTableInstance.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 27 | #include <networktables/StringTopic.h> |
| 28 | #include <wpi/DataLog.h> |
| 29 | #include <wpi/EventVector.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 30 | #include <wpi/condition_variable.h> |
| 31 | #include <wpi/mutex.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 32 | #include <wpi/timestamp.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 34 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | #include "frc/MotorSafety.h" |
| 36 | #include "frc/Timer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | |
| 38 | using namespace frc; |
| 39 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 40 | namespace { |
| 41 | // A simple class which caches the previous value written to an NT entry |
| 42 | // Used to prevent redundant, repeated writes of the same value |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 43 | template <typename Topic> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 44 | class MatchDataSenderEntry { |
| 45 | public: |
| 46 | MatchDataSenderEntry(const std::shared_ptr<nt::NetworkTable>& table, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 47 | std::string_view key, |
| 48 | typename Topic::ParamType initialVal) |
| 49 | : publisher{Topic{table->GetTopic(key)}.Publish()}, prevVal{initialVal} { |
| 50 | publisher.Set(initialVal); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 51 | } |
| 52 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 53 | void Set(typename Topic::ParamType val) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 54 | if (val != prevVal) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 55 | publisher.Set(val); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 56 | prevVal = val; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private: |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 61 | typename Topic::PublisherType publisher; |
| 62 | typename Topic::ValueType prevVal; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | struct MatchDataSender { |
| 66 | std::shared_ptr<nt::NetworkTable> table = |
| 67 | nt::NetworkTableInstance::GetDefault().GetTable("FMSInfo"); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 68 | MatchDataSenderEntry<nt::StringTopic> typeMetaData{table, ".type", "FMSInfo"}; |
| 69 | MatchDataSenderEntry<nt::StringTopic> gameSpecificMessage{ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 70 | table, "GameSpecificMessage", ""}; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 71 | MatchDataSenderEntry<nt::StringTopic> eventName{table, "EventName", ""}; |
| 72 | MatchDataSenderEntry<nt::IntegerTopic> matchNumber{table, "MatchNumber", 0}; |
| 73 | MatchDataSenderEntry<nt::IntegerTopic> replayNumber{table, "ReplayNumber", 0}; |
| 74 | MatchDataSenderEntry<nt::IntegerTopic> matchType{table, "MatchType", 0}; |
| 75 | MatchDataSenderEntry<nt::BooleanTopic> alliance{table, "IsRedAlliance", true}; |
| 76 | MatchDataSenderEntry<nt::IntegerTopic> station{table, "StationNumber", 1}; |
| 77 | MatchDataSenderEntry<nt::IntegerTopic> controlWord{table, "FMSControlData", |
| 78 | 0}; |
| 79 | }; |
| 80 | |
| 81 | class JoystickLogSender { |
| 82 | public: |
| 83 | void Init(wpi::log::DataLog& log, unsigned int stick, int64_t timestamp); |
| 84 | void Send(uint64_t timestamp); |
| 85 | |
| 86 | private: |
| 87 | void AppendButtons(HAL_JoystickButtons buttons, uint64_t timestamp); |
| 88 | void AppendPOVs(const HAL_JoystickPOVs& povs, uint64_t timestamp); |
| 89 | |
| 90 | unsigned int m_stick; |
| 91 | HAL_JoystickButtons m_prevButtons; |
| 92 | HAL_JoystickAxes m_prevAxes; |
| 93 | HAL_JoystickPOVs m_prevPOVs; |
| 94 | wpi::log::BooleanArrayLogEntry m_logButtons; |
| 95 | wpi::log::FloatArrayLogEntry m_logAxes; |
| 96 | wpi::log::IntegerArrayLogEntry m_logPOVs; |
| 97 | }; |
| 98 | |
| 99 | class DataLogSender { |
| 100 | public: |
| 101 | void Init(wpi::log::DataLog& log, bool logJoysticks, int64_t timestamp); |
| 102 | void Send(uint64_t timestamp); |
| 103 | |
| 104 | private: |
| 105 | std::atomic_bool m_initialized{false}; |
| 106 | |
| 107 | HAL_ControlWord m_prevControlWord; |
| 108 | wpi::log::BooleanLogEntry m_logEnabled; |
| 109 | wpi::log::BooleanLogEntry m_logAutonomous; |
| 110 | wpi::log::BooleanLogEntry m_logTest; |
| 111 | wpi::log::BooleanLogEntry m_logEstop; |
| 112 | |
| 113 | bool m_logJoysticks; |
| 114 | std::array<JoystickLogSender, DriverStation::kJoystickPorts> m_joysticks; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | struct Instance { |
| 118 | Instance(); |
| 119 | ~Instance(); |
| 120 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 121 | wpi::EventVector refreshEvents; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 122 | MatchDataSender matchDataSender; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 123 | std::atomic<DataLogSender*> dataLogSender{nullptr}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 124 | |
| 125 | // Joystick button rising/falling edge flags |
| 126 | wpi::mutex buttonEdgeMutex; |
| 127 | std::array<HAL_JoystickButtons, DriverStation::kJoystickPorts> |
| 128 | previousButtonStates; |
| 129 | std::array<uint32_t, DriverStation::kJoystickPorts> joystickButtonsPressed; |
| 130 | std::array<uint32_t, DriverStation::kJoystickPorts> joystickButtonsReleased; |
| 131 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 132 | bool silenceJoystickWarning = false; |
| 133 | |
| 134 | // Robot state status variables |
| 135 | bool userInDisabled = false; |
| 136 | bool userInAutonomous = false; |
| 137 | bool userInTeleop = false; |
| 138 | bool userInTest = false; |
| 139 | |
| 140 | units::second_t nextMessageTime = 0_s; |
| 141 | }; |
| 142 | } // namespace |
| 143 | |
| 144 | static constexpr auto kJoystickUnpluggedMessageInterval = 1_s; |
| 145 | |
| 146 | static Instance& GetInstance() { |
| 147 | static Instance instance; |
| 148 | return instance; |
| 149 | } |
| 150 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 151 | static void SendMatchData(); |
| 152 | |
| 153 | /** |
| 154 | * Reports errors related to unplugged joysticks. |
| 155 | * |
| 156 | * Throttles the errors so that they don't overwhelm the DS. |
| 157 | */ |
| 158 | static void ReportJoystickUnpluggedErrorV(fmt::string_view format, |
| 159 | fmt::format_args args); |
| 160 | |
| 161 | template <typename S, typename... Args> |
| 162 | static inline void ReportJoystickUnpluggedError(const S& format, |
| 163 | Args&&... args) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 164 | ReportJoystickUnpluggedErrorV(format, fmt::make_format_args(args...)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Reports errors related to unplugged joysticks. |
| 169 | * |
| 170 | * Throttles the errors so that they don't overwhelm the DS. |
| 171 | */ |
| 172 | static void ReportJoystickUnpluggedWarningV(fmt::string_view format, |
| 173 | fmt::format_args args); |
| 174 | |
| 175 | template <typename S, typename... Args> |
| 176 | static inline void ReportJoystickUnpluggedWarning(const S& format, |
| 177 | Args&&... args) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 178 | ReportJoystickUnpluggedWarningV(format, fmt::make_format_args(args...)); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 181 | Instance::Instance() { |
| 182 | HAL_Initialize(500, 0); |
| 183 | |
| 184 | // All joysticks should default to having zero axes, povs and buttons, so |
| 185 | // uninitialized memory doesn't get sent to motor controllers. |
| 186 | for (unsigned int i = 0; i < DriverStation::kJoystickPorts; i++) { |
| 187 | joystickButtonsPressed[i] = 0; |
| 188 | joystickButtonsReleased[i] = 0; |
| 189 | previousButtonStates[i].count = 0; |
| 190 | previousButtonStates[i].buttons = 0; |
| 191 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | Instance::~Instance() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 195 | if (dataLogSender) { |
| 196 | delete dataLogSender.load(); |
| 197 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 200 | bool DriverStation::GetStickButton(int stick, int button) { |
| 201 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 202 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | if (button <= 0) { |
| 206 | ReportJoystickUnpluggedError( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 207 | "Joystick Button {} index out of range; indexes begin at 1", button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
| 211 | HAL_JoystickButtons buttons; |
| 212 | HAL_GetJoystickButtons(stick, &buttons); |
| 213 | |
| 214 | if (button > buttons.count) { |
| 215 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 216 | "Joystick Button {} missing (max {}), check if all controllers are " |
| 217 | "plugged in", |
| 218 | button, buttons.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
| 222 | return buttons.buttons & 1 << (button - 1); |
| 223 | } |
| 224 | |
| 225 | bool DriverStation::GetStickButtonPressed(int stick, int button) { |
| 226 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 227 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | if (button <= 0) { |
| 231 | ReportJoystickUnpluggedError( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 232 | "Joystick Button {} index out of range; indexes begin at 1", button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | HAL_JoystickButtons buttons; |
| 237 | HAL_GetJoystickButtons(stick, &buttons); |
| 238 | |
| 239 | if (button > buttons.count) { |
| 240 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 241 | "Joystick Button {} missing (max {}), check if all controllers are " |
| 242 | "plugged in", |
| 243 | button, buttons.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 244 | return false; |
| 245 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 246 | auto& inst = ::GetInstance(); |
| 247 | std::unique_lock lock(inst.buttonEdgeMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 248 | // If button was pressed, clear flag and return true |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 249 | if (inst.joystickButtonsPressed[stick] & 1 << (button - 1)) { |
| 250 | inst.joystickButtonsPressed[stick] &= ~(1 << (button - 1)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 251 | return true; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 252 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 253 | return false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | bool DriverStation::GetStickButtonReleased(int stick, int button) { |
| 257 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 258 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | if (button <= 0) { |
| 262 | ReportJoystickUnpluggedError( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 263 | "Joystick Button {} index out of range; indexes begin at 1", button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | |
| 267 | HAL_JoystickButtons buttons; |
| 268 | HAL_GetJoystickButtons(stick, &buttons); |
| 269 | |
| 270 | if (button > buttons.count) { |
| 271 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 272 | "Joystick Button {} missing (max {}), check if all controllers are " |
| 273 | "plugged in", |
| 274 | button, buttons.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 275 | return false; |
| 276 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 277 | auto& inst = ::GetInstance(); |
| 278 | std::unique_lock lock(inst.buttonEdgeMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 279 | // If button was released, clear flag and return true |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 280 | if (inst.joystickButtonsReleased[stick] & 1 << (button - 1)) { |
| 281 | inst.joystickButtonsReleased[stick] &= ~(1 << (button - 1)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 282 | return true; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 283 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 284 | return false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | double DriverStation::GetStickAxis(int stick, int axis) { |
| 288 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 289 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 290 | return 0.0; |
| 291 | } |
| 292 | if (axis < 0 || axis >= HAL_kMaxJoystickAxes) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 293 | FRC_ReportError(warn::BadJoystickAxis, "axis {} out of range", axis); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 294 | return 0.0; |
| 295 | } |
| 296 | |
| 297 | HAL_JoystickAxes axes; |
| 298 | HAL_GetJoystickAxes(stick, &axes); |
| 299 | |
| 300 | if (axis >= axes.count) { |
| 301 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 302 | "Joystick Axis {} missing (max {}), check if all controllers are " |
| 303 | "plugged in", |
| 304 | axis, axes.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 305 | return 0.0; |
| 306 | } |
| 307 | |
| 308 | return axes.axes[axis]; |
| 309 | } |
| 310 | |
| 311 | int DriverStation::GetStickPOV(int stick, int pov) { |
| 312 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 313 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 314 | return -1; |
| 315 | } |
| 316 | if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 317 | FRC_ReportError(warn::BadJoystickAxis, "POV {} out of range", pov); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | HAL_JoystickPOVs povs; |
| 322 | HAL_GetJoystickPOVs(stick, &povs); |
| 323 | |
| 324 | if (pov >= povs.count) { |
| 325 | ReportJoystickUnpluggedWarning( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 326 | "Joystick POV {} missing (max {}), check if all controllers are " |
| 327 | "plugged in", |
| 328 | pov, povs.count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | return povs.povs[pov]; |
| 333 | } |
| 334 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 335 | int DriverStation::GetStickButtons(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 336 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 337 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | HAL_JoystickButtons buttons; |
| 342 | HAL_GetJoystickButtons(stick, &buttons); |
| 343 | |
| 344 | return buttons.buttons; |
| 345 | } |
| 346 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 347 | int DriverStation::GetStickAxisCount(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 348 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 349 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | HAL_JoystickAxes axes; |
| 354 | HAL_GetJoystickAxes(stick, &axes); |
| 355 | |
| 356 | return axes.count; |
| 357 | } |
| 358 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 359 | int DriverStation::GetStickPOVCount(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 360 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 361 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | HAL_JoystickPOVs povs; |
| 366 | HAL_GetJoystickPOVs(stick, &povs); |
| 367 | |
| 368 | return povs.count; |
| 369 | } |
| 370 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 371 | int DriverStation::GetStickButtonCount(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 372 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 373 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | HAL_JoystickButtons buttons; |
| 378 | HAL_GetJoystickButtons(stick, &buttons); |
| 379 | |
| 380 | return buttons.count; |
| 381 | } |
| 382 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 383 | bool DriverStation::GetJoystickIsXbox(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 384 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 385 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
| 389 | HAL_JoystickDescriptor descriptor; |
| 390 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 391 | |
| 392 | return static_cast<bool>(descriptor.isXbox); |
| 393 | } |
| 394 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 395 | int DriverStation::GetJoystickType(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 396 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 397 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | HAL_JoystickDescriptor descriptor; |
| 402 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 403 | |
| 404 | return static_cast<int>(descriptor.type); |
| 405 | } |
| 406 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 407 | std::string DriverStation::GetJoystickName(int stick) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 408 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 409 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | HAL_JoystickDescriptor descriptor; |
| 413 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 414 | |
| 415 | return descriptor.name; |
| 416 | } |
| 417 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 418 | int DriverStation::GetJoystickAxisType(int stick, int axis) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 419 | if (stick < 0 || stick >= kJoystickPorts) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 420 | FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 421 | return -1; |
| 422 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 423 | if (axis < 0 || axis >= HAL_kMaxJoystickAxes) { |
| 424 | FRC_ReportError(warn::BadJoystickAxis, "axis {} out of range", axis); |
| 425 | return -1; |
| 426 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 427 | |
| 428 | HAL_JoystickDescriptor descriptor; |
| 429 | HAL_GetJoystickDescriptor(stick, &descriptor); |
| 430 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 431 | return descriptor.axisTypes[axis]; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 434 | bool DriverStation::IsJoystickConnected(int stick) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 435 | return GetStickAxisCount(stick) > 0 || GetStickButtonCount(stick) > 0 || |
| 436 | GetStickPOVCount(stick) > 0; |
| 437 | } |
| 438 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 439 | bool DriverStation::IsEnabled() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 440 | HAL_ControlWord controlWord; |
| 441 | HAL_GetControlWord(&controlWord); |
| 442 | return controlWord.enabled && controlWord.dsAttached; |
| 443 | } |
| 444 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 445 | bool DriverStation::IsDisabled() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 446 | HAL_ControlWord controlWord; |
| 447 | HAL_GetControlWord(&controlWord); |
| 448 | return !(controlWord.enabled && controlWord.dsAttached); |
| 449 | } |
| 450 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 451 | bool DriverStation::IsEStopped() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 452 | HAL_ControlWord controlWord; |
| 453 | HAL_GetControlWord(&controlWord); |
| 454 | return controlWord.eStop; |
| 455 | } |
| 456 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 457 | bool DriverStation::IsAutonomous() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 458 | HAL_ControlWord controlWord; |
| 459 | HAL_GetControlWord(&controlWord); |
| 460 | return controlWord.autonomous; |
| 461 | } |
| 462 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 463 | bool DriverStation::IsAutonomousEnabled() { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 464 | HAL_ControlWord controlWord; |
| 465 | HAL_GetControlWord(&controlWord); |
| 466 | return controlWord.autonomous && controlWord.enabled; |
| 467 | } |
| 468 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 469 | bool DriverStation::IsTeleop() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 470 | HAL_ControlWord controlWord; |
| 471 | HAL_GetControlWord(&controlWord); |
| 472 | return !(controlWord.autonomous || controlWord.test); |
| 473 | } |
| 474 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 475 | bool DriverStation::IsTeleopEnabled() { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 476 | HAL_ControlWord controlWord; |
| 477 | HAL_GetControlWord(&controlWord); |
| 478 | return !controlWord.autonomous && !controlWord.test && controlWord.enabled; |
| 479 | } |
| 480 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 481 | bool DriverStation::IsTest() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 482 | HAL_ControlWord controlWord; |
| 483 | HAL_GetControlWord(&controlWord); |
| 484 | return controlWord.test; |
| 485 | } |
| 486 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 487 | bool DriverStation::IsTestEnabled() { |
| 488 | HAL_ControlWord controlWord; |
| 489 | HAL_GetControlWord(&controlWord); |
| 490 | return controlWord.test && controlWord.enabled; |
| 491 | } |
| 492 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 493 | bool DriverStation::IsDSAttached() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 494 | HAL_ControlWord controlWord; |
| 495 | HAL_GetControlWord(&controlWord); |
| 496 | return controlWord.dsAttached; |
| 497 | } |
| 498 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 499 | bool DriverStation::IsFMSAttached() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 500 | HAL_ControlWord controlWord; |
| 501 | HAL_GetControlWord(&controlWord); |
| 502 | return controlWord.fmsAttached; |
| 503 | } |
| 504 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 505 | std::string DriverStation::GetGameSpecificMessage() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 506 | HAL_MatchInfo info; |
| 507 | HAL_GetMatchInfo(&info); |
| 508 | return std::string(reinterpret_cast<char*>(info.gameSpecificMessage), |
| 509 | info.gameSpecificMessageSize); |
| 510 | } |
| 511 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 512 | std::string DriverStation::GetEventName() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 513 | HAL_MatchInfo info; |
| 514 | HAL_GetMatchInfo(&info); |
| 515 | return info.eventName; |
| 516 | } |
| 517 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 518 | DriverStation::MatchType DriverStation::GetMatchType() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 519 | HAL_MatchInfo info; |
| 520 | HAL_GetMatchInfo(&info); |
| 521 | return static_cast<DriverStation::MatchType>(info.matchType); |
| 522 | } |
| 523 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 524 | int DriverStation::GetMatchNumber() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 525 | HAL_MatchInfo info; |
| 526 | HAL_GetMatchInfo(&info); |
| 527 | return info.matchNumber; |
| 528 | } |
| 529 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 530 | int DriverStation::GetReplayNumber() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 531 | HAL_MatchInfo info; |
| 532 | HAL_GetMatchInfo(&info); |
| 533 | return info.replayNumber; |
| 534 | } |
| 535 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 536 | std::optional<DriverStation::Alliance> DriverStation::GetAlliance() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 537 | int32_t status = 0; |
| 538 | auto allianceStationID = HAL_GetAllianceStation(&status); |
| 539 | switch (allianceStationID) { |
| 540 | case HAL_AllianceStationID_kRed1: |
| 541 | case HAL_AllianceStationID_kRed2: |
| 542 | case HAL_AllianceStationID_kRed3: |
| 543 | return kRed; |
| 544 | case HAL_AllianceStationID_kBlue1: |
| 545 | case HAL_AllianceStationID_kBlue2: |
| 546 | case HAL_AllianceStationID_kBlue3: |
| 547 | return kBlue; |
| 548 | default: |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 549 | return {}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 553 | std::optional<int> DriverStation::GetLocation() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 554 | int32_t status = 0; |
| 555 | auto allianceStationID = HAL_GetAllianceStation(&status); |
| 556 | switch (allianceStationID) { |
| 557 | case HAL_AllianceStationID_kRed1: |
| 558 | case HAL_AllianceStationID_kBlue1: |
| 559 | return 1; |
| 560 | case HAL_AllianceStationID_kRed2: |
| 561 | case HAL_AllianceStationID_kBlue2: |
| 562 | return 2; |
| 563 | case HAL_AllianceStationID_kRed3: |
| 564 | case HAL_AllianceStationID_kBlue3: |
| 565 | return 3; |
| 566 | default: |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 567 | return {}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 571 | bool DriverStation::WaitForDsConnection(units::second_t timeout) { |
| 572 | wpi::Event event{true, false}; |
| 573 | HAL_ProvideNewDataEventHandle(event.GetHandle()); |
| 574 | bool result = false; |
| 575 | if (timeout == 0_s) { |
| 576 | result = wpi::WaitForObject(event.GetHandle()); |
| 577 | } else { |
| 578 | result = wpi::WaitForObject(event.GetHandle(), timeout.value(), nullptr); |
| 579 | } |
| 580 | |
| 581 | HAL_RemoveNewDataEventHandle(event.GetHandle()); |
| 582 | return result; |
| 583 | } |
| 584 | |
| 585 | units::second_t DriverStation::GetMatchTime() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 586 | int32_t status = 0; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 587 | return units::second_t{HAL_GetMatchTime(&status)}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 590 | double DriverStation::GetBatteryVoltage() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 591 | int32_t status = 0; |
| 592 | double voltage = HAL_GetVinVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 593 | FRC_CheckErrorStatus(status, "getVinVoltage"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 594 | |
| 595 | return voltage; |
| 596 | } |
| 597 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 598 | /** |
| 599 | * Copy data from the DS task for the user. |
| 600 | * |
| 601 | * If no new data exists, it will just be returned, otherwise |
| 602 | * the data will be copied from the DS polling loop. |
| 603 | */ |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 604 | void DriverStation::RefreshData() { |
| 605 | HAL_RefreshDSData(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 606 | auto& inst = ::GetInstance(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 607 | { |
| 608 | // Compute the pressed and released buttons |
| 609 | HAL_JoystickButtons currentButtons; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 610 | std::unique_lock lock(inst.buttonEdgeMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 611 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 612 | for (int32_t i = 0; i < DriverStation::kJoystickPorts; i++) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 613 | HAL_GetJoystickButtons(i, ¤tButtons); |
| 614 | |
| 615 | // 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] | 616 | inst.joystickButtonsPressed[i] |= |
| 617 | ~inst.previousButtonStates[i].buttons & currentButtons.buttons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 618 | |
| 619 | // 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] | 620 | inst.joystickButtonsReleased[i] |= |
| 621 | inst.previousButtonStates[i].buttons & ~currentButtons.buttons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 622 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 623 | inst.previousButtonStates[i] = currentButtons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 627 | inst.refreshEvents.Wakeup(); |
| 628 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 629 | SendMatchData(); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 630 | if (auto sender = inst.dataLogSender.load()) { |
| 631 | sender->Send(wpi::Now()); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void DriverStation::ProvideRefreshedDataEventHandle(WPI_EventHandle handle) { |
| 636 | auto& inst = ::GetInstance(); |
| 637 | inst.refreshEvents.Add(handle); |
| 638 | } |
| 639 | |
| 640 | void DriverStation::RemoveRefreshedDataEventHandle(WPI_EventHandle handle) { |
| 641 | auto& inst = ::GetInstance(); |
| 642 | inst.refreshEvents.Remove(handle); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 645 | void DriverStation::SilenceJoystickConnectionWarning(bool silence) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 646 | ::GetInstance().silenceJoystickWarning = silence; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 649 | bool DriverStation::IsJoystickConnectionWarningSilenced() { |
| 650 | return !IsFMSAttached() && ::GetInstance().silenceJoystickWarning; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 651 | } |
| 652 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 653 | void DriverStation::StartDataLog(wpi::log::DataLog& log, bool logJoysticks) { |
| 654 | auto& inst = ::GetInstance(); |
| 655 | // Note: cannot safely replace, because we wouldn't know when to delete the |
| 656 | // "old" one. Instead do a compare and exchange with nullptr. We check first |
| 657 | // with a simple load to avoid the new in the common case. |
| 658 | if (inst.dataLogSender.load()) { |
| 659 | return; |
| 660 | } |
| 661 | DataLogSender* oldSender = nullptr; |
| 662 | DataLogSender* newSender = new DataLogSender; |
| 663 | inst.dataLogSender.compare_exchange_strong(oldSender, newSender); |
| 664 | if (oldSender) { |
| 665 | delete newSender; // already had a sender |
| 666 | } else { |
| 667 | newSender->Init(log, logJoysticks, wpi::Now()); |
| 668 | } |
| 669 | } |
| 670 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 671 | void ReportJoystickUnpluggedErrorV(fmt::string_view format, |
| 672 | fmt::format_args args) { |
| 673 | auto& inst = GetInstance(); |
| 674 | auto currentTime = Timer::GetFPGATimestamp(); |
| 675 | if (currentTime > inst.nextMessageTime) { |
| 676 | ReportErrorV(err::Error, "", 0, "", format, args); |
| 677 | inst.nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 681 | void ReportJoystickUnpluggedWarningV(fmt::string_view format, |
| 682 | fmt::format_args args) { |
| 683 | auto& inst = GetInstance(); |
| 684 | if (DriverStation::IsFMSAttached() || !inst.silenceJoystickWarning) { |
| 685 | auto currentTime = Timer::GetFPGATimestamp(); |
| 686 | if (currentTime > inst.nextMessageTime) { |
| 687 | ReportErrorV(warn::Warning, "", 0, "", format, args); |
| 688 | inst.nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 689 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 693 | void SendMatchData() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 694 | int32_t status = 0; |
| 695 | HAL_AllianceStationID alliance = HAL_GetAllianceStation(&status); |
| 696 | bool isRedAlliance = false; |
| 697 | int stationNumber = 1; |
| 698 | switch (alliance) { |
| 699 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue1: |
| 700 | isRedAlliance = false; |
| 701 | stationNumber = 1; |
| 702 | break; |
| 703 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue2: |
| 704 | isRedAlliance = false; |
| 705 | stationNumber = 2; |
| 706 | break; |
| 707 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue3: |
| 708 | isRedAlliance = false; |
| 709 | stationNumber = 3; |
| 710 | break; |
| 711 | case HAL_AllianceStationID::HAL_AllianceStationID_kRed1: |
| 712 | isRedAlliance = true; |
| 713 | stationNumber = 1; |
| 714 | break; |
| 715 | case HAL_AllianceStationID::HAL_AllianceStationID_kRed2: |
| 716 | isRedAlliance = true; |
| 717 | stationNumber = 2; |
| 718 | break; |
| 719 | default: |
| 720 | isRedAlliance = true; |
| 721 | stationNumber = 3; |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | HAL_MatchInfo tmpDataStore; |
| 726 | HAL_GetMatchInfo(&tmpDataStore); |
| 727 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 728 | auto& inst = GetInstance(); |
| 729 | inst.matchDataSender.alliance.Set(isRedAlliance); |
| 730 | inst.matchDataSender.station.Set(stationNumber); |
| 731 | inst.matchDataSender.eventName.Set(tmpDataStore.eventName); |
| 732 | inst.matchDataSender.gameSpecificMessage.Set( |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 733 | std::string(reinterpret_cast<char*>(tmpDataStore.gameSpecificMessage), |
| 734 | tmpDataStore.gameSpecificMessageSize)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 735 | inst.matchDataSender.matchNumber.Set(tmpDataStore.matchNumber); |
| 736 | inst.matchDataSender.replayNumber.Set(tmpDataStore.replayNumber); |
| 737 | inst.matchDataSender.matchType.Set(static_cast<int>(tmpDataStore.matchType)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 738 | |
| 739 | HAL_ControlWord ctlWord; |
| 740 | HAL_GetControlWord(&ctlWord); |
| 741 | int32_t wordInt = 0; |
| 742 | std::memcpy(&wordInt, &ctlWord, sizeof(wordInt)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 743 | inst.matchDataSender.controlWord.Set(wordInt); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 744 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 745 | |
| 746 | void JoystickLogSender::Init(wpi::log::DataLog& log, unsigned int stick, |
| 747 | int64_t timestamp) { |
| 748 | m_stick = stick; |
| 749 | |
| 750 | m_logButtons = wpi::log::BooleanArrayLogEntry{ |
| 751 | log, fmt::format("DS:joystick{}/buttons", stick), timestamp}; |
| 752 | m_logAxes = wpi::log::FloatArrayLogEntry{ |
| 753 | log, fmt::format("DS:joystick{}/axes", stick), timestamp}; |
| 754 | m_logPOVs = wpi::log::IntegerArrayLogEntry{ |
| 755 | log, fmt::format("DS:joystick{}/povs", stick), timestamp}; |
| 756 | |
| 757 | HAL_GetJoystickButtons(m_stick, &m_prevButtons); |
| 758 | HAL_GetJoystickAxes(m_stick, &m_prevAxes); |
| 759 | HAL_GetJoystickPOVs(m_stick, &m_prevPOVs); |
| 760 | AppendButtons(m_prevButtons, timestamp); |
| 761 | m_logAxes.Append( |
| 762 | std::span<const float>{m_prevAxes.axes, |
| 763 | static_cast<size_t>(m_prevAxes.count)}, |
| 764 | timestamp); |
| 765 | AppendPOVs(m_prevPOVs, timestamp); |
| 766 | } |
| 767 | |
| 768 | void JoystickLogSender::Send(uint64_t timestamp) { |
| 769 | HAL_JoystickButtons buttons; |
| 770 | HAL_GetJoystickButtons(m_stick, &buttons); |
| 771 | if (buttons.count != m_prevButtons.count || |
| 772 | buttons.buttons != m_prevButtons.buttons) { |
| 773 | AppendButtons(buttons, timestamp); |
| 774 | } |
| 775 | m_prevButtons = buttons; |
| 776 | |
| 777 | HAL_JoystickAxes axes; |
| 778 | HAL_GetJoystickAxes(m_stick, &axes); |
| 779 | if (axes.count != m_prevAxes.count || |
| 780 | std::memcmp(axes.axes, m_prevAxes.axes, |
| 781 | sizeof(axes.axes[0]) * axes.count) != 0) { |
| 782 | m_logAxes.Append( |
| 783 | std::span<const float>{axes.axes, static_cast<size_t>(axes.count)}, |
| 784 | timestamp); |
| 785 | } |
| 786 | m_prevAxes = axes; |
| 787 | |
| 788 | HAL_JoystickPOVs povs; |
| 789 | HAL_GetJoystickPOVs(m_stick, &povs); |
| 790 | if (povs.count != m_prevPOVs.count || |
| 791 | std::memcmp(povs.povs, m_prevPOVs.povs, |
| 792 | sizeof(povs.povs[0]) * povs.count) != 0) { |
| 793 | AppendPOVs(povs, timestamp); |
| 794 | } |
| 795 | m_prevPOVs = povs; |
| 796 | } |
| 797 | |
| 798 | void JoystickLogSender::AppendButtons(HAL_JoystickButtons buttons, |
| 799 | uint64_t timestamp) { |
| 800 | uint8_t buttonsArr[32]; |
| 801 | for (unsigned int i = 0; i < buttons.count; ++i) { |
| 802 | buttonsArr[i] = (buttons.buttons & (1u << i)) != 0; |
| 803 | } |
| 804 | m_logButtons.Append(std::span<const uint8_t>{buttonsArr, buttons.count}, |
| 805 | timestamp); |
| 806 | } |
| 807 | |
| 808 | void JoystickLogSender::AppendPOVs(const HAL_JoystickPOVs& povs, |
| 809 | uint64_t timestamp) { |
| 810 | int64_t povsArr[HAL_kMaxJoystickPOVs]; |
| 811 | for (int i = 0; i < povs.count; ++i) { |
| 812 | povsArr[i] = povs.povs[i]; |
| 813 | } |
| 814 | m_logPOVs.Append( |
| 815 | std::span<const int64_t>{povsArr, static_cast<size_t>(povs.count)}, |
| 816 | timestamp); |
| 817 | } |
| 818 | |
| 819 | void DataLogSender::Init(wpi::log::DataLog& log, bool logJoysticks, |
| 820 | int64_t timestamp) { |
| 821 | m_logEnabled = wpi::log::BooleanLogEntry{log, "DS:enabled", timestamp}; |
| 822 | m_logAutonomous = wpi::log::BooleanLogEntry{log, "DS:autonomous", timestamp}; |
| 823 | m_logTest = wpi::log::BooleanLogEntry{log, "DS:test", timestamp}; |
| 824 | m_logEstop = wpi::log::BooleanLogEntry{log, "DS:estop", timestamp}; |
| 825 | |
| 826 | // append initial control word values |
| 827 | HAL_GetControlWord(&m_prevControlWord); |
| 828 | m_logEnabled.Append(m_prevControlWord.enabled, timestamp); |
| 829 | m_logAutonomous.Append(m_prevControlWord.autonomous, timestamp); |
| 830 | m_logTest.Append(m_prevControlWord.test, timestamp); |
| 831 | m_logEstop.Append(m_prevControlWord.eStop, timestamp); |
| 832 | |
| 833 | m_logJoysticks = logJoysticks; |
| 834 | if (logJoysticks) { |
| 835 | unsigned int i = 0; |
| 836 | for (auto&& joystick : m_joysticks) { |
| 837 | joystick.Init(log, i++, timestamp); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | m_initialized = true; |
| 842 | } |
| 843 | |
| 844 | void DataLogSender::Send(uint64_t timestamp) { |
| 845 | if (!m_initialized) { |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | // append control word value changes |
| 850 | HAL_ControlWord ctlWord; |
| 851 | HAL_GetControlWord(&ctlWord); |
| 852 | if (ctlWord.enabled != m_prevControlWord.enabled) { |
| 853 | m_logEnabled.Append(ctlWord.enabled, timestamp); |
| 854 | } |
| 855 | if (ctlWord.autonomous != m_prevControlWord.autonomous) { |
| 856 | m_logAutonomous.Append(ctlWord.autonomous, timestamp); |
| 857 | } |
| 858 | if (ctlWord.test != m_prevControlWord.test) { |
| 859 | m_logTest.Append(ctlWord.test, timestamp); |
| 860 | } |
| 861 | if (ctlWord.eStop != m_prevControlWord.eStop) { |
| 862 | m_logEstop.Append(ctlWord.eStop, timestamp); |
| 863 | } |
| 864 | m_prevControlWord = ctlWord; |
| 865 | |
| 866 | if (m_logJoysticks) { |
| 867 | // append joystick value changes |
| 868 | for (auto&& joystick : m_joysticks) { |
| 869 | joystick.Send(timestamp); |
| 870 | } |
| 871 | } |
| 872 | } |