A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lorawan-energy-model-example.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: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
9/*
10 * This script simulates a simple network to explain how the Lora energy model
11 * works.
12 */
13
14#include "ns3/basic-energy-source-helper.h"
15#include "ns3/core-module.h"
16#include "ns3/file-helper.h"
17#include "ns3/lorawan-module.h"
18#include "ns3/mobility-helper.h"
19
20using namespace ns3;
21using namespace lorawan;
22using namespace energy;
23
24NS_LOG_COMPONENT_DEFINE("LoraEnergyModelExample");
25
26int
27main(int argc, char* argv[])
28{
29 // Set up logging
30 LogComponentEnable("LoraEnergyModelExample", LOG_LEVEL_ALL);
31 // LogComponentEnable ("LoraRadioEnergyModel", LOG_LEVEL_ALL);
32 // LogComponentEnable ("LoraChannel", LOG_LEVEL_INFO);
33 // LogComponentEnable ("LoraPhy", LOG_LEVEL_ALL);
34 // LogComponentEnable ("EndDeviceLoraPhy", LOG_LEVEL_ALL);
35 // LogComponentEnable ("GatewayLoraPhy", LOG_LEVEL_ALL);
36 // LogComponentEnable ("LoraInterferenceHelper", LOG_LEVEL_ALL);
37 // LogComponentEnable ("LorawanMac", LOG_LEVEL_ALL);
38 // LogComponentEnable ("EndDeviceLorawanMac", LOG_LEVEL_ALL);
39 // LogComponentEnable ("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
40 // LogComponentEnable ("GatewayLorawanMac", LOG_LEVEL_ALL);
41 // LogComponentEnable ("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
42 // LogComponentEnable ("LogicalLoraChannel", LOG_LEVEL_ALL);
43 // LogComponentEnable ("LoraHelper", LOG_LEVEL_ALL);
44 // LogComponentEnable ("LoraPhyHelper", LOG_LEVEL_ALL);
45 // LogComponentEnable ("LorawanMacHelper", LOG_LEVEL_ALL);
46 // LogComponentEnable ("OneShotSenderHelper", LOG_LEVEL_ALL);
47 // LogComponentEnable ("OneShotSender", LOG_LEVEL_ALL);
48 // LogComponentEnable ("LorawanMacHeader", LOG_LEVEL_ALL);
49 // LogComponentEnable ("LoraFrameHeader", LOG_LEVEL_ALL);
53
54 /************************
55 * Create the channel *
56 ************************/
57
58 NS_LOG_INFO("Creating the channel...");
59
60 // Create the lora channel object
62 loss->SetPathLossExponent(3.76);
63 loss->SetReference(1, 7.7);
64
66
68
69 /************************
70 * Create the helpers *
71 ************************/
72
73 NS_LOG_INFO("Setting up helpers...");
74
77 allocator->Add(Vector(100, 0, 0));
78 allocator->Add(Vector(0, 0, 0));
79 mobility.SetPositionAllocator(allocator);
80 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
81
82 // Create the LoraPhyHelper
83 LoraPhyHelper phyHelper = LoraPhyHelper();
84 phyHelper.SetChannel(channel);
85
86 // Create the LorawanMacHelper
88
89 // Create the LoraHelper
90 LoraHelper helper = LoraHelper();
91
92 /************************
93 * Create End Devices *
94 ************************/
95
96 NS_LOG_INFO("Creating the end device...");
97
98 // Create a set of nodes
99 NodeContainer endDevices;
100 endDevices.Create(1);
101
102 // Assign a mobility model to the node
103 mobility.Install(endDevices);
104
105 // Create the LoraNetDevices of the end devices
108 NetDeviceContainer endDevicesNetDevices = helper.Install(phyHelper, macHelper, endDevices);
109
110 /*********************
111 * Create Gateways *
112 *********************/
113
114 NS_LOG_INFO("Creating the gateway...");
115 NodeContainer gateways;
116 gateways.Create(1);
117
118 mobility.SetPositionAllocator(allocator);
119 mobility.Install(gateways);
120
121 // Create a netdevice for each gateway
124 helper.Install(phyHelper, macHelper, gateways);
125
126 LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
127
128 /*********************************************
129 * Install applications on the end devices *
130 *********************************************/
131
132 // OneShotSenderHelper oneShotSenderHelper;
133 // oneShotSenderHelper.SetSendTime (Seconds (10));
134
135 // oneShotSenderHelper.Install (endDevices);
136
137 PeriodicSenderHelper periodicSenderHelper;
138 periodicSenderHelper.SetPeriod(Seconds(5));
139
140 periodicSenderHelper.Install(endDevices);
141
142 /************************
143 * Install Energy Model *
144 ************************/
145
146 BasicEnergySourceHelper basicSourceHelper;
147 LoraRadioEnergyModelHelper radioEnergyHelper;
148
149 // configure energy source
150 basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000)); // Energy in J
151 basicSourceHelper.Set("BasicEnergySupplyVoltageV", DoubleValue(3.3));
152
153 radioEnergyHelper.Set("StandbyCurrentA", DoubleValue(0.0014));
154 radioEnergyHelper.Set("TxCurrentA", DoubleValue(0.028));
155 radioEnergyHelper.Set("SleepCurrentA", DoubleValue(0.0000015));
156 radioEnergyHelper.Set("RxCurrentA", DoubleValue(0.0112));
157
158 radioEnergyHelper.SetTxCurrentModel("ns3::ConstantLoraTxCurrentModel",
159 "TxCurrent",
160 DoubleValue(0.028));
161
162 // install source on end devices' nodes
163 EnergySourceContainer sources = basicSourceHelper.Install(endDevices);
164 Names::Add("/Names/EnergySource", sources.Get(0));
165
166 // install device model
167 DeviceEnergyModelContainer deviceModels =
168 radioEnergyHelper.Install(endDevicesNetDevices, sources);
169
170 /**************
171 * Get output *
172 **************/
173 FileHelper fileHelper;
174 fileHelper.ConfigureFile("battery-level", FileAggregator::SPACE_SEPARATED);
175 fileHelper.WriteProbe("ns3::DoubleProbe", "/Names/EnergySource/RemainingEnergy", "Output");
176
177 /****************
178 * Simulation *
179 ****************/
180
182
184
186
187 return 0;
188}
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v) override
energy::DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< energy::EnergySource > source) const
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
energy::EnergySourceContainer Install(Ptr< Node > node) const
Helper class used to put data values into a file.
Definition file-helper.h:29
void WriteProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource)
void ConfigureFile(const std::string &outputFileNameWithoutExtension, FileAggregator::FileType fileType=FileAggregator::SPACE_SEPARATED)
Helper class used to assign positions and mobility models to nodes.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
Holds a vector of ns3::DeviceEnergyModel pointers.
Holds a vector of ns3::EnergySource pointers.
Ptr< EnergySource > Get(uint32_t i) const
Get the i-th Ptr<EnergySource> stored in this container.
Helps to create LoraNetDevice objects.
Definition lora-helper.h:34
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
Helper to install LoraPhy instances on multiple Nodes.
void SetDeviceType(enum DeviceType dt)
Set the kind of PHY this helper will create.
void SetChannel(Ptr< LoraChannel > channel)
Set the LoraChannel to connect the PHYs to.
Installs LoraRadioEnergyModel on devices.
void Set(std::string name, const AttributeValue &v) override
void SetTxCurrentModel(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Helper class for configuring and installing the LorawanMac class on devices and gateways.
void SetDeviceType(enum DeviceType dt)
Set the kind of MAC this helper will create.
static std::vector< int > SetSpreadingFactorsUp(NodeContainer endDevices, NodeContainer gateways, Ptr< LoraChannel > channel)
Initialize the end devices' data rate parameter.
This class can be used to install PeriodicSender applications on a wide range of nodes.
void SetPeriod(Time period)
Set the period to be used by the applications created by this helper.
ApplicationContainer Install(NodeContainer c) const
Install a PeriodicSender application on each node of the input container configured with all the attr...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:267
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
Time Hours(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:279
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:108
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:110
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:111
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:112
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:297
channel
Definition third.py:77
mobility
Definition third.py:92