A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
generic-battery-model.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Andrea Sacco: Li-Ion battery
3 * Copyright (c) 2023 Tokushima University, Japan:
4 * NiMh,NiCd,LeaAcid batteries and preset and multi-cell extensions.
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 * Author: Andrea Sacco <andrea.sacco85@gmail.com>
9 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
10 */
11
12#ifndef GENERIC_BATTERY_MODEL_H
13#define GENERIC_BATTERY_MODEL_H
14
15#include "energy-source.h"
16
17#include "ns3/event-id.h"
18#include "ns3/nstime.h"
19#include "ns3/traced-value.h"
20
21#include <array>
22
23namespace ns3
24{
25namespace energy
26{
27
28/**
29 * @ingroup energy
30 *
31 * Battery types.
32 * These are grouped according to their chemical characteristics
33 * present during a charge/discharge curve.
34 */
36{
37 LION_LIPO = 0, //!< Lithium-ion and Lithium-polymer batteries
38 NIMH_NICD = 1, //!< Nickel-metal hydride and Nickel cadmium batteries
39 LEADACID = 2 //!< Lead Acid Batteries
40};
41
42/**
43 * @ingroup energy
44 *
45 * Battery models that described the parameters of the the battery presets.
46 */
48{
49 PANASONIC_HHR650D_NIMH = 0, //!< Panasonic HHR650D NiMh battery
50 CSB_GP1272_LEADACID = 1, //!< CSB GP1272 Lead acid battery
51 PANASONIC_CGR18650DA_LION = 2, //!< Panasonic CGR18650DA Li-Ion battery
52 RSPRO_LGP12100_LEADACID = 3, //!< RS Pro LGP12100 Lead acid battery
53 PANASONIC_N700AAC_NICD = 4 //!< Panasonic N700AAC NiCd battery
54};
55
56/**
57 * @ingroup energy
58 *
59 * The structure containing the the parameter values that describe a
60 * battery preset.
61 */
63{
64 GenericBatteryType batteryType; //!< The type of battery used in the preset.
65 std::string description; //!< Additional information about the battery.
66 double vFull; //!< Initial voltage of the battery, in Volts
67 double qMax; //!< The maximum capacity of the battery, in Ah
68 double vNom; //!< Nominal voltage of the battery, in Volts
69 double qNom; //!< Battery capacity at the end of the nominal zone, in Ah
70 double vExp; //!< Battery voltage at the end of the exponential zone, in Volts
71 double qExp; //!< Capacity value at the end of the exponential zone, in Ah
72 double internalResistance; //!< Internal resistance of the battery, in Ohms
73 double typicalCurrent; //!< Typical discharge current used to fit the curves
74 double cuttoffVoltage; //!< The threshold voltage where the battery is considered depleted
75};
76
77/**
78 * @ingroup energy
79 *
80 * Contains the values that form the battery presents available in this module.
81 */
82static const auto g_batteryPreset = std::to_array<BatteryPresets>({
83 {
85 "Panasonic HHR650D | NiMH | 1.2V 6.5Ah | Size: D",
86 1.39,
87 7.0,
88 1.18,
89 6.25,
90 1.28,
91 1.3,
92 0.0046,
93 1.3,
94 1.0,
95 },
96 {
98 "CSB GP1272 | Lead Acid | 12V 7.2Ah",
99 12.8,
100 7.2,
101 11.5,
102 4.5,
103 12.5,
104 2,
105 0.056,
106 0.36,
107 8.0,
108 },
109 {
110 LION_LIPO,
111 "Panasonic CGR18650DA | Li-Ion | 3.6V 2.45Ah | Size: A",
112 4.17,
113 2.33,
114 3.57,
115 2.14,
116 3.714,
117 1.74,
118 0.0830,
119 0.466,
120 3.0,
121 },
122 {
123 LEADACID,
124 "Rs PRO LGP12100 | Lead Acid | 12V 100Ah",
125 12.60,
126 130,
127 12.44,
128 12.3,
129 12.52,
130 12,
131 0.00069,
132 5,
133 11,
134 },
135 {
136 NIMH_NICD,
137 "PANASONIC N-700AAC | NiCd | 1.2V 700mAh | Size: AA",
138 1.38,
139 0.790,
140 1.17,
141 0.60,
142 1.25,
143 0.24,
144 0.016,
145 0.7,
146 0.8,
147 },
148});
149
150/**
151 * @ingroup energy
152 * @brief A generic battery model for Li-Ion, NiCd, NiMh and Lead acid batteries
153 *
154 * The generic battery model can be used to describe the discharge behavior of
155 * the battery chemistries supported by the model.
156 */
158{
159 public:
160 /**
161 * @brief Get the type ID.
162 * @return The object TypeId.
163 */
164 static TypeId GetTypeId();
165
167
168 ~GenericBatteryModel() override;
169
170 /**
171 * Implements GetInitialEnergy. It returns the amount of energy in Joules stored in the
172 * battery when fully charged. This energy is different to the total amount of usable energy
173 * in the battery. This is because the battery cannot be used until Voltage = 0, only until
174 * it reaches the cutoff voltage.
175 *
176 * @return The initial energy stored in the fully charged battery, in Joules.
177 */
178 double GetInitialEnergy() const override;
179
180 /**
181 * Implements GetSupplyVoltage.
182 *
183 * @return Supply voltage at the energy source.
184 */
185 double GetSupplyVoltage() const override;
186
187 /**
188 * Implements GetRemainingEnergy.
189 *
190 * @return Remaining energy in energy source, in Joules
191 */
192 double GetRemainingEnergy() override;
193
194 /**
195 * Implements GetEnergyFraction. For the generic battery model, energy fraction
196 * is equivalent to the remaining usable capacity (i.e. The SoC).
197 *
198 * @return Energy fraction.
199 */
200 double GetEnergyFraction() override;
201
202 /**
203 * Implements UpdateEnergySource.
204 */
205 void UpdateEnergySource() override;
206
207 /**
208 * This function sets the interval between each energy update.
209 *
210 * @param interval Energy update interval.
211 */
212 void SetEnergyUpdateInterval(Time interval);
213
214 /**
215 * This function is used to change the initial capacity in the battery.
216 * A value of 0 means that the battery is fully charged. The value cannot
217 * be set to a value bigger than the rated capacity (fully discharged) or
218 * less than 0 (fully charged).
219 *
220 * @param drainedCapacity The capacity drained so far in the battery.
221 */
222 void SetDrainedCapacity(double drainedCapacity);
223
224 /**
225 * Obtain the amount of drained capacity from the battery based on the
226 * integral of the current over time (Coulomb counting method).
227 *
228 * @return The drainedCapacity (Ah)
229 */
230 double GetDrainedCapacity() const;
231
232 /**
233 * Calculates an estimate of the State of Charge (SoC).
234 * In essence, the amount of usable capacity remaining in the battery (%).
235 *
236 * @return The percentage of usable capacity remaining in the battery.
237 */
238 double GetStateOfCharge() const;
239
240 /**
241 * @return The interval between each energy update.
242 */
244
245 private:
246 void DoInitialize() override;
247 void DoDispose() override;
248
249 /**
250 * Handles the battery reaching its cutoff voltage. This function notifies
251 * all the energy models aggregated to the node about the usable energy in the
252 * battery has being depleted. Each energy model is then responsible for its own handler.
253 */
255
256 /**
257 * Handles the battery reaching its full voltage. This function notifies
258 * all the energy models aggregated to the node about the battery reaching its
259 * full energy charge.
260 */
261 void BatteryChargedEvent();
262
263 /**
264 * Calculates remaining energy. This function uses the total current from all
265 * device models to calculate the amount of energy to decrease. The energy to
266 * decrease is given by:
267 * energy to decrease = total current * supply voltage * time duration
268 * This function subtracts the calculated energy to decrease from remaining
269 * energy.
270 */
272
273 /**
274 * Get the battery voltage in function of the discharge current.
275 * It consider different discharge curves for different discharge currents
276 * and the remaining energy of the battery.
277 *
278 * @param current The actual discharge current value (+i).
279 * @return The voltage of the battery.
280 */
281 double GetVoltage(double current);
282
283 /**
284 * Obtain the battery voltage as a result of a charge current.
285 *
286 * @param current The actual charge current value (-i).
287 * @return The voltage of the battery.
288 */
289 double GetChargeVoltage(double current);
290
291 private:
292 TracedValue<double> m_remainingEnergyJ; //!< Remaining energy, in Joules
293 double m_drainedCapacity; //!< Capacity drained from the battery, in Ah
294 double m_currentFiltered; //!< The step response (a.k.a. low pass filter)
295 double m_entn; //!< The previous value of the exponential zone
296 //!< in NiMh,NiCd and LeadAcid.
297 double m_expZone; //!< Voltage value of the exponential zone
298 Time m_energyUpdateLapseTime; //!< The lapse of time between the last battery energy update and
299 //!< the current time.
300 double m_supplyVoltageV; //!< Actual voltage of the battery
301 double m_lowBatteryTh; //!< Low battery threshold, as a fraction of the initial energy
302 EventId m_energyUpdateEvent; //!< Energy update event
303 Time m_lastUpdateTime; //!< Last update time
304 Time m_energyUpdateInterval; //!< Energy update interval
305 double m_vFull; //!< Initial voltage of the battery, in Volts
306 double m_vNom; //!< Nominal voltage of the battery, in Volts
307 double m_vExp; //!< Battery voltage at the end of the exponential zone, in Volts
308 double m_internalResistance; //!< Internal resistance of the battery, in Ohms
309 double m_qMax; //!< The maximum capacity of the battery, in Ah
310 double m_qNom; //!< Battery capacity at the end of the nominal zone, in Ah
311 double m_qExp; //!< Capacity value at the end of the exponential zone, in Ah
312 double m_typicalCurrent; //!< Typical discharge current used to fit the curves
313 double m_cutoffVoltage; //!< The threshold voltage where the battery is considered depleted
314 GenericBatteryType m_batteryType; //!< Indicates the battery type used by the model
315};
316
317} // namespace energy
318} // namespace ns3
319
320#endif /* GENERIC_BATTERY_MODEL_H */
An identifier for simulation events.
Definition event-id.h:44
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:49
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.
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.
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.
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.
static const auto g_batteryPreset
Contains the values that form the battery presents available in this module.
BatteryModel
Battery models that described the parameters of the the battery presets.
GenericBatteryType
Battery types.
@ PANASONIC_HHR650D_NIMH
Panasonic HHR650D NiMh battery.
@ CSB_GP1272_LEADACID
CSB GP1272 Lead acid battery.
@ PANASONIC_CGR18650DA_LION
Panasonic CGR18650DA Li-Ion battery.
@ PANASONIC_N700AAC_NICD
Panasonic N700AAC NiCd battery.
@ RSPRO_LGP12100_LEADACID
RS Pro LGP12100 Lead acid battery.
@ LEADACID
Lead Acid Batteries.
@ NIMH_NICD
Nickel-metal hydride and Nickel cadmium batteries.
@ LION_LIPO
Lithium-ion and Lithium-polymer batteries.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
The structure containing the the parameter values that describe a battery preset.
GenericBatteryType batteryType
The type of battery used in the preset.
double qMax
The maximum capacity of the battery, in Ah.
std::string description
Additional information about the battery.
double cuttoffVoltage
The threshold voltage where the battery is considered depleted.
double qExp
Capacity value at the end of the exponential zone, in Ah.
double typicalCurrent
Typical discharge current used to fit the curves.
double internalResistance
Internal resistance of the battery, in Ohms.
double qNom
Battery capacity at the end of the nominal zone, in Ah.
double vFull
Initial voltage of the battery, in Volts.
double vExp
Battery voltage at the end of the exponential zone, in Volts.
double vNom
Nominal voltage of the battery, in Volts.