For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.

Robot.java (fragment)
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));
}
cpp/Robot.cpp (fragment)
#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});
}
Two graphs of the default discharge model. Resting voltage against state of charge falls slowly from 12.9 volts at full to 12.0 volts at 20 percent, then steeply to 9 volts when empty. Usable capacity against sustained current falls from 100 percent at 0.9 amps to 60 percent at 54 amps. Internal resistance rises from 20 milliohms when full to 60 milliohms when empty
The default discharge curves.

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.

Six sketches of current over time. Arm: spikes when it starts and stops, then a steady draw to hold against gravity. Elevator: largest going up, steady to hold a height. Flywheel, shooter, rollers: very large while spinning up, small at speed, a bump on every shot. Pivot, turret, wrist: short bursts when starting and stopping. Swerve drive: eight motors, usually the largest draw. All together: the draws add up and overlapping peaks cause the deepest sag
When each kind of mechanism draws current.

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(...).

Cover the whole range of charge, from 0 to 1. Outside the points you give, the voltage stays at the nearest point's, so a curve missing the low or high end will not sag realistically there. See Custom discharge curves for choosing the points.

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

  1. Publish the state of charge and the battery voltage as in Read the state of charge for telemetry, start the simulation, and graph Battery/StateOfCharge and Battery/Voltage in AdvantageScope.

  2. Run your mechanisms hard for a minute or two: spin up a shooter, drive the swerve back and forth.

  3. Battery/StateOfCharge should start at 1 and fall steadily while mechanisms run, and stay put while they are idle. Battery/Voltage should 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.

Last updated

Was this helpful?