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 * 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: Tom Henderson <thomas.r.henderson@boeing.com>
18 */
19
20/*
21 This program produces a gnuplot file that plots the packet success rate
22 as a function of distance for the 802.15.4 models, assuming a default
23 LogDistance propagation loss model, the 2.4 GHz OQPSK error model, a
24 default transmit power of 0 dBm, and a default packet size of 20 bytes of
25 802.15.4 payload and a default rx sensitivity of -106.58 dBm.
26
27 Tx power of the transmitter node and the Rx sensitivity of the receiving node
28 as well as the transmitted packet size can be adjusted to obtain a different
29 distance plot.
30
31 Node1 Node2
32 (dev0) --------------------->(dev1)
33
34 Usage:
35
36 ./ns3 run "lr-wpan-error-distance-plot --txPower= 0 --rxSensitivity=-92"
37
38*/
39#include <ns3/abort.h>
40#include <ns3/callback.h>
41#include <ns3/command-line.h>
42#include <ns3/constant-position-mobility-model.h>
43#include <ns3/gnuplot.h>
44#include <ns3/log.h>
45#include <ns3/lr-wpan-error-model.h>
46#include <ns3/lr-wpan-mac.h>
47#include <ns3/lr-wpan-net-device.h>
48#include <ns3/lr-wpan-spectrum-value-helper.h>
49#include <ns3/mac16-address.h>
50#include <ns3/multi-model-spectrum-channel.h>
51#include <ns3/net-device.h>
52#include <ns3/node.h>
53#include <ns3/nstime.h>
54#include <ns3/packet.h>
55#include <ns3/propagation-loss-model.h>
56#include <ns3/simulator.h>
57#include <ns3/single-model-spectrum-channel.h>
58#include <ns3/spectrum-value.h>
59#include <ns3/test.h>
60#include <ns3/uinteger.h>
61
62#include <fstream>
63#include <iostream>
64#include <string>
65#include <vector>
66
67using namespace ns3;
68using namespace ns3::lrwpan;
69
70uint32_t g_packetsReceived = 0; //!< number of packets received
71
72NS_LOG_COMPONENT_DEFINE("LrWpanErrorDistancePlot");
73
74/**
75 * Function called when a Data indication is invoked
76 * \param params MCPS data indication parameters
77 * \param p packet
78 */
79void
81{
83}
84
85int
86main(int argc, char* argv[])
87{
88 std::ostringstream os;
89 std::ofstream berfile("802.15.4-psr-distance.plt");
90
91 int minDistance = 1;
92 int maxDistance = 200; // meters
93 int increment = 1;
94 int maxPackets = 1000;
95 int packetSize = 7; // PSDU = 20 bytes (11 bytes MAC header + 7 bytes MSDU )
96 double txPower = 0;
97 uint32_t channelNumber = 11;
98 double rxSensitivity = -106.58; // dBm
99
100 CommandLine cmd(__FILE__);
101
102 cmd.AddValue("txPower", "transmit power (dBm)", txPower);
103 cmd.AddValue("packetSize", "packet (MSDU) size (bytes)", packetSize);
104 cmd.AddValue("channelNumber", "channel number", channelNumber);
105 cmd.AddValue("rxSensitivity", "the rx sensitivity (dBm)", rxSensitivity);
106
107 cmd.Parse(argc, argv);
108
109 os << "Packet (MSDU) size = " << packetSize << " bytes; tx power = " << txPower
110 << " dBm; channel = " << channelNumber << "; Rx sensitivity = " << rxSensitivity << " dBm";
111
112 Gnuplot psrplot = Gnuplot("802.15.4-psr-distance.eps");
113 Gnuplot2dDataset psrdataset("802.15.4-psr-vs-distance");
114
115 Ptr<Node> n0 = CreateObject<Node>();
116 Ptr<Node> n1 = CreateObject<Node>();
117 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
118 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
119 dev0->SetAddress(Mac16Address("00:01"));
120 dev1->SetAddress(Mac16Address("00:02"));
121 Ptr<MultiModelSpectrumChannel> channel = CreateObject<MultiModelSpectrumChannel>();
122 Ptr<LogDistancePropagationLossModel> model = CreateObject<LogDistancePropagationLossModel>();
123 channel->AddPropagationLossModel(model);
124 dev0->SetChannel(channel);
125 dev1->SetChannel(channel);
126 n0->AddDevice(dev0);
127 n1->AddDevice(dev1);
128 Ptr<ConstantPositionMobilityModel> mob0 = CreateObject<ConstantPositionMobilityModel>();
129 dev0->GetPhy()->SetMobility(mob0);
130 Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel>();
131 dev1->GetPhy()->SetMobility(mob1);
132
134 Ptr<SpectrumValue> psd = svh.CreateTxPowerSpectralDensity(txPower, channelNumber);
135 dev0->GetPhy()->SetTxPowerSpectralDensity(psd);
136
137 // Set Rx sensitivity of the receiving device
138 dev1->GetPhy()->SetRxSensitivity(rxSensitivity);
139
142 dev1->GetMac()->SetMcpsDataIndicationCallback(cb0);
143
145 params.m_srcAddrMode = SHORT_ADDR;
146 params.m_dstAddrMode = SHORT_ADDR;
147 params.m_dstPanId = 0;
148 params.m_dstAddr = Mac16Address("00:02");
149 params.m_msduHandle = 0;
150 params.m_txOptions = 0;
151
152 Ptr<Packet> p;
153 mob0->SetPosition(Vector(0, 0, 0));
154 mob1->SetPosition(Vector(minDistance, 0, 0));
155 for (int j = minDistance; j < maxDistance; j += increment)
156 {
157 for (int i = 0; i < maxPackets; i++)
158 {
159 p = Create<Packet>(packetSize);
160 Simulator::Schedule(Seconds(i), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p);
161 }
163 NS_LOG_DEBUG("Received " << g_packetsReceived << " packets for distance " << j);
164 psrdataset.Add(j, g_packetsReceived / 1000.0);
166
167 mob1->SetPosition(Vector(j, 0, 0));
168 }
169
170 psrplot.AddDataset(psrdataset);
171
172 psrplot.SetTitle(os.str());
173 psrplot.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
174 psrplot.SetLegend("distance (m)", "Packet Success Rate (PSR)");
175 psrplot.SetExtra("set xrange [0:200]\n\
176 set yrange [0:1]\n\
177 set grid\n\
178 set style line 1 linewidth 5\n\
179 set style increment user");
180 psrplot.GenerateOutput(berfile);
181 berfile.close();
182
184 return 0;
185}
Parse command-line arguments.
Definition: command-line.h:232
Class to represent a 2D points plot.
Definition: gnuplot.h:116
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:776
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:764
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:802
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:783
void SetTitle(const std::string &title)
Definition: gnuplot.cc:770
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
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.
Definition: lr-wpan-mac.cc:386
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:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:706
ns cmd
Definition: second.py:40
ns channel
Definition: third.py:88
FtrParams params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
MCPS-DATA.indication params.
static const uint32_t packetSize
Packet size generated at the AP.