mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-19 00:41:41 +00:00
[Examples] Limit minimum battery voltage in sim to 0.1V (#1600)
Occasionally, the sim projects are capable of simulating current draw of over 600A, which triggers a condition in `BatterySim::calculateDefaultBatteryLoadedVoltage` that limits the minimum measured battery voltage to 0V (to prevent it from going negative). When battery voltage measures 0, this causes NaN values to propagate through the drivetrain model, making sim inoperable. Specifically, [this is the line](https://github.com/PhotonVision/photonvision/blob/master/photonlib-java-examples/aimandrange/src/main/java/frc/robot/subsystems/drivetrain/SwerveDriveSim.java#L452) that causes the initial NaN values in simulation. This PR is posed as a patch to ensure that simulation doesn't break.
This commit is contained in:
@@ -127,8 +127,12 @@ public class Robot extends TimedRobot {
|
||||
gpLauncher.simulationPeriodic();
|
||||
|
||||
// Calculate battery voltage sag due to current draw
|
||||
RoboRioSim.setVInVoltage(
|
||||
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw()));
|
||||
var batteryVoltage =
|
||||
BatterySim.calculateDefaultBatteryLoadedVoltage(drivetrain.getCurrentDraw());
|
||||
|
||||
// Using max(0.1, voltage) here isn't a *physically correct* solution,
|
||||
// but it avoids problems with battery voltage measuring 0.
|
||||
RoboRioSim.setVInVoltage(Math.max(0.1, batteryVoltage));
|
||||
}
|
||||
|
||||
public void resetPose() {
|
||||
|
||||
Reference in New Issue
Block a user