A Discrete-Event Network Simulator
API
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// This program produces a gnuplot file that plots the packet success rate
21// as a function of distance for the 802.15.4 models, assuming a default
22// LogDistance propagation loss model, the 2.4 GHz OQPSK error model, a
23// default transmit power of 0 dBm, and a default packet size of 20 bytes of
24// 802.15.4 payload.
25#include <ns3/abort.h>
26#include <ns3/callback.h>
27#include <ns3/command-line.h>
28#include <ns3/constant-position-mobility-model.h>
29#include <ns3/gnuplot.h>
30#include <ns3/log.h>
31#include <ns3/lr-wpan-error-model.h>
32#include <ns3/lr-wpan-mac.h>
33#include <ns3/lr-wpan-net-device.h>
34#include <ns3/lr-wpan-spectrum-value-helper.h>
35#include <ns3/mac16-address.h>
36#include <ns3/multi-model-spectrum-channel.h>
37#include <ns3/net-device.h>
38#include <ns3/node.h>
39#include <ns3/nstime.h>
40#include <ns3/packet.h>
41#include <ns3/propagation-loss-model.h>
42#include <ns3/simulator.h>
43#include <ns3/single-model-spectrum-channel.h>
44#include <ns3/spectrum-value.h>
45#include <ns3/test.h>
46#include <ns3/uinteger.h>
47
48#include <fstream>
49#include <iostream>
50#include <string>
51#include <vector>
52
53using namespace ns3;
54
55static uint32_t g_received = 0;
56
57NS_LOG_COMPONENT_DEFINE("LrWpanErrorDistancePlot");
58
64static void
66{
67 g_received++;
68}
69
70int
71main(int argc, char* argv[])
72{
73 std::ostringstream os;
74 std::ofstream berfile("802.15.4-psr-distance.plt");
75
76 int minDistance = 1;
77 int maxDistance = 200; // meters
78 int increment = 1;
79 int maxPackets = 1000;
80 int packetSize = 20;
81 double txPower = 0;
82 uint32_t channelNumber = 11;
83
84 CommandLine cmd(__FILE__);
85
86 cmd.AddValue("txPower", "transmit power (dBm)", txPower);
87 cmd.AddValue("packetSize", "packet (MSDU) size (bytes)", packetSize);
88 cmd.AddValue("channelNumber", "channel number", channelNumber);
89
90 cmd.Parse(argc, argv);
91
92 os << "Packet (MSDU) size = " << packetSize << " bytes; tx power = " << txPower
93 << " dBm; channel = " << channelNumber;
94
95 Gnuplot psrplot = Gnuplot("802.15.4-psr-distance.eps");
96 Gnuplot2dDataset psrdataset("802.15.4-psr-vs-distance");
97
98 Ptr<Node> n0 = CreateObject<Node>();
99 Ptr<Node> n1 = CreateObject<Node>();
100 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
101 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
102 dev0->SetAddress(Mac16Address("00:01"));
103 dev1->SetAddress(Mac16Address("00:02"));
104 Ptr<MultiModelSpectrumChannel> channel = CreateObject<MultiModelSpectrumChannel>();
105 Ptr<LogDistancePropagationLossModel> model = CreateObject<LogDistancePropagationLossModel>();
106 channel->AddPropagationLossModel(model);
107 dev0->SetChannel(channel);
108 dev1->SetChannel(channel);
109 n0->AddDevice(dev0);
110 n1->AddDevice(dev1);
111 Ptr<ConstantPositionMobilityModel> mob0 = CreateObject<ConstantPositionMobilityModel>();
112 dev0->GetPhy()->SetMobility(mob0);
113 Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel>();
114 dev1->GetPhy()->SetMobility(mob1);
115
117 Ptr<SpectrumValue> psd = svh.CreateTxPowerSpectralDensity(txPower, channelNumber);
118 dev0->GetPhy()->SetTxPowerSpectralDensity(psd);
119
122 dev1->GetMac()->SetMcpsDataIndicationCallback(cb0);
123
125 params.m_srcAddrMode = SHORT_ADDR;
126 params.m_dstAddrMode = SHORT_ADDR;
127 params.m_dstPanId = 0;
128 params.m_dstAddr = Mac16Address("00:02");
129 params.m_msduHandle = 0;
130 params.m_txOptions = 0;
131
132 Ptr<Packet> p;
133 mob0->SetPosition(Vector(0, 0, 0));
134 mob1->SetPosition(Vector(minDistance, 0, 0));
135 for (int j = minDistance; j < maxDistance;)
136 {
137 for (int i = 0; i < maxPackets; i++)
138 {
139 p = Create<Packet>(packetSize);
140 Simulator::Schedule(Seconds(i), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p);
141 }
142 Simulator::Run();
143 NS_LOG_DEBUG("Received " << g_received << " packets for distance " << j);
144 psrdataset.Add(j, g_received / 1000.0);
145 g_received = 0;
146 j += increment;
147 mob1->SetPosition(Vector(j, 0, 0));
148 }
149
150 psrplot.AddDataset(psrdataset);
151
152 psrplot.SetTitle(os.str());
153 psrplot.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
154 psrplot.SetLegend("distance (m)", "Packet Success Rate (PSR)");
155 psrplot.SetExtra("set xrange [0:200]\n\
156set yrange [0:1]\n\
157set grid\n\
158set style line 1 linewidth 5\n\
159set style increment user");
160 psrplot.GenerateOutput(berfile);
161 berfile.close();
162
163 Simulator::Destroy();
164 return 0;
165}
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
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
This class defines all functions to create spectrum model for LrWpan.
Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint32_t channel)
create spectrum value
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
#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
@ SHORT_ADDR
Definition: lr-wpan-mac.h:156
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
static uint32_t g_received
number of packets received
static void LrWpanErrorDistanceCallback(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when a Data indication is invoked.
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:691
cmd
Definition: second.py:33
channel
Definition: third.py:81
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:373
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:345
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:346
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:347
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:348
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:349
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:351
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:352
static const uint32_t packetSize
Packet size generated at the AP.