A Discrete-Event Network Simulator
API
lr-wpan-error-distance-plot.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 The Boeing Company
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Tom Henderson <thomas.r.henderson@boeing.com>
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.
26#include <ns3/test.h>
27#include <ns3/log.h>
28#include <ns3/callback.h>
29#include <ns3/packet.h>
30#include <ns3/simulator.h>
31#include <ns3/lr-wpan-error-model.h>
32#include <ns3/propagation-loss-model.h>
33#include <ns3/lr-wpan-net-device.h>
34#include <ns3/spectrum-value.h>
35#include <ns3/lr-wpan-spectrum-value-helper.h>
36#include <ns3/lr-wpan-mac.h>
37#include <ns3/node.h>
38#include <ns3/net-device.h>
39#include <ns3/single-model-spectrum-channel.h>
40#include <ns3/multi-model-spectrum-channel.h>
41#include <ns3/mac16-address.h>
42#include <ns3/constant-position-mobility-model.h>
43#include <ns3/uinteger.h>
44#include <ns3/nstime.h>
45#include <ns3/abort.h>
46#include <ns3/command-line.h>
47#include <ns3/gnuplot.h>
48
49#include <fstream>
50#include <iostream>
51#include <string>
52#include <vector>
53
54using namespace ns3;
55using namespace std;
56
57static uint32_t g_received = 0;
58
59NS_LOG_COMPONENT_DEFINE ("LrWpanErrorDistancePlot");
60
66static void
68{
69 g_received++;
70}
71
72int main (int argc, char *argv[])
73{
74 std::ostringstream os;
75 std::ofstream berfile ("802.15.4-psr-distance.plt");
76
77 int minDistance = 1;
78 int maxDistance = 200; // meters
79 int increment = 1;
80 int maxPackets = 1000;
81 int packetSize = 20;
82 double txPower = 0;
83 uint32_t channelNumber = 11;
84
85 CommandLine cmd (__FILE__);
86
87 cmd.AddValue ("txPower", "transmit power (dBm)", txPower);
88 cmd.AddValue ("packetSize", "packet (MSDU) size (bytes)", packetSize);
89 cmd.AddValue ("channelNumber", "channel number", channelNumber);
90
91 cmd.Parse (argc, argv);
92
93 os << "Packet (MSDU) size = " << packetSize << " bytes; tx power = " << txPower << " 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),
141 &LrWpanMac::McpsDataRequest,
142 dev0->GetMac (), params, p);
143 }
144 Simulator::Run ();
145 NS_LOG_DEBUG ("Received " << g_received << " packets for distance " << j);
146 psrdataset.Add (j, g_received / 1000.0);
147 g_received = 0;
148 j += increment;
149 mob1->SetPosition (Vector (j,0,0));
150 }
151
152 psrplot.AddDataset (psrdataset);
153
154 psrplot.SetTitle (os.str ());
155 psrplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
156 psrplot.SetLegend ("distance (m)", "Packet Success Rate (PSR)");
157 psrplot.SetExtra ("set xrange [0:200]\n\
158set yrange [0:1]\n\
159set grid\n\
160set style line 1 linewidth 5\n\
161set style increment user");
162 psrplot.GenerateOutput (berfile);
163 berfile.close ();
164
165 Simulator::Destroy ();
166 return 0;
167}
168
Parse command-line arguments.
Definition: command-line.h:229
Class to represent a 2D points plot.
Definition: gnuplot.h:118
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:372
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:758
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:738
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:726
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:764
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:745
void SetTitle(const std::string &title)
Definition: gnuplot.cc:732
virtual void SetAddress(Address address)
This method indirects to LrWpanMac::SetShortAddress ()
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
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:42
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
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:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
@ SHORT_ADDR
Definition: lr-wpan-mac.h:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
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, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
STL namespace.
channel
Definition: third.py:92
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:271
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:237
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:246
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:247
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:248
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:249
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:251
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:252
static const uint32_t packetSize
Pcket size generated at the AP.