A Discrete-Event Network Simulator
API
basic-energy-source.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 "basic-energy-source.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 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE("BasicEnergySource");
32 
33 NS_OBJECT_ENSURE_REGISTERED(BasicEnergySource);
34 
35 TypeId
37 {
38  static TypeId tid =
39  TypeId("ns3::BasicEnergySource")
41  .SetGroupName("Energy")
42  .AddConstructor<BasicEnergySource>()
43  .AddAttribute("BasicEnergySourceInitialEnergyJ",
44  "Initial energy stored in basic energy source.",
45  DoubleValue(10), // in Joules
48  MakeDoubleChecker<double>())
49  .AddAttribute("BasicEnergySupplyVoltageV",
50  "Initial supply voltage for basic energy source.",
51  DoubleValue(3.0), // in Volts
54  MakeDoubleChecker<double>())
55  .AddAttribute("BasicEnergyLowBatteryThreshold",
56  "Low battery threshold for basic energy source.",
57  DoubleValue(0.10), // as a fraction of the initial energy
59  MakeDoubleChecker<double>())
60  .AddAttribute("BasicEnergyHighBatteryThreshold",
61  "High battery threshold for basic energy source.",
62  DoubleValue(0.15), // as a fraction of the initial energy
64  MakeDoubleChecker<double>())
65  .AddAttribute("PeriodicEnergyUpdateInterval",
66  "Time between two consecutive periodic energy updates.",
67  TimeValue(Seconds(1.0)),
71  .AddTraceSource("RemainingEnergy",
72  "Remaining energy at BasicEnergySource.",
74  "ns3::TracedValueCallback::Double");
75  return tid;
76 }
77 
79 {
80  NS_LOG_FUNCTION(this);
82  m_depleted = false;
83 }
84 
86 {
87  NS_LOG_FUNCTION(this);
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION(this << initialEnergyJ);
94  NS_ASSERT(initialEnergyJ >= 0);
95  m_initialEnergyJ = initialEnergyJ;
97 }
98 
99 void
101 {
102  NS_LOG_FUNCTION(this << supplyVoltageV);
103  m_supplyVoltageV = supplyVoltageV;
104 }
105 
106 void
108 {
109  NS_LOG_FUNCTION(this << interval);
110  m_energyUpdateInterval = interval;
111 }
112 
113 Time
115 {
116  NS_LOG_FUNCTION(this);
117  return m_energyUpdateInterval;
118 }
119 
120 double
122 {
123  NS_LOG_FUNCTION(this);
124  return m_supplyVoltageV;
125 }
126 
127 double
129 {
130  NS_LOG_FUNCTION(this);
131  return m_initialEnergyJ;
132 }
133 
134 double
136 {
137  NS_LOG_FUNCTION(this);
138  // update energy source to get the latest remaining energy.
140  return m_remainingEnergyJ;
141 }
142 
143 double
145 {
146  NS_LOG_FUNCTION(this);
147  // update energy source to get the latest remaining energy.
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION(this);
156  NS_LOG_DEBUG("BasicEnergySource:Updating remaining energy.");
157 
158  double remainingEnergy = m_remainingEnergyJ;
160 
162 
164  {
165  m_depleted = true;
167  }
169  {
170  m_depleted = false;
172  }
173  else if (m_remainingEnergyJ != remainingEnergy)
174  {
176  }
177 
179  {
182  this);
183  }
184 }
185 
186 /*
187  * Private functions start here.
188  */
189 
190 void
192 {
193  NS_LOG_FUNCTION(this);
194  UpdateEnergySource(); // start periodic update
195 }
196 
197 void
199 {
200  NS_LOG_FUNCTION(this);
201  BreakDeviceEnergyModelRefCycle(); // break reference cycle
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION(this);
208  NS_LOG_DEBUG("BasicEnergySource:Energy depleted!");
209  NotifyEnergyDrained(); // notify DeviceEnergyModel objects
210 }
211 
212 void
214 {
215  NS_LOG_FUNCTION(this);
216  NS_LOG_DEBUG("BasicEnergySource:Energy recharged!");
217  NotifyEnergyRecharged(); // notify DeviceEnergyModel objects
218 }
219 
220 void
222 {
223  NS_LOG_FUNCTION(this);
224  double totalCurrentA = CalculateTotalCurrent();
225  Time duration = Simulator::Now() - m_lastUpdateTime;
226  NS_ASSERT(duration.IsPositive());
227  // energy = current * voltage * time
228  double energyToDecreaseJ = (totalCurrentA * m_supplyVoltageV * duration).GetSeconds();
229  NS_ASSERT(m_remainingEnergyJ >= energyToDecreaseJ);
230  m_remainingEnergyJ -= energyToDecreaseJ;
231  NS_LOG_DEBUG("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
232 }
233 
234 } // namespace ns3
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
void HandleEnergyRechargedEvent()
Handles the remaining energy exceeding the high threshold after it went below the low threshold.
void SetEnergyUpdateInterval(Time interval)
double GetSupplyVoltage() const override
static TypeId GetTypeId()
Get the type ID.
Time m_energyUpdateInterval
energy update interval
double m_initialEnergyJ
initial energy, in Joules
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
double GetRemainingEnergy() override
double GetEnergyFraction() override
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
Time m_lastUpdateTime
last update time
double GetInitialEnergy() const override
void SetSupplyVoltage(double supplyVoltageV)
void DoDispose() override
Defined in ns3::Object.
EventId m_energyUpdateEvent
energy update event
void DoInitialize() override
Defined in ns3::Object.
TracedValue< double > m_remainingEnergyJ
remaining energy, in Joules
bool m_depleted
set to true when the remaining energy goes below the low threshold, set to false again when the remai...
double m_highBatteryTh
high battery threshold, as a fraction of the initial energy
void UpdateEnergySource() override
Implements UpdateEnergySource.
void SetInitialEnergy(double initialEnergyJ)
double m_supplyVoltageV
supply voltage, in Volts
void CalculateRemainingEnergy()
Calculates remaining energy.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
double CalculateTotalCurrent()
void NotifyEnergyRecharged()
This function notifies all DeviceEnergyModel of energy recharged event.
void NotifyEnergyChanged()
This function notifies all DeviceEnergyModel of energy changed 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.
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
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
bool IsPositive() const
Exactly equivalent to t >= 0.
Definition: nstime.h:333
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:1326
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)
Definition: nstime.h:1414
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43