A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/mac16-address.h>
41 #include <ns3/constant-position-mobility-model.h>
42 #include <ns3/uinteger.h>
43 #include <ns3/nstime.h>
44 #include <ns3/abort.h>
45 #include <ns3/command-line.h>
46 #include <ns3/gnuplot.h>
47 
48 #include <fstream>
49 #include <iostream>
50 #include <string>
51 #include <vector>
52 
53 using namespace ns3;
54 using namespace std;
55 
56 static uint32_t g_received = 0;
57 
58 NS_LOG_COMPONENT_DEFINE ("LrWpanErrorDistancePlot");
59 
60 static void
62 {
63  g_received++;
64 }
65 
66 int main (int argc, char *argv[])
67 {
68  std::ostringstream os;
69  std::ofstream berfile ("802.15.4-psr-distance.plt");
70 
71  int minDistance = 1;
72  int maxDistance = 200; // meters
73  int increment = 1;
74  int maxPackets = 1000;
75  int packetSize = 20;
76  double txPower = 0;
77  uint32_t channelNumber = 11;
78 
79  CommandLine cmd;
80 
81  cmd.AddValue ("txPower", "transmit power (dBm)", txPower);
82  cmd.AddValue ("packetSize", "packet (MSDU) size (bytes)", packetSize);
83  cmd.AddValue ("channelNumber", "channel number", channelNumber);
84 
85  cmd.Parse (argc, argv);
86 
87  os << "Packet (MSDU) size = " << packetSize << " bytes; tx power = " << txPower << " dBm; channel = " << channelNumber;
88 
89  Gnuplot psrplot = Gnuplot ("802.15.4-psr-distance.eps");
90  Gnuplot2dDataset psrdataset ("802.15.4-psr-vs-distance");
91 
92  Ptr<Node> n0 = CreateObject <Node> ();
93  Ptr<Node> n1 = CreateObject <Node> ();
94  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
95  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
96  dev0->SetAddress (Mac16Address ("00:01"));
97  dev1->SetAddress (Mac16Address ("00:02"));
98  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
99  Ptr<LogDistancePropagationLossModel> model = CreateObject<LogDistancePropagationLossModel> ();
100  channel->AddPropagationLossModel (model);
101  dev0->SetChannel (channel);
102  dev1->SetChannel (channel);
103  n0->AddDevice (dev0);
104  n1->AddDevice (dev1);
105  Ptr<ConstantPositionMobilityModel> mob0 = CreateObject<ConstantPositionMobilityModel> ();
106  dev0->GetPhy ()->SetMobility (mob0);
107  Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel> ();
108  dev1->GetPhy ()->SetMobility (mob1);
109 
111  Ptr<SpectrumValue> psd = svh.CreateTxPowerSpectralDensity (txPower, channelNumber);
112  dev0->GetPhy ()->SetTxPowerSpectralDensity (psd);
113 
116  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb0);
117 
118  McpsDataRequestParams params;
119  params.m_srcAddrMode = SHORT_ADDR;
120  params.m_dstAddrMode = SHORT_ADDR;
121  params.m_dstPanId = 0;
122  params.m_dstAddr = Mac16Address ("00:02");
123  params.m_msduHandle = 0;
124  params.m_txOptions = 0;
125 
126  Ptr<Packet> p;
127  mob0->SetPosition (Vector (0,0,0));
128  mob1->SetPosition (Vector (minDistance,0,0));
129  for (int j = minDistance; j < maxDistance; )
130  {
131  for (int i = 0; i < maxPackets; i++)
132  {
133  p = Create<Packet> (packetSize);
134  Simulator::Schedule (Seconds (i),
136  dev0->GetMac (), params, p);
137  }
138  Simulator::Run ();
139  NS_LOG_DEBUG ("Received " << g_received << " packets for distance " << j);
140  psrdataset.Add (j, g_received / 1000.0);
141  g_received = 0;
142  j += increment;
143  mob1->SetPosition (Vector (j,0,0));
144  }
145 
146  psrplot.AddDataset (psrdataset);
147 
148  psrplot.SetTitle (os.str ());
149  psrplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
150  psrplot.SetLegend ("distance (m)", "Packet Success Rate (PSR)");
151  psrplot.SetExtra ("set xrange [0:200]\n\
152 set yrange [0:1]\n\
153 set grid\n\
154 set style line 1 linewidth 5\n\
155 set style increment user");
156  psrplot.GenerateOutput (berfile);
157  berfile.close ();
158 
160  return 0;
161 }
162 
Class to represent a 2D points plot.
Definition: gnuplot.h:113
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
set the single-frequency propagation loss model to be used
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p)
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU. ...
Definition: lr-wpan-mac.cc:220
a 3d vector
Definition: vector.h:31
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:367
void SetTitle(const std::string &title)
Definition: gnuplot.cc:730
This class defines all functions to create spectrum model for LrWpan.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
void Add(double x, double y)
Definition: gnuplot.cc:359
Parse command-line arguments.
Definition: command-line.h:177
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
LrWpanAddressMode m_srcAddrMode
Definition: lr-wpan-mac.h:119
LrWpanAddressMode m_dstAddrMode
Definition: lr-wpan-mac.h:120
This class can contain 16 bit addresses.
Definition: mac16-address.h:39
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:743
void SetPosition(const Vector &position)
static void LrWpanErrorDistanceCallback(McpsDataIndicationParams params, Ptr< Packet > p)
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:120
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:435
Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint32_t channel)
create spectrum value
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
int main(int argc, char *argv[])
void Parse(int argc, char *argv[])
Parse the program arguments.
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
static uint32_t g_received