From a2fa5e3ff700e47f6905fed50e0dc908c0b7f93e Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Mon, 14 Mar 2022 13:09:55 -0400 Subject: [PATCH] [wpilibc] BatterySim: Provide non-initializer list versions of Calculate (#4076) --- .../include/frc/simulation/BatterySim.h | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/wpilibc/src/main/native/include/frc/simulation/BatterySim.h b/wpilibc/src/main/native/include/frc/simulation/BatterySim.h index 4a350cfd01..90523436c2 100644 --- a/wpilibc/src/main/native/include/frc/simulation/BatterySim.h +++ b/wpilibc/src/main/native/include/frc/simulation/BatterySim.h @@ -9,6 +9,7 @@ #include #include #include +#include namespace frc::sim { @@ -17,6 +18,25 @@ namespace frc::sim { */ class BatterySim { public: + /** + * Calculate the loaded battery voltage. Use this with + * RoboRioSim::SetVInVoltage(double) to set the simulated battery voltage, + * which can then be retrieved with the RobotController::GetBatteryVoltage() + * method. + * + * @param nominalVoltage The nominal battery voltage. Usually 12v. + * @param resistance The forward resistance of the battery. Most batteries + * are at or below 20 milliohms. + * @param currents The currents drawn from the battery. + * @return The battery's voltage under load. + */ + static units::volt_t Calculate(units::volt_t nominalVoltage, + units::ohm_t resistance, + wpi::span currents) { + return nominalVoltage - + std::accumulate(currents.begin(), currents.end(), 0_A) * resistance; + } + /** * Calculate the loaded battery voltage. Use this with * RoboRioSim::SetVInVoltage(double) to set the simulated battery voltage, @@ -36,6 +56,20 @@ class BatterySim { std::accumulate(currents.begin(), currents.end(), 0_A) * resistance; } + /** + * Calculate the loaded battery voltage. Use this with + * RoboRioSimSetVInVoltage(double) to set the simulated battery voltage, which + * can then be retrieved with the RobotController::GetBatteryVoltage() method. + * This function assumes a nominal voltage of 12V and a resistance of 20 + * milliohms (0.020 ohms). + * + * @param currents The currents drawn from the battery. + * @return The battery's voltage under load. + */ + static units::volt_t Calculate(wpi::span currents) { + return Calculate(12_V, 0.02_Ohm, currents); + } + /** * Calculate the loaded battery voltage. Use this with * RoboRioSimSetVInVoltage(double) to set the simulated battery voltage, which