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{
32
33NS_LOG_COMPONENT_DEFINE("RvBatteryModel");
34
35NS_OBJECT_ENSURE_REGISTERED(RvBatteryModel);
36
37TypeId
39{
40 static TypeId tid =
41 TypeId("ns3::RvBatteryModel")
43 .SetGroupName("Energy")
44 .AddConstructor<RvBatteryModel>()
45 .AddAttribute("RvBatteryModelPeriodicEnergyUpdateInterval",
46 "RV battery model sampling interval.",
47 TimeValue(Seconds(1.0)),
51 .AddAttribute("RvBatteryModelLowBatteryThreshold",
52 "Low battery threshold.",
53 DoubleValue(0.10), // as a fraction of the initial energy
55 MakeDoubleChecker<double>())
56 .AddAttribute("RvBatteryModelOpenCircuitVoltage",
57 "RV battery model open circuit voltage.",
58 DoubleValue(4.1),
61 MakeDoubleChecker<double>())
62 .AddAttribute("RvBatteryModelCutoffVoltage",
63 "RV battery model cutoff voltage.",
64 DoubleValue(3.0),
67 MakeDoubleChecker<double>())
68 .AddAttribute("RvBatteryModelAlphaValue",
69 "RV battery model alpha value.",
70 DoubleValue(35220.0),
72 MakeDoubleChecker<double>())
73 .AddAttribute("RvBatteryModelBetaValue",
74 "RV battery model beta value.",
75 DoubleValue(0.637),
77 MakeDoubleChecker<double>())
78 .AddAttribute(
79 "RvBatteryModelNumOfTerms",
80 "The number of terms of the infinite sum for estimating battery level.",
81 IntegerValue(10), // value used in paper
83 MakeIntegerChecker<int>())
84 .AddTraceSource("RvBatteryModelBatteryLevel",
85 "RV battery model battery level.",
87 "ns3::TracedValueCallback::Double")
88 .AddTraceSource("RvBatteryModelBatteryLifetime",
89 "RV battery model battery lifetime.",
91 "ns3::TracedValueCallback::Time");
92 return tid;
93}
94
96{
97 NS_LOG_FUNCTION(this);
100 m_previousLoad = -1.0;
101 m_batteryLevel = 1; // fully charged
102 m_lifetime = Seconds(0.0);
103}
104
106{
107 NS_LOG_FUNCTION(this);
108}
109
110double
112{
113 NS_LOG_FUNCTION(this);
114 return m_alpha * GetSupplyVoltage();
115}
116
117double
119{
120 NS_LOG_FUNCTION(this);
121 // average of Voc and Vcutoff
123}
124
125double
127{
128 NS_LOG_FUNCTION(this);
131}
132
133double
135{
136 NS_LOG_FUNCTION(this);
137 return GetBatteryLevel();
138}
139
140void
142{
143 NS_LOG_FUNCTION(this);
144
145 // do not update if battery is already dead
146 if (m_batteryLevel <= 0)
147 {
148 NS_LOG_DEBUG("RvBatteryModel:Battery is dead!");
149 return;
150 }
151
152 // do not update if simulation has finished
154 {
155 return;
156 }
157
158 NS_LOG_DEBUG("RvBatteryModel:Updating remaining energy!");
159
161
162 double currentLoad = CalculateTotalCurrent() * 1000; // must be in mA
163 double calculatedAlpha = Discharge(currentLoad, Simulator::Now());
164
165 NS_LOG_DEBUG("RvBatteryModel:Calculated alpha = " << calculatedAlpha << " time = "
166 << Simulator::Now().As(Time::S));
167
168 // calculate battery level
169 m_batteryLevel = 1 - (calculatedAlpha / m_alpha);
170 if (m_batteryLevel < 0)
171 {
172 m_batteryLevel = 0;
173 }
174
175 // check if battery level is below the low battery threshold.
177 {
179 NS_LOG_DEBUG("RvBatteryModel:Battery level below threshold!");
181 }
182
183 m_previousLoad = currentLoad;
187}
188
189void
191{
192 NS_LOG_FUNCTION(this << interval);
193 m_samplingInterval = interval;
194}
195
196Time
198{
199 NS_LOG_FUNCTION(this);
200 return m_samplingInterval;
201}
202
203void
205{
206 NS_LOG_FUNCTION(this << voltage);
207 NS_ASSERT(voltage >= 0);
208 m_openCircuitVoltage = voltage;
209}
210
211double
213{
214 NS_LOG_FUNCTION(this);
216}
217
218void
220{
221 NS_LOG_FUNCTION(this << voltage);
223 m_cutoffVoltage = voltage;
224}
225
226double
228{
229 NS_LOG_FUNCTION(this);
230 return m_cutoffVoltage;
231}
232
233void
235{
236 NS_LOG_FUNCTION(this << alpha);
237 NS_ASSERT(alpha >= 0);
238 m_alpha = alpha;
239}
240
241double
243{
244 NS_LOG_FUNCTION(this);
245 return m_alpha;
246}
247
248void
250{
251 NS_LOG_FUNCTION(this << beta);
252 NS_ASSERT(beta >= 0);
253 m_beta = beta;
254}
255
256double
258{
259 NS_LOG_FUNCTION(this);
260 return m_beta;
261}
262
263double
265{
266 NS_LOG_FUNCTION(this);
268 return m_batteryLevel;
269}
270
271Time
273{
274 NS_LOG_FUNCTION(this);
275 return m_lifetime;
276}
277
278void
280{
281 NS_LOG_FUNCTION(this << num);
282 m_numOfTerms = num;
283}
284
285int
287{
288 NS_LOG_FUNCTION(this);
289 return m_numOfTerms;
290}
291
292/*
293 * Private functions start here.
294 */
295
296void
298{
299 NS_LOG_FUNCTION(this);
300 NS_LOG_DEBUG("RvBatteryModel:Starting battery level update!");
301 UpdateEnergySource(); // start periodic sampling of load (total current)
302}
303
304void
306{
307 NS_LOG_FUNCTION(this);
308 BreakDeviceEnergyModelRefCycle(); // break reference cycle
309}
310
311void
313{
314 NS_LOG_FUNCTION(this);
315 NS_LOG_DEBUG("RvBatteryModel:Energy depleted!");
316 NotifyEnergyDrained(); // notify DeviceEnergyModel objects
317}
318
319double
321{
322 NS_LOG_FUNCTION(this << load << t);
323
324 // record only when load changes
325 if (load != m_previousLoad)
326 {
327 m_load.push_back(load);
328 m_previousLoad = load;
330 m_timeStamps.push_back(t);
331 }
332 else
333 {
334 if (!m_timeStamps.empty())
335 {
336 m_timeStamps[m_timeStamps.size() - 1] = t;
337 }
338 }
339
341
342 // calculate alpha for new t
343 NS_ASSERT(m_load.size() == m_timeStamps.size() - 1); // size must be equal
344 double calculatedAlpha = 0.0;
345 if (m_timeStamps.size() == 1)
346 {
347 // constant load
348 calculatedAlpha = m_load[0] * RvModelAFunction(t, t, Seconds(0.0), m_beta);
349 }
350 else
351 {
352 // changing load
353 for (uint64_t i = 1; i < m_timeStamps.size(); i++)
354 {
355 calculatedAlpha +=
356 m_load[i - 1] * RvModelAFunction(t, m_timeStamps[i], m_timeStamps[i - 1], m_beta);
357 }
358 }
359
360 return calculatedAlpha;
361}
362
363double
365{
366 NS_LOG_FUNCTION(this << t << sk << sk_1 << beta);
367
368 // everything is in minutes
369 double firstDelta = (t - sk).GetMinutes();
370 double secondDelta = (t - sk_1).GetMinutes();
371 double delta = (sk - sk_1).GetMinutes();
372
373 double sum = 0.0;
374 for (int m = 1; m <= m_numOfTerms; m++)
375 {
376 double square = beta * beta * m * m;
377 sum += (std::exp(-square * (firstDelta)) - std::exp(-square * (secondDelta))) / square;
378 }
379 return delta + 2 * sum;
380}
381
382} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Introspection did not find any typical Config paths.
Definition: energy-source.h:87
double CalculateTotalCurrent()
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.
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
Rakhmatov Vrudhula non-linear battery model.
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
void DoDispose() override
Defined in ns3::Object.
double m_openCircuitVoltage
Open circuit voltage (in Volts)
double GetBeta() const
void SetNumOfTerms(int num)
Sets the number of terms of the infinite sum for estimating battery level.
double m_cutoffVoltage
Cutoff voltage (in Volts)
EventId m_currentSampleEvent
Current sample event.
void SetSamplingInterval(Time interval)
Time m_lastSampleTime
Last sample time.
double GetCutoffVoltage() const
static TypeId GetTypeId()
Get the type ID.
double m_previousLoad
load value (total current) of previous sampling
std::vector< Time > m_timeStamps
time stamps of load profile
int m_numOfTerms
Number# of terms for infinite sum in battery level estimation.
double Discharge(double load, Time t)
Discharges the battery.
Time GetSamplingInterval() const
void SetCutoffVoltage(double voltage)
Sets cutoff voltage of battery.
void SetBeta(double beta)
Sets the beta value for the battery model.
double GetEnergyFraction() override
double GetInitialEnergy() const override
double GetOpenCircuitVoltage() const
std::vector< double > m_load
load profile
TracedValue< Time > m_lifetime
time of death of the battery
Time m_samplingInterval
Sampling interval.
void SetOpenCircuitVoltage(double voltage)
Sets open circuit voltage of battery.
double RvModelAFunction(Time t, Time sk, Time sk_1, double beta)
RV model A function.
void SetAlpha(double alpha)
Sets the alpha value for the battery model.
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
double GetAlpha() const
void DoInitialize() override
Defined in ns3::Object.
double m_alpha
alpha value of RV model, in Coulomb
double GetSupplyVoltage() const override
TracedValue< double > m_batteryLevel
Battery level is defined as: output of Discharge function / alpha value.
void UpdateEnergySource() override
Implements UpdateEnergySource.
double GetRemainingEnergy() override
double m_beta
beta value of RV model, in second^-1
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
AttributeValue implementation for Time.
Definition: nstime.h:1406
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#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
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition: integer.h:46
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1427
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1407
#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.