A Discrete-Event Network Simulator
API
acoustic-modem-energy-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Andrea Sacco
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  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
18  */
19 
21 
22 #include "ns3/double.h"
23 #include "ns3/energy-source.h"
24 #include "ns3/log.h"
25 #include "ns3/simulator.h"
26 #include "ns3/trace-source-accessor.h"
27 #include "ns3/uan-net-device.h"
28 #include "ns3/uan-phy.h"
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("AcousticModemEnergyModel");
34 
35 NS_OBJECT_ENSURE_REGISTERED(AcousticModemEnergyModel);
36 
37 TypeId
39 {
40  static TypeId tid =
41  TypeId("ns3::AcousticModemEnergyModel")
43  .AddConstructor<AcousticModemEnergyModel>()
44  .AddAttribute("TxPowerW",
45  "The modem Tx power in Watts",
46  DoubleValue(50),
49  MakeDoubleChecker<double>())
50  .AddAttribute("RxPowerW",
51  "The modem Rx power in Watts",
52  DoubleValue(0.158),
55  MakeDoubleChecker<double>())
56  .AddAttribute("IdlePowerW",
57  "The modem Idle power in Watts",
58  DoubleValue(0.158),
61  MakeDoubleChecker<double>())
62  .AddAttribute("SleepPowerW",
63  "The modem Sleep power in Watts",
64  DoubleValue(0.0058),
67  MakeDoubleChecker<double>())
68  .AddTraceSource(
69  "TotalEnergyConsumption",
70  "Total energy consumption of the modem device.",
72  "ns3::TracedValueCallback::Double");
73  return tid;
74 }
75 
77 {
78  NS_LOG_FUNCTION(this);
79  m_currentState = UanPhy::IDLE; // initially IDLE
82  m_node = nullptr;
83  m_source = nullptr;
84 }
85 
87 {
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION(this << node);
94  NS_ASSERT(node);
95  m_node = node;
96 }
97 
100 {
101  return m_node;
102 }
103 
104 void
106 {
107  NS_LOG_FUNCTION(this << source);
108  NS_ASSERT(source);
109  m_source = source;
110 }
111 
112 double
114 {
115  NS_LOG_FUNCTION(this);
117 }
118 
119 double
121 {
122  NS_LOG_FUNCTION(this);
123  return m_txPowerW;
124 }
125 
126 void
128 {
129  NS_LOG_FUNCTION(this << txPowerW);
130  m_txPowerW = txPowerW;
131 }
132 
133 double
135 {
136  NS_LOG_FUNCTION(this);
137  return m_rxPowerW;
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION(this << rxPowerW);
144  m_rxPowerW = rxPowerW;
145 }
146 
147 double
149 {
150  NS_LOG_FUNCTION(this);
151  return m_idlePowerW;
152 }
153 
154 void
156 {
157  NS_LOG_FUNCTION(this << idlePowerW);
158  m_idlePowerW = idlePowerW;
159 }
160 
161 double
163 {
164  NS_LOG_FUNCTION(this);
165  return m_sleepPowerW;
166 }
167 
168 void
170 {
171  NS_LOG_FUNCTION(this << sleepPowerW);
172  m_sleepPowerW = sleepPowerW;
173 }
174 
175 int
177 {
178  NS_LOG_FUNCTION(this);
179  return m_currentState;
180 }
181 
182 void
184 {
185  NS_LOG_FUNCTION(this);
186  if (callback.IsNull())
187  {
188  NS_LOG_DEBUG("AcousticModemEnergyModel:Setting NULL energy depletion callback!");
189  }
190  m_energyDepletionCallback = callback;
191 }
192 
193 void
195 {
196  NS_LOG_FUNCTION(this);
197  if (callback.IsNull())
198  {
199  NS_LOG_DEBUG("AcousticModemEnergyModel:Setting NULL energy recharge callback!");
200  }
201  m_energyRechargeCallback = callback;
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION(this << newState);
208  // NS_ASSERT (IsStateTransitionValid ((MicroModemState) newState));
209 
210  Time duration = Simulator::Now() - m_lastUpdateTime;
211  NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
212 
213  // energy to decrease = current * voltage * time
214  double energyToDecrease = 0.0;
215 
216  switch (m_currentState)
217  {
218  case UanPhy::TX:
219  energyToDecrease = duration.GetSeconds() * m_txPowerW;
220  break;
221  case UanPhy::RX:
222  energyToDecrease = duration.GetSeconds() * m_rxPowerW;
223  break;
224  case UanPhy::IDLE:
225  energyToDecrease = duration.GetSeconds() * m_idlePowerW;
226  break;
227  case UanPhy::SLEEP:
228  energyToDecrease = duration.GetSeconds() * m_sleepPowerW;
229  break;
230  case UanPhy::DISABLED:
231  energyToDecrease = 0;
232  break;
233  default:
234  NS_FATAL_ERROR("AcousticModemEnergyModel:Undefined radio state!");
235  }
236 
237  // update total energy consumption
238  m_totalEnergyConsumption += energyToDecrease;
239 
240  // update last update time stamp
242 
243  // notify energy source
244  m_source->UpdateEnergySource();
245 
247  {
248  // update current state & last update time stamp
249  SetMicroModemState(newState);
250  }
251 
252  // some debug message
253  NS_LOG_DEBUG("AcousticModemEnergyModel:Total energy consumption at node #"
254  << m_node->GetId() << " is " << m_totalEnergyConsumption << "J");
255 }
256 
257 void
259 {
260  NS_LOG_FUNCTION(this);
261  NS_LOG_DEBUG("AcousticModemEnergyModel:Energy is depleted at node #" << m_node->GetId());
262  // invoke energy depletion callback, if set.
264  {
266  }
267  // invoke the phy energy depletion handler
268  Ptr<UanNetDevice> dev = m_node->GetDevice(0)->GetObject<UanNetDevice>();
269  dev->GetPhy()->EnergyDepletionHandler();
271 }
272 
273 void
275 {
276  NS_LOG_FUNCTION(this);
277  NS_LOG_DEBUG("AcousticModemEnergyModel:Energy is recharged at node #" << m_node->GetId());
278  // invoke energy recharge callback, if set.
280  {
282  }
283  // invoke the phy energy recharge handler
284  Ptr<UanNetDevice> dev = m_node->GetDevice(0)->GetObject<UanNetDevice>();
285  dev->GetPhy()->EnergyRechargeHandler();
287 }
288 
289 void
291 {
292  NS_LOG_FUNCTION(this);
293  // Not implemented
294 }
295 
296 /*
297  * Private functions start here.
298  */
299 
300 void
302 {
303  NS_LOG_FUNCTION(this);
304  m_node = nullptr;
305  m_source = nullptr;
307 }
308 
309 double
311 {
312  NS_LOG_FUNCTION(this);
313 
314  double supplyVoltage = m_source->GetSupplyVoltage();
315  NS_ASSERT(supplyVoltage != 0.0);
316  double stateCurrent = 0.0;
317  switch (m_currentState)
318  {
319  case UanPhy::TX:
320  stateCurrent = m_txPowerW / supplyVoltage;
321  break;
322  case UanPhy::RX:
323  stateCurrent = m_rxPowerW / supplyVoltage;
324  break;
325  case UanPhy::IDLE:
326  stateCurrent = m_idlePowerW / supplyVoltage;
327  break;
328  case UanPhy::SLEEP:
329  stateCurrent = m_sleepPowerW / supplyVoltage;
330  break;
331  case UanPhy::DISABLED:
332  stateCurrent = 0.0;
333  break;
334  default:
335  NS_FATAL_ERROR("AcousticModemEnergyModel:Undefined radio state!");
336  }
337 
338  return stateCurrent;
339 }
340 
341 bool
343 {
344  NS_LOG_FUNCTION(this << destState);
345  return true;
346 }
347 
348 void
350 {
351  NS_LOG_FUNCTION(this);
352  if (IsStateTransitionValid(state))
353  {
354  m_currentState = state;
355  std::string stateName;
356  switch (state)
357  {
358  case UanPhy::TX:
359  stateName = "TX";
360  break;
361  case UanPhy::RX:
362  stateName = "RX";
363  break;
364  case UanPhy::IDLE:
365  stateName = "IDLE";
366  break;
367  case UanPhy::SLEEP:
368  stateName = "SLEEP";
369  break;
370  case UanPhy::DISABLED:
371  stateName = "DISABLED";
372  break;
373  }
374  NS_LOG_DEBUG("AcousticModemEnergyModel:Switching to state: " << stateName << " at time = "
375  << Simulator::Now());
376  }
377  else
378  {
379  NS_FATAL_ERROR("AcousticModemEnergyModel:Invalid state transition!");
380  }
381 }
382 
383 } // namespace ns3
double GetTotalEnergyConsumption() const override
Ptr< EnergySource > m_source
The energy source.
void SetEnergyRechargeCallback(AcousticModemEnergyRechargeCallback callback)
double m_rxPowerW
The receiver power, in watts.
double m_idlePowerW
The idle power, in watts.
void SetRxPowerW(double rxPowerW)
Set the receiving power of the modem.
Time m_lastUpdateTime
Time stamp of previous energy update.
void HandleEnergyRecharged() override
Handles energy recharged.
static TypeId GetTypeId()
Register this type.
double m_txPowerW
The transmitter power, in watts.
TracedValue< double > m_totalEnergyConsumption
The total energy consumed by this model.
virtual Ptr< Node > GetNode() const
Gets pointer to node.
void HandleEnergyDepletion() override
Handles energy depletion.
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
~AcousticModemEnergyModel() override
Dummy destructor, see DoDispose.
AcousticModemEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
double GetTxPowerW() const
Get the transmission power of the modem.
void ChangeState(int newState) override
Changes state of the AcousticModemEnergyModel.
void SetIdlePowerW(double idlePowerW)
Set the idle state power of the modem.
void SetEnergySource(Ptr< EnergySource > source) override
void DoDispose() override
Destructor implementation.
double m_sleepPowerW
The sleep power, in watts.
void SetSleepPowerW(double sleepPowerW)
Set the sleep power of the modem.
void SetEnergyDepletionCallback(AcousticModemEnergyDepletionCallback callback)
double GetRxPowerW() const
Get the receiving power.
double GetSleepPowerW() const
Get the sleep state power of the modem.
double GetIdlePowerW() const
Get the idle power of the modem.
int GetCurrentState() const
Get the current state of the modem.
AcousticModemEnergyRechargeCallback m_energyRechargeCallback
Energy recharge callback.
void SetTxPowerW(double txPowerW)
Set the transmission power of the modem.
void HandleEnergyChanged() override
Handles energy changed.
bool IsStateTransitionValid(const int destState)
Ptr< Node > m_node
The node hosting this transducer.
void Nullify()
Discard the implementation, set it to null.
Definition: callback.h:578
bool IsNull() const
Check for null implementation.
Definition: callback.h:572
Base class for device energy models.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
uint32_t GetId() const
Definition: node.cc:117
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:152
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:417
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Net device for UAN models.
@ RX
Receiving.
Definition: uan-phy.h:184
@ SLEEP
Sleeping.
Definition: uan-phy.h:186
@ IDLE
Idle state.
Definition: uan-phy.h:182
@ DISABLED
Disabled.
Definition: uan-phy.h:187
@ TX
Transmitting.
Definition: uan-phy.h:185
#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)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: double.h:43
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#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:1336
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.