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
269  Ptr<UanNetDevice> dev = m_node->GetDevice (0)->GetObject<UanNetDevice> ();
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
286  Ptr<UanNetDevice> dev = m_node->GetDevice (0)->GetObject<UanNetDevice> ();
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
virtual void ChangeState(int newState)
Changes state of the AcousticModemEnergyModel.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
virtual ~AcousticModemEnergyModel()
Dummy destructor, see DoDispose.
virtual void HandleEnergyDepletion(void)
Handles energy depletion.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
double m_idlePowerW
The idle power, in watts.
uint32_t GetId(void) const
Definition: node.cc:107
Transmitting.
Definition: uan-phy.h:184
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Base class for device energy models.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:142
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
double m_rxPowerW
The receiver power, in watts.
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
AcousticModemEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
void SetIdlePowerW(double idlePowerW)
Set the idle state power of the modem.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
bool IsStateTransitionValid(const int destState)
Ptr< Node > m_node
The node hosting this transducer.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual void HandleEnergyChanged(void)
Handles energy changed.
int GetCurrentState(void) const
Get the current state of the modem.
void SetEnergyDepletionCallback(AcousticModemEnergyDepletionCallback callback)
void SetEnergyRechargeCallback(AcousticModemEnergyRechargeCallback callback)
double m_sleepPowerW
The sleep power, in watts.
Receiving.
Definition: uan-phy.h:183
void SetTxPowerW(double txPowerW)
Set the transmission power of the modem.
void SetRxPowerW(double rxPowerW)
Set the receiving power of the modem.
virtual void SetEnergySource(Ptr< EnergySource > source)
Idle state.
Definition: uan-phy.h:181
Sleeping.
Definition: uan-phy.h:185
static TypeId GetTypeId(void)
Register this type.
virtual Ptr< Node > GetNode(void) const
Gets pointer to node.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:367
Ptr< EnergySource > m_source
The energy source.
void DoDispose(void)
Destructor implementation.
virtual double DoGetCurrentA(void) const
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
double GetTxPowerW(void) const
Get the transmission power of the modem.
Net device for UAN models.
double GetRxPowerW(void) const
Get the receiving power.
double GetSleepPowerW(void) const
Get the sleep state power of the modem.
virtual double GetTotalEnergyConsumption(void) const
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
Time m_lastUpdateTime
Time stamp of previous energy update.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
AcousticModemEnergyRechargeCallback m_energyRechargeCallback
Energy recharge callback.
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
virtual void HandleEnergyRecharged(void)
Handles energy recharged.
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1274
double GetIdlePowerW(void) const
Get the idle power of the modem.
void SetSleepPowerW(double sleepPowerW)
Set the sleep power of the modem.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
double m_txPowerW
The transmitter power, in watts.
a unique identifier for an interface.
Definition: type-id.h:58
TracedValue< double > m_totalEnergyConsumption
The total energy consumed by this model.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915