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/RobotController.h" |
| 6 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 7 | #include <cstddef> |
| 8 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | #include <hal/CAN.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include <hal/HALBase.h> |
| 11 | #include <hal/Power.h> |
| 12 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 13 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | |
| 15 | using namespace frc; |
| 16 | |
| 17 | int RobotController::GetFPGAVersion() { |
| 18 | int32_t status = 0; |
| 19 | int version = HAL_GetFPGAVersion(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 20 | FRC_CheckErrorStatus(status, "GetFPGAVersion"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | return version; |
| 22 | } |
| 23 | |
| 24 | int64_t RobotController::GetFPGARevision() { |
| 25 | int32_t status = 0; |
| 26 | int64_t revision = HAL_GetFPGARevision(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 27 | FRC_CheckErrorStatus(status, "GetFPGARevision"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | return revision; |
| 29 | } |
| 30 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 31 | std::string RobotController::GetSerialNumber() { |
| 32 | // Serial number is 8 characters |
| 33 | char serialNum[9]; |
| 34 | size_t len = HAL_GetSerialNumber(serialNum, sizeof(serialNum)); |
| 35 | return std::string(serialNum, len); |
| 36 | } |
| 37 | |
| 38 | std::string RobotController::GetComments() { |
| 39 | char comments[65]; |
| 40 | size_t len = HAL_GetComments(comments, sizeof(comments)); |
| 41 | return std::string(comments, len); |
| 42 | } |
| 43 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 44 | int32_t RobotController::GetTeamNumber() { |
| 45 | return HAL_GetTeamNumber(); |
| 46 | } |
| 47 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 48 | uint64_t RobotController::GetFPGATime() { |
| 49 | int32_t status = 0; |
| 50 | uint64_t time = HAL_GetFPGATime(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 51 | FRC_CheckErrorStatus(status, "GetFPGATime"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 52 | return time; |
| 53 | } |
| 54 | |
| 55 | bool RobotController::GetUserButton() { |
| 56 | int32_t status = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 57 | bool value = HAL_GetFPGAButton(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 58 | FRC_CheckErrorStatus(status, "GetUserButton"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | return value; |
| 60 | } |
| 61 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 62 | units::volt_t RobotController::GetBatteryVoltage() { |
| 63 | int32_t status = 0; |
| 64 | double retVal = HAL_GetVinVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 65 | FRC_CheckErrorStatus(status, "GetBatteryVoltage"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 66 | return units::volt_t{retVal}; |
| 67 | } |
| 68 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | bool RobotController::IsSysActive() { |
| 70 | int32_t status = 0; |
| 71 | bool retVal = HAL_GetSystemActive(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 72 | FRC_CheckErrorStatus(status, "IsSysActive"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | return retVal; |
| 74 | } |
| 75 | |
| 76 | bool RobotController::IsBrownedOut() { |
| 77 | int32_t status = 0; |
| 78 | bool retVal = HAL_GetBrownedOut(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 79 | FRC_CheckErrorStatus(status, "IsBrownedOut"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | return retVal; |
| 81 | } |
| 82 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 83 | bool RobotController::GetRSLState() { |
| 84 | int32_t status = 0; |
| 85 | bool retVal = HAL_GetRSLState(&status); |
| 86 | FRC_CheckErrorStatus(status, "GetRSLState"); |
| 87 | return retVal; |
| 88 | } |
| 89 | |
| 90 | bool RobotController::IsSystemTimeValid() { |
| 91 | int32_t status = 0; |
| 92 | bool retVal = HAL_GetSystemTimeValid(&status); |
| 93 | FRC_CheckErrorStatus(status, "IsSystemTimeValid"); |
| 94 | return retVal; |
| 95 | } |
| 96 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 97 | double RobotController::GetInputVoltage() { |
| 98 | int32_t status = 0; |
| 99 | double retVal = HAL_GetVinVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 100 | FRC_CheckErrorStatus(status, "GetInputVoltage"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 101 | return retVal; |
| 102 | } |
| 103 | |
| 104 | double RobotController::GetInputCurrent() { |
| 105 | int32_t status = 0; |
| 106 | double retVal = HAL_GetVinCurrent(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 107 | FRC_CheckErrorStatus(status, "GetInputCurrent"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | return retVal; |
| 109 | } |
| 110 | |
| 111 | double RobotController::GetVoltage3V3() { |
| 112 | int32_t status = 0; |
| 113 | double retVal = HAL_GetUserVoltage3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 114 | FRC_CheckErrorStatus(status, "GetVoltage3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 115 | return retVal; |
| 116 | } |
| 117 | |
| 118 | double RobotController::GetCurrent3V3() { |
| 119 | int32_t status = 0; |
| 120 | double retVal = HAL_GetUserCurrent3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 121 | FRC_CheckErrorStatus(status, "GetCurrent3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 122 | return retVal; |
| 123 | } |
| 124 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 125 | void RobotController::SetEnabled3V3(bool enabled) { |
| 126 | int32_t status = 0; |
| 127 | HAL_SetUserRailEnabled3V3(enabled, &status); |
| 128 | FRC_CheckErrorStatus(status, "SetEnabled3V3"); |
| 129 | } |
| 130 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 131 | bool RobotController::GetEnabled3V3() { |
| 132 | int32_t status = 0; |
| 133 | bool retVal = HAL_GetUserActive3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 134 | FRC_CheckErrorStatus(status, "GetEnabled3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 135 | return retVal; |
| 136 | } |
| 137 | |
| 138 | int RobotController::GetFaultCount3V3() { |
| 139 | int32_t status = 0; |
| 140 | int retVal = HAL_GetUserCurrentFaults3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 141 | FRC_CheckErrorStatus(status, "GetFaultCount3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 142 | return retVal; |
| 143 | } |
| 144 | |
| 145 | double RobotController::GetVoltage5V() { |
| 146 | int32_t status = 0; |
| 147 | double retVal = HAL_GetUserVoltage5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 148 | FRC_CheckErrorStatus(status, "GetVoltage5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 149 | return retVal; |
| 150 | } |
| 151 | |
| 152 | double RobotController::GetCurrent5V() { |
| 153 | int32_t status = 0; |
| 154 | double retVal = HAL_GetUserCurrent5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 155 | FRC_CheckErrorStatus(status, "GetCurrent5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 156 | return retVal; |
| 157 | } |
| 158 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 159 | void RobotController::SetEnabled5V(bool enabled) { |
| 160 | int32_t status = 0; |
| 161 | HAL_SetUserRailEnabled5V(enabled, &status); |
| 162 | FRC_CheckErrorStatus(status, "SetEnabled5V"); |
| 163 | } |
| 164 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 165 | bool RobotController::GetEnabled5V() { |
| 166 | int32_t status = 0; |
| 167 | bool retVal = HAL_GetUserActive5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 168 | FRC_CheckErrorStatus(status, "GetEnabled5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 169 | return retVal; |
| 170 | } |
| 171 | |
| 172 | int RobotController::GetFaultCount5V() { |
| 173 | int32_t status = 0; |
| 174 | int retVal = HAL_GetUserCurrentFaults5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 175 | FRC_CheckErrorStatus(status, "GetFaultCount5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 176 | return retVal; |
| 177 | } |
| 178 | |
| 179 | double RobotController::GetVoltage6V() { |
| 180 | int32_t status = 0; |
| 181 | double retVal = HAL_GetUserVoltage6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 182 | FRC_CheckErrorStatus(status, "GetVoltage6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 183 | return retVal; |
| 184 | } |
| 185 | |
| 186 | double RobotController::GetCurrent6V() { |
| 187 | int32_t status = 0; |
| 188 | double retVal = HAL_GetUserCurrent6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 189 | FRC_CheckErrorStatus(status, "GetCurrent6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 190 | return retVal; |
| 191 | } |
| 192 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 193 | void RobotController::SetEnabled6V(bool enabled) { |
| 194 | int32_t status = 0; |
| 195 | HAL_SetUserRailEnabled6V(enabled, &status); |
| 196 | FRC_CheckErrorStatus(status, "SetEnabled6V"); |
| 197 | } |
| 198 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 199 | bool RobotController::GetEnabled6V() { |
| 200 | int32_t status = 0; |
| 201 | bool retVal = HAL_GetUserActive6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 202 | FRC_CheckErrorStatus(status, "GetEnabled6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 203 | return retVal; |
| 204 | } |
| 205 | |
| 206 | int RobotController::GetFaultCount6V() { |
| 207 | int32_t status = 0; |
| 208 | int retVal = HAL_GetUserCurrentFaults6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 209 | FRC_CheckErrorStatus(status, "GetFaultCount6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 210 | return retVal; |
| 211 | } |
| 212 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 213 | units::volt_t RobotController::GetBrownoutVoltage() { |
| 214 | int32_t status = 0; |
| 215 | double retVal = HAL_GetBrownoutVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 216 | FRC_CheckErrorStatus(status, "GetBrownoutVoltage"); |
| 217 | return units::volt_t{retVal}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void RobotController::SetBrownoutVoltage(units::volt_t brownoutVoltage) { |
| 221 | int32_t status = 0; |
| 222 | HAL_SetBrownoutVoltage(brownoutVoltage.value(), &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 223 | FRC_CheckErrorStatus(status, "SetBrownoutVoltage"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 224 | } |
| 225 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 226 | units::celsius_t RobotController::GetCPUTemp() { |
| 227 | int32_t status = 0; |
| 228 | double retVal = HAL_GetCPUTemp(&status); |
| 229 | FRC_CheckErrorStatus(status, "GetCPUTemp"); |
| 230 | return units::celsius_t{retVal}; |
| 231 | } |
| 232 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 233 | CANStatus RobotController::GetCANStatus() { |
| 234 | int32_t status = 0; |
| 235 | float percentBusUtilization = 0; |
| 236 | uint32_t busOffCount = 0; |
| 237 | uint32_t txFullCount = 0; |
| 238 | uint32_t receiveErrorCount = 0; |
| 239 | uint32_t transmitErrorCount = 0; |
| 240 | HAL_CAN_GetCANStatus(&percentBusUtilization, &busOffCount, &txFullCount, |
| 241 | &receiveErrorCount, &transmitErrorCount, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 242 | FRC_CheckErrorStatus(status, "GetCANStatus"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 243 | return {percentBusUtilization, static_cast<int>(busOffCount), |
| 244 | static_cast<int>(txFullCount), static_cast<int>(receiveErrorCount), |
| 245 | static_cast<int>(transmitErrorCount)}; |
| 246 | } |