A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
wifi-phy-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/wifi-net-device.h"
21 #include "ns3/yans-wifi-channel.h"
22 #include "ns3/yans-wifi-phy.h"
23 #include "ns3/propagation-loss-model.h"
24 #include "ns3/propagation-delay-model.h"
25 #include "ns3/error-rate-model.h"
26 #include "ns3/yans-error-rate-model.h"
27 #include "ns3/ptr.h"
28 #include "ns3/mobility-model.h"
29 #include "ns3/constant-position-mobility-model.h"
30 #include "ns3/vector.h"
31 #include "ns3/packet.h"
32 #include "ns3/simulator.h"
33 #include "ns3/nstime.h"
34 #include "ns3/command-line.h"
35 #include "ns3/flow-id-tag.h"
36 
37 using namespace ns3;
38 
40 {
41 public:
42  struct Input
43  {
44  Input ();
45  double distance;
46  std::string txMode;
47  uint8_t txPowerLevel;
48  uint32_t packetSize;
49  uint32_t nPackets;
50  };
51  struct Output
52  {
53  uint32_t received;
54  };
55  PsrExperiment ();
56 
57  struct PsrExperiment::Output Run (struct PsrExperiment::Input input);
58 
59 private:
60  void Send (void);
61  void Receive (Ptr<Packet> p, double snr, WifiMode mode, enum WifiPreamble preamble);
62  Ptr<WifiPhy> m_tx;
63  struct Input m_input;
64  struct Output m_output;
65 };
66 
67 void
69 {
70  Ptr<Packet> p = Create<Packet> (m_input.packetSize);
71  WifiMode mode = WifiMode (m_input.txMode);
72  m_tx->SendPacket (p, mode, WIFI_PREAMBLE_SHORT, m_input.txPowerLevel);
73 }
74 
75 void
76 PsrExperiment::Receive (Ptr<Packet> p, double snr, WifiMode mode, enum WifiPreamble preamble)
77 {
78  m_output.received++;
79 }
80 
82 {
83 }
85  : distance (5.0),
86  txMode ("OfdmRate6Mbps"),
87  txPowerLevel (0),
88  packetSize (2304),
89  nPackets (400)
90 {
91 }
92 
94 PsrExperiment::Run (struct PsrExperiment::Input input)
95 {
96  m_output.received = 0;
97  m_input = input;
98 
99  Ptr<MobilityModel> posTx = CreateObject<ConstantPositionMobilityModel> ();
100  posTx->SetPosition (Vector (0.0, 0.0, 0.0));
101  Ptr<MobilityModel> posRx = CreateObject<ConstantPositionMobilityModel> ();
102  posRx->SetPosition (Vector (m_input.distance, 0.0, 0.0));
103 
104  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
105  channel->SetPropagationDelayModel (CreateObject<ConstantSpeedPropagationDelayModel> ());
106  Ptr<LogDistancePropagationLossModel> log = CreateObject<LogDistancePropagationLossModel> ();
107  channel->SetPropagationLossModel (log);
108 
109  Ptr<YansWifiPhy> tx = CreateObject<YansWifiPhy> ();
110  Ptr<YansWifiPhy> rx = CreateObject<YansWifiPhy> ();
111  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
112  tx->SetErrorRateModel (error);
113  rx->SetErrorRateModel (error);
114  tx->SetChannel (channel);
115  rx->SetChannel (channel);
116  tx->SetMobility (posTx);
117  rx->SetMobility (posRx);
118 
119  rx->SetReceiveOkCallback (MakeCallback (&PsrExperiment::Receive, this));
120 
121  for (uint32_t i = 0; i < m_input.nPackets; ++i)
122  {
123  Simulator::Schedule (Seconds (i), &PsrExperiment::Send, this);
124  }
125  m_tx = tx;
126  Simulator::Run ();
127  return m_output;
128 }
129 
130 
132 {
133 public:
134  struct Input
135  {
136  Input ();
138  double xA;
139  double xB;
140  std::string txModeA;
141  std::string txModeB;
142  uint8_t txPowerLevelA;
143  uint8_t txPowerLevelB;
144  uint32_t packetSizeA;
145  uint32_t packetSizeB;
146  uint32_t nPackets;
147  };
148  struct Output
149  {
150  uint32_t receivedA;
151  uint32_t receivedB;
152  };
154 
156 private:
157  void SendA (void) const;
158  void SendB (void) const;
159  void Receive (Ptr<Packet> p, double snr, WifiMode mode, enum WifiPreamble preamble);
160  Ptr<WifiPhy> m_txA;
161  Ptr<WifiPhy> m_txB;
162  uint32_t m_flowIdA;
163  uint32_t m_flowIdB;
164  struct Input m_input;
165  struct Output m_output;
166 };
167 
168 void
170 {
171  Ptr<Packet> p = Create<Packet> (m_input.packetSizeA);
172  p->AddByteTag (FlowIdTag (m_flowIdA));
173  m_txA->SendPacket (p, WifiMode (m_input.txModeA),
174  WIFI_PREAMBLE_SHORT, m_input.txPowerLevelA);
175 }
176 
177 void
179 {
180  Ptr<Packet> p = Create<Packet> (m_input.packetSizeB);
181  p->AddByteTag (FlowIdTag (m_flowIdB));
182  m_txB->SendPacket (p, WifiMode (m_input.txModeB),
183  WIFI_PREAMBLE_SHORT, m_input.txPowerLevelB);
184 }
185 
186 void
187 CollisionExperiment::Receive (Ptr<Packet> p, double snr, WifiMode mode, enum WifiPreamble preamble)
188 {
189  FlowIdTag tag;
190  p->FindFirstMatchingByteTag (tag);
191  if (tag.GetFlowId () == m_flowIdA)
192  {
193  m_output.receivedA++;
194  }
195  else if (tag.GetFlowId () == m_flowIdB)
196  {
197  m_output.receivedB++;
198  }
199 }
200 
202 {
203 }
205  : interval (MicroSeconds (0)),
206  xA (-5),
207  xB (5),
208  txModeA ("OfdmRate6Mbps"),
209  txModeB ("OfdmRate6Mbps"),
210  txPowerLevelA (0),
211  txPowerLevelB (0),
212  packetSizeA (2304),
213  packetSizeB (2304),
214  nPackets (400)
215 {
216 }
217 
219 CollisionExperiment::Run (struct CollisionExperiment::Input input)
220 {
221  m_output.receivedA = 0;
222  m_output.receivedB = 0;
223  m_input = input;
224 
225  m_flowIdA = FlowIdTag::AllocateFlowId ();
226  m_flowIdB = FlowIdTag::AllocateFlowId ();
227 
228  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
229  channel->SetPropagationDelayModel (CreateObject<ConstantSpeedPropagationDelayModel> ());
230  Ptr<LogDistancePropagationLossModel> log = CreateObject<LogDistancePropagationLossModel> ();
231  channel->SetPropagationLossModel (log);
232 
233  Ptr<MobilityModel> posTxA = CreateObject<ConstantPositionMobilityModel> ();
234  posTxA->SetPosition (Vector (input.xA, 0.0, 0.0));
235  Ptr<MobilityModel> posTxB = CreateObject<ConstantPositionMobilityModel> ();
236  posTxB->SetPosition (Vector (input.xB, 0.0, 0.0));
237  Ptr<MobilityModel> posRx = CreateObject<ConstantPositionMobilityModel> ();
238  posRx->SetPosition (Vector (0, 0.0, 0.0));
239 
240  Ptr<YansWifiPhy> txA = CreateObject<YansWifiPhy> ();
241  Ptr<YansWifiPhy> txB = CreateObject<YansWifiPhy> ();
242  Ptr<YansWifiPhy> rx = CreateObject<YansWifiPhy> ();
243 
244  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
245  txA->SetErrorRateModel (error);
246  txB->SetErrorRateModel (error);
247  rx->SetErrorRateModel (error);
248  txA->SetChannel (channel);
249  txB->SetChannel (channel);
250  rx->SetChannel (channel);
251  txA->SetMobility (posTxA);
252  txB->SetMobility (posTxB);
253  rx->SetMobility (posRx);
254 
255 
256  rx->SetReceiveOkCallback (MakeCallback (&CollisionExperiment::Receive, this));
257 
258  for (uint32_t i = 0; i < m_input.nPackets; ++i)
259  {
260  Simulator::Schedule (Seconds (i), &CollisionExperiment::SendA, this);
261  }
262  for (uint32_t i = 0; i < m_input.nPackets; ++i)
263  {
264  Simulator::Schedule (Seconds (i) + m_input.interval, &CollisionExperiment::SendB, this);
265  }
266  m_txA = txA;
267  m_txB = txB;
268  Simulator::Run ();
269  return m_output;
270 }
271 
272 
273 static void PrintPsr (int argc, char *argv[])
274 {
275  PsrExperiment experiment;
276  struct PsrExperiment::Input input;
277 
278  CommandLine cmd;
279  cmd.AddValue ("Distance", "The distance between two phys", input.distance);
280  cmd.AddValue ("PacketSize", "The size of each packet sent", input.packetSize);
281  cmd.AddValue ("TxMode", "The mode to use to send each packet", input.txMode);
282  cmd.AddValue ("NPackets", "The number of packets to send", input.nPackets);
283  cmd.AddValue ("TxPowerLevel", "The power level index to use to send each packet", input.txPowerLevel);
284  cmd.Parse (argc, argv);
285 
286  struct PsrExperiment::Output output;
287  output = experiment.Run (input);
288 
289  double psr = output.received;
290  psr /= input.nPackets;
291 
292  std::cout << psr << std::endl;
293 }
294 
295 double CalcPsr (struct PsrExperiment::Output output, struct PsrExperiment::Input input)
296 {
297  double psr = output.received;
298  psr /= input.nPackets;
299  return psr;
300 }
301 
302 static void PrintPsrVsDistance (int argc, char *argv[])
303 {
304  struct PsrExperiment::Input input;
305  CommandLine cmd;
306  cmd.AddValue ("TxPowerLevel", "The power level index to use to send each packet", input.txPowerLevel);
307  cmd.AddValue ("TxMode", "The mode to use to send each packet", input.txMode);
308  cmd.AddValue ("NPackets", "The number of packets to send", input.nPackets);
309  cmd.AddValue ("PacketSize", "The size of each packet sent", input.packetSize);
310  cmd.Parse (argc, argv);
311  for (input.distance = 1.0; input.distance < 165; input.distance += 2.0)
312  {
313  std::cout << input.distance;
314  PsrExperiment experiment;
315  struct PsrExperiment::Output output;
316 
317  input.txMode = "OfdmRate6Mbps";
318  output = experiment.Run (input);
319  std::cout << " " << CalcPsr (output, input);
320 
321  input.txMode = "OfdmRate9Mbps";
322  output = experiment.Run (input);
323  std::cout << " " << CalcPsr (output, input);
324 
325  input.txMode = "OfdmRate12Mbps";
326  output = experiment.Run (input);
327  std::cout << " " << CalcPsr (output, input);
328 
329  input.txMode = "OfdmRate18Mbps";
330  output = experiment.Run (input);
331  std::cout << " " << CalcPsr (output, input);
332 
333  input.txMode = "OfdmRate24Mbps";
334  output = experiment.Run (input);
335  std::cout << " " << CalcPsr (output, input);
336 
337  input.txMode = "OfdmRate36Mbps";
338  output = experiment.Run (input);
339  std::cout << " " << CalcPsr (output, input);
340 
341  input.txMode = "OfdmRate48Mbps";
342  output = experiment.Run (input);
343  std::cout << " " << CalcPsr (output, input);
344 
345  input.txMode = "OfdmRate54Mbps";
346  output = experiment.Run (input);
347  std::cout << " " << CalcPsr (output, input);
348 
349  std::cout << std::endl;
350  }
351 }
352 
353 static void PrintSizeVsRange (int argc, char *argv[])
354 {
355  double targetPsr = 0.05;
356  struct PsrExperiment::Input input;
357  CommandLine cmd;
358  cmd.AddValue ("TxPowerLevel", "The power level index to use to send each packet", input.txPowerLevel);
359  cmd.AddValue ("TxMode", "The mode to use to send each packet", input.txMode);
360  cmd.AddValue ("NPackets", "The number of packets to send", input.nPackets);
361  cmd.AddValue ("TargetPsr", "The psr needed to assume that we are within range", targetPsr);
362  cmd.Parse (argc, argv);
363  for (input.packetSize = 10; input.packetSize < 3000; input.packetSize += 40)
364  {
365  double precision = 0.1;
366  double low = 1.0;
367  double high = 200.0;
368  while (high - low > precision)
369  {
370  double middle = low + (high - low) / 2;
371  struct PsrExperiment::Output output;
372  PsrExperiment experiment;
373  input.distance = middle;
374  output = experiment.Run (input);
375  double psr = CalcPsr (output, input);
376  if (psr >= targetPsr)
377  {
378  low = middle;
379  }
380  else
381  {
382  high = middle;
383  }
384  }
385  std::cout << input.packetSize << " " << input.distance << std::endl;
386  }
387 }
388 
389 static void PrintPsrVsCollisionInterval (int argc, char *argv[])
390 {
392  input.nPackets = 100;
393  CommandLine cmd;
394  cmd.AddValue ("NPackets", "The number of packets to send for each transmitter", input.nPackets);
395  cmd.AddValue ("xA", "the position of transmitter A", input.xA);
396  cmd.AddValue ("xB", "the position of transmitter B", input.xB);
397  for (uint32_t i = 0; i < 100; i += 1)
398  {
399  CollisionExperiment experiment;
401  input.interval = MicroSeconds (i);
402  output = experiment.Run (input);
403  double perA = (output.receivedA + 0.0) / (input.nPackets + 0.0);
404  double perB = (output.receivedB + 0.0) / (input.nPackets + 0.0);
405  std::cout << i << " " << perA << " " << perB << std::endl;
406  }
407  for (uint32_t i = 100; i < 4000; i += 50)
408  {
409  CollisionExperiment experiment;
411  input.interval = MicroSeconds (i);
412  output = experiment.Run (input);
413  double perA = (output.receivedA + 0.0) / (input.nPackets + 0.0);
414  double perB = (output.receivedB + 0.0) / (input.nPackets + 0.0);
415  std::cout << i << " " << perA << " " << perB << std::endl;
416  }
417 }
418 
419 
420 
421 int main (int argc, char *argv[])
422 {
423  if (argc <= 1)
424  {
425  std::cout << "Available experiments: "
426  << "Psr "
427  << "SizeVsRange "
428  << "PsrVsDistance "
429  << "PsrVsCollisionInterval "
430  << std::endl;
431  return -1;
432  }
433  std::string type = argv[1];
434  argc--;
435  argv[1] = argv[0];
436  argv++;
437  if (type == "Psr")
438  {
439  PrintPsr (argc, argv);
440  }
441  else if (type == "SizeVsRange")
442  {
443  PrintSizeVsRange (argc, argv);
444  }
445  else if (type == "PsrVsDistance")
446  {
447  PrintPsrVsDistance (argc, argv);
448  }
449  else if (type == "PsrVsCollisionInterval")
450  {
451  PrintPsrVsCollisionInterval (argc, argv);
452  }
453 
454  return 0;
455 }