Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include <chrono> |
| 9 | #include <cstdio> |
| 10 | #include <cstdlib> |
| 11 | #include <cstring> |
| 12 | #include <limits> |
| 13 | |
| 14 | #include "FRC_NetworkCommunication/FRCComm.h" |
| 15 | #include "HAL/DriverStation.h" |
| 16 | #include "HAL/cpp/priority_condition_variable.h" |
| 17 | #include "HAL/cpp/priority_mutex.h" |
| 18 | |
| 19 | static_assert(sizeof(int32_t) >= sizeof(int), |
| 20 | "FRC_NetworkComm status variable is larger than 32 bits"); |
| 21 | |
| 22 | struct HAL_JoystickAxesInt { |
| 23 | int16_t count; |
| 24 | int16_t axes[HAL_kMaxJoystickAxes]; |
| 25 | }; |
| 26 | |
| 27 | static priority_mutex msgMutex; |
| 28 | static priority_condition_variable newDSDataAvailableCond; |
| 29 | static priority_mutex newDSDataAvailableMutex; |
| 30 | |
| 31 | extern "C" { |
| 32 | int32_t HAL_SetErrorData(const char* errors, int32_t errorsLength, |
| 33 | int32_t waitMs) { |
| 34 | return setErrorData(errors, errorsLength, waitMs); |
| 35 | } |
| 36 | |
| 37 | int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode, |
| 38 | const char* details, const char* location, |
| 39 | const char* callStack, HAL_Bool printMsg) { |
| 40 | // Avoid flooding console by keeping track of previous 5 error |
| 41 | // messages and only printing again if they're longer than 1 second old. |
| 42 | static constexpr int KEEP_MSGS = 5; |
| 43 | std::lock_guard<priority_mutex> lock(msgMutex); |
| 44 | static std::string prevMsg[KEEP_MSGS]; |
| 45 | static std::chrono::time_point<std::chrono::steady_clock> |
| 46 | prevMsgTime[KEEP_MSGS]; |
| 47 | static bool initialized = false; |
| 48 | if (!initialized) { |
| 49 | for (int i = 0; i < KEEP_MSGS; i++) { |
| 50 | prevMsgTime[i] = |
| 51 | std::chrono::steady_clock::now() - std::chrono::seconds(2); |
| 52 | } |
| 53 | initialized = true; |
| 54 | } |
| 55 | |
| 56 | auto curTime = std::chrono::steady_clock::now(); |
| 57 | int i; |
| 58 | for (i = 0; i < KEEP_MSGS; ++i) { |
| 59 | if (prevMsg[i] == details) break; |
| 60 | } |
| 61 | int retval = 0; |
| 62 | if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) { |
| 63 | retval = FRC_NetworkCommunication_sendError(isError, errorCode, isLVCode, |
| 64 | details, location, callStack); |
| 65 | if (printMsg) { |
| 66 | if (location && location[0] != '\0') { |
| 67 | std::fprintf(stderr, "%s at %s: ", isError ? "Error" : "Warning", |
| 68 | location); |
| 69 | } |
| 70 | std::fprintf(stderr, "%s\n", details); |
| 71 | if (callStack && callStack[0] != '\0') { |
| 72 | std::fprintf(stderr, "%s\n", callStack); |
| 73 | } |
| 74 | } |
| 75 | if (i == KEEP_MSGS) { |
| 76 | // replace the oldest one |
| 77 | i = 0; |
| 78 | auto first = prevMsgTime[0]; |
| 79 | for (int j = 1; j < KEEP_MSGS; ++j) { |
| 80 | if (prevMsgTime[j] < first) { |
| 81 | first = prevMsgTime[j]; |
| 82 | i = j; |
| 83 | } |
| 84 | } |
| 85 | prevMsg[i] = details; |
| 86 | } |
| 87 | prevMsgTime[i] = curTime; |
| 88 | } |
| 89 | return retval; |
| 90 | } |
| 91 | |
| 92 | int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) { |
| 93 | std::memset(controlWord, 0, sizeof(HAL_ControlWord)); |
| 94 | return FRC_NetworkCommunication_getControlWord( |
| 95 | reinterpret_cast<ControlWord_t*>(controlWord)); |
| 96 | } |
| 97 | |
| 98 | HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) { |
| 99 | HAL_AllianceStationID allianceStation; |
| 100 | *status = FRC_NetworkCommunication_getAllianceStation( |
| 101 | reinterpret_cast<AllianceStationID_t*>(&allianceStation)); |
| 102 | return allianceStation; |
| 103 | } |
| 104 | |
| 105 | int32_t HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) { |
| 106 | HAL_JoystickAxesInt axesInt; |
| 107 | |
| 108 | int retVal = FRC_NetworkCommunication_getJoystickAxes( |
| 109 | joystickNum, reinterpret_cast<JoystickAxes_t*>(&axesInt), |
| 110 | HAL_kMaxJoystickAxes); |
| 111 | |
| 112 | // copy integer values to double values |
| 113 | axes->count = axesInt.count; |
| 114 | // current scaling is -128 to 127, can easily be patched in the future by |
| 115 | // changing this function. |
| 116 | for (int32_t i = 0; i < axesInt.count; i++) { |
| 117 | int8_t value = axesInt.axes[i]; |
| 118 | if (value < 0) { |
| 119 | axes->axes[i] = value / 128.0; |
| 120 | } else { |
| 121 | axes->axes[i] = value / 127.0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return retVal; |
| 126 | } |
| 127 | |
| 128 | int32_t HAL_GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) { |
| 129 | return FRC_NetworkCommunication_getJoystickPOVs( |
| 130 | joystickNum, reinterpret_cast<JoystickPOV_t*>(povs), |
| 131 | HAL_kMaxJoystickPOVs); |
| 132 | } |
| 133 | |
| 134 | int32_t HAL_GetJoystickButtons(int32_t joystickNum, |
| 135 | HAL_JoystickButtons* buttons) { |
| 136 | return FRC_NetworkCommunication_getJoystickButtons( |
| 137 | joystickNum, &buttons->buttons, &buttons->count); |
| 138 | } |
| 139 | /** |
| 140 | * Retrieve the Joystick Descriptor for particular slot |
| 141 | * @param desc [out] descriptor (data transfer object) to fill in. desc is |
| 142 | * filled in regardless of success. In other words, if descriptor is not |
| 143 | * available, desc is filled in with default values matching the init-values in |
| 144 | * Java and C++ Driverstation for when caller requests a too-large joystick |
| 145 | * index. |
| 146 | * |
| 147 | * @return error code reported from Network Comm back-end. Zero is good, |
| 148 | * nonzero is bad. |
| 149 | */ |
| 150 | int32_t HAL_GetJoystickDescriptor(int32_t joystickNum, |
| 151 | HAL_JoystickDescriptor* desc) { |
| 152 | desc->isXbox = 0; |
| 153 | desc->type = std::numeric_limits<uint8_t>::max(); |
| 154 | desc->name[0] = '\0'; |
| 155 | desc->axisCount = |
| 156 | HAL_kMaxJoystickAxes; /* set to the desc->axisTypes's capacity */ |
| 157 | desc->buttonCount = 0; |
| 158 | desc->povCount = 0; |
| 159 | int retval = FRC_NetworkCommunication_getJoystickDesc( |
| 160 | joystickNum, &desc->isXbox, &desc->type, |
| 161 | reinterpret_cast<char*>(&desc->name), &desc->axisCount, |
| 162 | reinterpret_cast<uint8_t*>(&desc->axisTypes), &desc->buttonCount, |
| 163 | &desc->povCount); |
| 164 | /* check the return, if there is an error and the RIOimage predates FRC2017, |
| 165 | * then axisCount needs to be cleared */ |
| 166 | if (retval != 0) { |
| 167 | /* set count to zero so downstream code doesn't decode invalid axisTypes. */ |
| 168 | desc->axisCount = 0; |
| 169 | } |
| 170 | return retval; |
| 171 | } |
| 172 | |
| 173 | HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) { |
| 174 | HAL_JoystickDescriptor joystickDesc; |
| 175 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 176 | return 0; |
| 177 | } else { |
| 178 | return joystickDesc.isXbox; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | int32_t HAL_GetJoystickType(int32_t joystickNum) { |
| 183 | HAL_JoystickDescriptor joystickDesc; |
| 184 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 185 | return -1; |
| 186 | } else { |
| 187 | return joystickDesc.type; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | char* HAL_GetJoystickName(int32_t joystickNum) { |
| 192 | HAL_JoystickDescriptor joystickDesc; |
| 193 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 194 | char* name = static_cast<char*>(std::malloc(1)); |
| 195 | name[0] = '\0'; |
| 196 | return name; |
| 197 | } else { |
| 198 | size_t len = std::strlen(joystickDesc.name); |
| 199 | char* name = static_cast<char*>(std::malloc(len + 1)); |
| 200 | std::strncpy(name, joystickDesc.name, len); |
| 201 | name[len] = '\0'; |
| 202 | return name; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) { |
| 207 | HAL_JoystickDescriptor joystickDesc; |
| 208 | if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) { |
| 209 | return -1; |
| 210 | } else { |
| 211 | return joystickDesc.axisTypes[axis]; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs, |
| 216 | int32_t leftRumble, int32_t rightRumble) { |
| 217 | return FRC_NetworkCommunication_setJoystickOutputs(joystickNum, outputs, |
| 218 | leftRumble, rightRumble); |
| 219 | } |
| 220 | |
| 221 | double HAL_GetMatchTime(int32_t* status) { |
| 222 | float matchTime; |
| 223 | *status = FRC_NetworkCommunication_getMatchTime(&matchTime); |
| 224 | return matchTime; |
| 225 | } |
| 226 | |
| 227 | void HAL_ObserveUserProgramStarting(void) { |
| 228 | FRC_NetworkCommunication_observeUserProgramStarting(); |
| 229 | } |
| 230 | |
| 231 | void HAL_ObserveUserProgramDisabled(void) { |
| 232 | FRC_NetworkCommunication_observeUserProgramDisabled(); |
| 233 | } |
| 234 | |
| 235 | void HAL_ObserveUserProgramAutonomous(void) { |
| 236 | FRC_NetworkCommunication_observeUserProgramAutonomous(); |
| 237 | } |
| 238 | |
| 239 | void HAL_ObserveUserProgramTeleop(void) { |
| 240 | FRC_NetworkCommunication_observeUserProgramTeleop(); |
| 241 | } |
| 242 | |
| 243 | void HAL_ObserveUserProgramTest(void) { |
| 244 | FRC_NetworkCommunication_observeUserProgramTest(); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Waits for the newest DS packet to arrive. Note that this is a blocking call. |
| 249 | */ |
| 250 | void HAL_WaitForDSData(void) { |
| 251 | std::unique_lock<priority_mutex> lock(newDSDataAvailableMutex); |
| 252 | newDSDataAvailableCond.wait(lock); |
| 253 | } |
| 254 | |
| 255 | void HAL_InitializeDriverStation(void) { |
| 256 | // Set our DS new data condition variable. |
| 257 | setNewDataSem(newDSDataAvailableCond.native_handle()); |
| 258 | } |
| 259 | |
| 260 | } // extern "C" |