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 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 44 | uint64_t RobotController::GetFPGATime() { |
| 45 | int32_t status = 0; |
| 46 | uint64_t time = HAL_GetFPGATime(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 47 | FRC_CheckErrorStatus(status, "GetFPGATime"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 48 | return time; |
| 49 | } |
| 50 | |
| 51 | bool RobotController::GetUserButton() { |
| 52 | int32_t status = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | bool value = HAL_GetFPGAButton(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 54 | FRC_CheckErrorStatus(status, "GetUserButton"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | return value; |
| 56 | } |
| 57 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 58 | units::volt_t RobotController::GetBatteryVoltage() { |
| 59 | int32_t status = 0; |
| 60 | double retVal = HAL_GetVinVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 61 | FRC_CheckErrorStatus(status, "GetBatteryVoltage"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 62 | return units::volt_t{retVal}; |
| 63 | } |
| 64 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | bool RobotController::IsSysActive() { |
| 66 | int32_t status = 0; |
| 67 | bool retVal = HAL_GetSystemActive(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 68 | FRC_CheckErrorStatus(status, "IsSysActive"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | return retVal; |
| 70 | } |
| 71 | |
| 72 | bool RobotController::IsBrownedOut() { |
| 73 | int32_t status = 0; |
| 74 | bool retVal = HAL_GetBrownedOut(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 75 | FRC_CheckErrorStatus(status, "IsBrownedOut"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | return retVal; |
| 77 | } |
| 78 | |
| 79 | double RobotController::GetInputVoltage() { |
| 80 | int32_t status = 0; |
| 81 | double retVal = HAL_GetVinVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 82 | FRC_CheckErrorStatus(status, "GetInputVoltage"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 83 | return retVal; |
| 84 | } |
| 85 | |
| 86 | double RobotController::GetInputCurrent() { |
| 87 | int32_t status = 0; |
| 88 | double retVal = HAL_GetVinCurrent(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 89 | FRC_CheckErrorStatus(status, "GetInputCurrent"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 90 | return retVal; |
| 91 | } |
| 92 | |
| 93 | double RobotController::GetVoltage3V3() { |
| 94 | int32_t status = 0; |
| 95 | double retVal = HAL_GetUserVoltage3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 96 | FRC_CheckErrorStatus(status, "GetVoltage3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 97 | return retVal; |
| 98 | } |
| 99 | |
| 100 | double RobotController::GetCurrent3V3() { |
| 101 | int32_t status = 0; |
| 102 | double retVal = HAL_GetUserCurrent3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 103 | FRC_CheckErrorStatus(status, "GetCurrent3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 104 | return retVal; |
| 105 | } |
| 106 | |
| 107 | bool RobotController::GetEnabled3V3() { |
| 108 | int32_t status = 0; |
| 109 | bool retVal = HAL_GetUserActive3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 110 | FRC_CheckErrorStatus(status, "GetEnabled3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 111 | return retVal; |
| 112 | } |
| 113 | |
| 114 | int RobotController::GetFaultCount3V3() { |
| 115 | int32_t status = 0; |
| 116 | int retVal = HAL_GetUserCurrentFaults3V3(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 117 | FRC_CheckErrorStatus(status, "GetFaultCount3V3"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 118 | return retVal; |
| 119 | } |
| 120 | |
| 121 | double RobotController::GetVoltage5V() { |
| 122 | int32_t status = 0; |
| 123 | double retVal = HAL_GetUserVoltage5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 124 | FRC_CheckErrorStatus(status, "GetVoltage5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | return retVal; |
| 126 | } |
| 127 | |
| 128 | double RobotController::GetCurrent5V() { |
| 129 | int32_t status = 0; |
| 130 | double retVal = HAL_GetUserCurrent5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 131 | FRC_CheckErrorStatus(status, "GetCurrent5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 132 | return retVal; |
| 133 | } |
| 134 | |
| 135 | bool RobotController::GetEnabled5V() { |
| 136 | int32_t status = 0; |
| 137 | bool retVal = HAL_GetUserActive5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 138 | FRC_CheckErrorStatus(status, "GetEnabled5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 139 | return retVal; |
| 140 | } |
| 141 | |
| 142 | int RobotController::GetFaultCount5V() { |
| 143 | int32_t status = 0; |
| 144 | int retVal = HAL_GetUserCurrentFaults5V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 145 | FRC_CheckErrorStatus(status, "GetFaultCount5V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 146 | return retVal; |
| 147 | } |
| 148 | |
| 149 | double RobotController::GetVoltage6V() { |
| 150 | int32_t status = 0; |
| 151 | double retVal = HAL_GetUserVoltage6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 152 | FRC_CheckErrorStatus(status, "GetVoltage6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 153 | return retVal; |
| 154 | } |
| 155 | |
| 156 | double RobotController::GetCurrent6V() { |
| 157 | int32_t status = 0; |
| 158 | double retVal = HAL_GetUserCurrent6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 159 | FRC_CheckErrorStatus(status, "GetCurrent6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 160 | return retVal; |
| 161 | } |
| 162 | |
| 163 | bool RobotController::GetEnabled6V() { |
| 164 | int32_t status = 0; |
| 165 | bool retVal = HAL_GetUserActive6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 166 | FRC_CheckErrorStatus(status, "GetEnabled6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 167 | return retVal; |
| 168 | } |
| 169 | |
| 170 | int RobotController::GetFaultCount6V() { |
| 171 | int32_t status = 0; |
| 172 | int retVal = HAL_GetUserCurrentFaults6V(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 173 | FRC_CheckErrorStatus(status, "GetFaultCount6V"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 174 | return retVal; |
| 175 | } |
| 176 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 177 | units::volt_t RobotController::GetBrownoutVoltage() { |
| 178 | int32_t status = 0; |
| 179 | double retVal = HAL_GetBrownoutVoltage(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 180 | FRC_CheckErrorStatus(status, "GetBrownoutVoltage"); |
| 181 | return units::volt_t{retVal}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void RobotController::SetBrownoutVoltage(units::volt_t brownoutVoltage) { |
| 185 | int32_t status = 0; |
| 186 | HAL_SetBrownoutVoltage(brownoutVoltage.value(), &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 187 | FRC_CheckErrorStatus(status, "SetBrownoutVoltage"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 190 | CANStatus RobotController::GetCANStatus() { |
| 191 | int32_t status = 0; |
| 192 | float percentBusUtilization = 0; |
| 193 | uint32_t busOffCount = 0; |
| 194 | uint32_t txFullCount = 0; |
| 195 | uint32_t receiveErrorCount = 0; |
| 196 | uint32_t transmitErrorCount = 0; |
| 197 | HAL_CAN_GetCANStatus(&percentBusUtilization, &busOffCount, &txFullCount, |
| 198 | &receiveErrorCount, &transmitErrorCount, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 199 | FRC_CheckErrorStatus(status, "GetCANStatus"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 200 | return {percentBusUtilization, static_cast<int>(busOffCount), |
| 201 | static_cast<int>(txFullCount), static_cast<int>(receiveErrorCount), |
| 202 | static_cast<int>(transmitErrorCount)}; |
| 203 | } |