14#include <ns3/assert.h>
15#include <ns3/double.h>
17#include <ns3/simulator.h>
18#include <ns3/trace-source-accessor.h>
34 TypeId(
"ns3::energy::GenericBatteryModel")
37 .SetGroupName(
"Energy")
39 .AddAttribute(
"LowBatteryThreshold",
40 "Low battery threshold for generic battery model.",
44 .AddAttribute(
"FullVoltage",
45 "(Q_full) The voltage of the cell when fully charged (V).",
49 .AddAttribute(
"MaxCapacity",
50 "(Q) The maximum capacity of the cell (Ah).",
54 .AddAttribute(
"NominalVoltage",
55 "(V_nom) Nominal voltage of the cell (V).",
59 .AddAttribute(
"NominalCapacity",
60 "(Q_nom) Cell capacity at the end of the nominal zone (Ah)",
64 .AddAttribute(
"ExponentialVoltage",
65 "(V_exp) Cell voltage at the end of the exponential zone (V).",
69 .AddAttribute(
"ExponentialCapacity",
70 "(Q_exp) Cell Capacity at the end of the exponential zone (Ah).",
74 .AddAttribute(
"InternalResistance",
75 "(R) Internal resistance of the cell (Ohms)",
79 .AddAttribute(
"TypicalDischargeCurrent",
80 "Typical discharge current used in manufacturers datasheets (A)",
84 .AddAttribute(
"CutoffVoltage",
85 "The voltage where the battery is considered depleted (V).",
89 .AddAttribute(
"PeriodicEnergyUpdateInterval",
90 "Time between two consecutive periodic energy updates.",
95 .AddAttribute(
"BatteryType",
96 "Indicates the battery type used by the model",
105 .AddTraceSource(
"RemainingEnergy",
106 "Remaining energy of generic battery",
108 "ns3::TracedValueCallback::Double");
113 : m_drainedCapacity(0),
114 m_currentFiltered(0),
131 return initialEnergy;
268 double responseTime = (
Simulator::Now() / batteryResponseConstant).GetDouble();
278 if (totalCurrentA < 0)
304 double expZoneFull = A * std::exp(-B *
m_qNom);
311 double polResistance = 0;
312 double polVoltage = 0;
333 double entnPrime =
m_entn;
335 m_entn = B * std::abs(i) * (-expZonePrime + A);
340 polResistance = K *
m_qMax / (std::abs(it) + 0.1 *
m_qMax);
357 <<
"| E0 " << E0 <<
" | polRes " << polResistance <<
" | polVol "
358 << polVoltage <<
"| B " << B <<
" | ExpZone " <<
m_expZone
383 double expZoneFull = A * std::exp(-B *
m_qNom);
391 double polVoltage = polResistance;
406 double entnPrime =
m_entn;
409 m_entn = B * std::abs(i) * (-expZonePrime);
420 <<
" | A " << A <<
" | B " << B <<
" | ExpZone " <<
m_expZone
424 <<
" | K " << K <<
" | E0 " << E0);
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Hold variables of type enum.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static bool IsFinished()
Check if the simulation should finish.
static Time Now()
Return the current simulation virtual time.
Simulation virtual time values and global simulation resolution.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
a unique identifier for an interface.
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Energy source base class.
void BreakDeviceEnergyModelRefCycle()
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
void NotifyEnergyDrained()
This function notifies all DeviceEnergyModel of energy depletion event.
double CalculateTotalCurrent()
void NotifyEnergyRecharged()
This function notifies all DeviceEnergyModel of energy recharged event.
A generic battery model for Li-Ion, NiCd, NiMh and Lead acid batteries.
void DoDispose() override
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
double m_expZone
Voltage value of the exponential zone.
double m_cutoffVoltage
The threshold voltage where the battery is considered depleted.
double GetChargeVoltage(double current)
Obtain the battery voltage as a result of a charge current.
static TypeId GetTypeId()
Get the type ID.
void BatteryChargedEvent()
Handles the battery reaching its full voltage.
double GetEnergyFraction() override
Implements GetEnergyFraction.
EventId m_energyUpdateEvent
Energy update event.
double m_lowBatteryTh
Low battery threshold, as a fraction of the initial energy.
double m_currentFiltered
The step response (a.k.a.
Time m_energyUpdateLapseTime
The lapse of time between the last battery energy update and the current time.
~GenericBatteryModel() override
double m_qMax
The maximum capacity of the battery, in Ah.
double m_supplyVoltageV
Actual voltage of the battery.
double m_qNom
Battery capacity at the end of the nominal zone, in Ah.
double m_drainedCapacity
Capacity drained from the battery, in Ah.
Time m_lastUpdateTime
Last update time.
double m_vFull
Initial voltage of the battery, in Volts.
double m_entn
The previous value of the exponential zone in NiMh,NiCd and LeadAcid.
double m_qExp
Capacity value at the end of the exponential zone, in Ah.
void SetDrainedCapacity(double drainedCapacity)
This function is used to change the initial capacity in the battery.
double GetSupplyVoltage() const override
Implements GetSupplyVoltage.
TracedValue< double > m_remainingEnergyJ
Remaining energy, in Joules.
double GetRemainingEnergy() override
Implements GetRemainingEnergy.
void BatteryDepletedEvent()
Handles the battery reaching its cutoff voltage.
double GetVoltage(double current)
Get the battery voltage in function of the discharge current.
double m_typicalCurrent
Typical discharge current used to fit the curves.
GenericBatteryType m_batteryType
Indicates the battery type used by the model.
void DoInitialize() override
Initialize() implementation.
Time GetEnergyUpdateInterval() const
double GetStateOfCharge() const
Calculates an estimate of the State of Charge (SoC).
double GetInitialEnergy() const override
Implements GetInitialEnergy.
double m_vNom
Nominal voltage of the battery, in Volts.
double GetDrainedCapacity() const
Obtain the amount of drained capacity from the battery based on the integral of the current over time...
void CalculateRemainingEnergy()
Calculates remaining energy.
void UpdateEnergySource() override
Implements UpdateEnergySource.
double m_vExp
Battery voltage at the end of the exponential zone, in Volts.
void SetEnergyUpdateInterval(Time interval)
This function sets the interval between each energy update.
double m_internalResistance
Internal resistance of the battery, in Ohms.
Time m_energyUpdateInterval
Energy update interval.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
@ LEADACID
Lead Acid Batteries.
@ NIMH_NICD
Nickel-metal hydride and Nickel cadmium batteries.
@ LION_LIPO
Lithium-ion and Lithium-polymer batteries.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Seconds(double value)
Construct a Time in the indicated unit.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Ptr< const AttributeChecker > MakeDoubleChecker()
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.