Class BatterySim
java.lang.Object
yams.core.motorcontrollers.simulation.BatterySim
Battery Simulation that takes the entirety of the robot into account by storing all currently
used currents on the robot.
Thread safe: every method synchronizes on this class, so mechanisms can run their simulation
in their own Notifiers while sharing the one battery.
-
Method Summary
Modifier and TypeMethodDescriptionstatic doublecalculateVoltage(UUID id, double current) Calculate the voltage based on the currents used by the robot.static doublecalculateVoltage(UUID id, org.wpilib.units.measure.Current current) Calculate the voltage based on the currents used by the robot.static voidDisable battery discharge simulation, reverting to a constantbatteryVoltageandbatteryResistance.static voidenableDischarge(double batteryCapacityAmpHours, org.wpilib.units.measure.Voltage nomVoltage, org.wpilib.units.measure.Resistance nomResistance) Enable realistic battery discharge simulation.static doubleGet the simulated state of charge of the battery, from 0 (empty) to 1 (full).static voidremoveCurrent(UUID id) Stop counting the given simulation's current draw towards the shared battery load, e.g.static voidreplaceCapacityDerating(org.wpilib.math.interpolation.InterpolatingDoubleTreeMap currentToCapacityFraction) Replace the default discharge current → capacity fraction interpolation table used to derate the battery's usable amp-hour capacity at high discharge rates (the Peukert effect).static voidreplaceSOCInterpolation(org.wpilib.math.interpolation.InterpolatingDoubleTreeMap socToVoltage) Replace the default state-of-charge → open circuit voltage interpolation table used when discharge simulation is enabled withenableDischarge(double, Voltage, Resistance).static voidreset()Clear every tracked per-consumer current draw and reset discharge simulation to a fresh, fully-charged, disabled state.static voidReset the simulated battery back to a full charge.
-
Method Details
-
replaceSOCInterpolation
public static void replaceSOCInterpolation(org.wpilib.math.interpolation.InterpolatingDoubleTreeMap socToVoltage) Replace the default state-of-charge → open circuit voltage interpolation table used when discharge simulation is enabled withenableDischarge(double, Voltage, Resistance).Not every battery discharges like YAMS's built-in sealed lead-acid curve. Reach for this method when you want to model something different, for example:
- A well-used competition battery that sags earlier and harder than a fresh one.
- Matching a curve you measured from an actual battery on a load tester, for the most accurate brownout predictions possible.
// Model a well-used competition battery that sags earlier and more severely than a new one. InterpolatingDoubleTreeMap wornBatteryCurve = new InterpolatingDoubleTreeMap(); wornBatteryCurve.put(0.00, 8.0); wornBatteryCurve.put(0.05, 9.5); wornBatteryCurve.put(0.10, 10.5); wornBatteryCurve.put(0.20, 11.2); wornBatteryCurve.put(0.40, 11.6); wornBatteryCurve.put(0.60, 11.9); wornBatteryCurve.put(0.80, 12.2); wornBatteryCurve.put(0.90, 12.4); wornBatteryCurve.put(1.00, 12.6); BatterySim.replaceSOCInterpolation(wornBatteryCurve); // Pair with a reduced usable capacity and higher resistance to match a worn battery. BatterySim.enableDischarge(15.0, Volts.of(12.6), MilliOhms.of(28));- Parameters:
socToVoltage- Interpolation table mapping state of charge[0, 1]to open circuit voltage. Call this beforeenableDischarge(double, Voltage, Resistance)so discharge simulation uses the new curve from the start.
-
replaceCapacityDerating
public static void replaceCapacityDerating(org.wpilib.math.interpolation.InterpolatingDoubleTreeMap currentToCapacityFraction) Replace the default discharge current → capacity fraction interpolation table used to derate the battery's usable amp-hour capacity at high discharge rates (the Peukert effect).Reach for this if you have measured discharge-rate-vs-capacity data for your specific battery, for example from a load tester, rather than the averaged multi-manufacturer defaults.
- Parameters:
currentToCapacityFraction- Interpolation table mapping discharge current in Amps to the fraction (0 to 1) of the nominal amp-hour capacity delivered at that current. Call this beforeenableDischarge(double, Voltage, Resistance)so discharge simulation uses the new curve from the start.
-
enableDischarge
public static void enableDischarge(double batteryCapacityAmpHours, org.wpilib.units.measure.Voltage nomVoltage, org.wpilib.units.measure.Resistance nomResistance) Enable realistic battery discharge simulation. As current is drawn from the battery over time its state of charge will drop, reducing the open circuit voltage and increasing the internal resistance to more realistically model a depleted battery.- Parameters:
batteryCapacityAmpHours- Capacity of the battery in amp-hours (Ah). A typical FRC battery is around 18 Ah.nomVoltage- Nominal (fully charged) open circuit voltage of the battery.nomResistance- Nominal internal resistance of the battery.
-
disableDischarge
public static void disableDischarge()Disable battery discharge simulation, reverting to a constantbatteryVoltageandbatteryResistance. -
resetDischarge
public static void resetDischarge()Reset the simulated battery back to a full charge. -
removeCurrent
Stop counting the given simulation's current draw towards the shared battery load, e.g. once itsSmartMotorControllerhas been closed. Without this, a closed simulation's last-known current draw would linger incurrentsindefinitely.- Parameters:
id-UUIDof the simulation to remove.
-
reset
public static void reset()Clear every tracked per-consumer current draw and reset discharge simulation to a fresh, fully-charged, disabled state. -
getStateOfCharge
public static double getStateOfCharge()Get the simulated state of charge of the battery, from 0 (empty) to 1 (full).- Returns:
- State of charge of the battery.
-
calculateVoltage
-
calculateVoltage
-