NS3网络仿真(7): Wifi节点

快乐虾

http://blog.csdn.net/lights_joy/

欢迎转载,但请保留作者信息


在上一节中,我们仿真了一个总线型网络,这一节尝试将上一节中的n0变成一个无线的AP,再连上几个节点。这也是NS3中的示例third.cc干的事情,只是我们用Python实现。

// Default Network Topology
//
//   Wifi 10.1.3.0
//                 AP
//  *    *    *    *
//  |    |    |    |    10.1.1.0
// n5   n6   n7   n0 -------------- n1   n2   n3   n4
//                   point-to-point  |    |    |    |
//                                   ================
//                                     LAN 10.1.2.0


与上一节一样,先构造p2p网络,再构建总线型网线:

# 构建点对点连接
p2pNodes = ns.network.NodeContainer()
p2pNodes.Create (2)

pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute ("DataRate", ns.core.StringValue ("5Mbps"))
pointToPoint.SetChannelAttribute ("Delay", ns.core.StringValue ("2ms"))
p2pDevices = pointToPoint.Install (p2pNodes)

# 构建总线连接
nCsma = 3

csmaNodes = ns.network.NodeContainer()
csmaNodes.Add (p2pNodes.Get (1))
csmaNodes.Create (nCsma)

csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("100Mbps"))
csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.NanoSeconds (6560)))
csmaDevices = csma.Install (csmaNodes)

接着构建无线网络:

# 构建Wifi连接
nWifi = 3
wifiStaNodes = ns.network.NodeContainer()
wifiStaNodes.Create (nWifi)
wifiApNode = p2pNodes.Get (0)

channel = ns.wifi.YansWifiChannelHelper.Default ()
phy = ns.wifi.YansWifiPhyHelper.Default ()
phy.SetChannel (channel.Create ())

接着配置AP

# 配置AP
wifi = ns.wifi.WifiHelper.Default ()
wifi.SetRemoteStationManager ("ns3::AarfWifiManager")

mac = ns.wifi.NqosWifiMacHelper.Default ()

ssid = ns.wifi.Ssid ("ns-3-ssid")
mac.SetType ("ns3::StaWifiMac",
            "Ssid", ns.wifi.SsidValue (ssid),
            "ActiveProbing", ns.core.BooleanValue (False))

staDevices = wifi.Install (phy, mac, wifiStaNodes)

mac.SetType ("ns3::ApWifiMac",
            "Ssid", ns.wifi.SsidValue (ssid))

apDevices = wifi.Install (phy, mac, wifiApNode);

接着配置无线节点的位置参数:

# 配置无线节点的位置
mobility = ns.mobility.MobilityHelper()

mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                                "MinX", ns.core.DoubleValue (0.0),
                                "MinY", ns.core.DoubleValue (0.0),
                                "DeltaX", ns.core.DoubleValue (5.0),
                                "DeltaY", ns.core.DoubleValue (10.0),
                                "GridWidth", ns.core.UintegerValue (3),
                                "LayoutType", ns.core.StringValue ("RowFirst"))

mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                            "Bounds", ns.mobility.RectangleValue (ns.mobility.Rectangle (-50, 50, -50, 50)))
mobility.Install (wifiStaNodes)

mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel")
mobility.Install (wifiApNode)

接着安装协议栈:

# 安装协议栈
stack = ns.internet.InternetStackHelper()
stack.Install (csmaNodes)
stack.Install (wifiApNode)
stack.Install (wifiStaNodes)

配置IP,这个和上一节一样,只是加上10.1.3.0网段而已:

# 配置IP
address = ns.internet.Ipv4AddressHelper()
address.SetBase (
    ns.network.Ipv4Address("10.1.1.0"), 
    ns.network.Ipv4Mask("255.255.255.0"))
p2pInterfaces = address.Assign (p2pDevices)

address.SetBase (
    ns.network.Ipv4Address("10.1.2.0"), 
    ns.network.Ipv4Mask("255.255.255.0"))
csmaInterfaces = address.Assign (csmaDevices)

address.SetBase (
    ns.network.Ipv4Address("10.1.3.0"), 
    ns.network.Ipv4Mask("255.255.255.0"))
address.Assign (staDevices)
address.Assign (apDevices)

接下来模拟一个Echo服务,这个与上一节相同,只是Client安装在了Wifi节点上。

# 配置应用程序
echoServer = ns.applications.UdpEchoServerHelper (9)

serverApps = echoServer.Install (csmaNodes.Get (nCsma))
serverApps.Start (ns.core.Seconds (1.0))
serverApps.Stop (ns.core.Seconds (20.0))

echoClient = ns.applications.UdpEchoClientHelper (csmaInterfaces.GetAddress (nCsma), 9)
echoClient.SetAttribute ("MaxPackets", ns.core.UintegerValue (5))
echoClient.SetAttribute ("Interval", ns.core.TimeValue (ns.core.Seconds (1.0)))
echoClient.SetAttribute ("PacketSize", ns.core.UintegerValue (1024))

clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1))
clientApps.Start (ns.core.Seconds (2.0))
clientApps.Stop (ns.core.Seconds (20.0))

接下来的部分与上一节几乎完全相同,只是加上了Simulator.Stop,因为如果没有这个函数调用,那么将导致Simulator.Run永远不会停止:

# 全局路由管理器根据节点产生 的链路通告为每个节点建立路由表
ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()

ns.core.Simulator.Stop (ns.core.Seconds (10.0));

pointToPoint.EnablePcapAll ("third");
csma.EnablePcap ("third", csmaDevices.Get (1), True)
phy.EnablePcap ("third", apDevices.Get (0))

anim = ns.netanim.AnimationInterface('third.xml')
anim.SetConstantPosition(p2pNodes.Get(0), 10, 10)
anim.SetConstantPosition(csmaNodes.Get(0), 30, 10)
anim.SetConstantPosition(csmaNodes.Get(1), 40, 10)
anim.SetConstantPosition(csmaNodes.Get(2), 50, 10)
anim.SetConstantPosition(csmaNodes.Get(3), 60, 10)
anim.EnablePacketMetadata(True)

# 开始仿真
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()

 


看看NetAnim显示的仿真结果:


再看看third-0-1.pcap的内容:


如我们所愿,802.11协议,呵呵~~~~~


 


 








已标记关键词 清除标记
在尝试在NS3已有场景代码lena-dual-stripe.cc基础上更改得到车联网V2I场景时,设置切换算法代码如下: ``` //lteHelper->SetHandoverAlgorithmType ("ns3::NoOpHandoverAlgorithm"); lteHelper->SetHandoverAlgorithmType ("ns3::A2A4RsrqHandoverAlgorithm"); //lteHelper->SetHandoverAlgorithmType ("ns3::A3RsrpHandoverAlgorithm"); lteHelper->SetHandoverAlgorithmAttribute ("ServingCellThreshold", UintegerValue (30)); lteHelper->SetHandoverAlgorithmAttribute ("NeighbourCellOffset", UintegerValue (1)); ``` ``` if (epc) { // this enables handover for macro eNBs lteHelper->AddX2Interface (macroEnbs); lteHelper->AddX2Interface (cellEnbs); } ``` 在单给macroEnbs添加x2接口时能够正确切换,其结果如下 ![图片说明](https://img-ask.csdn.net/upload/202002/16/1581856593_988305.png) 可以正常进行切换。 但在加入了小基站的x2接口后便会报错:![图片说明](https://img-ask.csdn.net/upload/202002/16/1581856716_355589.png) ``` assert failed. cond="m_x2InterfaceSockets.find (remoteCellId) == m_x2InterfaceSockets.end ()", msg="Mapping for remoteCellId = 2 is already known", file=../src/lte/model/epc-x2.cc, line=156 ``` 分析大概是cellID的问题,但是不知道怎么设置改正,求大神指导! 下面是小基站配置的代码: ``` Ptr<ListPositionAllocator> enbPositionAlloc = CreateObject<ListPositionAllocator> (); enbPositionAlloc->Add (Vector (150, 80, 0)); enbPositionAlloc->Add (Vector (200, 120, 0)); enbPositionAlloc->Add (Vector (250, 80, 0)); enbPositionAlloc->Add (Vector (300, 120, 0)); enbPositionAlloc->Add (Vector (350, 80, 0)); enbPositionAlloc->Add (Vector (400, 120, 0)); enbPositionAlloc->Add (Vector (450, 80, 0)); enbPositionAlloc->Add (Vector (500, 120, 0)); enbPositionAlloc->Add (Vector (550, 80, 0)); enbPositionAlloc->Add (Vector (600, 120, 0)); enbPositionAlloc->Add (Vector (450, 160, 0)); enbPositionAlloc->Add (Vector (550, 160, 0)); enbPositionAlloc->Add (Vector (500, 200, 0)); enbPositionAlloc->Add (Vector (600, 200, 0)); enbPositionAlloc->Add (Vector (550, 240, 0)); enbPositionAlloc->Add (Vector (650, 240, 0)); enbPositionAlloc->Add (Vector (650, 80, 0)); enbPositionAlloc->Add (Vector (700, 120, 0)); //enbPositionAlloc->Add (Vector (0, 40, 0)); //enbPositionAlloc->Add (Vector (50, 0, 0)); /*enbPositionAlloc->Add (Vector (450, 480, 0)); enbPositionAlloc->Add (Vector (550, 480, 0)); enbPositionAlloc->Add (Vector (360, 195, 0)); enbPositionAlloc->Add (Vector (440, 195, 0)); enbPositionAlloc->Add (Vector (100, 305, 0)); enbPositionAlloc->Add (Vector (300, 305, 0)); enbPositionAlloc->Add (Vector (60, 370, 0)); enbPositionAlloc->Add (Vector (140, 370, 0)); enbPositionAlloc->Add (Vector (260, 370, 0)); enbPositionAlloc->Add (Vector (340, 370, 0));*/ mobility.SetPositionAllocator (enbPositionAlloc); mobility.Install (smallEnbs); BuildingsHelper::Install(smallEnbs); Config::SetDefault ("ns3::LteEnbPhy::TxPower", DoubleValue (smallEnbTxPowerDbm)); lteHelper->SetEnbAntennaModelType ("ns3::IsotropicAntennaModel"); lteHelper->SetEnbDeviceAttribute ("DlEarfcn", UintegerValue (smallEnbDlEarfcn)); lteHelper->SetEnbDeviceAttribute ("UlEarfcn", UintegerValue (smallEnbDlEarfcn + 18000)); lteHelper->SetEnbDeviceAttribute ("DlBandwidth", UintegerValue (smallEnbBandwidth)); lteHelper->SetEnbDeviceAttribute ("UlBandwidth", UintegerValue (smallEnbBandwidth)); lteHelper->SetEnbDeviceAttribute ("CsgId", UintegerValue (1)); lteHelper->SetEnbDeviceAttribute ("CsgIndication", BooleanValue (true)); lteHelper->SetAttribute ("PathlossModel", StringValue ("ns3::FriisSpectrumPropagationLossModel")); lteHelper->SetSpectrumChannelType ("ns3::MultiModelSpectrumChannel"); lteHelper->SetFfrAlgorithmType ("ns3::LteFrHardAlgorithm"); NetDeviceContainer smallEnbDevs = lteHelper->InstallEnbDevice (smallEnbs); ```
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页