25 #include <ns3/assert.h>
26 #include <ns3/double.h>
28 #include <ns3/simulator.h>
29 #include <ns3/trace-source-accessor.h>
44 TypeId(
"ns3::GenericBatteryModel")
46 .SetGroupName(
"Energy")
48 .AddAttribute(
"LowBatteryThreshold",
49 "Low battery threshold for generic battery model.",
52 MakeDoubleChecker<double>())
53 .AddAttribute(
"FullVoltage",
54 "(Q_full) The voltage of the cell when fully charged (V).",
57 MakeDoubleChecker<double>())
58 .AddAttribute(
"MaxCapacity",
59 "(Q) The maximum capacity of the cell (Ah).",
62 MakeDoubleChecker<double>())
63 .AddAttribute(
"NominalVoltage",
64 "(V_nom) Nominal voltage of the cell (V).",
67 MakeDoubleChecker<double>())
68 .AddAttribute(
"NominalCapacity",
69 "(Q_nom) Cell capacity at the end of the nominal zone (Ah)",
72 MakeDoubleChecker<double>())
73 .AddAttribute(
"ExponentialVoltage",
74 "(V_exp) Cell voltage at the end of the exponential zone (V).",
77 MakeDoubleChecker<double>())
78 .AddAttribute(
"ExponentialCapacity",
79 "(Q_exp) Cell Capacity at the end of the exponential zone (Ah).",
82 MakeDoubleChecker<double>())
83 .AddAttribute(
"InternalResistance",
84 "(R) Internal resistance of the cell (Ohms)",
87 MakeDoubleChecker<double>())
88 .AddAttribute(
"TypicalDischargeCurrent",
89 "Typical discharge current used in manufacters datasheets (A)",
92 MakeDoubleChecker<double>())
93 .AddAttribute(
"CutoffVoltage",
94 "The voltage where the battery is considered depleted (V).",
97 MakeDoubleChecker<double>())
98 .AddAttribute(
"PeriodicEnergyUpdateInterval",
99 "Time between two consecutive periodic energy updates.",
104 .AddAttribute(
"BatteryType",
105 "Indicates the battery type used by the model",
114 .AddTraceSource(
"RemainingEnergy",
115 "Remaining energy of generic battery",
117 "ns3::TracedValueCallback::Double");
122 : m_drainedCapacity(0),
123 m_currentFiltered(0),
140 return initialEnergy;
277 double responseTime = (
Simulator::Now() / batteryResponseConstant).GetDouble();
287 if (totalCurrentA < 0)
313 double expZoneFull = A * std::exp(-B *
m_qNom);
320 double polResistance = 0;
321 double polVoltage = 0;
342 double entnPrime =
m_entn;
344 m_entn = B * std::abs(i) * (-expZonePrime + A);
349 polResistance = K *
m_qMax / (std::abs(it) + 0.1 *
m_qMax);
366 <<
"| E0 " << E0 <<
" | polRes " << polResistance <<
" | polVol "
367 << polVoltage <<
"| B " << B <<
" | ExpZone " <<
m_expZone
392 double expZoneFull = A * std::exp(-B *
m_qNom);
400 double polVoltage = polResistance;
415 double entnPrime =
m_entn;
418 m_entn = B * std::abs(i) * (-expZonePrime);
429 <<
" | A " << A <<
" | B " << B <<
" | ExpZone " <<
m_expZone
433 <<
" | K " << K <<
" | E0 " << E0);
This class can be used to hold variables of floating point type such as 'double' or 'float'.
double CalculateTotalCurrent()
void NotifyEnergyRecharged()
This function notifies all DeviceEnergyModel of energy recharged event.
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.
Hold variables of type enum.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
A generic battery model for Li-Ion, NiCd, NiMh and Lead acid batteries.
double m_entn
The previous value of the exponential zone in NiMh,NiCd and LeadAcid.
double m_expZone
Voltage value of the exponential zone.
void BatteryChargedEvent()
Handles the battery reaching its full voltage.
double GetEnergyFraction() override
Implements GetEnergyFraction.
Time m_lastUpdateTime
Last update time.
double m_lowBatteryTh
Low battery threshold, as a fraction of the initial energy.
double m_typicalCurrent
Typical discharge current used to fit the curves.
double GetStateOfCharge() const
Calculates an estimate of the State of Charge (SoC).
double GetDrainedCapacity() const
Obtain the amount of drained capacity from the battery based on the integral of the current over time...
Time m_energyUpdateInterval
Energy update interval.
void UpdateEnergySource() override
Implements UpdateEnergySource.
double m_currentFiltered
The step response (a.k.a.
double m_qMax
The maximum capacity of the battery, in Ah.
void CalculateRemainingEnergy()
Calculates remaining energy.
double GetVoltage(double current)
Get the battery voltage in function of the discharge current.
void DoInitialize() override
Initialize() implementation.
double m_vExp
Battery voltage at the end of the exponential zone, in Volts.
TracedValue< double > m_remainingEnergyJ
Remaining energy, in Joules.
~GenericBatteryModel() override
double GetSupplyVoltage() const override
Implements GetSupplyVoltage.
Time GetEnergyUpdateInterval() const
double m_vNom
Nominal voltage of the battery, in Volts.
EventId m_energyUpdateEvent
Energy update event.
double GetChargeVoltage(double current)
Obtain the battery voltage as a result of a charge current.
double GetRemainingEnergy() override
Implements GetRemainingEnergy.
double m_internalResistance
Internal resistance of the battery, in Ohms.
void SetEnergyUpdateInterval(Time interval)
This function sets the interval between each energy update.
void DoDispose() override
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
GenericBatteryType m_batteryType
Indicates the battery type used by the model.
double m_qNom
Battery capacity at the end of the nominal zone, in Ah.
static TypeId GetTypeId()
Get the type ID.
double m_vFull
Initial voltage of the battery, in Volts.
double m_drainedCapacity
Capacity drained from the battery, in Ah.
Time m_energyUpdateLapseTime
The lapse of time between the last battery energy update and the current time.
double m_cutoffVoltage
The threshold voltage where the battery is considered depleted.
void SetDrainedCapacity(double drainedCapacity)
This function is used to change the initial capacity in the battery.
double m_qExp
Capacity value at the end of the exponential zone, in Ah.
void BatteryDepletedEvent()
Handles the battery reaching its cutoff voltage.
double GetInitialEnergy() const override
Implements GetInitialEnergy.
double m_supplyVoltageV
Actual voltage of the battery.
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 SetParent(TypeId tid)
Set the parent TypeId.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
@ NIMH_NICD
Nickel-metal hydride and Nickel cadmium batteries.
@ LION_LIPO
Lithium-ion and Lithium-polymer batteries.
@ LEADACID
Lead Acid 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 > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)