How do I enable battery discharge simulation?
Goal: make the simulated battery run down over a match, so the voltage sags harder near the end than at the start, as a real battery does.
By default the simulated battery sags while your mechanisms draw current, but it never runs down: every moment of the run starts from a full 12 V battery. Discharge simulation adds the battery's state of charge, how full it is from 1 (full) to 0 (empty). As your mechanisms draw current, the charge goes down. A battery that is less full rests at a lower voltage, and its internal resistance, which makes the voltage sag under load, goes up. So late in a long simulated run, the same load sags the voltage further.
For why YAMS models one shared battery at all, see Battery Simulation.
Turn on discharge modeling
Call enableDischarge(...) once, early on, with the battery's capacity in amp-hours (how much charge it holds), its voltage when full, and its internal resistance when full. The Robot constructor is a good place for it. A fresh 18 Ah competition battery is a reasonable default if you have no measured numbers for your own.
import static org.wpilib.units.Units.MilliOhms;
import static org.wpilib.units.Units.Volts;
import yams.core.motorcontrollers.simulation.BatterySim;
public Robot() {
// 18 Ah, 12.9 V and 20 milliohms when full.
BatterySim.enableDischarge(18.0, Volts.of(12.9), MilliOhms.of(20));
}#include <yams/motorcontrollers/simulation/BatterySim.hpp>
Robot::Robot() {
// 18 Ah, 12.9 V and 20 milliohms when full.
yams::motorcontrollers::simulation::BatterySim::EnableDischarge(
18.0, wpi::units::volt_t{12.9}, wpi::units::ohm_t{0.020});
}
Make sure your mechanisms report accurate current draw
The simulated battery only runs down as fast as the current your mechanisms draw, and that comes from each mechanism's physics simulation. If a mechanism's moment of inertia is guessed rather than measured, its simulated current draw, and so how fast it drains the battery, will be off. Set a real one on each SmartMotorControllerConfig with withMomentOfInertia(...) before relying on discharge numbers: see MOI for how.

Reset between runs (optional)
The simulated battery keeps its charge from one simulated match to the next, as a real battery does. To start every simulated match or unit test from a full battery, call resetDischarge() at the start of it: in the setup of your unit tests, or wherever you reset robot state between simulated matches. disableDischarge() turns discharge modeling back off.
Read the state of charge for telemetry (optional)
getStateOfCharge() returns the charge from 0 (empty) to 1 (full). Publish it every loop to watch the battery run down over a simulated match. YAMS sets the simulated battery voltage but does not publish it, so publish that too: it is what RobotController reports, in simulation as on the robot.
Replace the state-of-charge curve (optional)
enableDischarge(...) lowers the resting voltage along a curve for a typical FRC sealed lead-acid battery: flat through most of the charge, then dropping quickly near empty. To model a different battery, such as a well-used one that sags earlier, or one you measured with a load tester, call replaceSOCInterpolation(...) with your own curve of state of charge to voltage, before enableDischarge(...).
Replace the capacity derating curve (optional)
A lead-acid battery gives fewer amp-hours the faster it is drained (the Peukert effect). enableDischarge(...) accounts for this with a curve of sustained current to the fraction of the capacity you get, averaged from real FRC battery testing. If you measured your own battery, call replaceCapacityDerating(...) with your curve, before enableDischarge(...).
See Capacity derating for where the default curve comes from.
Check it
Publish the state of charge and the battery voltage as in Read the state of charge for telemetry, start the simulation, and graph
Battery/StateOfChargeandBattery/Voltagein AdvantageScope.Run your mechanisms hard for a minute or two: spin up a shooter, drive the swerve back and forth.
Battery/StateOfChargeshould start at 1 and fall steadily while mechanisms run, and stay put while they are idle.Battery/Voltageshould sag more under the same load late in the run than at the start, and its resting value should creep down.
If the state of charge never moves, enableDischarge(...) was not called. If it barely moves, check the moments of inertia: see Make sure your mechanisms report accurate current draw.
Related pages
Battery Simulation, the full explanation of the shared battery model and why it matters
MOI, accurate current draw starts with an accurate moment of inertia
Last updated
Was this helpful?
