Skip to content

Commit

Permalink
[wpilib] Clamp sim battery voltage to 0 (wpilibsuite#7325)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacey-sooty authored Nov 3, 2024
1 parent 23e71e1 commit debb521
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions wpilibc/src/main/native/include/frc/simulation/BatterySim.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <algorithm>
#include <numeric>
#include <span>

Expand Down Expand Up @@ -33,8 +34,9 @@ class BatterySim {
static units::volt_t Calculate(units::volt_t nominalVoltage,
units::ohm_t resistance,
std::span<const units::ampere_t> currents) {
return nominalVoltage -
std::accumulate(currents.begin(), currents.end(), 0_A) * resistance;
return std::max(0_V, nominalVoltage - std::accumulate(currents.begin(),
currents.end(), 0_A) *
resistance);
}

/**
Expand All @@ -52,8 +54,9 @@ class BatterySim {
static units::volt_t Calculate(
units::volt_t nominalVoltage, units::ohm_t resistance,
std::initializer_list<units::ampere_t> currents) {
return nominalVoltage -
std::accumulate(currents.begin(), currents.end(), 0_A) * resistance;
return std::max(0_V, nominalVoltage - std::accumulate(currents.begin(),
currents.end(), 0_A) *
resistance);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static double calculateLoadedBatteryVoltage(
for (var current : currents) {
retval -= current * resistanceOhms;
}
return retval;
return Math.max(0.0, retval);
}

/**
Expand Down

0 comments on commit debb521

Please sign in to comment.