A Discrete-Event Network Simulator
API
acoustic-modem-energy-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Andrea Sacco
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  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/double.h"
23 #include "ns3/simulator.h"
24 #include "ns3/trace-source-accessor.h"
25 #include "ns3/energy-source.h"
26 #include "ns3/uan-phy.h"
27 #include "ns3/uan-net-device.h"
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("AcousticModemEnergyModel");
33 
34 NS_OBJECT_ENSURE_REGISTERED (AcousticModemEnergyModel);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::AcousticModemEnergyModel")
41  .AddConstructor<AcousticModemEnergyModel> ()
42  .AddAttribute ("TxPowerW",
43  "The modem Tx power in Watts",
44  DoubleValue (50),
47  MakeDoubleChecker<double> ())
48  .AddAttribute ("RxPowerW",
49  "The modem Rx power in Watts",
50  DoubleValue (0.158),
53  MakeDoubleChecker<double> ())
54  .AddAttribute ("IdlePowerW",
55  "The modem Idle power in Watts",
56  DoubleValue (0.158),
59  MakeDoubleChecker<double> ())
60  .AddAttribute ("SleepPowerW",
61  "The modem Sleep power in Watts",
62  DoubleValue (0.0058),
65  MakeDoubleChecker<double> ())
66  .AddTraceSource ("TotalEnergyConsumption",
67  "Total energy consumption of the modem device.",
69  "ns3::TracedValueCallback::Double")
70  ;
71  return tid;
72 }
73 
75 {
76  NS_LOG_FUNCTION (this);
77  m_currentState = UanPhy::IDLE; // initially IDLE
78  m_lastUpdateTime = Seconds (0.0);
80  m_node = 0;
81  m_source = 0;
82 }
83 
85 {
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION (this << node);
92  NS_ASSERT (node != 0);
93  m_node = node;
94 }
95 
98 {
99  return m_node;
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this << source);
106  NS_ASSERT (source != 0);
107  m_source = source;
108 }
109 
110 double
112 {
113  NS_LOG_FUNCTION (this);
115 }
116 
117 double
119 {
120  NS_LOG_FUNCTION (this);
121  return m_txPowerW;
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION (this << txPowerW);
128  m_txPowerW = txPowerW;
129 }
130 
131 double
133 {
134  NS_LOG_FUNCTION (this);
135  return m_rxPowerW;
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this << rxPowerW);
142  m_rxPowerW = rxPowerW;
143 }
144 
145 double
147 {
148  NS_LOG_FUNCTION (this);
149  return m_idlePowerW;
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION (this << idlePowerW);
156  m_idlePowerW = idlePowerW;
157 }
158 
159 double
161 {
162  NS_LOG_FUNCTION (this);
163  return m_sleepPowerW;
164 }
165 
166 void
168 {
169  NS_LOG_FUNCTION (this << sleepPowerW);
170  m_sleepPowerW = sleepPowerW;
171 }
172 
173 int
175 {
176  NS_LOG_FUNCTION (this);
177  return m_currentState;
178 }
179 
180 void
183 {
184  NS_LOG_FUNCTION (this);
185  if (callback.IsNull ())
186  {
187  NS_LOG_DEBUG ("AcousticModemEnergyModel:Setting NULL energy depletion callback!");
188  }
189  m_energyDepletionCallback = callback;
190 }
191 
192 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 #" <<
262  m_node->GetId ());
263  // invoke energy depletion callback, if set.
265  {
267  }
268  // invoke the phy energy depletion handler
270  dev->GetPhy ()->EnergyDepletionHandler ();
272 }
273 
274 void
276 {
277  NS_LOG_FUNCTION (this);
278  NS_LOG_DEBUG ("AcousticModemEnergyModel:Energy is recharged at node #" <<
279  m_node->GetId ());
280  // invoke energy recharge callback, if set.
282  {
284  }
285  // invoke the phy energy recharge handler
287  dev->GetPhy ()->EnergyRechargeHandler ();
289 }
290 
291 void
293 {
294  NS_LOG_FUNCTION (this);
295  //Not implemented
296 }
297 
298 
299 /*
300  * Private functions start here.
301  */
302 
303 void
305 {
306  NS_LOG_FUNCTION (this);
307  m_node = 0;
308  m_source = 0;
310 }
311 
312 double
314 {
315  NS_LOG_FUNCTION (this);
316 
317  double supplyVoltage = m_source->GetSupplyVoltage ();
318  NS_ASSERT (supplyVoltage != 0.0);
319  double stateCurrent = 0.0;
320  switch (m_currentState)
321  {
322  case UanPhy::TX:
323  stateCurrent = m_txPowerW / supplyVoltage;
324  break;
325  case UanPhy::RX:
326  stateCurrent = m_rxPowerW / supplyVoltage;
327  break;
328  case UanPhy::IDLE:
329  stateCurrent = m_idlePowerW / supplyVoltage;
330  break;
331  case UanPhy::SLEEP:
332  stateCurrent = m_sleepPowerW / supplyVoltage;
333  break;
334  case UanPhy::DISABLED:
335  stateCurrent = 0.0;
336  break;
337  default:
338  NS_FATAL_ERROR ("AcousticModemEnergyModel:Undefined radio state!");
339  }
340 
341  return stateCurrent;
342 }
343 
344 bool
346 {
347  NS_LOG_FUNCTION (this << destState);
348  return true;
349 }
350 
351 void
353 {
354  NS_LOG_FUNCTION (this);
355  if (IsStateTransitionValid (state))
356  {
357  m_currentState = state;
358  std::string stateName;
359  switch (state)
360  {
361  case UanPhy::TX:
362  stateName = "TX";
363  break;
364  case UanPhy::RX:
365  stateName = "RX";
366  break;
367  case UanPhy::IDLE:
368  stateName = "IDLE";
369  break;
370  case UanPhy::SLEEP:
371  stateName = "SLEEP";
372  break;
373  case UanPhy::DISABLED:
374  stateName = "DISABLED";
375  break;
376  }
377  NS_LOG_DEBUG ("AcousticModemEnergyModel:Switching to state: " << stateName <<
378  " at time = " << Simulator::Now ());
379  }
380  else
381  {
382  NS_FATAL_ERROR ("AcousticModemEnergyModel:Invalid state transition!");
383  }
384 }
385 
386 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
ns3::AcousticModemEnergyModel::m_lastUpdateTime
Time m_lastUpdateTime
Time stamp of previous energy update.
Definition: acoustic-modem-energy-model.h:234
ns3::AcousticModemEnergyModel::m_energyRechargeCallback
AcousticModemEnergyRechargeCallback m_energyRechargeCallback
Energy recharge callback.
Definition: acoustic-modem-energy-model.h:240
ns3::AcousticModemEnergyModel::SetRxPowerW
void SetRxPowerW(double rxPowerW)
Set the receiving power of the modem.
Definition: acoustic-modem-energy-model.cc:139
ns3::AcousticModemEnergyModel::GetTypeId
static TypeId GetTypeId(void)
Register this type.
Definition: acoustic-modem-energy-model.cc:37
ns3::UanPhy::SLEEP
@ SLEEP
Sleeping.
Definition: uan-phy.h:188
ns3::Node::GetId
uint32_t GetId(void) const
Definition: node.cc:109
ns3::Callback< void >
ns3::UanPhy::RX
@ RX
Receiving.
Definition: uan-phy.h:186
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::AcousticModemEnergyModel::SetEnergySource
virtual void SetEnergySource(Ptr< EnergySource > source)
Definition: acoustic-modem-energy-model.cc:103
ns3::Callback::IsNull
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
ns3::AcousticModemEnergyModel::HandleEnergyDepletion
virtual void HandleEnergyDepletion(void)
Handles energy depletion.
Definition: acoustic-modem-energy-model.cc:258
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::AcousticModemEnergyModel::GetSleepPowerW
double GetSleepPowerW(void) const
Get the sleep state power of the modem.
Definition: acoustic-modem-energy-model.cc:160
acoustic-modem-energy-model.h
ns3::AcousticModemEnergyModel::m_currentState
int m_currentState
Current modem state.
Definition: acoustic-modem-energy-model.h:233
ns3::AcousticModemEnergyModel::SetMicroModemState
void SetMicroModemState(const int state)
Definition: acoustic-modem-energy-model.cc:352
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::AcousticModemEnergyModel::GetTxPowerW
double GetTxPowerW(void) const
Get the transmission power of the modem.
Definition: acoustic-modem-energy-model.cc:118
ns3::AcousticModemEnergyModel::m_totalEnergyConsumption
TracedValue< double > m_totalEnergyConsumption
The total energy consumed by this model.
Definition: acoustic-modem-energy-model.h:230
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Definition: trace-source-accessor.h:202
ns3::AcousticModemEnergyModel::m_rxPowerW
double m_rxPowerW
The receiver power, in watts.
Definition: acoustic-modem-energy-model.h:225
ns3::AcousticModemEnergyModel::HandleEnergyRecharged
virtual void HandleEnergyRecharged(void)
Handles energy recharged.
Definition: acoustic-modem-energy-model.cc:275
ns3::Ptr< Node >
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
ns3::AcousticModemEnergyModel::m_idlePowerW
double m_idlePowerW
The idle power, in watts.
Definition: acoustic-modem-energy-model.h:226
ns3::UanPhy::IDLE
@ IDLE
Idle state.
Definition: uan-phy.h:184
ns3::AcousticModemEnergyModel::m_source
Ptr< EnergySource > m_source
The energy source.
Definition: acoustic-modem-energy-model.h:221
ns3::UanNetDevice
Net device for UAN models.
Definition: uan-net-device.h:48
ns3::AcousticModemEnergyModel::GetCurrentState
int GetCurrentState(void) const
Get the current state of the modem.
Definition: acoustic-modem-energy-model.cc:174
ns3::Node::GetDevice
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:144
ns3::AcousticModemEnergyModel::~AcousticModemEnergyModel
virtual ~AcousticModemEnergyModel()
Dummy destructor, see DoDispose.
Definition: acoustic-modem-energy-model.cc:84
ns3::AcousticModemEnergyModel::AcousticModemEnergyModel
AcousticModemEnergyModel()
Constructor.
Definition: acoustic-modem-energy-model.cc:74
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::AcousticModemEnergyModel::SetIdlePowerW
void SetIdlePowerW(double idlePowerW)
Set the idle state power of the modem.
Definition: acoustic-modem-energy-model.cc:153
ns3::Time::GetNanoSeconds
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:392
ns3::MakeDoubleAccessor
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:42
ns3::AcousticModemEnergyModel::GetIdlePowerW
double GetIdlePowerW(void) const
Get the idle power of the modem.
Definition: acoustic-modem-energy-model.cc:146
ns3::AcousticModemEnergyModel::GetNode
virtual Ptr< Node > GetNode(void) const
Gets pointer to node.
Definition: acoustic-modem-energy-model.cc:97
ns3::AcousticModemEnergyModel::GetRxPowerW
double GetRxPowerW(void) const
Get the receiving power.
Definition: acoustic-modem-energy-model.cc:132
ns3::AcousticModemEnergyModel::DoGetCurrentA
virtual double DoGetCurrentA(void) const
Definition: acoustic-modem-energy-model.cc:313
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::AcousticModemEnergyModel::SetEnergyRechargeCallback
void SetEnergyRechargeCallback(AcousticModemEnergyRechargeCallback callback)
Definition: acoustic-modem-energy-model.cc:193
ns3::AcousticModemEnergyModel::ChangeState
virtual void ChangeState(int newState)
Changes state of the AcousticModemEnergyModel.
Definition: acoustic-modem-energy-model.cc:205
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::DeviceEnergyModel
Base class for device energy models.
Definition: device-energy-model.h:44
ns3::AcousticModemEnergyModel::HandleEnergyChanged
virtual void HandleEnergyChanged(void)
Handles energy changed.
Definition: acoustic-modem-energy-model.cc:292
ns3::AcousticModemEnergyModel::DoDispose
void DoDispose(void)
Destructor implementation.
Definition: acoustic-modem-energy-model.cc:304
ns3::AcousticModemEnergyModel::IsStateTransitionValid
bool IsStateTransitionValid(const int destState)
Definition: acoustic-modem-energy-model.cc:345
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::UanPhy::TX
@ TX
Transmitting.
Definition: uan-phy.h:187
ns3::AcousticModemEnergyModel::m_energyDepletionCallback
AcousticModemEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
Definition: acoustic-modem-energy-model.h:237
ns3::AcousticModemEnergyModel::m_txPowerW
double m_txPowerW
The transmitter power, in watts.
Definition: acoustic-modem-energy-model.h:224
ns3::AcousticModemEnergyModel::m_sleepPowerW
double m_sleepPowerW
The sleep power, in watts.
Definition: acoustic-modem-energy-model.h:227
ns3::AcousticModemEnergyModel::m_node
Ptr< Node > m_node
The node hosting this transducer.
Definition: acoustic-modem-energy-model.h:220
ns3::AcousticModemEnergyModel::SetSleepPowerW
void SetSleepPowerW(double sleepPowerW)
Set the sleep power of the modem.
Definition: acoustic-modem-energy-model.cc:167
ns3::AcousticModemEnergyModel::SetEnergyDepletionCallback
void SetEnergyDepletionCallback(AcousticModemEnergyDepletionCallback callback)
Definition: acoustic-modem-energy-model.cc:181
ns3::AcousticModemEnergyModel::SetTxPowerW
void SetTxPowerW(double txPowerW)
Set the transmission power of the modem.
Definition: acoustic-modem-energy-model.cc:125
ns3::UanPhy::DISABLED
@ DISABLED
Disabled.
Definition: uan-phy.h:189
ns3::AcousticModemEnergyModel::SetNode
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
Definition: acoustic-modem-energy-model.cc:89
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
ns3::AcousticModemEnergyModel::GetTotalEnergyConsumption
virtual double GetTotalEnergyConsumption(void) const
Definition: acoustic-modem-energy-model.cc:111
ns3::Callback::Nullify
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1391