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