A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
28namespace ns3
29{
30namespace energy
31{
32
33NS_LOG_COMPONENT_DEFINE("BasicEnergySource");
34NS_OBJECT_ENSURE_REGISTERED(BasicEnergySource);
35
36TypeId
38{
39 static TypeId tid =
40 TypeId("ns3::BasicEnergySource")
42 .SetGroupName("Energy")
43 .AddConstructor<BasicEnergySource>()
44 .AddAttribute("BasicEnergySourceInitialEnergyJ",
45 "Initial energy stored in basic energy source.",
46 DoubleValue(10), // in Joules
49 MakeDoubleChecker<double>())
50 .AddAttribute("BasicEnergySupplyVoltageV",
51 "Initial supply voltage for basic energy source.",
52 DoubleValue(3.0), // in Volts
55 MakeDoubleChecker<double>())
56 .AddAttribute("BasicEnergyLowBatteryThreshold",
57 "Low battery threshold for basic energy source.",
58 DoubleValue(0.10), // as a fraction of the initial energy
60 MakeDoubleChecker<double>())
61 .AddAttribute("BasicEnergyHighBatteryThreshold",
62 "High battery threshold for basic energy source.",
63 DoubleValue(0.15), // as a fraction of the initial energy
65 MakeDoubleChecker<double>())
66 .AddAttribute("PeriodicEnergyUpdateInterval",
67 "Time between two consecutive periodic energy updates.",
68 TimeValue(Seconds(1.0)),
72 .AddTraceSource("RemainingEnergy",
73 "Remaining energy at BasicEnergySource.",
75 "ns3::TracedValueCallback::Double");
76 return tid;
77}
78
80{
81 NS_LOG_FUNCTION(this);
83 m_depleted = false;
84}
85
87{
88 NS_LOG_FUNCTION(this);
89}
90
91void
93{
94 NS_LOG_FUNCTION(this << initialEnergyJ);
95 NS_ASSERT(initialEnergyJ >= 0);
96 m_initialEnergyJ = initialEnergyJ;
98}
99
100void
102{
103 NS_LOG_FUNCTION(this << supplyVoltageV);
104 m_supplyVoltageV = supplyVoltageV;
105}
106
107void
109{
110 NS_LOG_FUNCTION(this << interval);
111 m_energyUpdateInterval = interval;
112}
113
114Time
116{
117 NS_LOG_FUNCTION(this);
119}
120
121double
123{
124 NS_LOG_FUNCTION(this);
125 return m_supplyVoltageV;
126}
127
128double
130{
131 NS_LOG_FUNCTION(this);
132 return m_initialEnergyJ;
133}
134
135double
137{
138 NS_LOG_FUNCTION(this);
139 // update energy source to get the latest remaining energy.
141 return m_remainingEnergyJ;
142}
143
144double
146{
147 NS_LOG_FUNCTION(this);
148 // update energy source to get the latest remaining energy.
151}
152
153void
155{
156 NS_LOG_FUNCTION(this);
157 NS_LOG_DEBUG("BasicEnergySource:Updating remaining energy.");
158
159 double remainingEnergy = m_remainingEnergyJ;
161
163
165 {
166 m_depleted = true;
168 }
170 {
171 m_depleted = false;
173 }
174 else if (m_remainingEnergyJ != remainingEnergy)
175 {
177 }
178
180 {
183 this);
184 }
185}
186
187/*
188 * Private functions start here.
189 */
190
191void
193{
194 NS_LOG_FUNCTION(this);
195 UpdateEnergySource(); // start periodic update
196}
197
198void
200{
201 NS_LOG_FUNCTION(this);
202 BreakDeviceEnergyModelRefCycle(); // break reference cycle
203}
204
205void
207{
208 NS_LOG_FUNCTION(this);
209 NS_LOG_DEBUG("BasicEnergySource:Energy depleted!");
210 NotifyEnergyDrained(); // notify DeviceEnergyModel objects
211}
212
213void
215{
216 NS_LOG_FUNCTION(this);
217 NS_LOG_DEBUG("BasicEnergySource:Energy recharged!");
218 NotifyEnergyRecharged(); // notify DeviceEnergyModel objects
219}
220
221void
223{
224 NS_LOG_FUNCTION(this);
225 double totalCurrentA = CalculateTotalCurrent();
226 Time duration = Simulator::Now() - m_lastUpdateTime;
227 NS_ASSERT(duration.IsPositive());
228 // energy = current * voltage * time
229 double energyToDecreaseJ = (totalCurrentA * m_supplyVoltageV * duration).GetSeconds();
230 NS_ASSERT(m_remainingEnergyJ >= energyToDecreaseJ);
231 m_remainingEnergyJ -= energyToDecreaseJ;
232 NS_LOG_DEBUG("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
233}
234
235} // namespace energy
236} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
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:932
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
double m_initialEnergyJ
initial energy, in Joules
Time m_lastUpdateTime
last update time
static TypeId GetTypeId()
Get the type ID.
double GetSupplyVoltage() const override
Time m_energyUpdateInterval
energy update interval
EventId m_energyUpdateEvent
energy update event
void SetInitialEnergy(double initialEnergyJ)
bool m_depleted
set to true when the remaining energy goes below the low threshold, set to false again when the remai...
TracedValue< double > m_remainingEnergyJ
remaining energy, in Joules
void DoDispose() override
Defined in ns3::Object.
void SetSupplyVoltage(double supplyVoltageV)
void SetEnergyUpdateInterval(Time interval)
double GetInitialEnergy() const override
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
void UpdateEnergySource() override
Implements UpdateEnergySource.
void CalculateRemainingEnergy()
Calculates remaining energy.
double m_highBatteryTh
high battery threshold, as a fraction of the initial energy
void HandleEnergyRechargedEvent()
Handles the remaining energy exceeding the high threshold after it went below the low threshold.
void DoInitialize() override
Defined in ns3::Object.
double m_supplyVoltageV
supply voltage, in Volts
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.
void NotifyEnergyRecharged()
This function notifies all DeviceEnergyModel of energy recharged event.
void NotifyEnergyChanged()
This function notifies all DeviceEnergyModel of energy changed event.
#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 > 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