A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-error-distance-plot.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tom Henderson <thomas.r.henderson@boeing.com>
7 */
8
9/*
10 This program produces a gnuplot file that plots the packet success rate
11 as a function of distance for the 802.15.4 models, assuming a default
12 LogDistance propagation loss model, the 2.4 GHz OQPSK error model, a
13 default transmit power of 0 dBm, and a default packet size of 20 bytes of
14 802.15.4 payload and a default rx sensitivity of -106.58 dBm.
15
16 Tx power of the transmitter node and the Rx sensitivity of the receiving node
17 as well as the transmitted packet size can be adjusted to obtain a different
18 distance plot.
19
20 Node1 Node2
21 (dev0) --------------------->(dev1)
22
23 Usage:
24
25 ./ns3 run "lr-wpan-error-distance-plot --txPower= 0 --rxSensitivity=-92"
26
27*/
28#include <ns3/abort.h>
29#include <ns3/callback.h>
30#include <ns3/command-line.h>
31#include <ns3/constant-position-mobility-model.h>
32#include <ns3/gnuplot.h>
33#include <ns3/log.h>
34#include <ns3/lr-wpan-error-model.h>
35#include <ns3/lr-wpan-mac.h>
36#include <ns3/lr-wpan-net-device.h>
37#include <ns3/lr-wpan-spectrum-value-helper.h>
38#include <ns3/mac16-address.h>
39#include <ns3/multi-model-spectrum-channel.h>
40#include <ns3/net-device.h>
41#include <ns3/node.h>
42#include <ns3/nstime.h>
43#include <ns3/packet.h>
44#include <ns3/propagation-loss-model.h>
45#include <ns3/simulator.h>
46#include <ns3/single-model-spectrum-channel.h>
47#include <ns3/spectrum-value.h>
48#include <ns3/test.h>
49#include <ns3/uinteger.h>
50
51#include <fstream>
52#include <iostream>
53#include <string>
54#include <vector>
55
56using namespace ns3;
57using namespace ns3::lrwpan;
58
59uint32_t g_packetsReceived = 0; //!< number of packets received
60
61NS_LOG_COMPONENT_DEFINE("LrWpanErrorDistancePlot");
62
63/**
64 * Function called when a Data indication is invoked
65 * \param params MCPS data indication parameters
66 * \param p packet
67 */
68void
73
74int
75main(int argc, char* argv[])
76{
77 std::ostringstream os;
78 std::ofstream berfile("802.15.4-psr-distance.plt");
79
80 int minDistance = 1;
81 int maxDistance = 200; // meters
82 int increment = 1;
83 int maxPackets = 1000;
84 int packetSize = 7; // PSDU = 20 bytes (11 bytes MAC header + 7 bytes MSDU )
85 double txPower = 0;
86 uint32_t channelNumber = 11;
87 double rxSensitivity = -106.58; // dBm
88
89 CommandLine cmd(__FILE__);
90
91 cmd.AddValue("txPower", "transmit power (dBm)", txPower);
92 cmd.AddValue("packetSize", "packet (MSDU) size (bytes)", packetSize);
93 cmd.AddValue("channelNumber", "channel number", channelNumber);
94 cmd.AddValue("rxSensitivity", "the rx sensitivity (dBm)", rxSensitivity);
95
96 cmd.Parse(argc, argv);
97
98 os << "Packet (MSDU) size = " << packetSize << " bytes; tx power = " << txPower
99 << " dBm; channel = " << channelNumber << "; Rx sensitivity = " << rxSensitivity << " dBm";
100
101 Gnuplot psrplot = Gnuplot("802.15.4-psr-distance.eps");
102 Gnuplot2dDataset psrdataset("802.15.4-psr-vs-distance");
103
108 dev0->SetAddress(Mac16Address("00:01"));
109 dev1->SetAddress(Mac16Address("00:02"));
112 channel->AddPropagationLossModel(model);
113 dev0->SetChannel(channel);
114 dev1->SetChannel(channel);
115 n0->AddDevice(dev0);
116 n1->AddDevice(dev1);
118 dev0->GetPhy()->SetMobility(mob0);
120 dev1->GetPhy()->SetMobility(mob1);
121
123 Ptr<SpectrumValue> psd = svh.CreateTxPowerSpectralDensity(txPower, channelNumber);
124 dev0->GetPhy()->SetTxPowerSpectralDensity(psd);
125
126 // Set Rx sensitivity of the receiving device
127 dev1->GetPhy()->SetRxSensitivity(rxSensitivity);
128
131 dev1->GetMac()->SetMcpsDataIndicationCallback(cb0);
132
134 params.m_srcAddrMode = SHORT_ADDR;
135 params.m_dstAddrMode = SHORT_ADDR;
136 params.m_dstPanId = 0;
137 params.m_dstAddr = Mac16Address("00:02");
138 params.m_msduHandle = 0;
139 params.m_txOptions = 0;
140
141 Ptr<Packet> p;
142 mob0->SetPosition(Vector(0, 0, 0));
143 mob1->SetPosition(Vector(minDistance, 0, 0));
144 for (int j = minDistance; j < maxDistance; j += increment)
145 {
146 for (int i = 0; i < maxPackets; i++)
147 {
149 Simulator::Schedule(Seconds(i), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p);
150 }
152 NS_LOG_DEBUG("Received " << g_packetsReceived << " packets for distance " << j);
153 psrdataset.Add(j, g_packetsReceived / 1000.0);
155
156 mob1->SetPosition(Vector(j, 0, 0));
157 }
158
159 psrplot.AddDataset(psrdataset);
160
161 psrplot.SetTitle(os.str());
162 psrplot.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
163 psrplot.SetLegend("distance (m)", "Packet Success Rate (PSR)");
164 psrplot.SetExtra("set xrange [0:200]\n\
165 set yrange [0:1]\n\
166 set grid\n\
167 set style line 1 linewidth 5\n\
168 set style increment user");
169 psrplot.GenerateOutput(berfile);
170 berfile.close();
171
173 return 0;
174}
Parse command-line arguments.
Class to represent a 2D points plot.
Definition gnuplot.h:105
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition gnuplot.h:359
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition gnuplot.cc:765
void SetTerminal(const std::string &terminal)
Definition gnuplot.cc:753
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
void SetExtra(const std::string &extra)
Definition gnuplot.cc:772
void SetTitle(const std::string &title)
Definition gnuplot.cc:759
This class can contain 16 bit addresses.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
This class defines all functions to create spectrum model for LrWpan.
Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint32_t channel)
create spectrum value
#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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
void LrWpanErrorDistanceCallback(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when a Data indication is invoked.
uint32_t g_packetsReceived
number of packets received
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
channel
Definition third.py:77
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
static const uint32_t packetSize
Packet size generated at the AP.