A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
adhoc-aloha-ideal-phy-with-microwave-oven.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 CTTC
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
20#include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
21#include <ns3/applications-module.h>
22#include <ns3/core-module.h>
23#include <ns3/friis-spectrum-propagation-loss.h>
24#include <ns3/ism-spectrum-value-helper.h>
25#include <ns3/log.h>
26#include <ns3/microwave-oven-spectrum-value-helper.h>
27#include <ns3/mobility-module.h>
28#include <ns3/multi-model-spectrum-channel.h>
29#include <ns3/network-module.h>
30#include <ns3/non-communicating-net-device.h>
31#include <ns3/propagation-delay-model.h>
32#include <ns3/spectrum-analyzer-helper.h>
33#include <ns3/spectrum-analyzer.h>
34#include <ns3/spectrum-helper.h>
35#include <ns3/spectrum-model-300kHz-300GHz-log.h>
36#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
37#include <ns3/waveform-generator-helper.h>
38#include <ns3/waveform-generator.h>
39
40#include <iostream>
41#include <string>
42
43using namespace ns3;
44
45NS_LOG_COMPONENT_DEFINE("OfdmWithWaveformGenerator");
46
47/// True for verbose output.
48static bool g_verbose = false;
49
50/**
51 *
52 *
53 * \param context
54 * \param p
55 */
56void
57PhyTxStartTrace(std::string context, Ptr<const Packet> p)
58{
59 if (g_verbose)
60 {
61 std::cout << context << " PHY TX START p: " << p << std::endl;
62 }
63}
64
65/**
66 * PHY start TX trace.
67 *
68 * \param context The context.
69 * \param p The packet.
70 */
71void
72PhyTxEndTrace(std::string context, Ptr<const Packet> p)
73{
74 if (g_verbose)
75 {
76 std::cout << context << " PHY TX END p: " << p << std::endl;
77 }
78}
79
80/**
81 * PHY end TX trace.
82 *
83 * \param context The context.
84 * \param p The packet.
85 */
86void
87PhyRxStartTrace(std::string context, Ptr<const Packet> p)
88{
89 if (g_verbose)
90 {
91 std::cout << context << " PHY RX START p:" << p << std::endl;
92 }
93}
94
95/**
96 * PHY end OK RX trace.
97 *
98 * \param context The context.
99 * \param p The packet.
100 */
101void
102PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
103{
104 if (g_verbose)
105 {
106 std::cout << context << " PHY RX END OK p:" << p << std::endl;
107 }
108}
109
110/**
111 * PHY end error RX trace.
112 *
113 * \param context The context.
114 * \param p The packet.
115 */
116void
118{
119 if (g_verbose)
120 {
121 std::cout << context << " PHY RX END ERROR p:" << p << std::endl;
122 }
123}
124
125/**
126 * Receive callback.
127 *
128 * \param socket The receiving socket.
129 */
130void
132{
133 Ptr<Packet> packet;
134 uint64_t bytes = 0;
135 while ((packet = socket->Recv()))
136 {
137 bytes += packet->GetSize();
138 }
139 if (g_verbose)
140 {
141 std::cout << "SOCKET received " << bytes << " bytes" << std::endl;
142 }
143}
144
145/**
146 * Create a socket and prepare it for packet reception.
147 *
148 * \param node The node.
149 * \return a new socket
150 */
153{
154 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
156 sink->Bind();
157 sink->SetRecvCallback(MakeCallback(&ReceivePacket));
158 return sink;
159}
160
161int
162main(int argc, char** argv)
163{
164 CommandLine cmd(__FILE__);
165 cmd.AddValue("verbose", "Print trace information if true", g_verbose);
166 cmd.Parse(argc, argv);
167
168 NodeContainer ofdmNodes;
169 NodeContainer waveformGeneratorNodes;
170 NodeContainer spectrumAnalyzerNodes;
171 NodeContainer allNodes;
172
173 ofdmNodes.Create(2);
174 waveformGeneratorNodes.Create(1);
175 spectrumAnalyzerNodes.Create(1);
176 allNodes.Add(ofdmNodes);
177 allNodes.Add(waveformGeneratorNodes);
178 allNodes.Add(spectrumAnalyzerNodes);
179
181 Ptr<ListPositionAllocator> nodePositionList = CreateObject<ListPositionAllocator>();
182 nodePositionList->Add(Vector(5.0, 0.0, 0.0)); // TX node
183 nodePositionList->Add(Vector(0.0, 0.0, 0.0)); // RX node
184 nodePositionList->Add(Vector(30.0, 0.0, 0.0)); // Microwave Oven
185 nodePositionList->Add(Vector(0.0, 0.0, 0.0)); // Spectrum Analyzer
186 mobility.SetPositionAllocator(nodePositionList);
187 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
188
189 mobility.Install(allNodes);
190
192 channelHelper.SetChannel("ns3::MultiModelSpectrumChannel");
193 Ptr<SpectrumChannel> channel = channelHelper.Create();
194
195 /////////////////////////
196 // Configure ofdm nodes
197 ////////////////////////
198
200
201 double txPower = 0.1; // Watts
202 uint32_t channelNumber = 4;
203 Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPower, channelNumber);
204
205 // for the noise, we use the Power Spectral Density of thermal noise
206 // at room temperature. The value of the PSD will be constant over the band of interest.
207 const double k = 1.381e-23; // Boltzmann's constant
208 const double T = 290; // temperature in Kelvin
209 double noisePsdValue = k * T; // watts per hertz
210 Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
211
212 AdhocAlohaNoackIdealPhyHelper adhocAlohaOfdmHelper;
213 adhocAlohaOfdmHelper.SetChannel(channel);
214 adhocAlohaOfdmHelper.SetTxPowerSpectralDensity(txPsd);
215 adhocAlohaOfdmHelper.SetNoisePowerSpectralDensity(noisePsd);
216 adhocAlohaOfdmHelper.SetPhyAttribute("Rate", DataRateValue(DataRate("1Mbps")));
217 NetDeviceContainer ofdmDevices = adhocAlohaOfdmHelper.Install(ofdmNodes);
218
219 PacketSocketHelper packetSocket;
220 packetSocket.Install(ofdmNodes);
221
222 PacketSocketAddress socket;
223 socket.SetSingleDevice(ofdmDevices.Get(0)->GetIfIndex());
224 socket.SetPhysicalAddress(ofdmDevices.Get(1)->GetAddress());
225 socket.SetProtocol(1);
226
227 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
228 onoff.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=0.04]"));
229 onoff.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=0.01]"));
230 onoff.SetAttribute("DataRate", DataRateValue(DataRate("0.4Mbps")));
231 onoff.SetAttribute("PacketSize", UintegerValue(1500));
232
233 ApplicationContainer apps = onoff.Install(ofdmNodes.Get(0));
234 apps.Start(Seconds(0.0));
235 apps.Stop(Seconds(1));
236
237 Ptr<Socket> recvSink = SetupPacketReceive(ofdmNodes.Get(1));
238
239 /////////////////////////////////
240 // Configure waveform generator
241 /////////////////////////////////
242
244 NS_LOG_INFO("mwoPsd : " << *mwoPsd);
245
246 WaveformGeneratorHelper waveformGeneratorHelper;
247 waveformGeneratorHelper.SetChannel(channel);
248 waveformGeneratorHelper.SetTxPowerSpectralDensity(mwoPsd);
249
250 waveformGeneratorHelper.SetPhyAttribute("Period",
251 TimeValue(Seconds(1.0 / 60))); // corresponds to 60 Hz
252 waveformGeneratorHelper.SetPhyAttribute("DutyCycle", DoubleValue(0.5));
253 NetDeviceContainer waveformGeneratorDevices =
254 waveformGeneratorHelper.Install(waveformGeneratorNodes);
255
258 waveformGeneratorDevices.Get(0)
259 ->GetObject<NonCommunicatingNetDevice>()
260 ->GetPhy()
262
263 /////////////////////////////////
264 // Configure spectrum analyzer
265 /////////////////////////////////
266
267 SpectrumAnalyzerHelper spectrumAnalyzerHelper;
268 spectrumAnalyzerHelper.SetChannel(channel);
270 spectrumAnalyzerHelper.SetPhyAttribute("Resolution", TimeValue(MilliSeconds(2)));
271 spectrumAnalyzerHelper.SetPhyAttribute("NoisePowerSpectralDensity",
272 DoubleValue(1e-15)); // -120 dBm/Hz
273 spectrumAnalyzerHelper.EnableAsciiAll("spectrum-analyzer-output");
274 NetDeviceContainer spectrumAnalyzerDevices =
275 spectrumAnalyzerHelper.Install(spectrumAnalyzerNodes);
276
277 /*
278 you can get a nice plot of the output of SpectrumAnalyzer with this gnuplot script:
279
280 unset surface
281 set pm3d at s
282 set palette
283 set key off
284 set view 50,50
285 set xlabel "time (ms)"
286 set ylabel "freq (MHz)"
287 set zlabel "PSD (dBW/Hz)" offset 15,0,0
288 splot "./spectrum-analyzer-output-3-0.tr" using ($1*1000.0):($2/1e6):(10*log10($3))
289 */
290
291 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback(&PhyTxStartTrace));
292 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback(&PhyTxEndTrace));
293 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback(&PhyRxStartTrace));
294 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
295 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback(&PhyRxEndErrorTrace));
296
298
300
302
303 return 0;
304}
void PhyTxEndTrace(std::string context, Ptr< const Packet > p)
PHY start TX trace.
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
void PhyRxEndErrorTrace(std::string context, Ptr< const Packet > p)
PHY end error RX trace.
void PhyRxStartTrace(std::string context, Ptr< const Packet > p)
PHY end TX trace.
void ReceivePacket(Ptr< Socket > socket)
Receive callback.
static bool g_verbose
True for verbose output.
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
PHY end OK RX trace.
void PhyTxStartTrace(std::string context, Ptr< const Packet > p)
a polymophic address class
Definition: address.h:101
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
This class implements a device which does not communicate, in the sense that it does not interact wit...
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:72
Class to allow the Spectrum Analysis.
NetDeviceContainer Install(NodeContainer c) const
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetChannel(Ptr< SpectrumChannel > channel)
Set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper.
void EnableAsciiAll(std::string prefix)
Enable ASCII output.
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the created SpectrumAnalyzer instances to represent incoming signals.
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
static SpectrumChannelHelper Default()
Setup a default SpectrumChannel.
void SetChannel(std::string type, Ts &&... args)
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
Creates a SpectrumValue instance with a constant value for all frequencies.
virtual Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint8_t channel)
Creates a SpectrumValue instance that represents the TX Power Spectral Density of a wifi device corre...
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1406
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
Hold an unsigned integer type.
Definition: uinteger.h:45
Create a Waveform generator, which can be used to inject specific noise in the channel.
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
void SetPhyAttribute(std::string name, const AttributeValue &v)
Simple SpectrumPhy implementation that sends customizable waveform.
virtual void Start()
Start the waveform generator.
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Ptr< SpectrumModel > SpectrumModelIsm2400MhzRes1Mhz
Spectrum model logger for frequencies in the 2.4 GHz ISM band with 1 MHz resolution.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:706
ns cmd
Definition: second.py:40
ns channel
Definition: third.py:88
ns mobility
Definition: third.py:105
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55