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/class-a-end-device-lorawan-mac.h"
16#include "ns3/command-line.h"
17#include "ns3/constant-position-mobility-model.h"
18#include "ns3/end-device-lora-phy.h"
19#include "ns3/file-helper.h"
20#include "ns3/gateway-lora-phy.h"
21#include "ns3/gateway-lorawan-mac.h"
22#include "ns3/log.h"
23#include "ns3/lora-helper.h"
24#include "ns3/lora-radio-energy-model-helper.h"
25#include "ns3/mobility-helper.h"
26#include "ns3/names.h"
27#include "ns3/node-container.h"
28#include "ns3/periodic-sender-helper.h"
29#include "ns3/position-allocator.h"
30#include "ns3/simulator.h"
31
32#include <algorithm>
33#include <ctime>
34
35using namespace ns3;
36using namespace lorawan;
37
38NS_LOG_COMPONENT_DEFINE("LoraEnergyModelExample");
39
40int
41main(int argc, char* argv[])
42{
43 // Set up logging
44 LogComponentEnable("LoraEnergyModelExample", LOG_LEVEL_ALL);
45 // LogComponentEnable ("LoraRadioEnergyModel", LOG_LEVEL_ALL);
46 // LogComponentEnable ("LoraChannel", LOG_LEVEL_INFO);
47 // LogComponentEnable ("LoraPhy", LOG_LEVEL_ALL);
48 // LogComponentEnable ("EndDeviceLoraPhy", LOG_LEVEL_ALL);
49 // LogComponentEnable ("GatewayLoraPhy", LOG_LEVEL_ALL);
50 // LogComponentEnable ("LoraInterferenceHelper", LOG_LEVEL_ALL);
51 // LogComponentEnable ("LorawanMac", LOG_LEVEL_ALL);
52 // LogComponentEnable ("EndDeviceLorawanMac", LOG_LEVEL_ALL);
53 // LogComponentEnable ("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
54 // LogComponentEnable ("GatewayLorawanMac", LOG_LEVEL_ALL);
55 // LogComponentEnable ("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
56 // LogComponentEnable ("LogicalLoraChannel", LOG_LEVEL_ALL);
57 // LogComponentEnable ("LoraHelper", LOG_LEVEL_ALL);
58 // LogComponentEnable ("LoraPhyHelper", LOG_LEVEL_ALL);
59 // LogComponentEnable ("LorawanMacHelper", LOG_LEVEL_ALL);
60 // LogComponentEnable ("OneShotSenderHelper", LOG_LEVEL_ALL);
61 // LogComponentEnable ("OneShotSender", LOG_LEVEL_ALL);
62 // LogComponentEnable ("LorawanMacHeader", LOG_LEVEL_ALL);
63 // LogComponentEnable ("LoraFrameHeader", LOG_LEVEL_ALL);
67
68 /************************
69 * Create the channel *
70 ************************/
71
72 NS_LOG_INFO("Creating the channel...");
73
74 // Create the lora channel object
76 loss->SetPathLossExponent(3.76);
77 loss->SetReference(1, 7.7);
78
80
82
83 /************************
84 * Create the helpers *
85 ************************/
86
87 NS_LOG_INFO("Setting up helpers...");
88
91 allocator->Add(Vector(100, 0, 0));
92 allocator->Add(Vector(0, 0, 0));
93 mobility.SetPositionAllocator(allocator);
94 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
95
96 // Create the LoraPhyHelper
97 LoraPhyHelper phyHelper = LoraPhyHelper();
98 phyHelper.SetChannel(channel);
99
100 // Create the LorawanMacHelper
102
103 // Create the LoraHelper
104 LoraHelper helper = LoraHelper();
105
106 /************************
107 * Create End Devices *
108 ************************/
109
110 NS_LOG_INFO("Creating the end device...");
111
112 // Create a set of nodes
113 NodeContainer endDevices;
114 endDevices.Create(1);
115
116 // Assign a mobility model to the node
117 mobility.Install(endDevices);
118
119 // Create the LoraNetDevices of the end devices
122 NetDeviceContainer endDevicesNetDevices = helper.Install(phyHelper, macHelper, endDevices);
123
124 /*********************
125 * Create Gateways *
126 *********************/
127
128 NS_LOG_INFO("Creating the gateway...");
129 NodeContainer gateways;
130 gateways.Create(1);
131
132 mobility.SetPositionAllocator(allocator);
133 mobility.Install(gateways);
134
135 // Create a netdevice for each gateway
138 helper.Install(phyHelper, macHelper, gateways);
139
140 LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
141
142 /*********************************************
143 * Install applications on the end devices *
144 *********************************************/
145
146 // OneShotSenderHelper oneShotSenderHelper;
147 // oneShotSenderHelper.SetSendTime (Seconds (10));
148
149 // oneShotSenderHelper.Install (endDevices);
150
151 PeriodicSenderHelper periodicSenderHelper;
152 periodicSenderHelper.SetPeriod(Seconds(5));
153
154 periodicSenderHelper.Install(endDevices);
155
156 /************************
157 * Install Energy Model *
158 ************************/
159
160 BasicEnergySourceHelper basicSourceHelper;
161 LoraRadioEnergyModelHelper radioEnergyHelper;
162
163 // configure energy source
164 basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000)); // Energy in J
165 basicSourceHelper.Set("BasicEnergySupplyVoltageV", DoubleValue(3.3));
166
167 radioEnergyHelper.Set("StandbyCurrentA", DoubleValue(0.0014));
168 radioEnergyHelper.Set("TxCurrentA", DoubleValue(0.028));
169 radioEnergyHelper.Set("SleepCurrentA", DoubleValue(0.0000015));
170 radioEnergyHelper.Set("RxCurrentA", DoubleValue(0.0112));
171
172 radioEnergyHelper.SetTxCurrentModel("ns3::ConstantLoraTxCurrentModel",
173 "TxCurrent",
174 DoubleValue(0.028));
175
176 // install source on end devices' nodes
177 EnergySourceContainer sources = basicSourceHelper.Install(endDevices);
178 Names::Add("/Names/EnergySource", sources.Get(0));
179
180 // install device model
181 DeviceEnergyModelContainer deviceModels =
182 radioEnergyHelper.Install(endDevicesNetDevices, sources);
183
184 /**************
185 * Get output *
186 **************/
187 FileHelper fileHelper;
188 fileHelper.ConfigureFile("battery-level", FileAggregator::SPACE_SEPARATED);
189 fileHelper.WriteProbe("ns3::DoubleProbe", "/Names/EnergySource/RemainingEnergy", "Output");
190
191 /****************
192 * Simulation *
193 ****************/
194
196
198
200
201 return 0;
202}
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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:37
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:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time Hours(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1284
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:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:107
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:109
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
channel
Definition third.py:77
mobility
Definition third.py:92