NS3仿真wifi网络环境 - 设置无线节点的通信传输范围

参考链接:

https://www.zhihu.com/question/24549780/answer/66061998

实验结论:

1. 如果采用默认的wifi信道和物理层,则结点的默认通信距离是100m。

void
AodvExample::CreateDevices ()
{
  WifiMacHelper wifiMac;
  wifiMac.SetType ("ns3::AdhocWifiMac");
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
  wifiPhy.SetChannel (wifiChannel.Create ());
  WifiHelper wifi;
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
  devices = wifi.Install (wifiPhy, wifiMac, nodes); 

  if (pcap)
    {
      wifiPhy.EnablePcapAll (std::string ("aodv"));
    }
}

 上述代码来源于*/ns-3.29/src/aodv/examples目录下的aodv.cc

2.  一种简单的通信范围设置方法

          将信道衰落模型(PropagationLossModel)设置为RangePropagationLossModel,然后它有个属性是“MaxRange”,设置它就几乎是直接设置了通信范围。  原理是:这个衰落模型是一个很理想化的模型,其核心思想就是小于通信半径(MaxRange)时返回接收增益为0db,大于通信半径时返回接收增益为-1000db,所以就会造成上述效果。

  YansWifiChannelHelper wifiChannel;
  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue(nodeRange));  //可修改结点通信范围
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  wifiPhy.SetChannel (wifiChannel.Create ());

此时可以通过修改nodeRange的值来设置结点通信范围。

3.完整代码

// Network topology
//       n0 ------ n1
//           wifi
// - UDP flows from n0 to n1

#include <fstream>
#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "ns3/ns2-mobility-helper.h"
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/netanim-module.h"

using namespace ns3;
using namespace std;

NS_LOG_COMPONENT_DEFINE ("UdpClientServerExample");

int
main (int argc, char *argv[])
{
  LogComponentEnable ("UdpClient", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpServer", LOG_LEVEL_INFO);

  bool useV6 = false;
  Address serverAddress;             //服务器地址
  double nodeRange = 250;            //结点通信范围
  double totalTime = 15;             //仿真时间

  CommandLine cmd;
  cmd.AddValue ("useIpv6", "Use Ipv6", useV6);
  cmd.Parse (argc, argv);

// Explicitly create the nodes required by the topology (shown above).
  NS_LOG_INFO ("Create nodes.");
  NodeContainer n;
  n.Create (2);

  //创建移动模型,初始化两结点位置
  MobilityHelper nMobility;
  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  positionAlloc->Add (Vector (0.0, 250.0, 0.0));
  positionAlloc->Add (Vector (200.0, 250.0, 0.0));
  nMobility.SetPositionAllocator (positionAlloc);
  nMobility.SetMobilityModel ("ns3::ConstantVelocityMobilityModel");
  nMobility.Install (n);

  //结点0不动,结点1动起来
  Ptr<ConstantVelocityMobilityModel> mob = n.Get(1)->GetObject<ConstantVelocityMobilityModel>();
  mob->SetVelocity(Vector(10, 0, 0));

  InternetStackHelper internet;
  internet.Install (n);

  // Explicitly create the channels required by the topology (shown above).
  NS_LOG_INFO ("Create channels.");  
  WifiMacHelper wifiMac;
  wifiMac.SetType ("ns3::AdhocWifiMac");

  YansWifiChannelHelper wifiChannel;
  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue(nodeRange));  //可修改结点通信范围
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  wifiPhy.SetChannel (wifiChannel.Create ());
  //此处可设置物理层其他参数
  wifiPhy.EnablePcapAll (std::string ("udptest"));     //生成pcap格式文件,可用wirkshark打开并分析

  WifiHelper wifi;
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
  NetDeviceContainer d;
  d = wifi.Install (wifiPhy, wifiMac, n); 

  // 分配ip地址
  NS_LOG_INFO ("Assign IP Addresses.");
  if (useV6 == false)
    {
      Ipv4AddressHelper ipv4;
      ipv4.SetBase ("10.1.1.0", "255.255.255.0");
      Ipv4InterfaceContainer i = ipv4.Assign (d);
      serverAddress = Address (i.GetAddress (1));
    }
  else
    {
      Ipv6AddressHelper ipv6;
      ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64));
      Ipv6InterfaceContainer i6 = ipv6.Assign (d);
      serverAddress = Address(i6.GetAddress (1,1));
    }

  NS_LOG_INFO ("Create Applications.");

// Create one udpServer applications on node one.
  uint16_t port = 4000;
  UdpServerHelper server (port);
  ApplicationContainer apps = server.Install (n.Get (1));
  apps.Start (Seconds (1.0));
  apps.Stop (Seconds (10.0));

// Create one UdpClient application to send UDP datagrams from node zero to node one.
  uint32_t MaxPacketSize = 1024;
  Time interPacketInterval = Seconds (0.5);
  uint32_t maxPacketCount = 10;
  UdpClientHelper client (serverAddress, port);
  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
  apps = client.Install (n.Get (0));
  apps.Start (Seconds (2.0));
  apps.Stop (Seconds (10.0));

// Now, do the actual simulation.
  NS_LOG_INFO ("Run Simulation.");
  AnimationInterface anim ("udpclientserver.xml");

  Simulator::Stop (Seconds (totalTime));
  Simulator::Run ();
  Simulator::Destroy ();
  NS_LOG_INFO ("Done.");
}

程序功能:

        该wifi网络共有两个结点n0和n1,结点通信范围为250m。其中n0安装udpcilent应用,n1安装udpserver应用。客户端n0向n1共发送10个包,每个包间隔0.5s。但是由于n1是不断移动,n1只能成功收到5个数据包。当n1走到(250,250,0)位置后,两结点之间距离开始大于250m,此时没法继续收包)。

4. 实验结果可视化分析

 用wireshark打开生成的pcap文件,发现发送端n0中只成功发送5个包,接收端收到5个包。

欢迎点赞和收藏

  • 6
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值