Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.82 KB | None | 0 0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2014 Universidad de la República - Uruguay
  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: Matias Richart <mrichart@fing.edu.uy>
  19. */
  20.  
  21. /**
  22. * This example program is designed to illustrate the behavior of three
  23. * power/rate-adaptive WiFi rate controls; namely, ns3::ParfWifiManager,
  24. * ns3::AparfWifiManager and ns3::RrpaaWifiManager.
  25. *
  26. * The output of this is typically two plot files, named throughput-parf.plt
  27. * (or throughput-aparf.plt, if Aparf is used) and power-parf.plt. If
  28. * Gnuplot program is available, one can use it to convert the plt file
  29. * into an eps file, by running:
  30. * \code{.sh}
  31. * gnuplot throughput-parf.plt
  32. * \endcode
  33. * Also, to enable logging of rate and power changes to the terminal, set this
  34. * environment variable:
  35. * \code{.sh}
  36. * export NS_LOG=PowerAdaptationDistance=level_info
  37. * \endcode
  38. *
  39. * This simulation consist of 2 nodes, one AP and one STA.
  40. * The AP generates UDP traffic with a CBR of 54 Mbps to the STA.
  41. * The AP can use any power and rate control mechanism and the STA uses
  42. * only Minstrel rate control.
  43. * The STA can be configured to move away from (or towards to) the AP.
  44. * By default, the AP is at coordinate (0,0,0) and the STA starts at
  45. * coordinate (5,0,0) (meters) and moves away on the x axis by 1 meter every
  46. * second.
  47. *
  48. * The output consists of:
  49. * - A plot of average throughput vs. distance.
  50. * - A plot of average transmit power vs. distance.
  51. * - (if logging is enabled) the changes of power and rate to standard output.
  52. *
  53. * The Average Transmit Power is defined as an average of the power
  54. * consumed per measurement interval, expressed in milliwatts. The
  55. * power level for each frame transmission is reported by the simulator,
  56. * and the energy consumed is obtained by multiplying the power by the
  57. * frame duration. At every 'stepTime' (defaulting to 1 second), the
  58. * total energy for the collection period is divided by the step time
  59. * and converted from dbm to milliwatt units, and this average is
  60. * plotted against time.
  61. *
  62. * When neither Parf, Aparf or Rrpaa is selected as the rate control, the
  63. * generation of the plot of average transmit power vs distance is suppressed
  64. * since the other Wifi rate controls do not support the necessary callbacks
  65. * for computing the average power.
  66. *
  67. * To display all the possible arguments and their defaults:
  68. * \code{.sh}
  69. * ./waf --run "power-adaptation-distance --help"
  70. * \endcode
  71. *
  72. * Example usage (selecting Aparf rather than Parf):
  73. * \code{.sh}
  74. * ./waf --run "power-adaptation-distance --manager=ns3::AparfWifiManager --outputFileName=aparf"
  75. * \endcode
  76. *
  77. * Another example (moving towards the AP):
  78. * \code{.sh}
  79. * ./waf --run "power-adaptation-distance --manager=ns3::AparfWifiManager --outputFileName=aparf --stepsSize=-1 --STA1_x=200"
  80. * \endcode
  81. *
  82. * To enable the log of rate and power changes:
  83. * \code{.sh}
  84. * export NS_LOG=PowerAdaptationDistance=level_info
  85. * \endcode
  86. */
  87.  
  88. #include "ns3/gnuplot.h"
  89. #include "ns3/command-line.h"
  90. #include "ns3/config.h"
  91. #include "ns3/uinteger.h"
  92. #include "ns3/double.h"
  93. #include "ns3/log.h"
  94. #include "ns3/yans-wifi-helper.h"
  95. #include "ns3/ssid.h"
  96. #include "ns3/mobility-helper.h"
  97. #include "ns3/internet-stack-helper.h"
  98. #include "ns3/ipv4-address-helper.h"
  99. #include "ns3/packet-sink-helper.h"
  100. #include "ns3/on-off-helper.h"
  101. #include "ns3/yans-wifi-channel.h"
  102. #include "ns3/wifi-net-device.h"
  103. #include "ns3/wifi-mac.h"
  104. #include "ns3/wifi-mac-header.h"
  105. #include "ns3/mobility-model.h"
  106. #include "ns3/propagation-loss-model.h"
  107. #include "ns3/flow-monitor.h"
  108. #include "ns3/flow-monitor-helper.h"
  109. #include "ns3/ipv4-flow-classifier.h"
  110. #include "ns3/ipv4-global-routing-helper.h"
  111.  
  112. using namespace ns3;
  113. using namespace std;
  114.  
  115. NodeContainer wifiApNodes;
  116. NodeContainer wifiStaNodes;
  117.  
  118. NetDeviceContainer wifiApDevices1;
  119. NetDeviceContainer wifiApDevices2;
  120. NetDeviceContainer wifiApDevices3;
  121. NetDeviceContainer wifiStaDevices;
  122.  
  123. MobilityHelper mobility;
  124.  
  125. FlowMonitorHelper flowmon;
  126. Ptr<FlowMonitor> monitor;
  127.  
  128. double maxPower = 17;
  129. double minPower = 0;
  130.  
  131. double frequency = 5.150e9;
  132. double C = 299792458.0;
  133. double m_lambda = C / frequency;
  134. double m_systemLoss = 1.0;
  135. double m_minLoss = 0;
  136. double m_pi = 3.141592;
  137.  
  138. double max(double a, double b){
  139. return a>b?a:b;
  140. }
  141.  
  142. struct point
  143. {
  144. double x,y;
  145. };
  146.  
  147. double norm (point p) // get the norm of a vector
  148. {
  149. return pow(pow(p.x,2)+pow(p.y,2),.5);
  150. }
  151.  
  152. point trilateration(point point1, point point2, point point3, double r1, double r2, double r3) {
  153. point resultPose;
  154. //unit vector in a direction from point1 to point 2
  155. double p2p1Distance = pow(pow(point2.x-point1.x,2) + pow(point2.y- point1.y,2),0.5);
  156. point ex = {(point2.x-point1.x)/p2p1Distance, (point2.y-point1.y)/p2p1Distance};
  157. point aux = {point3.x-point1.x,point3.y-point1.y};
  158. //signed magnitude of the x component
  159. double i = ex.x * aux.x + ex.y * aux.y;
  160. //the unit vector in the y direction.
  161. point aux2 = { point3.x-point1.x-i*ex.x, point3.y-point1.y-i*ex.y};
  162. point ey = { aux2.x / norm (aux2), aux2.y / norm (aux2) };
  163. //the signed magnitude of the y component
  164. double j = ey.x * aux.x + ey.y * aux.y;
  165. //coordinates
  166. double x = (pow(r1,2) - pow(r2,2) + pow(p2p1Distance,2))/ (2 * p2p1Distance);
  167. double y = (pow(r1,2) - pow(r3,2) + pow(i,2) + pow(j,2))/(2*j) - i*x/j;
  168. //result coordinates
  169. double finalX = point1.x+ x*ex.x + y*ey.x;
  170. double finalY = point1.y+ x*ex.y + y*ey.y;
  171. resultPose.x = finalX;
  172. resultPose.y = finalY;
  173. return resultPose;
  174. }
  175.  
  176. double DoCalcRxPower (double txPowerDbm, Ptr<MobilityModel> a, Ptr<MobilityModel> b){
  177. /*
  178. * Friis free space equation:
  179. * where Pt, Gr, Gr and P are in Watt units
  180. * L is in meter units.
  181. *
  182. * P Gt * Gr * (lambda^2)
  183. * --- = ---------------------
  184. * Pt (4 * pi * d)^2 * L
  185. *
  186. * Gt: tx gain (unit-less)
  187. * Gr: rx gain (unit-less)
  188. * Pt: tx power (W)
  189. * d: distance (m)
  190. * L: system loss
  191. * lambda: wavelength (m)
  192. *
  193. * Here, we ignore tx and rx gain and the input and output values
  194. * are in dB or dBm:
  195. *
  196. * lambda^2
  197. * rx = tx + 10 log10 (-------------------)
  198. * (4 * pi * d)^2 * L
  199. *
  200. * rx: rx power (dB)
  201. * tx: tx power (dB)
  202. * d: distance (m)
  203. * L: system loss (unit-less)
  204. * lambda: wavelength (m)
  205. */
  206. double distance = a->GetDistanceFrom (b);
  207. if (distance < 3*m_lambda)
  208. {
  209. NS_LOG_UNCOND ("distance not within the far field region => inaccurate propagation loss value");
  210. }
  211. if (distance <= 0)
  212. {
  213. return txPowerDbm - m_minLoss;
  214. }
  215. double numerator = m_lambda * m_lambda;
  216. double denominator = 16 * m_pi * m_pi * distance * distance * m_systemLoss;
  217. double lossDb = -10 * log10 (numerator / denominator);
  218. //NS_LOG_UNCOND ("distance=" << distance<< "m, loss=" << lossDb <<"dB");
  219. return txPowerDbm - max(lossDb, m_minLoss);
  220. }
  221.  
  222. double doCalcDistance(double Pt, double Pr){
  223. double loss = Pt - Pr;
  224. double x = pow(10, (loss / -10));
  225. double xx = (m_lambda * m_lambda) / x;
  226. double xxx = xx / m_systemLoss / 16 / m_pi / m_pi;
  227. double distance = sqrt(xxx);
  228. return distance;
  229. }
  230.  
  231. void scheduling(int stepsSize, int stepsTime){
  232. // signal
  233. monitor->CheckForLostPackets ();
  234. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  235. std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  236. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
  237. {
  238. Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
  239. NS_LOG_UNCOND("Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n");
  240. NS_LOG_UNCOND(" Tx Bytes: " << i->second.txBytes << "\n");
  241. NS_LOG_UNCOND(" Rx Bytes: " << i->second.rxBytes << "\n");
  242. NS_LOG_UNCOND(" Packet Loss Ratio: " << (i->second.txPackets - i->second.rxPackets)*100/(double)i->second.txPackets << " %\n");
  243. }
  244.  
  245. // calculate distance
  246. Ptr<MobilityModel> mobilitySta = wifiStaNodes.Get(0)->GetObject<MobilityModel> ();
  247. Ptr<MobilityModel> mobilityAp1 = wifiApNodes.Get(0)->GetObject<MobilityModel> ();
  248. Ptr<MobilityModel> mobilityAp2 = wifiApNodes.Get(1)->GetObject<MobilityModel> ();
  249. Ptr<MobilityModel> mobilityAp3 = wifiApNodes.Get(2)->GetObject<MobilityModel> ();
  250. //NS_LOG_UNCOND("sta position: " << mobilitySta->GetPosition());
  251. Vector ap1_coord = mobilityAp1->GetPosition();
  252. Vector ap2_coord = mobilityAp2->GetPosition();
  253. Vector ap3_coord = mobilityAp3->GetPosition();
  254. //NS_LOG_UNCOND("ap1 position: " << ap1_coord);
  255. //NS_LOG_UNCOND("ap2 position: " << ap2_coord);
  256. //NS_LOG_UNCOND("ap3 position: " << ap3_coord);
  257. double Rx1 = DoCalcRxPower(maxPower, mobilitySta, mobilityAp1);
  258. double Rx2 = DoCalcRxPower(maxPower, mobilitySta, mobilityAp2);
  259. double Rx3 = DoCalcRxPower(maxPower, mobilitySta, mobilityAp3);
  260. //NS_LOG_UNCOND("RX1 POWER: " << Rx1);
  261. //NS_LOG_UNCOND("RX2 POWER: " << Rx2);
  262. //NS_LOG_UNCOND("RX3 POWER: " << Rx3);
  263. double distance1 = doCalcDistance(maxPower, Rx1);
  264. double distance2 = doCalcDistance(maxPower, Rx2);
  265. double distance3 = doCalcDistance(maxPower, Rx3);
  266. //NS_LOG_UNCOND("distance1: " << distance1);
  267. //NS_LOG_UNCOND("distance2: " << distance2);
  268. //NS_LOG_UNCOND("distance3: " << distance3);
  269.  
  270. point ap1_double_coord = {ap1_coord.x, ap1_coord.y};
  271. point ap2_double_coord = {ap2_coord.x, ap2_coord.y};
  272. point ap3_double_coord = {ap3_coord.x, ap3_coord.y};
  273.  
  274. point coord = trilateration(ap1_double_coord,
  275. ap2_double_coord,
  276. ap3_double_coord,
  277. distance1,
  278. distance2,
  279. distance3);
  280.  
  281. NS_LOG_UNCOND("trilaterated coord, x: " << coord.x << " y: " << coord.y);
  282.  
  283. // advance position
  284. Vector sta_coord = mobilitySta->GetPosition();
  285. sta_coord.x += 1;
  286. sta_coord.y -= 0.5;
  287. mobilitySta->SetPosition(sta_coord);
  288.  
  289. if(stepsTime < 5) Simulator::Schedule(Seconds(stepsSize), &scheduling, stepsSize, stepsTime + 1);
  290. }
  291.  
  292. int main (int argc, char *argv[]){
  293. uint32_t powerLevels = 18;
  294.  
  295. uint32_t rtsThreshold = 2346;
  296. std::string manager = "ns3::ParfWifiManager";
  297. int ap1_x = 1;
  298. int ap1_y = 1;
  299. int ap2_x = 1;
  300. int ap2_y = 4;
  301. int ap3_x = 6;
  302. int ap3_y = 1;
  303. int sta1_x = 3;
  304. int sta1_y = 3;
  305. int packetSize = 5;
  306. double simuTime = 6;
  307. double rss = -80; // -dBm
  308. //uint32_t steps = 200;
  309.  
  310. //Define the APs
  311. wifiApNodes.Create (3);
  312.  
  313. //Define the STAs
  314. wifiStaNodes.Create (1);
  315.  
  316. WifiHelper wifi;
  317. wifi.SetStandard (WIFI_PHY_STANDARD_80211a);
  318. wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
  319. WifiMacHelper wifiMac;
  320. YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  321.  
  322. YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
  323. wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  324. wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rss));
  325. wifiPhy.SetChannel (wifiChannel.Create ());
  326.  
  327. //Configure the STA node
  328. wifi.SetRemoteStationManager ("ns3::MinstrelWifiManager", "RtsCtsThreshold", UintegerValue (rtsThreshold));
  329. wifiPhy.Set ("TxPowerStart", DoubleValue (maxPower));
  330. wifiPhy.Set ("TxPowerEnd", DoubleValue (maxPower));
  331. wifiPhy.Set ("TxPowerLevels", UintegerValue (powerLevels));
  332.  
  333. Ssid ssid = Ssid ("AP");
  334. wifiMac.SetType ("ns3::StaWifiMac",
  335. "Ssid", SsidValue (ssid));
  336. wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes));
  337.  
  338. ssid = Ssid ("AP");
  339. wifiMac.SetType ("ns3::ApWifiMac",
  340. "Ssid", SsidValue (ssid));
  341. wifiApDevices1.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes));
  342. wifiApDevices2.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes));
  343. wifiApDevices3.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes));
  344.  
  345. //Configure the mobility.
  346. Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  347. //Initial position of AP and STA
  348. positionAlloc->Add (Vector (ap1_x, ap1_y, 0.0));
  349. NS_LOG_UNCOND ("Setting initial AP1 position to " << Vector (ap1_x, ap1_y, 0.0));
  350. positionAlloc->Add (Vector (ap2_x, ap2_y, 0.0));
  351. NS_LOG_UNCOND ("Setting initial AP2 position to " << Vector (ap2_x, ap2_y, 0.0));
  352. positionAlloc->Add (Vector (ap3_x, ap3_y, 0.0));
  353. NS_LOG_UNCOND ("Setting initial AP3 position to " << Vector (ap3_x, ap3_y, 0.0));
  354. positionAlloc->Add (Vector (sta1_x, sta1_y, 0.0));
  355. NS_LOG_UNCOND ("Setting initial STA position to " << Vector (sta1_x, sta1_y, 0.0));
  356.  
  357.  
  358. mobility.SetPositionAllocator (positionAlloc);
  359. mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  360. mobility.Install (wifiStaNodes);
  361. mobility.Install (wifiApNodes);
  362.  
  363. //Configure the IP stack
  364. InternetStackHelper stack;
  365. stack.Install (wifiStaNodes);
  366. stack.Install (wifiApNodes);
  367.  
  368. Ipv4AddressHelper address1;
  369. address1.SetBase ("10.1.1.0", "255.255.255.0");
  370. address1.Assign(wifiStaDevices.Get (0));
  371. //address2.Assign(wifiStaDevices.Get (0));
  372. //address3.Assign(wifiStaDevices.Get (0));
  373. Ipv4InterfaceContainer i1 = address1.Assign (wifiApDevices1.Get (0));
  374. Ipv4InterfaceContainer i2 = address1.Assign (wifiApDevices2.Get (0));
  375. Ipv4InterfaceContainer i3 = address1.Assign (wifiApDevices3.Get (0));
  376. Ipv4Address sinkAddress1 = i1.GetAddress (0);
  377. Ipv4Address sinkAddress2 = i2.GetAddress (0);
  378. Ipv4Address sinkAddress3 = i3.GetAddress (0);
  379. uint16_t port = 9;
  380.  
  381. OnOffHelper onoff1 ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress1, port));
  382. OnOffHelper onoff2 ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress2, port));
  383. OnOffHelper onoff3 ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress3, port));
  384. onoff1.SetConstantRate (DataRate ("54Mb/s"), packetSize);
  385. onoff1.SetAttribute ("StartTime", TimeValue (Seconds (0)));
  386. onoff1.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
  387. onoff2.SetConstantRate (DataRate ("54Mb/s"), packetSize);
  388. onoff2.SetAttribute ("StartTime", TimeValue (Seconds (0)));
  389. onoff2.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
  390. onoff3.SetConstantRate (DataRate ("54Mb/s"), packetSize);
  391. onoff3.SetAttribute ("StartTime", TimeValue (Seconds (0)));
  392. onoff3.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
  393. ApplicationContainer apps1;
  394. apps1.Add(onoff1.Install (wifiStaNodes.Get(0)));
  395. //apps2.Add(onoff1.Install (wifiStaNodes.Get (0)));
  396. //apps3.Add(onoff1.Install (wifiStaNodes.Get (0)));
  397.  
  398. //Configure the CBR generator
  399. PacketSinkHelper sink1 ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress1, port));
  400. PacketSinkHelper sink2 ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress2, port));
  401. PacketSinkHelper sink3 ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress3, port));
  402. ApplicationContainer apps2;
  403. apps2.Add(sink1.Install(wifiApNodes.Get(0)));
  404. //ApplicationContainer apps2 = sink2.Install (wifiApNodes.Get (1));
  405. //ApplicationContainer apps3 = sink3.Install (wifiApNodes.Get (2));
  406.  
  407.  
  408. //apps_sink1.Start (Seconds (0));
  409. //apps_sink1.Stop (Seconds (simuTime));
  410. //apps_sink2.Start (Seconds (0));
  411. //apps_sink2.Stop (Seconds (simuTime));
  412. //apps_sink3.Start (Seconds (0));
  413. //apps_sink3.Stop (Seconds (simuTime));
  414.  
  415. monitor = flowmon.Install(wifiStaNodes);
  416. monitor = flowmon.Install(wifiApNodes);
  417.  
  418. apps1.Start(Seconds(0));
  419. apps1.Stop(Seconds(simuTime));
  420. apps2.Start(Seconds(0));
  421. apps2.Stop(Seconds(simuTime));
  422.  
  423. Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  424.  
  425. //apps2.Start(Seconds(0));
  426. //apps2.Stop(Seconds(simuTime));
  427.  
  428. //apps3.Start(Seconds(0));
  429. //apps3.Stop(Seconds(simuTime));
  430.  
  431. //Simulator schedule
  432. Simulator::Schedule (Seconds(0.1), &scheduling, 1, 0);
  433.  
  434. NS_LOG_UNCOND ("Run Simulation.");
  435. NS_LOG_UNCOND ("STA moving (1,-0.5) at one time");
  436. Simulator::Stop (Seconds (simuTime));
  437. Simulator::Run();
  438. Simulator::Destroy();
  439. NS_LOG_UNCOND ("End Simulation.");
  440.  
  441. return 0;
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement