A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
adhoc-aloha-ideal-phy-with-microwave-oven.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 CTTC
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: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 
22 
23 #include <iostream>
24 
25 #include <ns3/core-module.h>
26 #include <ns3/network-module.h>
27 #include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
28 #include <ns3/spectrum-model-300kHz-300GHz-log.h>
29 #include <ns3/wifi-spectrum-value-helper.h>
30 #include <ns3/multi-model-spectrum-channel.h>
31 #include <ns3/waveform-generator.h>
32 #include <ns3/spectrum-analyzer.h>
33 #include <ns3/log.h>
34 #include <string>
35 #include <ns3/friis-spectrum-propagation-loss.h>
36 #include <ns3/propagation-delay-model.h>
37 #include <ns3/mobility-module.h>
38 #include <ns3/spectrum-helper.h>
39 #include <ns3/applications-module.h>
40 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
41 #include <ns3/waveform-generator-helper.h>
42 #include <ns3/spectrum-analyzer-helper.h>
43 #include <ns3/non-communicating-net-device.h>
44 #include <ns3/microwave-oven-spectrum-value-helper.h>
45 
46 NS_LOG_COMPONENT_DEFINE ("OfdmWithWaveformGenerator");
47 
48 using namespace ns3;
49 
50 static bool g_verbose = false;
51 
52 
53 void
54 PhyTxStartTrace (std::string context, Ptr<const Packet> p)
55 {
56  if (g_verbose)
57  {
58  std::cout << context << " PHY TX START p: " << p << std::endl;
59  }
60 }
61 
62 
63 void
64 PhyTxEndTrace (std::string context, Ptr<const Packet> p)
65 {
66  if (g_verbose)
67  {
68  std::cout << context << " PHY TX END p: " << p << std::endl;
69  }
70 }
71 
72 void
73 PhyRxStartTrace (std::string context, Ptr<const Packet> p)
74 {
75  if (g_verbose)
76  {
77  std::cout << context << " PHY RX START p:" << p << std::endl;
78  }
79 }
80 
81 void
82 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
83 {
84  if (g_verbose)
85  {
86  std::cout << context << " PHY RX END OK p:" << p << std::endl;
87  }
88 }
89 
90 void
91 PhyRxEndErrorTrace (std::string context, Ptr<const Packet> p)
92 {
93  if (g_verbose)
94  {
95  std::cout << context << " PHY RX END ERROR p:" << p << std::endl;
96  }
97 }
98 
99 
100 void
102 {
103  Ptr<Packet> packet;
104  uint64_t bytes = 0;
105  while ((packet = socket->Recv ()))
106  {
107  bytes += packet->GetSize ();
108  }
109  if (g_verbose)
110  {
111  std::cout << "SOCKET received " << bytes << " bytes" << std::endl;
112  }
113 }
114 
117 {
118  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
119  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
120  sink->Bind ();
122  return sink;
123 }
124 
125 int main (int argc, char** argv)
126 {
127  CommandLine cmd;
128  cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
129  cmd.Parse (argc, argv);
130 
131  NodeContainer ofdmNodes;
132  NodeContainer waveformGeneratorNodes;
133  NodeContainer spectrumAnalyzerNodes;
134  NodeContainer allNodes;
135 
136  ofdmNodes.Create (2);
137  waveformGeneratorNodes.Create (1);
138  spectrumAnalyzerNodes.Create (1);
139  allNodes.Add (ofdmNodes);
140  allNodes.Add (waveformGeneratorNodes);
141  allNodes.Add (spectrumAnalyzerNodes);
142 
143  MobilityHelper mobility;
144  Ptr<ListPositionAllocator> nodePositionList = CreateObject<ListPositionAllocator> ();
145  nodePositionList->Add (Vector (5.0, 0.0, 0.0)); // TX node
146  nodePositionList->Add (Vector (0.0, 0.0, 0.0)); // RX node
147  nodePositionList->Add (Vector (30.0, 0.0, 0.0)); // Microwave Oven
148  nodePositionList->Add (Vector (0.0, 0.0, 0.0)); // Spectrum Analyzer
149  mobility.SetPositionAllocator (nodePositionList);
150  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
151 
152  mobility.Install (allNodes);
153 
154 
156  channelHelper.SetChannel ("ns3::MultiModelSpectrumChannel");
157  Ptr<SpectrumChannel> channel = channelHelper.Create ();
158 
159 
161  // Configure ofdm nodes
163 
165 
166  double txPower = 0.1; // Watts
167  uint32_t channelNumber = 4;
168  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPower, channelNumber);
169 
170  // for the noise, we use the Power Spectral Density of thermal noise
171  // at room temperature. The value of the PSD will be constant over the band of interest.
172  const double k = 1.381e-23; //Boltzmann's constant
173  const double T = 290; // temperature in Kelvin
174  double noisePsdValue = k * T; // watts per hertz
175  Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
176 
177 
178  AdhocAlohaNoackIdealPhyHelper adhocAlohaOfdmHelper;
179  adhocAlohaOfdmHelper.SetChannel (channel);
180  adhocAlohaOfdmHelper.SetTxPowerSpectralDensity (txPsd);
181  adhocAlohaOfdmHelper.SetNoisePowerSpectralDensity (noisePsd);
182  adhocAlohaOfdmHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate ("1Mbps")));
183  NetDeviceContainer ofdmDevices = adhocAlohaOfdmHelper.Install (ofdmNodes);
184 
185  PacketSocketHelper packetSocket;
186  packetSocket.Install (ofdmNodes);
187 
188  PacketSocketAddress socket;
189  socket.SetSingleDevice (ofdmDevices.Get (0)->GetIfIndex ());
190  socket.SetPhysicalAddress (ofdmDevices.Get (1)->GetAddress ());
191  socket.SetProtocol (1);
192 
193  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
194  onoff.SetAttribute ("OnTime", StringValue ("ns3::ExponentialRandomVariable[Mean=0.04]"));
195  onoff.SetAttribute ("OffTime", StringValue ("ns3::ExponentialRandomVariable[Mean=0.01]"));
196  onoff.SetAttribute ("DataRate", DataRateValue (DataRate ("0.4Mbps")));
197  onoff.SetAttribute ("PacketSize", UintegerValue (1500));
198 
199  ApplicationContainer apps = onoff.Install (ofdmNodes.Get (0));
200  apps.Start (Seconds (0.0));
201  apps.Stop (Seconds (1));
202 
203  Ptr<Socket> recvSink = SetupPacketReceive (ofdmNodes.Get (1));
204 
205 
206 
207 
208 
210  // Configure waveform generator
212 
214  NS_LOG_INFO ("mwoPsd : " << *mwoPsd);
215 
216  WaveformGeneratorHelper waveformGeneratorHelper;
217  waveformGeneratorHelper.SetChannel (channel);
218  waveformGeneratorHelper.SetTxPowerSpectralDensity (mwoPsd);
219 
220  waveformGeneratorHelper.SetPhyAttribute ("Period", TimeValue (Seconds (1.0 / 60))); // corresponds to 60 Hz
221  waveformGeneratorHelper.SetPhyAttribute ("DutyCycle", DoubleValue (0.5));
222  NetDeviceContainer waveformGeneratorDevices = waveformGeneratorHelper.Install (waveformGeneratorNodes);
223 
225  waveformGeneratorDevices.Get (0)->GetObject<NonCommunicatingNetDevice> ()->GetPhy ()->GetObject<WaveformGenerator> ());
226 
227 
228 
230  // Configure spectrum analyzer
232 
233 
234  SpectrumAnalyzerHelper spectrumAnalyzerHelper;
235  spectrumAnalyzerHelper.SetChannel (channel);
236  spectrumAnalyzerHelper.SetRxSpectrumModel (SpectrumModelIsm2400MhzRes1Mhz);
237  spectrumAnalyzerHelper.SetPhyAttribute ("Resolution", TimeValue (MilliSeconds (2)));
238  spectrumAnalyzerHelper.SetPhyAttribute ("NoisePowerSpectralDensity", DoubleValue (1e-15)); // -120 dBm/Hz
239  spectrumAnalyzerHelper.EnableAsciiAll ("spectrum-analyzer-output");
240  NetDeviceContainer spectrumAnalyzerDevices = spectrumAnalyzerHelper.Install (spectrumAnalyzerNodes);
241 
242  /*
243  you can get a nice plot of the output of SpectrumAnalyzer with this gnuplot script:
244 
245  unset surface
246  set pm3d at s
247  set palette
248  set key off
249  set view 50,50
250  set xlabel "time (ms)"
251  set ylabel "freq (MHz)"
252  set zlabel "PSD (dBW/Hz)" offset 15,0,0
253  splot "./spectrum-analyzer-output-3-0.tr" using ($1*1000.0):($2/1e6):(10*log10($3))
254  */
255 
256 
257 
258 
259 
260  Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback (&PhyTxStartTrace));
261  Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback (&PhyTxEndTrace));
262  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback (&PhyRxStartTrace));
263  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
264  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback (&PhyRxEndErrorTrace));
265 
266 
267 
268 
269  Simulator::Stop (Seconds (0.3));
270 
271  Simulator::Run ();
272 
274 
275 }
276 
277 
holds a vector of ns3::Application pointers.
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
hold variables of type string
Definition: string.h:18
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
NetDeviceContainer Install(NodeContainer c) const
NetDeviceContainer Install(NodeContainer c) const
void SetPhyAttribute(std::string name, const AttributeValue &v)
an address for a packet socket
void SetPhyAttribute(std::string name, const AttributeValue &v)
Ptr< SpectrumModel > SpectrumModelIsm2400MhzRes1Mhz
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
Ptr< SpectrumChannel > Create(void) const
void ReceivePacket(Ptr< Socket > socket)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
void PhyRxStartTrace(std::string context, Ptr< const Packet > p)
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
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
This class implements a device which does not communicate, in the sense that it does not interact wit...
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
a 3d vector
Definition: vector.h:31
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
virtual void Start()
Start the waveform generator.
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
Simple SpectrumPhy implementation that sends customizable waveform.
void PhyTxStartTrace(std::string context, Ptr< const Packet > p)
virtual Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint32_t channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
hold objects of type ns3::Time
Definition: nstime.h:1008
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
void PhyRxEndErrorTrace(std::string context, Ptr< const Packet > p)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:70
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetPhyAttribute(std::string name, const AttributeValue &v)
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void SetChannel(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Helper class used to assign positions and mobility models to nodes.
static SpectrumChannelHelper Default()
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
int main(int argc, char **argv)
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
hold objects of type ns3::DataRate
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:435
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void PhyTxEndTrace(std::string context, Ptr< const Packet > p)
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper ...
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper ...
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the created SpectrumAnalyzer instances to represent incoming signals...
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
void EnableAsciiAll(std::string prefix)
Enable ASCII output.
void Parse(int argc, char *argv[])
Parse the program arguments.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
NetDeviceContainer Install(NodeContainer c) const
Hold a floating point type.
Definition: double.h:41
Ptr< T > GetObject(void) const
Definition: object.h:362
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
a unique identifier for an interface.
Definition: type-id.h:49
Implements WifiSpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper ...
static TypeId LookupByName(std::string name)
Definition: type-id.cc:535