A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
rv-battery-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
18 */
19
20#include "rv-battery-model.h"
21
22#include "ns3/assert.h"
23#include "ns3/double.h"
24#include "ns3/log.h"
25#include "ns3/simulator.h"
26#include "ns3/trace-source-accessor.h"
27
28#include <cmath>
29
30namespace ns3
31{
32namespace energy
33{
34
35NS_LOG_COMPONENT_DEFINE("RvBatteryModel");
36NS_OBJECT_ENSURE_REGISTERED(RvBatteryModel);
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::RvBatteryModel")
44 .SetGroupName("Energy")
45 .AddConstructor<RvBatteryModel>()
46 .AddAttribute("RvBatteryModelPeriodicEnergyUpdateInterval",
47 "RV battery model sampling interval.",
48 TimeValue(Seconds(1.0)),
52 .AddAttribute("RvBatteryModelLowBatteryThreshold",
53 "Low battery threshold.",
54 DoubleValue(0.10), // as a fraction of the initial energy
56 MakeDoubleChecker<double>())
57 .AddAttribute("RvBatteryModelOpenCircuitVoltage",
58 "RV battery model open circuit voltage.",
59 DoubleValue(4.1),
62 MakeDoubleChecker<double>())
63 .AddAttribute("RvBatteryModelCutoffVoltage",
64 "RV battery model cutoff voltage.",
65 DoubleValue(3.0),
68 MakeDoubleChecker<double>())
69 .AddAttribute("RvBatteryModelAlphaValue",
70 "RV battery model alpha value.",
71 DoubleValue(35220.0),
73 MakeDoubleChecker<double>())
74 .AddAttribute("RvBatteryModelBetaValue",
75 "RV battery model beta value.",
76 DoubleValue(0.637),
78 MakeDoubleChecker<double>())
79 .AddAttribute(
80 "RvBatteryModelNumOfTerms",
81 "The number of terms of the infinite sum for estimating battery level.",
82 IntegerValue(10), // value used in paper
84 MakeIntegerChecker<int>())
85 .AddTraceSource("RvBatteryModelBatteryLevel",
86 "RV battery model battery level.",
88 "ns3::TracedValueCallback::Double")
89 .AddTraceSource("RvBatteryModelBatteryLifetime",
90 "RV battery model battery lifetime.",
92 "ns3::TracedValueCallback::Time");
93 return tid;
94}
95
97{
98 NS_LOG_FUNCTION(this);
101 m_previousLoad = -1.0;
102 m_batteryLevel = 1; // fully charged
103 m_lifetime = Seconds(0.0);
104}
105
107{
108 NS_LOG_FUNCTION(this);
109}
110
111double
113{
114 NS_LOG_FUNCTION(this);
115 return m_alpha * GetSupplyVoltage();
116}
117
118double
120{
121 NS_LOG_FUNCTION(this);
122 // average of Voc and Vcutoff
124}
125
126double
128{
129 NS_LOG_FUNCTION(this);
132}
133
134double
136{
137 NS_LOG_FUNCTION(this);
138 return GetBatteryLevel();
139}
140
141void
143{
144 NS_LOG_FUNCTION(this);
145
146 // do not update if battery is already dead
147 if (m_batteryLevel <= 0)
148 {
149 NS_LOG_DEBUG("RvBatteryModel:Battery is dead!");
150 return;
151 }
152
153 // do not update if simulation has finished
155 {
156 return;
157 }
158
159 NS_LOG_DEBUG("RvBatteryModel:Updating remaining energy!");
160
162
163 double currentLoad = CalculateTotalCurrent() * 1000; // must be in mA
164 double calculatedAlpha = Discharge(currentLoad, Simulator::Now());
165
166 NS_LOG_DEBUG("RvBatteryModel:Calculated alpha = " << calculatedAlpha << " time = "
167 << Simulator::Now().As(Time::S));
168
169 // calculate battery level
170 m_batteryLevel = 1 - (calculatedAlpha / m_alpha);
171 if (m_batteryLevel < 0)
172 {
173 m_batteryLevel = 0;
174 }
175
176 // check if battery level is below the low battery threshold.
178 {
180 NS_LOG_DEBUG("RvBatteryModel:Battery level below threshold!");
182 }
183
184 m_previousLoad = currentLoad;
188}
189
190void
192{
193 NS_LOG_FUNCTION(this << interval);
194 m_samplingInterval = interval;
195}
196
197Time
199{
200 NS_LOG_FUNCTION(this);
201 return m_samplingInterval;
202}
203
204void
206{
207 NS_LOG_FUNCTION(this << voltage);
208 NS_ASSERT(voltage >= 0);
209 m_openCircuitVoltage = voltage;
210}
211
212double
214{
215 NS_LOG_FUNCTION(this);
217}
218
219void
221{
222 NS_LOG_FUNCTION(this << voltage);
224 m_cutoffVoltage = voltage;
225}
226
227double
229{
230 NS_LOG_FUNCTION(this);
231 return m_cutoffVoltage;
232}
233
234void
236{
237 NS_LOG_FUNCTION(this << alpha);
238 NS_ASSERT(alpha >= 0);
239 m_alpha = alpha;
240}
241
242double
244{
245 NS_LOG_FUNCTION(this);
246 return m_alpha;
247}
248
249void
251{
252 NS_LOG_FUNCTION(this << beta);
253 NS_ASSERT(beta >= 0);
254 m_beta = beta;
255}
256
257double
259{
260 NS_LOG_FUNCTION(this);
261 return m_beta;
262}
263
264double
266{
267 NS_LOG_FUNCTION(this);
269 return m_batteryLevel;
270}
271
272Time
274{
275 NS_LOG_FUNCTION(this);
276 return m_lifetime;
277}
278
279void
281{
282 NS_LOG_FUNCTION(this << num);
283 m_numOfTerms = num;
284}
285
286int
288{
289 NS_LOG_FUNCTION(this);
290 return m_numOfTerms;
291}
292
293/*
294 * Private functions start here.
295 */
296
297void
299{
300 NS_LOG_FUNCTION(this);
301 NS_LOG_DEBUG("RvBatteryModel:Starting battery level update!");
302 UpdateEnergySource(); // start periodic sampling of load (total current)
303}
304
305void
307{
308 NS_LOG_FUNCTION(this);
309 BreakDeviceEnergyModelRefCycle(); // break reference cycle
310}
311
312void
314{
315 NS_LOG_FUNCTION(this);
316 NS_LOG_DEBUG("RvBatteryModel:Energy depleted!");
317 NotifyEnergyDrained(); // notify DeviceEnergyModel objects
318}
319
320double
322{
323 NS_LOG_FUNCTION(this << load << t);
324
325 // record only when load changes
326 if (load != m_previousLoad)
327 {
328 m_load.push_back(load);
329 m_previousLoad = load;
331 m_timeStamps.push_back(t);
332 }
333 else
334 {
335 if (!m_timeStamps.empty())
336 {
337 m_timeStamps[m_timeStamps.size() - 1] = t;
338 }
339 }
340
342
343 // calculate alpha for new t
344 NS_ASSERT(m_load.size() == m_timeStamps.size() - 1); // size must be equal
345 double calculatedAlpha = 0.0;
346 if (m_timeStamps.size() == 1)
347 {
348 // constant load
349 calculatedAlpha = m_load[0] * RvModelAFunction(t, t, Seconds(0.0), m_beta);
350 }
351 else
352 {
353 // changing load
354 for (uint64_t i = 1; i < m_timeStamps.size(); i++)
355 {
356 calculatedAlpha +=
357 m_load[i - 1] * RvModelAFunction(t, m_timeStamps[i], m_timeStamps[i - 1], m_beta);
358 }
359 }
360
361 return calculatedAlpha;
362}
363
364double
366{
367 NS_LOG_FUNCTION(this << t << sk << sk_1 << beta);
368
369 // everything is in minutes
370 double firstDelta = (t - sk).GetMinutes();
371 double secondDelta = (t - sk_1).GetMinutes();
372 double delta = (sk - sk_1).GetMinutes();
373
374 double sum = 0.0;
375 for (int m = 1; m <= m_numOfTerms; m++)
376 {
377 double square = beta * beta * m * m;
378 sum += (std::exp(-square * (firstDelta)) - std::exp(-square * (secondDelta))) / square;
379 }
380 return delta + 2 * sum;
381}
382
383} // namespace energy
384} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
Hold a signed integer type.
Definition: integer.h:45
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:171
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Energy source base class.
Definition: energy-source.h:88
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.
Rakhmatov Vrudhula non-linear battery model.
double m_beta
beta value of RV model, in second^-1
double RvModelAFunction(Time t, Time sk, Time sk_1, double beta)
RV model A function.
static TypeId GetTypeId()
Get the type ID.
TracedValue< Time > m_lifetime
time of death of the battery
TracedValue< double > m_batteryLevel
Battery level is defined as: output of Discharge function / alpha value.
Time m_lastSampleTime
Last sample time.
double GetRemainingEnergy() override
EventId m_currentSampleEvent
Current sample event.
void DoDispose() override
Defined in ns3::Object.
double GetInitialEnergy() const override
double GetSupplyVoltage() const override
std::vector< double > m_load
load profile
void UpdateEnergySource() override
Implements UpdateEnergySource.
double Discharge(double load, Time t)
Discharges the battery.
void SetNumOfTerms(int num)
Sets the number of terms of the infinite sum for estimating battery level.
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
double m_alpha
alpha value of RV model, in Coulomb
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
int m_numOfTerms
Number# of terms for infinite sum in battery level estimation.
void SetBeta(double beta)
Sets the beta value for the battery model.
double m_openCircuitVoltage
Open circuit voltage (in Volts)
std::vector< Time > m_timeStamps
time stamps of load profile
void DoInitialize() override
Defined in ns3::Object.
void SetCutoffVoltage(double voltage)
Sets cutoff voltage of battery.
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
double m_previousLoad
load value (total current) of previous sampling
double GetEnergyFraction() override
void SetSamplingInterval(Time interval)
double m_cutoffVoltage
Cutoff voltage (in Volts)
Time m_samplingInterval
Sampling interval.
void SetOpenCircuitVoltage(double voltage)
Sets open circuit voltage of battery.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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 > MakeIntegerAccessor(T1 a1)
Definition: integer.h:46
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1407
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1427