2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2017-12-10 21:52:49 -08:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.system;
|
2017-12-10 21:52:49 -08:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import static org.wpilib.units.Units.Amps;
|
|
|
|
|
import static org.wpilib.units.Units.Celsius;
|
|
|
|
|
import static org.wpilib.units.Units.Microseconds;
|
|
|
|
|
import static org.wpilib.units.Units.Volts;
|
2024-10-28 02:41:15 -04:00
|
|
|
|
2025-11-07 19:57:21 -05:00
|
|
|
import java.util.function.LongSupplier;
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.hardware.hal.HAL;
|
|
|
|
|
import org.wpilib.hardware.hal.HALUtil;
|
|
|
|
|
import org.wpilib.hardware.hal.PowerJNI;
|
|
|
|
|
import org.wpilib.hardware.hal.can.CANJNI;
|
|
|
|
|
import org.wpilib.hardware.hal.can.CANStatus;
|
|
|
|
|
import org.wpilib.units.measure.Current;
|
|
|
|
|
import org.wpilib.units.measure.Temperature;
|
|
|
|
|
import org.wpilib.units.measure.Time;
|
|
|
|
|
import org.wpilib.units.measure.Voltage;
|
2017-12-10 21:52:49 -08:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Contains functions for roboRIO functionality. */
|
2017-12-10 21:52:49 -08:00
|
|
|
public final class RobotController {
|
2024-11-16 10:43:38 -05:00
|
|
|
private static LongSupplier m_timeSource = RobotController::getFPGATime;
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
private RobotController() {
|
2018-05-24 20:39:15 -04:00
|
|
|
throw new UnsupportedOperationException("This is a utility class!");
|
2017-12-10 21:52:49 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-09 00:58:55 -05:00
|
|
|
/**
|
|
|
|
|
* Return the serial number of the roboRIO.
|
|
|
|
|
*
|
|
|
|
|
* @return The serial number of the roboRIO.
|
|
|
|
|
*/
|
|
|
|
|
public static String getSerialNumber() {
|
|
|
|
|
return HALUtil.getSerialNumber();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 14:39:51 -05:00
|
|
|
/**
|
|
|
|
|
* Return the comments from the roboRIO web interface.
|
|
|
|
|
*
|
2022-12-30 07:15:37 -05:00
|
|
|
* <p>The comments string is cached after the first call to this function on the RoboRIO - restart
|
|
|
|
|
* the robot code to reload the comments string after changing it in the web interface.
|
|
|
|
|
*
|
2022-12-26 14:39:51 -05:00
|
|
|
* @return the comments from the roboRIO web interface.
|
|
|
|
|
*/
|
|
|
|
|
public static String getComments() {
|
|
|
|
|
return HALUtil.getComments();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-02 02:34:18 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the team number configured for the robot controller.
|
|
|
|
|
*
|
|
|
|
|
* @return team number, or 0 if not found.
|
|
|
|
|
*/
|
|
|
|
|
public static int getTeamNumber() {
|
|
|
|
|
return HALUtil.getTeamNumber();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 10:43:38 -05:00
|
|
|
/**
|
|
|
|
|
* Sets a new source to provide the clock time in microseconds. Changing this affects the return
|
|
|
|
|
* value of {@code getTime} in Java.
|
|
|
|
|
*
|
|
|
|
|
* @param supplier Function to return the time in microseconds.
|
|
|
|
|
*/
|
|
|
|
|
public static void setTimeSource(LongSupplier supplier) {
|
|
|
|
|
m_timeSource = supplier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the microsecond timestamp. By default, the time is based on the FPGA hardware clock in
|
|
|
|
|
* microseconds since the FPGA started. However, the return value of this method may be modified
|
|
|
|
|
* to use any time base, including non-monotonic and non-continuous time bases.
|
|
|
|
|
*
|
|
|
|
|
* @return The current time in microseconds.
|
|
|
|
|
*/
|
|
|
|
|
public static long getTime() {
|
|
|
|
|
return m_timeSource.getAsLong();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the microsecond timestamp. By default, the time is based on the FPGA hardware clock in
|
|
|
|
|
* microseconds since the FPGA started. However, the return value of this method may be modified
|
|
|
|
|
* to use any time base, including non-monotonic and non-continuous time bases.
|
|
|
|
|
*
|
|
|
|
|
* @return The current time in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Time getMeasureTime() {
|
|
|
|
|
return Microseconds.of(m_timeSource.getAsLong());
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Read the microsecond timer from the FPGA.
|
|
|
|
|
*
|
|
|
|
|
* @return The current time in microseconds according to the FPGA.
|
|
|
|
|
*/
|
|
|
|
|
public static long getFPGATime() {
|
|
|
|
|
return HALUtil.getFPGATime();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Read the microsecond timer in a measure from the FPGA.
|
|
|
|
|
*
|
|
|
|
|
* @return The current time according to the FPGA in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Time getMeasureFPGATime() {
|
2024-11-08 19:29:51 -07:00
|
|
|
return Microseconds.of(HALUtil.getFPGATime());
|
2024-10-28 02:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Read the battery voltage.
|
|
|
|
|
*
|
|
|
|
|
* @return The battery voltage in Volts.
|
|
|
|
|
*/
|
2018-01-04 23:12:13 -06:00
|
|
|
public static double getBatteryVoltage() {
|
2017-12-10 21:52:49 -08:00
|
|
|
return PowerJNI.getVinVoltage();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Read the battery voltage in a measure.
|
|
|
|
|
*
|
|
|
|
|
* @return The battery voltage in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Voltage getMeasureBatteryVoltage() {
|
2024-11-08 19:29:51 -07:00
|
|
|
return Volts.of(PowerJNI.getVinVoltage());
|
2024-10-28 02:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Gets a value indicating whether the FPGA outputs are enabled. The outputs may be disabled if
|
|
|
|
|
* the robot is disabled or e-stopped, the watchdog has expired, or if the roboRIO browns out.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the FPGA outputs are enabled.
|
|
|
|
|
*/
|
2017-12-11 11:48:54 -08:00
|
|
|
public static boolean isSysActive() {
|
2017-12-10 21:52:49 -08:00
|
|
|
return HAL.getSystemActive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the system is browned out.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the system is browned out
|
|
|
|
|
*/
|
2017-12-11 11:48:54 -08:00
|
|
|
public static boolean isBrownedOut() {
|
2017-12-10 21:52:49 -08:00
|
|
|
return HAL.getBrownedOut();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 02:25:02 -04:00
|
|
|
/**
|
|
|
|
|
* Gets the number of times the system has been disabled due to communication errors with the
|
|
|
|
|
* Driver Station.
|
|
|
|
|
*
|
|
|
|
|
* @return number of disables due to communication errors.
|
|
|
|
|
*/
|
|
|
|
|
public static int getCommsDisableCount() {
|
|
|
|
|
return HAL.getCommsDisableCount();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 00:30:19 -04:00
|
|
|
/**
|
|
|
|
|
* Gets the current state of the Robot Signal Light (RSL).
|
|
|
|
|
*
|
|
|
|
|
* @return The current state of the RSL- true if on, false if off
|
|
|
|
|
*/
|
|
|
|
|
public static boolean getRSLState() {
|
|
|
|
|
return HAL.getRSLState();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-30 09:22:51 -07:00
|
|
|
/**
|
|
|
|
|
* Gets if the system time is valid.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the system time is valid, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isSystemTimeValid() {
|
|
|
|
|
return HAL.getSystemTimeValid();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Get the input voltage to the robot controller.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller input voltage value in Volts
|
|
|
|
|
*/
|
|
|
|
|
public static double getInputVoltage() {
|
|
|
|
|
return PowerJNI.getVinVoltage();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Get the input voltage to the robot controller in a measure.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller input voltage value in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Voltage getMeasureInputVoltage() {
|
2024-11-08 19:29:51 -07:00
|
|
|
return Volts.of(PowerJNI.getVinVoltage());
|
2024-10-28 02:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Get the voltage of the 3.3V rail.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller 3.3V rail voltage value in Volts
|
|
|
|
|
*/
|
|
|
|
|
public static double getVoltage3V3() {
|
|
|
|
|
return PowerJNI.getUserVoltage3V3();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Get the voltage in a measure of the 3.3V rail.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller 3.3V rail voltage value in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Voltage getMeasureVoltage3V3() {
|
2024-11-08 19:29:51 -07:00
|
|
|
return Volts.of(PowerJNI.getUserVoltage3V3());
|
2024-10-28 02:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Get the current output of the 3.3V rail.
|
|
|
|
|
*
|
2021-08-05 18:04:20 -07:00
|
|
|
* @return The controller 3.3V rail output current value in Amps
|
2017-12-10 21:52:49 -08:00
|
|
|
*/
|
|
|
|
|
public static double getCurrent3V3() {
|
|
|
|
|
return PowerJNI.getUserCurrent3V3();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Get the current output in a measure of the 3.3V rail.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller 3.3V rail output current value in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Current getMeasureCurrent3V3() {
|
2024-11-08 19:29:51 -07:00
|
|
|
return Amps.of(PowerJNI.getUserCurrent3V3());
|
2024-10-28 02:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-04 02:48:29 -04:00
|
|
|
/**
|
|
|
|
|
* Enables or disables the 3.3V rail.
|
|
|
|
|
*
|
|
|
|
|
* @param enabled whether to enable the 3.3V rail.
|
|
|
|
|
*/
|
|
|
|
|
public static void setEnabled3V3(boolean enabled) {
|
|
|
|
|
PowerJNI.setUserEnabled3V3(enabled);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Get the enabled state of the 3.3V rail. The rail may be disabled due to a controller brownout,
|
|
|
|
|
* a short circuit on the rail, or controller over-voltage.
|
|
|
|
|
*
|
|
|
|
|
* @return The controller 3.3V rail enabled value
|
|
|
|
|
*/
|
|
|
|
|
public static boolean getEnabled3V3() {
|
|
|
|
|
return PowerJNI.getUserActive3V3();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-11 02:25:02 -04:00
|
|
|
* Get the count of the total current faults on the 3.3V rail since the code started.
|
2017-12-10 21:52:49 -08:00
|
|
|
*
|
|
|
|
|
* @return The number of faults
|
|
|
|
|
*/
|
|
|
|
|
public static int getFaultCount3V3() {
|
|
|
|
|
return PowerJNI.getUserCurrentFaults3V3();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 02:25:02 -04:00
|
|
|
/** Reset the overcurrent fault counters for all user rails to 0. */
|
|
|
|
|
public static void resetRailFaultCounts() {
|
|
|
|
|
PowerJNI.resetUserCurrentFaults();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 19:14:27 -07:00
|
|
|
/**
|
2021-10-14 07:28:21 -07:00
|
|
|
* Get the current brownout voltage setting.
|
2021-10-13 19:14:27 -07:00
|
|
|
*
|
|
|
|
|
* @return The brownout voltage
|
|
|
|
|
*/
|
|
|
|
|
public static double getBrownoutVoltage() {
|
|
|
|
|
return PowerJNI.getBrownoutVoltage();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Get the current brownout voltage setting in a measure.
|
|
|
|
|
*
|
|
|
|
|
* @return The brownout voltage in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Voltage getMeasureBrownoutVoltage() {
|
2024-11-08 19:29:51 -07:00
|
|
|
return Volts.of(PowerJNI.getBrownoutVoltage());
|
2024-10-28 02:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-13 19:14:27 -07:00
|
|
|
/**
|
2021-10-14 07:28:21 -07:00
|
|
|
* Set the voltage the roboRIO will brownout and disable all outputs.
|
2021-10-13 19:14:27 -07:00
|
|
|
*
|
|
|
|
|
* <p>Note that this only does anything on the roboRIO 2. On the roboRIO it is a no-op.
|
|
|
|
|
*
|
|
|
|
|
* @param brownoutVoltage The brownout voltage
|
|
|
|
|
*/
|
|
|
|
|
public static void setBrownoutVoltage(double brownoutVoltage) {
|
|
|
|
|
PowerJNI.setBrownoutVoltage(brownoutVoltage);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Set the voltage in a measure the roboRIO will brownout and disable all outputs.
|
|
|
|
|
*
|
|
|
|
|
* <p>Note that this only does anything on the roboRIO 2. On the roboRIO it is a no-op.
|
|
|
|
|
*
|
|
|
|
|
* @param brownoutVoltage The brownout voltage in a measure
|
|
|
|
|
*/
|
|
|
|
|
public static void setBrownoutVoltage(Voltage brownoutVoltage) {
|
|
|
|
|
PowerJNI.setBrownoutVoltage(brownoutVoltage.baseUnitMagnitude());
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 02:48:29 -04:00
|
|
|
/**
|
|
|
|
|
* Get the current CPU temperature in degrees Celsius.
|
|
|
|
|
*
|
|
|
|
|
* @return current CPU temperature in degrees Celsius
|
|
|
|
|
*/
|
|
|
|
|
public static double getCPUTemp() {
|
|
|
|
|
return PowerJNI.getCPUTemp();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 02:41:15 -04:00
|
|
|
/**
|
|
|
|
|
* Get the current CPU temperature in a measure.
|
|
|
|
|
*
|
|
|
|
|
* @return current CPU temperature in a measure.
|
|
|
|
|
*/
|
|
|
|
|
public static Temperature getMeasureCPUTemp() {
|
2024-11-08 19:29:51 -07:00
|
|
|
return Celsius.of(PowerJNI.getCPUTemp());
|
2024-10-28 02:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-10 21:52:49 -08:00
|
|
|
/**
|
|
|
|
|
* Get the current status of the CAN bus.
|
|
|
|
|
*
|
2025-02-25 19:07:01 -08:00
|
|
|
* @param busId The bus ID
|
2017-12-10 21:52:49 -08:00
|
|
|
* @return The status of the CAN bus
|
|
|
|
|
*/
|
2025-02-25 19:07:01 -08:00
|
|
|
public static CANStatus getCANStatus(int busId) {
|
2017-12-10 21:52:49 -08:00
|
|
|
CANStatus status = new CANStatus();
|
2025-02-25 19:07:01 -08:00
|
|
|
CANJNI.getCANStatus(busId, status);
|
2017-12-10 21:52:49 -08:00
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|