Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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 "frc/RobotController.h" |
| 9 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 10 | #include <hal/CAN.h> |
| 11 | #include <hal/FRCUsageReporting.h> |
| 12 | #include <hal/HALBase.h> |
| 13 | #include <hal/Power.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 14 | |
| 15 | #include "frc/ErrorBase.h" |
| 16 | |
| 17 | using namespace frc; |
| 18 | |
| 19 | int RobotController::GetFPGAVersion() { |
| 20 | int32_t status = 0; |
| 21 | int version = HAL_GetFPGAVersion(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 22 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 23 | return version; |
| 24 | } |
| 25 | |
| 26 | int64_t RobotController::GetFPGARevision() { |
| 27 | int32_t status = 0; |
| 28 | int64_t revision = HAL_GetFPGARevision(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 29 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 30 | return revision; |
| 31 | } |
| 32 | |
| 33 | uint64_t RobotController::GetFPGATime() { |
| 34 | int32_t status = 0; |
| 35 | uint64_t time = HAL_GetFPGATime(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 36 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 37 | return time; |
| 38 | } |
| 39 | |
| 40 | bool RobotController::GetUserButton() { |
| 41 | int32_t status = 0; |
| 42 | |
| 43 | bool value = HAL_GetFPGAButton(&status); |
| 44 | wpi_setGlobalError(status); |
| 45 | |
| 46 | return value; |
| 47 | } |
| 48 | |
| 49 | bool RobotController::IsSysActive() { |
| 50 | int32_t status = 0; |
| 51 | bool retVal = HAL_GetSystemActive(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 52 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 53 | return retVal; |
| 54 | } |
| 55 | |
| 56 | bool RobotController::IsBrownedOut() { |
| 57 | int32_t status = 0; |
| 58 | bool retVal = HAL_GetBrownedOut(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 59 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 60 | return retVal; |
| 61 | } |
| 62 | |
| 63 | double RobotController::GetInputVoltage() { |
| 64 | int32_t status = 0; |
| 65 | double retVal = HAL_GetVinVoltage(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 66 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 67 | return retVal; |
| 68 | } |
| 69 | |
| 70 | double RobotController::GetInputCurrent() { |
| 71 | int32_t status = 0; |
| 72 | double retVal = HAL_GetVinCurrent(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 73 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 74 | return retVal; |
| 75 | } |
| 76 | |
| 77 | double RobotController::GetVoltage3V3() { |
| 78 | int32_t status = 0; |
| 79 | double retVal = HAL_GetUserVoltage3V3(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 80 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 81 | return retVal; |
| 82 | } |
| 83 | |
| 84 | double RobotController::GetCurrent3V3() { |
| 85 | int32_t status = 0; |
| 86 | double retVal = HAL_GetUserCurrent3V3(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 87 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 88 | return retVal; |
| 89 | } |
| 90 | |
| 91 | bool RobotController::GetEnabled3V3() { |
| 92 | int32_t status = 0; |
| 93 | bool retVal = HAL_GetUserActive3V3(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 94 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 95 | return retVal; |
| 96 | } |
| 97 | |
| 98 | int RobotController::GetFaultCount3V3() { |
| 99 | int32_t status = 0; |
| 100 | int retVal = HAL_GetUserCurrentFaults3V3(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 101 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 102 | return retVal; |
| 103 | } |
| 104 | |
| 105 | double RobotController::GetVoltage5V() { |
| 106 | int32_t status = 0; |
| 107 | double retVal = HAL_GetUserVoltage5V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 108 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 109 | return retVal; |
| 110 | } |
| 111 | |
| 112 | double RobotController::GetCurrent5V() { |
| 113 | int32_t status = 0; |
| 114 | double retVal = HAL_GetUserCurrent5V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 115 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 116 | return retVal; |
| 117 | } |
| 118 | |
| 119 | bool RobotController::GetEnabled5V() { |
| 120 | int32_t status = 0; |
| 121 | bool retVal = HAL_GetUserActive5V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 122 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 123 | return retVal; |
| 124 | } |
| 125 | |
| 126 | int RobotController::GetFaultCount5V() { |
| 127 | int32_t status = 0; |
| 128 | int retVal = HAL_GetUserCurrentFaults5V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 129 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 130 | return retVal; |
| 131 | } |
| 132 | |
| 133 | double RobotController::GetVoltage6V() { |
| 134 | int32_t status = 0; |
| 135 | double retVal = HAL_GetUserVoltage6V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 136 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 137 | return retVal; |
| 138 | } |
| 139 | |
| 140 | double RobotController::GetCurrent6V() { |
| 141 | int32_t status = 0; |
| 142 | double retVal = HAL_GetUserCurrent6V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 143 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 144 | return retVal; |
| 145 | } |
| 146 | |
| 147 | bool RobotController::GetEnabled6V() { |
| 148 | int32_t status = 0; |
| 149 | bool retVal = HAL_GetUserActive6V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 150 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 151 | return retVal; |
| 152 | } |
| 153 | |
| 154 | int RobotController::GetFaultCount6V() { |
| 155 | int32_t status = 0; |
| 156 | int retVal = HAL_GetUserCurrentFaults6V(&status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 157 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 158 | return retVal; |
| 159 | } |
| 160 | |
| 161 | CANStatus RobotController::GetCANStatus() { |
| 162 | int32_t status = 0; |
| 163 | float percentBusUtilization = 0; |
| 164 | uint32_t busOffCount = 0; |
| 165 | uint32_t txFullCount = 0; |
| 166 | uint32_t receiveErrorCount = 0; |
| 167 | uint32_t transmitErrorCount = 0; |
| 168 | HAL_CAN_GetCANStatus(&percentBusUtilization, &busOffCount, &txFullCount, |
| 169 | &receiveErrorCount, &transmitErrorCount, &status); |
| 170 | if (status != 0) { |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 171 | wpi_setGlobalHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 172 | return {}; |
| 173 | } |
| 174 | return {percentBusUtilization, static_cast<int>(busOffCount), |
| 175 | static_cast<int>(txFullCount), static_cast<int>(receiveErrorCount), |
| 176 | static_cast<int>(transmitErrorCount)}; |
| 177 | } |