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 
54 using namespace ns3;
55 using namespace std;
56 
57 static uint32_t g_received = 0;
58 
59 NS_LOG_COMPONENT_DEFINE ("LrWpanErrorDistancePlot");
60 
61 static void
63 {
64  g_received++;
65 }
66 
67 int main (int argc, char *argv[])
68 {
69  std::ostringstream os;
70  std::ofstream berfile ("802.15.4-psr-distance.plt");
71 
72  int minDistance = 1;
73  int maxDistance = 200; // meters
74  int increment = 1;
75  int maxPackets = 1000;
76  int packetSize = 20;
77  double txPower = 0;
78  uint32_t channelNumber = 11;
79 
81 
82  cmd.AddValue ("txPower", "transmit power (dBm)", txPower);
83  cmd.AddValue ("packetSize", "packet (MSDU) size (bytes)", packetSize);
84  cmd.AddValue ("channelNumber", "channel number", channelNumber);
85 
86  cmd.Parse (argc, argv);
87 
88  os << "Packet (MSDU) size = " << packetSize << " bytes; tx power = " << txPower << " dBm; channel = " << channelNumber;
89 
90  Gnuplot psrplot = Gnuplot ("802.15.4-psr-distance.eps");
91  Gnuplot2dDataset psrdataset ("802.15.4-psr-vs-distance");
92 
93  Ptr<Node> n0 = CreateObject <Node> ();
94  Ptr<Node> n1 = CreateObject <Node> ();
95  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
96  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
97  dev0->SetAddress (Mac16Address ("00:01"));
98  dev1->SetAddress (Mac16Address ("00:02"));
99  Ptr<MultiModelSpectrumChannel> channel = CreateObject<MultiModelSpectrumChannel> ();
100  Ptr<LogDistancePropagationLossModel> model = CreateObject<LogDistancePropagationLossModel> ();
101  channel->AddPropagationLossModel (model);
102  dev0->SetChannel (channel);
103  dev1->SetChannel (channel);
104  n0->AddDevice (dev0);
105  n1->AddDevice (dev1);
106  Ptr<ConstantPositionMobilityModel> mob0 = CreateObject<ConstantPositionMobilityModel> ();
107  dev0->GetPhy ()->SetMobility (mob0);
108  Ptr<ConstantPositionMobilityModel> mob1 = CreateObject<ConstantPositionMobilityModel> ();
109  dev1->GetPhy ()->SetMobility (mob1);
110 
112  Ptr<SpectrumValue> psd = svh.CreateTxPowerSpectralDensity (txPower, channelNumber);
113  dev0->GetPhy ()->SetTxPowerSpectralDensity (psd);
114 
117  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb0);
118 
119  McpsDataRequestParams params;
120  params.m_srcAddrMode = SHORT_ADDR;
121  params.m_dstAddrMode = SHORT_ADDR;
122  params.m_dstPanId = 0;
123  params.m_dstAddr = Mac16Address ("00:02");
124  params.m_msduHandle = 0;
125  params.m_txOptions = 0;
126 
127  Ptr<Packet> p;
128  mob0->SetPosition (Vector (0,0,0));
129  mob1->SetPosition (Vector (minDistance,0,0));
130  for (int j = minDistance; j < maxDistance; )
131  {
132  for (int i = 0; i < maxPackets; i++)
133  {
134  p = Create<Packet> (packetSize);
137  dev0->GetMac (), params, p);
138  }
139  Simulator::Run ();
140  NS_LOG_DEBUG ("Received " << g_received << " packets for distance " << j);
141  psrdataset.Add (j, g_received / 1000.0);
142  g_received = 0;
143  j += increment;
144  mob1->SetPosition (Vector (j,0,0));
145  }
146 
147  psrplot.AddDataset (psrdataset);
148 
149  psrplot.SetTitle (os.str ());
150  psrplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
151  psrplot.SetLegend ("distance (m)", "Packet Success Rate (PSR)");
152  psrplot.SetExtra ("set xrange [0:200]\n\
153 set yrange [0:1]\n\
154 set grid\n\
155 set style line 1 linewidth 5\n\
156 set style increment user");
157  psrplot.GenerateOutput (berfile);
158  berfile.close ();
159 
161  return 0;
162 }
163 
tuple channel
Definition: third.py:85
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:159
Class to represent a 2D points plot.
Definition: gnuplot.h:113
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:160
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Set the single-frequency propagation loss model to be used.
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
STL namespace.
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:250
tuple cmd
Definition: second.py:35
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:161
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
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:1489
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:162
Parse command-line arguments.
Definition: command-line.h:205
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:157
Every class exported by the ns3 library is enclosed in the ns3 namespace.
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:158
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
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)
Associate a NetDevice to this node.
Definition: node.cc:128
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
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:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Parse(int argc, char *argv[])
Parse the program arguments.
static const uint32_t packetSize
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
static uint32_t g_received
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:181