001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.wpilibj; 006 007import static edu.wpi.first.units.Units.Amps; 008import static edu.wpi.first.units.Units.Celsius; 009import static edu.wpi.first.units.Units.Microseconds; 010import static edu.wpi.first.units.Units.Volts; 011 012import edu.wpi.first.hal.HAL; 013import edu.wpi.first.hal.HALUtil; 014import edu.wpi.first.hal.PowerJNI; 015import edu.wpi.first.hal.can.CANJNI; 016import edu.wpi.first.hal.can.CANStatus; 017import edu.wpi.first.units.measure.Current; 018import edu.wpi.first.units.measure.Temperature; 019import edu.wpi.first.units.measure.Time; 020import edu.wpi.first.units.measure.Voltage; 021import java.util.function.LongSupplier; 022 023/** Contains functions for roboRIO functionality. */ 024public final class RobotController { 025 private static LongSupplier m_timeSource = RobotController::getFPGATime; 026 027 private RobotController() { 028 throw new UnsupportedOperationException("This is a utility class!"); 029 } 030 031 /** 032 * Return the serial number of the roboRIO. 033 * 034 * @return The serial number of the roboRIO. 035 */ 036 public static String getSerialNumber() { 037 return HALUtil.getSerialNumber(); 038 } 039 040 /** 041 * Return the comments from the roboRIO web interface. 042 * 043 * <p>The comments string is cached after the first call to this function on the RoboRIO - restart 044 * the robot code to reload the comments string after changing it in the web interface. 045 * 046 * @return the comments from the roboRIO web interface. 047 */ 048 public static String getComments() { 049 return HALUtil.getComments(); 050 } 051 052 /** 053 * Returns the team number configured for the robot controller. 054 * 055 * @return team number, or 0 if not found. 056 */ 057 public static int getTeamNumber() { 058 return HALUtil.getTeamNumber(); 059 } 060 061 /** 062 * Sets a new source to provide the clock time in microseconds. Changing this affects the return 063 * value of {@code getTime} in Java. 064 * 065 * @param supplier Function to return the time in microseconds. 066 */ 067 public static void setTimeSource(LongSupplier supplier) { 068 m_timeSource = supplier; 069 } 070 071 /** 072 * Read the microsecond timestamp. By default, the time is based on the FPGA hardware clock in 073 * microseconds since the FPGA started. However, the return value of this method may be modified 074 * to use any time base, including non-monotonic and non-continuous time bases. 075 * 076 * @return The current time in microseconds. 077 */ 078 public static long getTime() { 079 return m_timeSource.getAsLong(); 080 } 081 082 /** 083 * Read the microsecond timestamp. By default, the time is based on the FPGA hardware clock in 084 * microseconds since the FPGA started. However, the return value of this method may be modified 085 * to use any time base, including non-monotonic and non-continuous time bases. 086 * 087 * @return The current time in a measure. 088 */ 089 public static Time getMeasureTime() { 090 return Microseconds.of(m_timeSource.getAsLong()); 091 } 092 093 /** 094 * Read the microsecond timer from the FPGA. 095 * 096 * @return The current time in microseconds according to the FPGA. 097 */ 098 public static long getFPGATime() { 099 return HALUtil.getFPGATime(); 100 } 101 102 /** 103 * Read the microsecond timer in a measure from the FPGA. 104 * 105 * @return The current time according to the FPGA in a measure. 106 */ 107 public static Time getMeasureFPGATime() { 108 return Microseconds.of(HALUtil.getFPGATime()); 109 } 110 111 /** 112 * Read the battery voltage. 113 * 114 * @return The battery voltage in Volts. 115 */ 116 public static double getBatteryVoltage() { 117 return PowerJNI.getVinVoltage(); 118 } 119 120 /** 121 * Read the battery voltage in a measure. 122 * 123 * @return The battery voltage in a measure. 124 */ 125 public static Voltage getMeasureBatteryVoltage() { 126 return Volts.of(PowerJNI.getVinVoltage()); 127 } 128 129 /** 130 * Gets a value indicating whether the FPGA outputs are enabled. The outputs may be disabled if 131 * the robot is disabled or e-stopped, the watchdog has expired, or if the roboRIO browns out. 132 * 133 * @return True if the FPGA outputs are enabled. 134 */ 135 public static boolean isSysActive() { 136 return HAL.getSystemActive(); 137 } 138 139 /** 140 * Check if the system is browned out. 141 * 142 * @return True if the system is browned out 143 */ 144 public static boolean isBrownedOut() { 145 return HAL.getBrownedOut(); 146 } 147 148 /** 149 * Gets the number of times the system has been disabled due to communication errors with the 150 * Driver Station. 151 * 152 * @return number of disables due to communication errors. 153 */ 154 public static int getCommsDisableCount() { 155 return HAL.getCommsDisableCount(); 156 } 157 158 /** 159 * Gets the current state of the Robot Signal Light (RSL). 160 * 161 * @return The current state of the RSL- true if on, false if off 162 */ 163 public static boolean getRSLState() { 164 return HAL.getRSLState(); 165 } 166 167 /** 168 * Gets if the system time is valid. 169 * 170 * @return True if the system time is valid, false otherwise 171 */ 172 public static boolean isSystemTimeValid() { 173 return HAL.getSystemTimeValid(); 174 } 175 176 /** 177 * Get the input voltage to the robot controller. 178 * 179 * @return The controller input voltage value in Volts 180 */ 181 public static double getInputVoltage() { 182 return PowerJNI.getVinVoltage(); 183 } 184 185 /** 186 * Get the input voltage to the robot controller in a measure. 187 * 188 * @return The controller input voltage value in a measure. 189 */ 190 public static Voltage getMeasureInputVoltage() { 191 return Volts.of(PowerJNI.getVinVoltage()); 192 } 193 194 /** 195 * Get the voltage of the 3.3V rail. 196 * 197 * @return The controller 3.3V rail voltage value in Volts 198 */ 199 public static double getVoltage3V3() { 200 return PowerJNI.getUserVoltage3V3(); 201 } 202 203 /** 204 * Get the voltage in a measure of the 3.3V rail. 205 * 206 * @return The controller 3.3V rail voltage value in a measure. 207 */ 208 public static Voltage getMeasureVoltage3V3() { 209 return Volts.of(PowerJNI.getUserVoltage3V3()); 210 } 211 212 /** 213 * Get the current output of the 3.3V rail. 214 * 215 * @return The controller 3.3V rail output current value in Amps 216 */ 217 public static double getCurrent3V3() { 218 return PowerJNI.getUserCurrent3V3(); 219 } 220 221 /** 222 * Get the current output in a measure of the 3.3V rail. 223 * 224 * @return The controller 3.3V rail output current value in a measure. 225 */ 226 public static Current getMeasureCurrent3V3() { 227 return Amps.of(PowerJNI.getUserCurrent3V3()); 228 } 229 230 /** 231 * Enables or disables the 3.3V rail. 232 * 233 * @param enabled whether to enable the 3.3V rail. 234 */ 235 public static void setEnabled3V3(boolean enabled) { 236 PowerJNI.setUserEnabled3V3(enabled); 237 } 238 239 /** 240 * Get the enabled state of the 3.3V rail. The rail may be disabled due to a controller brownout, 241 * a short circuit on the rail, or controller over-voltage. 242 * 243 * @return The controller 3.3V rail enabled value 244 */ 245 public static boolean getEnabled3V3() { 246 return PowerJNI.getUserActive3V3(); 247 } 248 249 /** 250 * Get the count of the total current faults on the 3.3V rail since the code started. 251 * 252 * @return The number of faults 253 */ 254 public static int getFaultCount3V3() { 255 return PowerJNI.getUserCurrentFaults3V3(); 256 } 257 258 /** Reset the overcurrent fault counters for all user rails to 0. */ 259 public static void resetRailFaultCounts() { 260 PowerJNI.resetUserCurrentFaults(); 261 } 262 263 /** 264 * Get the current brownout voltage setting. 265 * 266 * @return The brownout voltage 267 */ 268 public static double getBrownoutVoltage() { 269 return PowerJNI.getBrownoutVoltage(); 270 } 271 272 /** 273 * Get the current brownout voltage setting in a measure. 274 * 275 * @return The brownout voltage in a measure. 276 */ 277 public static Voltage getMeasureBrownoutVoltage() { 278 return Volts.of(PowerJNI.getBrownoutVoltage()); 279 } 280 281 /** 282 * Set the voltage the roboRIO will brownout and disable all outputs. 283 * 284 * <p>Note that this only does anything on the roboRIO 2. On the roboRIO it is a no-op. 285 * 286 * @param brownoutVoltage The brownout voltage 287 */ 288 public static void setBrownoutVoltage(double brownoutVoltage) { 289 PowerJNI.setBrownoutVoltage(brownoutVoltage); 290 } 291 292 /** 293 * Set the voltage in a measure the roboRIO will brownout and disable all outputs. 294 * 295 * <p>Note that this only does anything on the roboRIO 2. On the roboRIO it is a no-op. 296 * 297 * @param brownoutVoltage The brownout voltage in a measure 298 */ 299 public static void setBrownoutVoltage(Voltage brownoutVoltage) { 300 PowerJNI.setBrownoutVoltage(brownoutVoltage.baseUnitMagnitude()); 301 } 302 303 /** 304 * Get the current CPU temperature in degrees Celsius. 305 * 306 * @return current CPU temperature in degrees Celsius 307 */ 308 public static double getCPUTemp() { 309 return PowerJNI.getCPUTemp(); 310 } 311 312 /** 313 * Get the current CPU temperature in a measure. 314 * 315 * @return current CPU temperature in a measure. 316 */ 317 public static Temperature getMeasureCPUTemp() { 318 return Celsius.of(PowerJNI.getCPUTemp()); 319 } 320 321 /** 322 * Get the current status of the CAN bus. 323 * 324 * @param busId The bus ID 325 * @return The status of the CAN bus 326 */ 327 public static CANStatus getCANStatus(int busId) { 328 CANStatus status = new CANStatus(); 329 CANJNI.getCANStatus(busId, status); 330 return status; 331 } 332}