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/log.h"
13#include "ns3/pointer.h"
14#include "ns3/simulator.h"
15
16namespace ns3
17{
18namespace lorawan
19{
20
21NS_LOG_COMPONENT_DEFINE("LoraRadioEnergyModel");
22
23NS_OBJECT_ENSURE_REGISTERED(LoraRadioEnergyModel);
24
25TypeId
27{
28 static TypeId tid =
29 TypeId("ns3::LoraRadioEnergyModel")
31 .SetGroupName("Energy")
32 .AddConstructor<LoraRadioEnergyModel>()
33 .AddAttribute("StandbyCurrentA",
34 "The default radio Standby current in Ampere.",
35 DoubleValue(0.0014), // idle mode = 1.4mA
39 .AddAttribute("TxCurrentA",
40 "The radio Tx current in Ampere.",
41 DoubleValue(0.028), // transmit at 0dBm = 28mA
45 .AddAttribute("RxCurrentA",
46 "The radio Rx current in Ampere.",
47 DoubleValue(0.0112), // receive mode = 11.2mA
51 .AddAttribute("SleepCurrentA",
52 "The radio Sleep current in Ampere.",
53 DoubleValue(0.0000015), // sleep mode = 1.5microA
57 .AddAttribute("TxCurrentModel",
58 "A pointer to the attached tx current model.",
62 .AddTraceSource(
63 "TotalEnergyConsumption",
64 "Total energy consumption of the radio device.",
66 "ns3::TracedValueCallback::Double");
67 return tid;
68}
69
86
92
93void
95{
96 NS_LOG_FUNCTION(this << source);
97 NS_ASSERT(source);
98 m_source = source;
99}
100
101double
107
108double
114
115void
117{
118 NS_LOG_FUNCTION(this << idleCurrentA);
119 m_idleCurrentA = idleCurrentA;
120}
121
122double
124{
125 NS_LOG_FUNCTION(this);
126 return m_txCurrentA;
127}
128
129void
131{
132 NS_LOG_FUNCTION(this << txCurrentA);
133 m_txCurrentA = txCurrentA;
134}
135
136double
138{
139 NS_LOG_FUNCTION(this);
140 return m_rxCurrentA;
141}
142
143void
145{
146 NS_LOG_FUNCTION(this << rxCurrentA);
147 m_rxCurrentA = rxCurrentA;
148}
149
150double
156
157void
159{
160 NS_LOG_FUNCTION(this << sleepCurrentA);
161 m_sleepCurrentA = sleepCurrentA;
162}
163
170
171void
173{
174 NS_LOG_FUNCTION(this);
175 if (callback.IsNull())
176 {
177 NS_LOG_DEBUG("LoraRadioEnergyModel:Setting NULL energy depletion callback!");
178 }
179 m_energyDepletionCallback = callback;
180}
181
182void
184{
185 NS_LOG_FUNCTION(this);
186 if (callback.IsNull())
187 {
188 NS_LOG_DEBUG("LoraRadioEnergyModel:Setting NULL energy recharged callback!");
189 }
190 m_energyRechargedCallback = callback;
191}
192
193void
198
199void
201{
203 {
204 m_txCurrentA = m_txCurrentModel->CalcTxCurrent(txPowerDbm);
205 }
206}
207
208void
210{
211 NS_LOG_FUNCTION(this << newState);
212
213 Time duration = Simulator::Now() - m_lastUpdateTime;
214 NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
215
216 // energy to decrease = current * voltage * time
217 double energyToDecrease = 0.0;
218 double supplyVoltage = m_source->GetSupplyVoltage();
219 switch (m_currentState)
220 {
222 energyToDecrease = duration.GetSeconds() * m_idleCurrentA * supplyVoltage;
223 break;
225 energyToDecrease = duration.GetSeconds() * m_txCurrentA * supplyVoltage;
226 break;
228 energyToDecrease = duration.GetSeconds() * m_rxCurrentA * supplyVoltage;
229 break;
231 energyToDecrease = duration.GetSeconds() * m_sleepCurrentA * supplyVoltage;
232 break;
233 default:
234 NS_FATAL_ERROR("LoraRadioEnergyModel:Undefined radio state: " << m_currentState);
235 }
236
237 // update total energy consumption
238 m_totalEnergyConsumption += energyToDecrease;
239
240 // update last update time stamp
242
244
245 // notify energy source
246 m_source->UpdateEnergySource();
247
248 // in case the energy source is found to be depleted during the last update, a callback might be
249 // invoked that might cause a change in the Lora PHY state (e.g., the PHY is put into SLEEP
250 // mode). This in turn causes a new call to this member function, with the consequence that the
251 // previous instance is resumed after the termination of the new instance. In particular, the
252 // state set by the previous instance is erroneously the final state stored in m_currentState.
253 // The check below ensures that previous instances do not change m_currentState.
254
256 {
257 // update current state & last update time stamp
259
260 // some debug message
261 NS_LOG_DEBUG("LoraRadioEnergyModel:Total energy consumption is " << m_totalEnergyConsumption
262 << "J");
263 }
264
266
268}
269
270void
272{
273 NS_LOG_FUNCTION(this);
274 NS_LOG_DEBUG("LoraRadioEnergyModel:Energy is depleted!");
275 // invoke energy depletion callback, if set.
277 {
279 }
280}
281
282void
284{
285 NS_LOG_FUNCTION(this);
286 NS_LOG_DEBUG("LoraRadioEnergyModel:Energy changed!");
287}
288
289void
291{
292 NS_LOG_FUNCTION(this);
293 NS_LOG_DEBUG("LoraRadioEnergyModel:Energy is recharged!");
294 // invoke energy recharged callback, if set.
296 {
298 }
299}
300
307
308/*
309 * Private functions start here.
310 */
311
312void
319
320double
322{
323 NS_LOG_FUNCTION(this);
324 switch (m_currentState)
325 {
327 return m_idleCurrentA;
329 return m_txCurrentA;
331 return m_rxCurrentA;
333 return m_sleepCurrentA;
334 default:
335 NS_FATAL_ERROR("LoraRadioEnergyModel:Undefined radio state:" << m_currentState);
336 }
337}
338
339void
341{
342 NS_LOG_FUNCTION(this << state);
343 m_currentState = state;
344 std::string stateName;
345 switch (state)
346 {
348 stateName = "STANDBY";
349 break;
351 stateName = "TX";
352 break;
354 stateName = "RX";
355 break;
357 stateName = "SLEEP";
358 break;
359 }
360 NS_LOG_DEBUG("LoraRadioEnergyModel:Switching to state: "
361 << stateName << " at time = " << Simulator::Now().GetSeconds() << " s");
362}
363
364// -------------------------------------------------------------------------- //
365
372
377
378void
386
387void
394
395void
397{
398 NS_LOG_FUNCTION(this);
400 {
401 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
402 }
404}
405
406void
408{
409 NS_LOG_FUNCTION(this << txPowerDbm);
411 {
412 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Update tx current callback not set!");
413 }
414 m_updateTxCurrentCallback(txPowerDbm);
416 {
417 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
418 }
420}
421
422void
424{
425 NS_LOG_FUNCTION(this);
427 {
428 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
429 }
431}
432
433void
435{
436 NS_LOG_FUNCTION(this);
438 {
439 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
440 }
442}
443
444/*
445 * Private function state here.
446 */
447
448void
450{
451 NS_LOG_FUNCTION(this);
453 {
454 NS_FATAL_ERROR("LoraRadioEnergyModelPhyListener:Change state callback not set!");
455 }
457}
458
459} // namespace lorawan
460} // namespace ns3
void Nullify()
Discard the implementation, set it to null.
Definition callback.h:561
bool IsNull() const
Check for null implementation.
Definition callback.h:555
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.
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:407
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Base class for device energy models.
virtual void ChangeState(int newState)=0
State
An enumeration of the possible states of an EndDeviceLoraPhy.
@ RX
The PHY layer is receiving a packet.
@ TX
The PHY layer is sending a packet.
@ SLEEP
The PHY layer is sleeping.
@ STANDBY
The PHY layer is in STANDBY.
LoraRadioEnergyModelPhyListener * m_listener
EndDeviceLoraPhy listener.
LoraRadioEnergyModelPhyListener * GetPhyListener()
void HandleEnergyChanged() override
Handles energy recharged.
uint8_t m_nPendingChangeState
pending state change
void SetEnergySource(Ptr< EnergySource > source) override
Sets pointer to EnergySouce installed on node.
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.
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
void SetEnergyDepletionCallback(LoraRadioEnergyDepletionCallback callback)
void SetSleepCurrentA(double sleepCurrentA)
Sets sleep current.
void SetRxCurrentA(double rxCurrentA)
Sets receive current.
Ptr< EnergySource > m_source
energy source
void SetTxCurrentA(double txCurrentA)
Sets transmit current.
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.
DeviceEnergyModel::ChangeStateCallback m_changeStateCallback
Change state callback used to notify the LoraRadioEnergyModel of a state change.
void NotifyStandby() override
Defined in ns3::LoraEndDevicePhyListener.
void NotifySleep() override
Defined in ns3::LoraEndDevicePhyListener.
void SetChangeStateCallback(DeviceEnergyModel::ChangeStateCallback callback)
Sets the change state callback.
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:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#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:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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 Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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.
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:684
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32