A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lora-radio-energy-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Romagnolo Stefano <romagnolostefano93@gmail.com>
7 */
8
10
11#include "ns3/energy-source.h"
12#include "ns3/pointer.h"
13#include "ns3/simulator.h"
14
15namespace ns3
16{
17namespace lorawan
18{
19
20NS_LOG_COMPONENT_DEFINE("LoraRadioEnergyModel");
21
23
24TypeId
26{
27 static TypeId tid =
28 TypeId("ns3::LoraRadioEnergyModel")
30 .SetGroupName("Energy")
31 .AddConstructor<LoraRadioEnergyModel>()
32 .AddAttribute("StandbyCurrentA",
33 "The default radio Standby current in Ampere.",
34 DoubleValue(0.0014), // idle mode = 1.4mA
38 .AddAttribute("TxCurrentA",
39 "The radio Tx current in Ampere.",
40 DoubleValue(0.028), // transmit at 0dBm = 28mA
44 .AddAttribute("RxCurrentA",
45 "The radio Rx current in Ampere.",
46 DoubleValue(0.0112), // receive mode = 11.2mA
50 .AddAttribute("SleepCurrentA",
51 "The radio Sleep current in Ampere.",
52 DoubleValue(0.0000015), // sleep mode = 1.5microA
56 .AddAttribute("TxCurrentModel",
57 "A pointer to the attached tx current model.",
61 .AddTraceSource(
62 "TotalEnergyConsumption",
63 "Total energy consumption of the radio device.",
65 "ns3::TracedValueCallback::Double");
66 return tid;
67}
68
70{
71 NS_LOG_FUNCTION(this);
77 m_source = nullptr;
78 // set callback for EndDeviceLoraPhy listener
80 m_listener->SetChangeStateCallback(MakeCallback(&DeviceEnergyModel::ChangeState, this));
81 // set callback for updating the tx current
82 m_listener->SetUpdateTxCurrentCallback(
84}
85
91
92void
94{
95 NS_LOG_FUNCTION(this << source);
96 NS_ASSERT(source);
97 m_source = source;
98}
99
100double
106
107double
113
114void
116{
117 NS_LOG_FUNCTION(this << idleCurrentA);
118 m_idleCurrentA = idleCurrentA;
119}
120
121double
123{
124 NS_LOG_FUNCTION(this);
125 return m_txCurrentA;
126}
127
128void
130{
131 NS_LOG_FUNCTION(this << txCurrentA);
132 m_txCurrentA = txCurrentA;
133}
134
135double
137{
138 NS_LOG_FUNCTION(this);
139 return m_rxCurrentA;
140}
141
142void
144{
145 NS_LOG_FUNCTION(this << rxCurrentA);
146 m_rxCurrentA = rxCurrentA;
147}
148
149double
155
156void
158{
159 NS_LOG_FUNCTION(this << sleepCurrentA);
160 m_sleepCurrentA = sleepCurrentA;
161}
162
169
170void
172{
173 NS_LOG_FUNCTION(this);
174 if (callback.IsNull())
175 {
176 NS_LOG_DEBUG("LoraRadioEnergyModel:Setting NULL energy depletion callback!");
177 }
178 m_energyDepletionCallback = callback;
179}
180
181void
183{
184 NS_LOG_FUNCTION(this);
185 if (callback.IsNull())
186 {
187 NS_LOG_DEBUG("LoraRadioEnergyModel:Setting NULL energy recharged callback!");
188 }
189 m_energyRechargedCallback = callback;
190}
191
192void
197
198void
200{
202 {
203 m_txCurrentA = m_txCurrentModel->CalcTxCurrent(txPowerDbm);
204 }
205}
206
207void
209{
210 NS_LOG_FUNCTION(this << EndDeviceLoraPhy::State(newState));
211
212 Time duration = Now() - m_lastUpdateTime;
213 NS_ASSERT(duration.IsPositive()); // check if duration is valid
214
215 // energy to decrease = current * voltage * time
216 double energyToDecrease = 0.0;
217 double supplyVoltage = m_source->GetSupplyVoltage();
218 switch (m_currentState)
219 {
221 energyToDecrease = duration.GetSeconds() * m_idleCurrentA * supplyVoltage;
222 break;
224 energyToDecrease = duration.GetSeconds() * m_txCurrentA * supplyVoltage;
225 break;
227 energyToDecrease = duration.GetSeconds() * m_rxCurrentA * supplyVoltage;
228 break;
230 energyToDecrease = duration.GetSeconds() * m_sleepCurrentA * supplyVoltage;
231 break;
232 default:
233 NS_FATAL_ERROR("LoraRadioEnergyModel:Undefined radio state: " << m_currentState);
234 }
235
236 // update total energy consumption
237 m_totalEnergyConsumption += energyToDecrease;
238
239 // update last update time stamp
241
243
244 // notify energy source
245 m_source->UpdateEnergySource();
246
247 // in case the energy source is found to be depleted during the last update, a callback might be
248 // invoked that might cause a change in the Lora PHY state (e.g., the PHY is put into SLEEP
249 // mode). This in turn causes a new call to this member function, with the consequence that the
250 // previous instance is resumed after the termination of the new instance. In particular, the
251 // state set by the previous instance is erroneously the final state stored in m_currentState.
252 // The check below ensures that previous instances do not change m_currentState.
253
255 {
256 // update current state & last update time stamp
258
259 // some debug message
260 NS_LOG_DEBUG("LoraRadioEnergyModel:Total energy consumption is " << m_totalEnergyConsumption
261 << "J");
262 }
263
265
267}
268
269void
271{
272 NS_LOG_FUNCTION(this);
273 NS_LOG_DEBUG("LoraRadioEnergyModel:Energy is depleted!");
274 // invoke energy depletion callback, if set.
275 if (!m_energyDepletionCallback.IsNull())
276 {
278 }
279}
280
281void
283{
284 NS_LOG_FUNCTION(this);
285 NS_LOG_DEBUG("LoraRadioEnergyModel:Energy changed!");
286}
287
288void
290{
291 NS_LOG_FUNCTION(this);
292 NS_LOG_DEBUG("LoraRadioEnergyModel:Energy is recharged!");
293 // invoke energy recharged callback, if set.
294 if (!m_energyRechargedCallback.IsNull())
295 {
297 }
298}
299
306
307/*
308 * Private functions start here.
309 */
310
311void
313{
314 NS_LOG_FUNCTION(this);
315 m_source = nullptr;
317}
318
319double
321{
322 NS_LOG_FUNCTION(this);
323 switch (m_currentState)
324 {
326 return m_idleCurrentA;
328 return m_txCurrentA;
330 return m_rxCurrentA;
332 return m_sleepCurrentA;
333 default:
334 NS_FATAL_ERROR("LoraRadioEnergyModel:Undefined radio state:" << m_currentState);
335 }
336}
337
338void
340{
341 NS_LOG_FUNCTION(this << state);
342 m_currentState = state;
343 NS_LOG_DEBUG("Switching to state: " << state);
344}
345
346// -------------------------------------------------------------------------- //
347
354
359
360void
368
369void
376
377void
379{
380 NS_LOG_FUNCTION(this);
381 if (m_changeStateCallback.IsNull())
382 {
383 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
384 }
386}
387
388void
390{
391 NS_LOG_FUNCTION(this << txPowerDbm);
392 if (m_updateTxCurrentCallback.IsNull())
393 {
394 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Update tx current callback not set!");
395 }
396 m_updateTxCurrentCallback(txPowerDbm);
397 if (m_changeStateCallback.IsNull())
398 {
399 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
400 }
402}
403
404void
406{
407 NS_LOG_FUNCTION(this);
408 if (m_changeStateCallback.IsNull())
409 {
410 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
411 }
413}
414
415void
417{
418 NS_LOG_FUNCTION(this);
419 if (m_changeStateCallback.IsNull())
420 {
421 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
422 }
424}
425
426/*
427 * Private function state here.
428 */
429
430void
432{
433 NS_LOG_FUNCTION(this);
434 if (m_changeStateCallback.IsNull())
435 {
436 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
437 }
439}
440
441} // namespace lorawan
442} // namespace ns3
bool IsNull() const
Check for null implementation.
Definition callback.h:561
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
bool IsPositive() const
Exactly equivalent to t >= 0.
Definition nstime.h:323
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:398
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
Callback< void, int > ChangeStateCallback
Callback type for ChangeState function.
State
An enumeration of the possible states of an EndDeviceLoraPhy.
@ TX
The PHY layer is sending a packet.
@ STANDBY
The PHY layer is in STANDBY.
@ RX
The PHY layer is receiving a packet.
LoraRadioEnergyModelPhyListener * m_listener
EndDeviceLoraPhy listener.
LoraRadioEnergyModelPhyListener * GetPhyListener()
void HandleEnergyChanged() override
Handles energy recharged.
Callback< void > LoraRadioEnergyDepletionCallback
Callback type for energy depletion handling.
uint8_t m_nPendingChangeState
pending state change
double GetTxCurrentA() const
Gets transmit current.
void ChangeState(int newState) override
Changes state of the LoraRadioEnergyMode.
double GetRxCurrentA() const
Gets receive current.
void SetStandbyCurrentA(double idleCurrentA)
Sets idle current.
void SetTxCurrentFromModel(double txPowerDbm)
Calls the CalcTxCurrent method of the tx current model to compute the tx current based on such model.
LoraRadioEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
void DoDispose() override
Destructor implementation.
Ptr< energy::EnergySource > m_source
energy source
EndDeviceLoraPhy::State m_currentState
current state the radio is in
bool m_isSupersededChangeState
superseded change state
void HandleEnergyRecharged() override
Handles energy recharged.
void HandleEnergyDepletion() override
Handles energy depletion.
void SetEnergyRechargedCallback(LoraRadioEnergyRechargedCallback callback)
void SetLoraRadioState(const EndDeviceLoraPhy::State state)
Time m_lastUpdateTime
time stamp of previous energy update
double GetStandbyCurrentA() const
Gets idle current.
Ptr< LoraTxCurrentModel > m_txCurrentModel
current model
Callback< void > LoraRadioEnergyRechargedCallback
Callback type for energy recharged handling.
void SetEnergyDepletionCallback(LoraRadioEnergyDepletionCallback callback)
void SetSleepCurrentA(double sleepCurrentA)
Sets sleep current.
void SetRxCurrentA(double rxCurrentA)
Sets receive current.
void SetTxCurrentA(double txCurrentA)
Sets transmit current.
void SetEnergySource(Ptr< energy::EnergySource > source) override
Sets pointer to EnergySouce installed on node.
double GetSleepCurrentA() const
Gets sleep current.
void SetTxCurrentModel(Ptr< LoraTxCurrentModel > model)
static TypeId GetTypeId()
Register this type.
EndDeviceLoraPhy::State GetCurrentState() const
LoraRadioEnergyRechargedCallback m_energyRechargedCallback
Energy recharged callback.
TracedValue< double > m_totalEnergyConsumption
This variable keeps track of the total energy consumed by this model.
Installable listener for LoRa physiscal layer state changes.
void NotifyTxStart(double txPowerDbm) override
Switches the LoraRadioEnergyModel to TX state and switches back to STANDBY after TX duration.
void NotifyStandby() override
Defined in ns3::LoraEndDevicePhyListener.
void NotifySleep() override
Defined in ns3::LoraEndDevicePhyListener.
void SetChangeStateCallback(energy::DeviceEnergyModel::ChangeStateCallback callback)
Sets the change state callback.
energy::DeviceEnergyModel::ChangeStateCallback m_changeStateCallback
Change state callback used to notify the LoraRadioEnergyModel of a state change.
Callback< void, double > UpdateTxCurrentCallback
Callback type for updating the transmit current based on the nominal tx power.
void SetUpdateTxCurrentCallback(UpdateTxCurrentCallback callback)
Sets the update tx current callback.
void SwitchToStandby()
A helper function that makes scheduling m_changeStateCallback possible.
void NotifyRxStart() override
Switches the LoraRadioEnergyModel to RX state.
UpdateTxCurrentCallback m_updateTxCurrentCallback
Callback used to update the tx current stored in LoraRadioEnergyModel based on the nominal tx power u...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:250
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:273
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:690
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#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:35
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:288
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
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 AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32