A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
basic-energy-source.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
19  */
20 
21 #include "basic-energy-source.h"
22 #include "ns3/log.h"
23 #include "ns3/assert.h"
24 #include "ns3/double.h"
25 #include "ns3/trace-source-accessor.h"
26 #include "ns3/simulator.h"
27 
28 NS_LOG_COMPONENT_DEFINE ("BasicEnergySource");
29 
30 namespace ns3 {
31 
32 NS_OBJECT_ENSURE_REGISTERED (BasicEnergySource);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::BasicEnergySource")
39  .AddConstructor<BasicEnergySource> ()
40  .AddAttribute ("BasicEnergySourceInitialEnergyJ",
41  "Initial energy stored in basic energy source.",
42  DoubleValue (10), // in Joules
43  MakeDoubleAccessor (&BasicEnergySource::SetInitialEnergy,
45  MakeDoubleChecker<double> ())
46  .AddAttribute ("BasicEnergySupplyVoltageV",
47  "Initial supply voltage for basic energy source.",
48  DoubleValue (3.0), // in Volts
49  MakeDoubleAccessor (&BasicEnergySource::SetSupplyVoltage,
51  MakeDoubleChecker<double> ())
52  .AddAttribute ("BasicEnergyLowBatteryThreshold",
53  "Low battery threshold for basic energy source.",
54  DoubleValue (0.10), // as a fraction of the initial energy
55  MakeDoubleAccessor (&BasicEnergySource::m_lowBatteryTh),
56  MakeDoubleChecker<double> ())
57  .AddAttribute ("BasicEnergyHighBatteryThreshold",
58  "High battery threshold for basic energy source.",
59  DoubleValue (0.15), // as a fraction of the initial energy
60  MakeDoubleAccessor (&BasicEnergySource::m_highBatteryTh),
61  MakeDoubleChecker<double> ())
62  .AddAttribute ("PeriodicEnergyUpdateInterval",
63  "Time between two consecutive periodic energy updates.",
64  TimeValue (Seconds (1.0)),
67  MakeTimeChecker ())
68  .AddTraceSource ("RemainingEnergy",
69  "Remaining energy at BasicEnergySource.",
71  ;
72  return tid;
73 }
74 
76 {
77  NS_LOG_FUNCTION (this);
78  m_lastUpdateTime = Seconds (0.0);
79  m_depleted = false;
80 }
81 
83 {
84  NS_LOG_FUNCTION (this);
85 }
86 
87 void
88 BasicEnergySource::SetInitialEnergy (double initialEnergyJ)
89 {
90  NS_LOG_FUNCTION (this << initialEnergyJ);
91  NS_ASSERT (initialEnergyJ >= 0);
92  m_initialEnergyJ = initialEnergyJ;
94 }
95 
96 void
97 BasicEnergySource::SetSupplyVoltage (double supplyVoltageV)
98 {
99  NS_LOG_FUNCTION (this << supplyVoltageV);
100  m_supplyVoltageV = supplyVoltageV;
101 }
102 
103 void
105 {
106  NS_LOG_FUNCTION (this << interval);
107  m_energyUpdateInterval = interval;
108 }
109 
110 Time
112 {
113  NS_LOG_FUNCTION (this);
114  return m_energyUpdateInterval;
115 }
116 
117 double
119 {
120  NS_LOG_FUNCTION (this);
121  return m_supplyVoltageV;
122 }
123 
124 double
126 {
127  NS_LOG_FUNCTION (this);
128  return m_initialEnergyJ;
129 }
130 
131 double
133 {
134  NS_LOG_FUNCTION (this);
135  // update energy source to get the latest remaining energy.
137  return m_remainingEnergyJ;
138 }
139 
140 double
142 {
143  NS_LOG_FUNCTION (this);
144  // update energy source to get the latest remaining energy.
147 }
148 
149 void
151 {
152  NS_LOG_FUNCTION (this);
153  NS_LOG_DEBUG ("BasicEnergySource:Updating remaining energy.");
154 
155  // do not update if simulation has finished
156  if (Simulator::IsFinished ())
157  {
158  return;
159  }
160 
162 
164 
166 
168  {
169  m_depleted = true;
171  }
172 
173  if (m_depleted && m_remainingEnergyJ > m_highBatteryTh * m_initialEnergyJ)
174  {
175  m_depleted = false;
177  }
178 
181  this);
182 }
183 
184 /*
185  * Private functions start here.
186  */
187 
188 void
190 {
191  NS_LOG_FUNCTION (this);
192  UpdateEnergySource (); // start periodic update
193 }
194 
195 void
197 {
198  NS_LOG_FUNCTION (this);
199  BreakDeviceEnergyModelRefCycle (); // break reference cycle
200 }
201 
202 void
204 {
205  NS_LOG_FUNCTION (this);
206  NS_LOG_DEBUG ("BasicEnergySource:Energy depleted!");
207  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
208  if (m_remainingEnergyJ <= 0)
209  {
210  m_remainingEnergyJ = 0; // energy never goes below 0
211  }
212 }
213 
214 void
216 {
217  NS_LOG_FUNCTION (this);
218  NS_LOG_DEBUG ("BasicEnergySource:Energy recharged!");
219  NotifyEnergyRecharged (); // notify DeviceEnergyModel objects
220 }
221 
222 void
224 {
225  NS_LOG_FUNCTION (this);
226  double totalCurrentA = CalculateTotalCurrent ();
227  Time duration = Simulator::Now () - m_lastUpdateTime;
228  NS_ASSERT (duration.GetSeconds () >= 0);
229  // energy = current * voltage * time
230  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
231  m_remainingEnergyJ -= energyToDecreaseJ;
232  NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
233 }
234 
235 } // namespace ns3
void CalculateRemainingEnergy(void)
Calculates remaining energy.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Time GetEnergyUpdateInterval(void) const
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
virtual double GetSupplyVoltage(void) const
Doxygen introspection did not find any typical Config paths.
Definition: energy-source.h:81
virtual double GetRemainingEnergy(void)
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
void HandleEnergyDrainedEvent(void)
Handles the remaining energy going to zero event.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:444
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:322
virtual double GetInitialEnergy(void) const
void DoDispose(void)
Defined in ns3::Object.
Attribute for objects of type ns3::Time.
Definition: nstime.h:912
TracedValue< double > m_remainingEnergyJ
void NotifyEnergyRecharged(void)
This function notifies all DeviceEnergyModel of energy recharged event.
void HandleEnergyRechargedEvent(void)
Handles the remaining energy exceeding the high threshold after it went below the low threshold...
static TypeId GetTypeId(void)
double CalculateTotalCurrent(void)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void DoInitialize(void)
Defined in ns3::Object.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
void SetSupplyVoltage(double supplyVoltageV)
void SetEnergyUpdateInterval(Time interval)
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
static bool IsFinished(void)
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
Definition: simulator.cc:150
void SetInitialEnergy(double initialEnergyJ)
Hold a floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
virtual double GetEnergyFraction(void)