2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
|
|
#include <units/current.h>
|
|
|
|
|
#include <units/impedance.h>
|
|
|
|
|
#include <units/voltage.h>
|
2022-03-14 13:09:55 -04:00
|
|
|
#include <wpi/span.h>
|
2020-09-20 09:39:52 -07:00
|
|
|
|
|
|
|
|
namespace frc::sim {
|
|
|
|
|
|
2021-06-09 07:01:00 -07:00
|
|
|
/**
|
|
|
|
|
* A utility class to simulate the robot battery.
|
|
|
|
|
*/
|
2020-09-20 09:39:52 -07:00
|
|
|
class BatterySim {
|
|
|
|
|
public:
|
2022-03-14 13:09:55 -04:00
|
|
|
/**
|
|
|
|
|
* 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<const units::ampere_t> currents) {
|
|
|
|
|
return nominalVoltage -
|
|
|
|
|
std::accumulate(currents.begin(), currents.end(), 0_A) * resistance;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 09:39:52 -07:00
|
|
|
/**
|
|
|
|
|
* Calculate the loaded battery voltage. Use this with
|
2021-10-14 18:09:38 -07:00
|
|
|
* RoboRioSim::SetVInVoltage(double) to set the simulated battery voltage,
|
|
|
|
|
* which can then be retrieved with the RobotController::GetBatteryVoltage()
|
|
|
|
|
* method.
|
2020-09-20 09:39:52 -07:00
|
|
|
*
|
|
|
|
|
* @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,
|
|
|
|
|
std::initializer_list<units::ampere_t> currents) {
|
|
|
|
|
return nominalVoltage -
|
|
|
|
|
std::accumulate(currents.begin(), currents.end(), 0_A) * resistance;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:09:55 -04:00
|
|
|
/**
|
|
|
|
|
* 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<const units::ampere_t> currents) {
|
|
|
|
|
return Calculate(12_V, 0.02_Ohm, currents);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 09:39:52 -07:00
|
|
|
/**
|
|
|
|
|
* Calculate the loaded battery voltage. Use this with
|
2021-10-14 18:09:38 -07:00
|
|
|
* 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).
|
2020-09-20 09:39:52 -07:00
|
|
|
*
|
|
|
|
|
* @param currents The currents drawn from the battery.
|
|
|
|
|
* @return The battery's voltage under load.
|
|
|
|
|
*/
|
|
|
|
|
static units::volt_t Calculate(
|
|
|
|
|
std::initializer_list<units::ampere_t> currents) {
|
|
|
|
|
return Calculate(12_V, 0.02_Ohm, currents);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace frc::sim
|