[wpilibj] RobotController: Add Java unit support (#7278)

This commit is contained in:
Brendan Raykoff
2024-10-28 02:41:15 -04:00
committed by GitHub
parent 42a433b6fa
commit 6207992709

View File

@@ -4,15 +4,42 @@
package edu.wpi.first.wpilibj;
import static edu.wpi.first.units.Units.Amps;
import static edu.wpi.first.units.Units.Celsius;
import static edu.wpi.first.units.Units.Microseconds;
import static edu.wpi.first.units.Units.Volts;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.hal.HALUtil;
import edu.wpi.first.hal.LEDJNI;
import edu.wpi.first.hal.PowerJNI;
import edu.wpi.first.hal.can.CANJNI;
import edu.wpi.first.hal.can.CANStatus;
import edu.wpi.first.units.measure.Current;
import edu.wpi.first.units.measure.MutCurrent;
import edu.wpi.first.units.measure.MutTemperature;
import edu.wpi.first.units.measure.MutTime;
import edu.wpi.first.units.measure.MutVoltage;
import edu.wpi.first.units.measure.Temperature;
import edu.wpi.first.units.measure.Time;
import edu.wpi.first.units.measure.Voltage;
/** Contains functions for roboRIO functionality. */
public final class RobotController {
// Mutable measures
private static final MutTime m_mutFPGATime = Microseconds.mutable(0.0);
private static final MutVoltage m_mutBatteryVoltage = Volts.mutable(0.0);
private static final MutVoltage m_mutInputVoltage = Volts.mutable(0.0);
private static final MutCurrent m_mutInputCurrent = Amps.mutable(0.0);
private static final MutVoltage m_mutVoltage3V3 = Volts.mutable(0.0);
private static final MutCurrent m_mutCurrent3V3 = Amps.mutable(0.0);
private static final MutVoltage m_mutVoltage5V = Volts.mutable(0.0);
private static final MutCurrent m_mutCurrent5V = Amps.mutable(0.0);
private static final MutVoltage m_mutVoltage6V = Volts.mutable(0.0);
private static final MutCurrent m_mutCurrent6V = Amps.mutable(0.0);
private static final MutVoltage m_mutBrownoutVoltage = Volts.mutable(0.0);
private static final MutTemperature m_mutCPUTemp = Celsius.mutable(0.0);
private RobotController() {
throw new UnsupportedOperationException("This is a utility class!");
}
@@ -76,6 +103,16 @@ public final class RobotController {
return HALUtil.getFPGATime();
}
/**
* 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() {
m_mutFPGATime.mut_replace(HALUtil.getFPGATime(), Microseconds);
return m_mutFPGATime;
}
/**
* Get the state of the "USER" button on the roboRIO.
*
@@ -98,6 +135,16 @@ public final class RobotController {
return PowerJNI.getVinVoltage();
}
/**
* Read the battery voltage in a measure.
*
* @return The battery voltage in a measure.
*/
public static Voltage getMeasureBatteryVoltage() {
m_mutBatteryVoltage.mut_replace(PowerJNI.getVinVoltage(), Volts);
return m_mutBatteryVoltage;
}
/**
* 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.
@@ -154,6 +201,16 @@ public final class RobotController {
return PowerJNI.getVinVoltage();
}
/**
* Get the input voltage to the robot controller in a measure.
*
* @return The controller input voltage value in a measure.
*/
public static Voltage getMeasureInputVoltage() {
m_mutInputVoltage.mut_replace(PowerJNI.getVinVoltage(), Volts);
return m_mutInputVoltage;
}
/**
* Get the input current to the robot controller.
*
@@ -163,6 +220,16 @@ public final class RobotController {
return PowerJNI.getVinCurrent();
}
/**
* Get the input current to the robot controller in a measure.
*
* @return The controller input current value in a measure.
*/
public static Current getMeasureInputCurrent() {
m_mutInputCurrent.mut_replace(PowerJNI.getVinCurrent(), Amps);
return m_mutInputCurrent;
}
/**
* Get the voltage of the 3.3V rail.
*
@@ -172,6 +239,16 @@ public final class RobotController {
return PowerJNI.getUserVoltage3V3();
}
/**
* 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() {
m_mutVoltage3V3.mut_replace(PowerJNI.getUserVoltage3V3(), Volts);
return m_mutVoltage3V3;
}
/**
* Get the current output of the 3.3V rail.
*
@@ -181,6 +258,16 @@ public final class RobotController {
return PowerJNI.getUserCurrent3V3();
}
/**
* 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() {
m_mutCurrent3V3.mut_replace(PowerJNI.getUserCurrent3V3(), Amps);
return m_mutCurrent3V3;
}
/**
* Enables or disables the 3.3V rail.
*
@@ -218,6 +305,16 @@ public final class RobotController {
return PowerJNI.getUserVoltage5V();
}
/**
* Get the voltage in a measure of the 5V rail.
*
* @return The controller 5V rail voltage value in a measure.
*/
public static Voltage getMeasureVoltage5V() {
m_mutVoltage5V.mut_replace(PowerJNI.getUserVoltage5V(), Volts);
return m_mutVoltage5V;
}
/**
* Get the current output of the 5V rail.
*
@@ -227,6 +324,16 @@ public final class RobotController {
return PowerJNI.getUserCurrent5V();
}
/**
* Get the current output in a measure of the 5V rail.
*
* @return The controller 5V rail output current value in a measure.
*/
public static Current getMeasureCurrent5V() {
m_mutCurrent5V.mut_replace(PowerJNI.getUserCurrent5V(), Amps);
return m_mutCurrent5V;
}
/**
* Enables or disables the 5V rail.
*
@@ -264,6 +371,16 @@ public final class RobotController {
return PowerJNI.getUserVoltage6V();
}
/**
* Get the voltage in a measure of the 6V rail.
*
* @return The controller 6V rail voltage value in a measure.
*/
public static Voltage getMeasureVoltage6V() {
m_mutVoltage6V.mut_replace(PowerJNI.getUserVoltage6V(), Volts);
return m_mutVoltage6V;
}
/**
* Get the current output of the 6V rail.
*
@@ -273,6 +390,16 @@ public final class RobotController {
return PowerJNI.getUserCurrent6V();
}
/**
* Get the current output in a measure of the 6V rail.
*
* @return The controller 6V rail output current value in a measure.
*/
public static Current getMeasureCurrent6V() {
m_mutCurrent6V.mut_replace(PowerJNI.getUserCurrent6V(), Amps);
return m_mutCurrent6V;
}
/**
* Enables or disables the 6V rail.
*
@@ -315,6 +442,16 @@ public final class RobotController {
return PowerJNI.getBrownoutVoltage();
}
/**
* Get the current brownout voltage setting in a measure.
*
* @return The brownout voltage in a measure.
*/
public static Voltage getMeasureBrownoutVoltage() {
m_mutBrownoutVoltage.mut_replace(PowerJNI.getBrownoutVoltage(), Volts);
return m_mutBrownoutVoltage;
}
/**
* Set the voltage the roboRIO will brownout and disable all outputs.
*
@@ -326,6 +463,17 @@ public final class RobotController {
PowerJNI.setBrownoutVoltage(brownoutVoltage);
}
/**
* 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());
}
/**
* Get the current CPU temperature in degrees Celsius.
*
@@ -335,6 +483,16 @@ public final class RobotController {
return PowerJNI.getCPUTemp();
}
/**
* Get the current CPU temperature in a measure.
*
* @return current CPU temperature in a measure.
*/
public static Temperature getMeasureCPUTemp() {
m_mutCPUTemp.mut_replace(PowerJNI.getCPUTemp(), Celsius);
return m_mutCPUTemp;
}
/** State for the radio led. */
public enum RadioLEDState {
/** Off. */