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 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 487 | bool DriverStation::IsDSAttached() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 488 | HAL_ControlWord controlWord; |
| 489 | HAL_GetControlWord(&controlWord); |
| 490 | return controlWord.dsAttached; |
| 491 | } |
| 492 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 493 | bool DriverStation::IsFMSAttached() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 494 | HAL_ControlWord controlWord; |
| 495 | HAL_GetControlWord(&controlWord); |
| 496 | return controlWord.fmsAttached; |
| 497 | } |
| 498 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 499 | std::string DriverStation::GetGameSpecificMessage() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 500 | HAL_MatchInfo info; |
| 501 | HAL_GetMatchInfo(&info); |
| 502 | return std::string(reinterpret_cast<char*>(info.gameSpecificMessage), |
| 503 | info.gameSpecificMessageSize); |
| 504 | } |
| 505 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 506 | std::string DriverStation::GetEventName() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 507 | HAL_MatchInfo info; |
| 508 | HAL_GetMatchInfo(&info); |
| 509 | return info.eventName; |
| 510 | } |
| 511 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 512 | DriverStation::MatchType DriverStation::GetMatchType() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 513 | HAL_MatchInfo info; |
| 514 | HAL_GetMatchInfo(&info); |
| 515 | return static_cast<DriverStation::MatchType>(info.matchType); |
| 516 | } |
| 517 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 518 | int DriverStation::GetMatchNumber() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 519 | HAL_MatchInfo info; |
| 520 | HAL_GetMatchInfo(&info); |
| 521 | return info.matchNumber; |
| 522 | } |
| 523 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 524 | int DriverStation::GetReplayNumber() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 525 | HAL_MatchInfo info; |
| 526 | HAL_GetMatchInfo(&info); |
| 527 | return info.replayNumber; |
| 528 | } |
| 529 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 530 | DriverStation::Alliance DriverStation::GetAlliance() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 531 | int32_t status = 0; |
| 532 | auto allianceStationID = HAL_GetAllianceStation(&status); |
| 533 | switch (allianceStationID) { |
| 534 | case HAL_AllianceStationID_kRed1: |
| 535 | case HAL_AllianceStationID_kRed2: |
| 536 | case HAL_AllianceStationID_kRed3: |
| 537 | return kRed; |
| 538 | case HAL_AllianceStationID_kBlue1: |
| 539 | case HAL_AllianceStationID_kBlue2: |
| 540 | case HAL_AllianceStationID_kBlue3: |
| 541 | return kBlue; |
| 542 | default: |
| 543 | return kInvalid; |
| 544 | } |
| 545 | } |
| 546 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 547 | int DriverStation::GetLocation() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 548 | int32_t status = 0; |
| 549 | auto allianceStationID = HAL_GetAllianceStation(&status); |
| 550 | switch (allianceStationID) { |
| 551 | case HAL_AllianceStationID_kRed1: |
| 552 | case HAL_AllianceStationID_kBlue1: |
| 553 | return 1; |
| 554 | case HAL_AllianceStationID_kRed2: |
| 555 | case HAL_AllianceStationID_kBlue2: |
| 556 | return 2; |
| 557 | case HAL_AllianceStationID_kRed3: |
| 558 | case HAL_AllianceStationID_kBlue3: |
| 559 | return 3; |
| 560 | default: |
| 561 | return 0; |
| 562 | } |
| 563 | } |
| 564 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 565 | double DriverStation::GetMatchTime() { |
| 566 | int32_t status = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 567 | return HAL_GetMatchTime(&status); |
| 568 | } |
| 569 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 570 | double DriverStation::GetBatteryVoltage() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 571 | int32_t status = 0; |
| 572 | double voltage = HAL_GetVinVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 573 | FRC_CheckErrorStatus(status, "getVinVoltage"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 574 | |
| 575 | return voltage; |
| 576 | } |
| 577 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 578 | /** |
| 579 | * Copy data from the DS task for the user. |
| 580 | * |
| 581 | * If no new data exists, it will just be returned, otherwise |
| 582 | * the data will be copied from the DS polling loop. |
| 583 | */ |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 584 | void DriverStation::RefreshData() { |
| 585 | HAL_RefreshDSData(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 586 | auto& inst = ::GetInstance(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 587 | { |
| 588 | // Compute the pressed and released buttons |
| 589 | HAL_JoystickButtons currentButtons; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 590 | std::unique_lock lock(inst.buttonEdgeMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 591 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 592 | for (int32_t i = 0; i < DriverStation::kJoystickPorts; i++) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 593 | HAL_GetJoystickButtons(i, ¤tButtons); |
| 594 | |
| 595 | // 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] | 596 | inst.joystickButtonsPressed[i] |= |
| 597 | ~inst.previousButtonStates[i].buttons & currentButtons.buttons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 598 | |
| 599 | // 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] | 600 | inst.joystickButtonsReleased[i] |= |
| 601 | inst.previousButtonStates[i].buttons & ~currentButtons.buttons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 602 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 603 | inst.previousButtonStates[i] = currentButtons; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 607 | inst.refreshEvents.Wakeup(); |
| 608 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 609 | SendMatchData(); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 610 | if (auto sender = inst.dataLogSender.load()) { |
| 611 | sender->Send(wpi::Now()); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | void DriverStation::ProvideRefreshedDataEventHandle(WPI_EventHandle handle) { |
| 616 | auto& inst = ::GetInstance(); |
| 617 | inst.refreshEvents.Add(handle); |
| 618 | } |
| 619 | |
| 620 | void DriverStation::RemoveRefreshedDataEventHandle(WPI_EventHandle handle) { |
| 621 | auto& inst = ::GetInstance(); |
| 622 | inst.refreshEvents.Remove(handle); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 625 | void DriverStation::SilenceJoystickConnectionWarning(bool silence) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 626 | ::GetInstance().silenceJoystickWarning = silence; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 629 | bool DriverStation::IsJoystickConnectionWarningSilenced() { |
| 630 | return !IsFMSAttached() && ::GetInstance().silenceJoystickWarning; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 631 | } |
| 632 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 633 | void DriverStation::StartDataLog(wpi::log::DataLog& log, bool logJoysticks) { |
| 634 | auto& inst = ::GetInstance(); |
| 635 | // Note: cannot safely replace, because we wouldn't know when to delete the |
| 636 | // "old" one. Instead do a compare and exchange with nullptr. We check first |
| 637 | // with a simple load to avoid the new in the common case. |
| 638 | if (inst.dataLogSender.load()) { |
| 639 | return; |
| 640 | } |
| 641 | DataLogSender* oldSender = nullptr; |
| 642 | DataLogSender* newSender = new DataLogSender; |
| 643 | inst.dataLogSender.compare_exchange_strong(oldSender, newSender); |
| 644 | if (oldSender) { |
| 645 | delete newSender; // already had a sender |
| 646 | } else { |
| 647 | newSender->Init(log, logJoysticks, wpi::Now()); |
| 648 | } |
| 649 | } |
| 650 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 651 | void ReportJoystickUnpluggedErrorV(fmt::string_view format, |
| 652 | fmt::format_args args) { |
| 653 | auto& inst = GetInstance(); |
| 654 | auto currentTime = Timer::GetFPGATimestamp(); |
| 655 | if (currentTime > inst.nextMessageTime) { |
| 656 | ReportErrorV(err::Error, "", 0, "", format, args); |
| 657 | inst.nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 661 | void ReportJoystickUnpluggedWarningV(fmt::string_view format, |
| 662 | fmt::format_args args) { |
| 663 | auto& inst = GetInstance(); |
| 664 | if (DriverStation::IsFMSAttached() || !inst.silenceJoystickWarning) { |
| 665 | auto currentTime = Timer::GetFPGATimestamp(); |
| 666 | if (currentTime > inst.nextMessageTime) { |
| 667 | ReportErrorV(warn::Warning, "", 0, "", format, args); |
| 668 | inst.nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 669 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 673 | void SendMatchData() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 674 | int32_t status = 0; |
| 675 | HAL_AllianceStationID alliance = HAL_GetAllianceStation(&status); |
| 676 | bool isRedAlliance = false; |
| 677 | int stationNumber = 1; |
| 678 | switch (alliance) { |
| 679 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue1: |
| 680 | isRedAlliance = false; |
| 681 | stationNumber = 1; |
| 682 | break; |
| 683 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue2: |
| 684 | isRedAlliance = false; |
| 685 | stationNumber = 2; |
| 686 | break; |
| 687 | case HAL_AllianceStationID::HAL_AllianceStationID_kBlue3: |
| 688 | isRedAlliance = false; |
| 689 | stationNumber = 3; |
| 690 | break; |
| 691 | case HAL_AllianceStationID::HAL_AllianceStationID_kRed1: |
| 692 | isRedAlliance = true; |
| 693 | stationNumber = 1; |
| 694 | break; |
| 695 | case HAL_AllianceStationID::HAL_AllianceStationID_kRed2: |
| 696 | isRedAlliance = true; |
| 697 | stationNumber = 2; |
| 698 | break; |
| 699 | default: |
| 700 | isRedAlliance = true; |
| 701 | stationNumber = 3; |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | HAL_MatchInfo tmpDataStore; |
| 706 | HAL_GetMatchInfo(&tmpDataStore); |
| 707 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 708 | auto& inst = GetInstance(); |
| 709 | inst.matchDataSender.alliance.Set(isRedAlliance); |
| 710 | inst.matchDataSender.station.Set(stationNumber); |
| 711 | inst.matchDataSender.eventName.Set(tmpDataStore.eventName); |
| 712 | inst.matchDataSender.gameSpecificMessage.Set( |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 713 | std::string(reinterpret_cast<char*>(tmpDataStore.gameSpecificMessage), |
| 714 | tmpDataStore.gameSpecificMessageSize)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 715 | inst.matchDataSender.matchNumber.Set(tmpDataStore.matchNumber); |
| 716 | inst.matchDataSender.replayNumber.Set(tmpDataStore.replayNumber); |
| 717 | inst.matchDataSender.matchType.Set(static_cast<int>(tmpDataStore.matchType)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 718 | |
| 719 | HAL_ControlWord ctlWord; |
| 720 | HAL_GetControlWord(&ctlWord); |
| 721 | int32_t wordInt = 0; |
| 722 | std::memcpy(&wordInt, &ctlWord, sizeof(wordInt)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 723 | inst.matchDataSender.controlWord.Set(wordInt); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 724 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 725 | |
| 726 | void JoystickLogSender::Init(wpi::log::DataLog& log, unsigned int stick, |
| 727 | int64_t timestamp) { |
| 728 | m_stick = stick; |
| 729 | |
| 730 | m_logButtons = wpi::log::BooleanArrayLogEntry{ |
| 731 | log, fmt::format("DS:joystick{}/buttons", stick), timestamp}; |
| 732 | m_logAxes = wpi::log::FloatArrayLogEntry{ |
| 733 | log, fmt::format("DS:joystick{}/axes", stick), timestamp}; |
| 734 | m_logPOVs = wpi::log::IntegerArrayLogEntry{ |
| 735 | log, fmt::format("DS:joystick{}/povs", stick), timestamp}; |
| 736 | |
| 737 | HAL_GetJoystickButtons(m_stick, &m_prevButtons); |
| 738 | HAL_GetJoystickAxes(m_stick, &m_prevAxes); |
| 739 | HAL_GetJoystickPOVs(m_stick, &m_prevPOVs); |
| 740 | AppendButtons(m_prevButtons, timestamp); |
| 741 | m_logAxes.Append( |
| 742 | std::span<const float>{m_prevAxes.axes, |
| 743 | static_cast<size_t>(m_prevAxes.count)}, |
| 744 | timestamp); |
| 745 | AppendPOVs(m_prevPOVs, timestamp); |
| 746 | } |
| 747 | |
| 748 | void JoystickLogSender::Send(uint64_t timestamp) { |
| 749 | HAL_JoystickButtons buttons; |
| 750 | HAL_GetJoystickButtons(m_stick, &buttons); |
| 751 | if (buttons.count != m_prevButtons.count || |
| 752 | buttons.buttons != m_prevButtons.buttons) { |
| 753 | AppendButtons(buttons, timestamp); |
| 754 | } |
| 755 | m_prevButtons = buttons; |
| 756 | |
| 757 | HAL_JoystickAxes axes; |
| 758 | HAL_GetJoystickAxes(m_stick, &axes); |
| 759 | if (axes.count != m_prevAxes.count || |
| 760 | std::memcmp(axes.axes, m_prevAxes.axes, |
| 761 | sizeof(axes.axes[0]) * axes.count) != 0) { |
| 762 | m_logAxes.Append( |
| 763 | std::span<const float>{axes.axes, static_cast<size_t>(axes.count)}, |
| 764 | timestamp); |
| 765 | } |
| 766 | m_prevAxes = axes; |
| 767 | |
| 768 | HAL_JoystickPOVs povs; |
| 769 | HAL_GetJoystickPOVs(m_stick, &povs); |
| 770 | if (povs.count != m_prevPOVs.count || |
| 771 | std::memcmp(povs.povs, m_prevPOVs.povs, |
| 772 | sizeof(povs.povs[0]) * povs.count) != 0) { |
| 773 | AppendPOVs(povs, timestamp); |
| 774 | } |
| 775 | m_prevPOVs = povs; |
| 776 | } |
| 777 | |
| 778 | void JoystickLogSender::AppendButtons(HAL_JoystickButtons buttons, |
| 779 | uint64_t timestamp) { |
| 780 | uint8_t buttonsArr[32]; |
| 781 | for (unsigned int i = 0; i < buttons.count; ++i) { |
| 782 | buttonsArr[i] = (buttons.buttons & (1u << i)) != 0; |
| 783 | } |
| 784 | m_logButtons.Append(std::span<const uint8_t>{buttonsArr, buttons.count}, |
| 785 | timestamp); |
| 786 | } |
| 787 | |
| 788 | void JoystickLogSender::AppendPOVs(const HAL_JoystickPOVs& povs, |
| 789 | uint64_t timestamp) { |
| 790 | int64_t povsArr[HAL_kMaxJoystickPOVs]; |
| 791 | for (int i = 0; i < povs.count; ++i) { |
| 792 | povsArr[i] = povs.povs[i]; |
| 793 | } |
| 794 | m_logPOVs.Append( |
| 795 | std::span<const int64_t>{povsArr, static_cast<size_t>(povs.count)}, |
| 796 | timestamp); |
| 797 | } |
| 798 | |
| 799 | void DataLogSender::Init(wpi::log::DataLog& log, bool logJoysticks, |
| 800 | int64_t timestamp) { |
| 801 | m_logEnabled = wpi::log::BooleanLogEntry{log, "DS:enabled", timestamp}; |
| 802 | m_logAutonomous = wpi::log::BooleanLogEntry{log, "DS:autonomous", timestamp}; |
| 803 | m_logTest = wpi::log::BooleanLogEntry{log, "DS:test", timestamp}; |
| 804 | m_logEstop = wpi::log::BooleanLogEntry{log, "DS:estop", timestamp}; |
| 805 | |
| 806 | // append initial control word values |
| 807 | HAL_GetControlWord(&m_prevControlWord); |
| 808 | m_logEnabled.Append(m_prevControlWord.enabled, timestamp); |
| 809 | m_logAutonomous.Append(m_prevControlWord.autonomous, timestamp); |
| 810 | m_logTest.Append(m_prevControlWord.test, timestamp); |
| 811 | m_logEstop.Append(m_prevControlWord.eStop, timestamp); |
| 812 | |
| 813 | m_logJoysticks = logJoysticks; |
| 814 | if (logJoysticks) { |
| 815 | unsigned int i = 0; |
| 816 | for (auto&& joystick : m_joysticks) { |
| 817 | joystick.Init(log, i++, timestamp); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | m_initialized = true; |
| 822 | } |
| 823 | |
| 824 | void DataLogSender::Send(uint64_t timestamp) { |
| 825 | if (!m_initialized) { |
| 826 | return; |
| 827 | } |
| 828 | |
| 829 | // append control word value changes |
| 830 | HAL_ControlWord ctlWord; |
| 831 | HAL_GetControlWord(&ctlWord); |
| 832 | if (ctlWord.enabled != m_prevControlWord.enabled) { |
| 833 | m_logEnabled.Append(ctlWord.enabled, timestamp); |
| 834 | } |
| 835 | if (ctlWord.autonomous != m_prevControlWord.autonomous) { |
| 836 | m_logAutonomous.Append(ctlWord.autonomous, timestamp); |
| 837 | } |
| 838 | if (ctlWord.test != m_prevControlWord.test) { |
| 839 | m_logTest.Append(ctlWord.test, timestamp); |
| 840 | } |
| 841 | if (ctlWord.eStop != m_prevControlWord.eStop) { |
| 842 | m_logEstop.Append(ctlWord.eStop, timestamp); |
| 843 | } |
| 844 | m_prevControlWord = ctlWord; |
| 845 | |
| 846 | if (m_logJoysticks) { |
| 847 | // append joystick value changes |
| 848 | for (auto&& joystick : m_joysticks) { |
| 849 | joystick.Send(timestamp); |
| 850 | } |
| 851 | } |
| 852 | } |