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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
9#include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
10#include <ns3/applications-module.h>
11#include <ns3/core-module.h>
12#include <ns3/friis-spectrum-propagation-loss.h>
13#include <ns3/ism-spectrum-value-helper.h>
14#include <ns3/log.h>
15#include <ns3/microwave-oven-spectrum-value-helper.h>
16#include <ns3/mobility-module.h>
17#include <ns3/multi-model-spectrum-channel.h>
18#include <ns3/network-module.h>
19#include <ns3/non-communicating-net-device.h>
20#include <ns3/propagation-delay-model.h>
21#include <ns3/spectrum-analyzer-helper.h>
22#include <ns3/spectrum-analyzer.h>
23#include <ns3/spectrum-helper.h>
24#include <ns3/spectrum-model-300kHz-300GHz-log.h>
25#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
26#include <ns3/waveform-generator-helper.h>
27#include <ns3/waveform-generator.h>
28
29#include <iostream>
30#include <string>
31
32using namespace ns3;
33
34NS_LOG_COMPONENT_DEFINE("OfdmWithWaveformGenerator");
35
36/// True for verbose output.
37static bool g_verbose = false;
38
39/**
40 *
41 *
42 * \param context
43 * \param p
44 */
45void
46PhyTxStartTrace(std::string context, Ptr<const Packet> p)
47{
48 if (g_verbose)
49 {
50 std::cout << context << " PHY TX START p: " << p << std::endl;
51 }
52}
53
54/**
55 * PHY start TX trace.
56 *
57 * \param context The context.
58 * \param p The packet.
59 */
60void
61PhyTxEndTrace(std::string context, Ptr<const Packet> p)
62{
63 if (g_verbose)
64 {
65 std::cout << context << " PHY TX END p: " << p << std::endl;
66 }
67}
68
69/**
70 * PHY end TX trace.
71 *
72 * \param context The context.
73 * \param p The packet.
74 */
75void
76PhyRxStartTrace(std::string context, Ptr<const Packet> p)
77{
78 if (g_verbose)
79 {
80 std::cout << context << " PHY RX START p:" << p << std::endl;
81 }
82}
83
84/**
85 * PHY end OK RX trace.
86 *
87 * \param context The context.
88 * \param p The packet.
89 */
90void
91PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
92{
93 if (g_verbose)
94 {
95 std::cout << context << " PHY RX END OK p:" << p << std::endl;
96 }
97}
98
99/**
100 * PHY end error RX trace.
101 *
102 * \param context The context.
103 * \param p The packet.
104 */
105void
107{
108 if (g_verbose)
109 {
110 std::cout << context << " PHY RX END ERROR p:" << p << std::endl;
111 }
112}
113
114/**
115 * Receive callback.
116 *
117 * \param socket The receiving socket.
118 */
119void
121{
122 Ptr<Packet> packet;
123 uint64_t bytes = 0;
124 while ((packet = socket->Recv()))
125 {
126 bytes += packet->GetSize();
127 }
128 if (g_verbose)
129 {
130 std::cout << "SOCKET received " << bytes << " bytes" << std::endl;
131 }
132}
133
134/**
135 * Create a socket and prepare it for packet reception.
136 *
137 * \param node The node.
138 * \return a new socket
139 */
142{
143 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
145 sink->Bind();
146 sink->SetRecvCallback(MakeCallback(&ReceivePacket));
147 return sink;
148}
149
150int
151main(int argc, char** argv)
152{
153 CommandLine cmd(__FILE__);
154 cmd.AddValue("verbose", "Print trace information if true", g_verbose);
155 cmd.Parse(argc, argv);
156
157 NodeContainer ofdmNodes;
158 NodeContainer waveformGeneratorNodes;
159 NodeContainer spectrumAnalyzerNodes;
160 NodeContainer allNodes;
161
162 ofdmNodes.Create(2);
163 waveformGeneratorNodes.Create(1);
164 spectrumAnalyzerNodes.Create(1);
165 allNodes.Add(ofdmNodes);
166 allNodes.Add(waveformGeneratorNodes);
167 allNodes.Add(spectrumAnalyzerNodes);
168
171 nodePositionList->Add(Vector(5.0, 0.0, 0.0)); // TX node
172 nodePositionList->Add(Vector(0.0, 0.0, 0.0)); // RX node
173 nodePositionList->Add(Vector(30.0, 0.0, 0.0)); // Microwave Oven
174 nodePositionList->Add(Vector(0.0, 0.0, 0.0)); // Spectrum Analyzer
175 mobility.SetPositionAllocator(nodePositionList);
176 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
177
178 mobility.Install(allNodes);
179
181 channelHelper.SetChannel("ns3::MultiModelSpectrumChannel");
182 Ptr<SpectrumChannel> channel = channelHelper.Create();
183
184 /////////////////////////
185 // Configure ofdm nodes
186 ////////////////////////
187
189
190 double txPower = 0.1; // Watts
191 uint32_t channelNumber = 4;
192 Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPower, channelNumber);
193
194 // for the noise, we use the Power Spectral Density of thermal noise
195 // at room temperature. The value of the PSD will be constant over the band of interest.
196 const double k = 1.381e-23; // Boltzmann's constant
197 const double T = 290; // temperature in Kelvin
198 double noisePsdValue = k * T; // watts per hertz
199 Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
200
201 AdhocAlohaNoackIdealPhyHelper adhocAlohaOfdmHelper;
202 adhocAlohaOfdmHelper.SetChannel(channel);
203 adhocAlohaOfdmHelper.SetTxPowerSpectralDensity(txPsd);
204 adhocAlohaOfdmHelper.SetNoisePowerSpectralDensity(noisePsd);
205 adhocAlohaOfdmHelper.SetPhyAttribute("Rate", DataRateValue(DataRate("1Mbps")));
206 NetDeviceContainer ofdmDevices = adhocAlohaOfdmHelper.Install(ofdmNodes);
207
208 PacketSocketHelper packetSocket;
209 packetSocket.Install(ofdmNodes);
210
211 PacketSocketAddress socket;
212 socket.SetSingleDevice(ofdmDevices.Get(0)->GetIfIndex());
213 socket.SetPhysicalAddress(ofdmDevices.Get(1)->GetAddress());
214 socket.SetProtocol(1);
215
216 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
217 onoff.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=0.04]"));
218 onoff.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=0.01]"));
219 onoff.SetAttribute("DataRate", DataRateValue(DataRate("0.4Mbps")));
220 onoff.SetAttribute("PacketSize", UintegerValue(1500));
221
222 ApplicationContainer apps = onoff.Install(ofdmNodes.Get(0));
223 apps.Start(Seconds(0.0));
224 apps.Stop(Seconds(1));
225
226 Ptr<Socket> recvSink = SetupPacketReceive(ofdmNodes.Get(1));
227
228 /////////////////////////////////
229 // Configure waveform generator
230 /////////////////////////////////
231
233 NS_LOG_INFO("mwoPsd : " << *mwoPsd);
234
235 WaveformGeneratorHelper waveformGeneratorHelper;
236 waveformGeneratorHelper.SetChannel(channel);
237 waveformGeneratorHelper.SetTxPowerSpectralDensity(mwoPsd);
238
239 waveformGeneratorHelper.SetPhyAttribute("Period",
240 TimeValue(Seconds(1.0 / 60))); // corresponds to 60 Hz
241 waveformGeneratorHelper.SetPhyAttribute("DutyCycle", DoubleValue(0.5));
242 NetDeviceContainer waveformGeneratorDevices =
243 waveformGeneratorHelper.Install(waveformGeneratorNodes);
244
247 waveformGeneratorDevices.Get(0)
248 ->GetObject<NonCommunicatingNetDevice>()
249 ->GetPhy()
251
252 /////////////////////////////////
253 // Configure spectrum analyzer
254 /////////////////////////////////
255
256 SpectrumAnalyzerHelper spectrumAnalyzerHelper;
257 spectrumAnalyzerHelper.SetChannel(channel);
259 spectrumAnalyzerHelper.SetPhyAttribute("Resolution", TimeValue(MilliSeconds(2)));
260 spectrumAnalyzerHelper.SetPhyAttribute("NoisePowerSpectralDensity",
261 DoubleValue(1e-15)); // -120 dBm/Hz
262 spectrumAnalyzerHelper.EnableAsciiAll("spectrum-analyzer-output");
263 NetDeviceContainer spectrumAnalyzerDevices =
264 spectrumAnalyzerHelper.Install(spectrumAnalyzerNodes);
265
266 /*
267 you can get a nice plot of the output of SpectrumAnalyzer with this gnuplot script:
268
269 unset surface
270 set pm3d at s
271 set palette
272 set key off
273 set view 50,50
274 set xlabel "time (ms)"
275 set ylabel "freq (MHz)"
276 set zlabel "PSD (dBW/Hz)" offset 15,0,0
277 splot "./spectrum-analyzer-output-3-0.tr" using ($1*1000.0):($2/1e6):(10*log10($3))
278 */
279
280 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback(&PhyTxStartTrace));
281 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback(&PhyTxEndTrace));
282 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback(&PhyRxStartTrace));
283 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
284 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback(&PhyRxEndErrorTrace));
285
287
289
291
292 return 0;
293}
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:90
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.
Class for representing data rates.
Definition data-rate.h:78
AttributeValue implementation for DataRate.
Definition data-rate.h:285
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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:511
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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:61
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:45
AttributeValue implementation for Time.
Definition nstime.h:1395
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
Hold an unsigned integer type.
Definition uinteger.h:34
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:967
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
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:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:684
channel
Definition third.py:77
mobility
Definition third.py:92
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44