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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 */
19
20/*
21 * This script simulates a simple network to explain how the Lora energy model
22 * works.
23 */
24
25#include "ns3/basic-energy-source-helper.h"
26#include "ns3/class-a-end-device-lorawan-mac.h"
27#include "ns3/command-line.h"
28#include "ns3/constant-position-mobility-model.h"
29#include "ns3/end-device-lora-phy.h"
30#include "ns3/file-helper.h"
31#include "ns3/gateway-lora-phy.h"
32#include "ns3/gateway-lorawan-mac.h"
33#include "ns3/log.h"
34#include "ns3/lora-helper.h"
35#include "ns3/lora-radio-energy-model-helper.h"
36#include "ns3/mobility-helper.h"
37#include "ns3/names.h"
38#include "ns3/node-container.h"
39#include "ns3/periodic-sender-helper.h"
40#include "ns3/position-allocator.h"
41#include "ns3/simulator.h"
42
43#include <algorithm>
44#include <ctime>
45
46using namespace ns3;
47using namespace lorawan;
48
49NS_LOG_COMPONENT_DEFINE("LoraEnergyModelExample");
50
51int
52main(int argc, char* argv[])
53{
54 // Set up logging
55 LogComponentEnable("LoraEnergyModelExample", LOG_LEVEL_ALL);
56 // LogComponentEnable ("LoraRadioEnergyModel", LOG_LEVEL_ALL);
57 // LogComponentEnable ("LoraChannel", LOG_LEVEL_INFO);
58 // LogComponentEnable ("LoraPhy", LOG_LEVEL_ALL);
59 // LogComponentEnable ("EndDeviceLoraPhy", LOG_LEVEL_ALL);
60 // LogComponentEnable ("GatewayLoraPhy", LOG_LEVEL_ALL);
61 // LogComponentEnable ("LoraInterferenceHelper", LOG_LEVEL_ALL);
62 // LogComponentEnable ("LorawanMac", LOG_LEVEL_ALL);
63 // LogComponentEnable ("EndDeviceLorawanMac", LOG_LEVEL_ALL);
64 // LogComponentEnable ("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
65 // LogComponentEnable ("GatewayLorawanMac", LOG_LEVEL_ALL);
66 // LogComponentEnable ("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
67 // LogComponentEnable ("LogicalLoraChannel", LOG_LEVEL_ALL);
68 // LogComponentEnable ("LoraHelper", LOG_LEVEL_ALL);
69 // LogComponentEnable ("LoraPhyHelper", LOG_LEVEL_ALL);
70 // LogComponentEnable ("LorawanMacHelper", LOG_LEVEL_ALL);
71 // LogComponentEnable ("OneShotSenderHelper", LOG_LEVEL_ALL);
72 // LogComponentEnable ("OneShotSender", LOG_LEVEL_ALL);
73 // LogComponentEnable ("LorawanMacHeader", LOG_LEVEL_ALL);
74 // LogComponentEnable ("LoraFrameHeader", LOG_LEVEL_ALL);
78
79 /************************
80 * Create the channel *
81 ************************/
82
83 NS_LOG_INFO("Creating the channel...");
84
85 // Create the lora channel object
86 Ptr<LogDistancePropagationLossModel> loss = CreateObject<LogDistancePropagationLossModel>();
87 loss->SetPathLossExponent(3.76);
88 loss->SetReference(1, 7.7);
89
90 Ptr<PropagationDelayModel> delay = CreateObject<ConstantSpeedPropagationDelayModel>();
91
92 Ptr<LoraChannel> channel = CreateObject<LoraChannel>(loss, delay);
93
94 /************************
95 * Create the helpers *
96 ************************/
97
98 NS_LOG_INFO("Setting up helpers...");
99
101 Ptr<ListPositionAllocator> allocator = CreateObject<ListPositionAllocator>();
102 allocator->Add(Vector(100, 0, 0));
103 allocator->Add(Vector(0, 0, 0));
104 mobility.SetPositionAllocator(allocator);
105 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
106
107 // Create the LoraPhyHelper
108 LoraPhyHelper phyHelper = LoraPhyHelper();
109 phyHelper.SetChannel(channel);
110
111 // Create the LorawanMacHelper
113
114 // Create the LoraHelper
115 LoraHelper helper = LoraHelper();
116
117 /************************
118 * Create End Devices *
119 ************************/
120
121 NS_LOG_INFO("Creating the end device...");
122
123 // Create a set of nodes
124 NodeContainer endDevices;
125 endDevices.Create(1);
126
127 // Assign a mobility model to the node
128 mobility.Install(endDevices);
129
130 // Create the LoraNetDevices of the end devices
133 NetDeviceContainer endDevicesNetDevices = helper.Install(phyHelper, macHelper, endDevices);
134
135 /*********************
136 * Create Gateways *
137 *********************/
138
139 NS_LOG_INFO("Creating the gateway...");
140 NodeContainer gateways;
141 gateways.Create(1);
142
143 mobility.SetPositionAllocator(allocator);
144 mobility.Install(gateways);
145
146 // Create a netdevice for each gateway
149 helper.Install(phyHelper, macHelper, gateways);
150
151 LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
152
153 /*********************************************
154 * Install applications on the end devices *
155 *********************************************/
156
157 // OneShotSenderHelper oneShotSenderHelper;
158 // oneShotSenderHelper.SetSendTime (Seconds (10));
159
160 // oneShotSenderHelper.Install (endDevices);
161
162 PeriodicSenderHelper periodicSenderHelper;
163 periodicSenderHelper.SetPeriod(Seconds(5));
164
165 periodicSenderHelper.Install(endDevices);
166
167 /************************
168 * Install Energy Model *
169 ************************/
170
171 BasicEnergySourceHelper basicSourceHelper;
172 LoraRadioEnergyModelHelper radioEnergyHelper;
173
174 // configure energy source
175 basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000)); // Energy in J
176 basicSourceHelper.Set("BasicEnergySupplyVoltageV", DoubleValue(3.3));
177
178 radioEnergyHelper.Set("StandbyCurrentA", DoubleValue(0.0014));
179 radioEnergyHelper.Set("TxCurrentA", DoubleValue(0.028));
180 radioEnergyHelper.Set("SleepCurrentA", DoubleValue(0.0000015));
181 radioEnergyHelper.Set("RxCurrentA", DoubleValue(0.0112));
182
183 radioEnergyHelper.SetTxCurrentModel("ns3::ConstantLoraTxCurrentModel",
184 "TxCurrent",
185 DoubleValue(0.028));
186
187 // install source on end devices' nodes
188 EnergySourceContainer sources = basicSourceHelper.Install(endDevices);
189 Names::Add("/Names/EnergySource", sources.Get(0));
190
191 // install device model
192 DeviceEnergyModelContainer deviceModels =
193 radioEnergyHelper.Install(endDevicesNetDevices, sources);
194
195 /**************
196 * Get output *
197 **************/
198 FileHelper fileHelper;
199 fileHelper.ConfigureFile("battery-level", FileAggregator::SPACE_SEPARATED);
200 fileHelper.WriteProbe("ns3::DoubleProbe", "/Names/EnergySource/RemainingEnergy", "Output");
201
202 /****************
203 * Simulation *
204 ****************/
205
207
209
211
212 return 0;
213}
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:42
energy::EnergySourceContainer Install(Ptr< Node > node) const
Helper class used to put data values into a file.
Definition: file-helper.h:40
void WriteProbe(const std::string &typeId, const std::string &path, const std::string &probeTraceSource)
Definition: file-helper.cc:91
void ConfigureFile(const std::string &outputFileNameWithoutExtension, FileAggregator::FileType fileType=FileAggregator::SPACE_SEPARATED)
Definition: file-helper.cc:69
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:775
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:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:48
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
Definition: lora-helper.cc:44
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:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time Hours(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1295
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:302
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition: log.h:118
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
ns channel
Definition: third.py:88
ns mobility
Definition: third.py:103