NS3 移动模型

您所在的位置:网站首页 NS3车联网仿真 NS3 移动模型

NS3 移动模型

2023-09-19 21:24| 来源: 网络整理| 查看: 265

转自https://blog.csdn.net/qq_41816035/article/details/88071765

NS3整体介绍 目录结构 src目录是ns-3的源代码目录,其目录结构基本和ns-3模块相对应

这些模块目录的子目录都是固定的。

bindings/ 绑定python语言的 doc/ 帮助文档 examples/ 应用该模块的示例代码 helper/ 模块对应的helper类源文件 model/ 模块代码的.cc和.h文件 test/ 模块设计者编写的模块测试代码 wscript 一些常用模块

模块    用途 core    ns3内核模块,实现了ns-3的基本机制,智能指针,属性,回调,随机变量,日志,追踪,事件调度等 network    网络数据分组模块,一般仿真都会使用 Internet    实现了TCP/IP相关的协议族,IP, ARP, UDP, TCP等 applications    几种常见的应用层协议 mobility    移动模型模块,为节点添加移动属性 status    统计框架模块,方便对仿真数据的收集、分析和统计 tools    统计工具,作图工具gunplot netanim    动画演示工具 visualizer    可视化界面工具 几种典型的网络模块

模块    用途 point-to-point    点对点网络 CSMA    实现基于IEEE802.3的以太网网络,包括MAC层,物理层和媒体信道 Wi-Fi    实现基于IEEE802.11a/b/g的无线网络,也可以是有基础设施的ad hoc网络 mesh    实现基于IEEE802.11s的无线mesh网络 wimax    实现基于IEEE802.16标准的无线城域网络 几种ad hoc网络路由协议模块 :aodv, dsdv, olsr。

这些模块当中,core,network,internet和status是ns-3的基础模块,学习和开发其它模块首先要掌握这几个基础模块。

ns3 关键概念 节点 ns-3中的基本计算设备被抽象为节点。此节点是一个可以添加各种功能的计算机。当然仅仅是个计算机,要想使它工作,还需要添加网卡,协议栈,应用程序。

在ns-3中节点用Node类来描述,下面两行代码会创建两个节点对象,它们在仿真中代表计算机

NodeContainer nodes; nodes.Create(2); 应用 计算机软件中通常分为系统软件和应用软件。应用软件完成用户工作,系统软件管理计算机中的内存,CPU,硬盘,网络等各种资源。在ns-3中并没有正常的操作系统的概念,但是用应用程序的概念。在ns-3中,需要被仿真的用户程序被抽象为应用。在C++中用Application类描述。

bulk-send-application on-off-application udp-client/server udp-echo-client/server

信道 通常把网络中数据流过的媒介称作为信道,在ns-3中,节点需要连接到信道上来进行数据交换,在C++中用Channel类来描述,一个信道实例可以模拟一条简单的线缆,也可以是一个复杂的巨型以太网交换机,甚至是一个无线网络中充满障碍物的三维空间。

CsmaChannel PointToPointChannel Wi-FiChannel 网络设备 把计算机连接到网络上,必须用网线连接到网卡上。现在计算机出厂的时候都已经配置了网卡,所以用户一般看不到这些模块。一张网卡只是个外围设备,设备需要驱动软件来控制,如果缺少软件驱动它还是不能工作的。在ns-3中,网络设备这一抽象概念相当硬件设备和软件驱动的总和。网络设备安装在节点上,然后节点之间通过信道和其他节点通信。这个网络和信道是相对应的,就像无线网卡不能连接网线,只能在在无线环境中使用。C++中用NetDevice类来描述网络设备。

CsmaNetDevice PointToPointNetDevice Wi-FiNetNevice 搭建网络仿真场景和搭建真实网络类似 首先要有网络节点(Node),节点需要有网络设备(NetDevice),网络设备需要通过传输媒体(Channel)连接

仿真脚本的编写 脚本编写一般都是经过以下步骤

步骤    实例 1. 创建节点    NodeContainer 2. 创建链路类型    XxxDeviceHelper 3. 安装链路类型,生成网卡    XxxDeviceContainer=XxxDeviceHelper.install(NodeContainer) 4. 安装协议栈    XxxStack.install(NodeContainer) 5. 配置IP地址    XxxAddressHelper.setBase(“IP”,“NETMASK”) 6. 生成网络接口    XxxInterfaceContainer = XxxAddressHelper.Assign(NetDeviceContainer ) 7. 安装应用    ApplicationContainer = XxxHelper.Install(NodeContainer); 8. 开始仿真     结合 tutroial/first.cc 例子讲解,first.cc两个P2P节点,client节点向server节点发送数据,server回显数据。

示例中仅发送了一个数据包。MaxPacket属性是数据包数量,Interval是发送间隔,PacketSize是数据包大小

main (int argc, char *argv[]) {     CommandLine cmd;     cmd.Parse (argc, argv);          Time::SetResolution (Time::NS);     LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);     LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);          //1. 创建节点     NodeContainer nodes;     nodes.Create (2);          //2.为节点创建P2P类型的链路,并配置链路属性     PointToPointHelper pointToPoint;     pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));     pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));          //3.安装链路,生成网卡     NetDeviceContainer devices;     devices = pointToPoint.Install (nodes);          //4.安装协议栈     InternetStackHelper stack;     stack.Install (nodes);          //5.为网卡配置IP     Ipv4AddressHelper address;     address.SetBase ("10.1.1.0", "255.255.255.0");     //6.生成网络接口     Ipv4InterfaceContainer interfaces = address.Assign (devices);          UdpEchoServerHelper echoServer (9);     ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));     serverApps.Start (Seconds (1.0));     serverApps.Stop (Seconds (10.0));          //7.配置应用     UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);     echoClient.SetAttribute ("MaxPackets", UintegerValue (1));     echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));     echoClient.SetAttribute ("PacketSize", UintegerValue (1024));          ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));     clientApps.Start (Seconds (2.0));     clientApps.Stop (Seconds (10.0));          //8.开始仿真     Simulator::Run ();     Simulator::Destroy ();     return 0; }

ns3拓扑构造 构建拓扑一般是使用以下步骤

先构建节点,网络类型。节点使用网络连接起来生产网卡 给网卡绑定IP地址,生成网络接口(之后可以在接口上做业务) 使用拓扑帮助

Device = Helper.Install(Node); /*  生成网卡     Device是网卡     Helper是网络类型,     Node是要来连接的节点 */

Interface = Address.Assign(Device); /*     绑定IP     Interface是网络接口接口,供后续Application使用     Address是网络地址,是一个网段     Device是网卡 */

Ipv4GlobalRoutingHelper::PopulateRoutingTables (); /*     用拓扑帮助类构建路由 */ 点对点网络示例

NodeContainer c; c.create(2);

NodeContainer n0n1 = NodeContainer(c.Get(0), c.Get(1));

PointToPointHelper p2p; p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); p2p.SetChannelAttribute ("Delay", StringValue ("2ms")); NetDeviceContainer d0d1 = p2p.Install (n0n1);

Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); xxx = address.Assign (d0d1); // 为d0d1里的两个节点分配ip, 10.1.1.1 -------10.1.1.2

Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 以太网总线网络

NodeContainer c; c.create(4);

CsmaHelper n0n1n2n3 = NodeContainer(c.Get(0), c.Get(1), c.Get(2), c.Get(3)); //CsmaHelper n0n1n2n3 = NodeContianer(c); csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer d0d1d2d3 = csma.Install (n0n1n2n3);

Ipv4AddressHelper address; address.SetBase ("10.1.2.0", "255.255.255.0"); xxx = address.Assign (d0d1d2d3); // 为d0d1d2d3中的节点分别分配ip // 10.1.2.1----10.1.2.2----10.1.2.3----10.1.2.4 Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 无线网拓扑

NodeContainer c; c.create(3);

// 让第一个节点做AP节点,其他的连接这个AP NodeContainer ApNode = c.Get(0);

// 其他的做无线设备,连接AP节点 NodeContainer StaNode = NodeContainer(c.Get(1), c.Get(2));

WifiHelper wifi; wifi.SetRemoteStationManager ("ns3::AarfWifiManager");

WifiMacHelper mac; Ssid ssid = Ssid ("ns-3-ssid");

//设置AP节点网络 mac.SetType ("ns3::ApWifiMac",            "Ssid", SsidValue (ssid)); NetDeviceContainer apDevices = wifi.Install (phy, mac, wifiApNode);

//设置无线设备网络 mac.SetType ("ns3::StaWifiMac",            "Ssid", SsidValue (ssid),            "ActiveProbing", BooleanValue (false)); NetDeviceContainer staDevices = wifi.Install (phy, mac, wifiStaNodes);

Ipv4AddressHelper address; address.SetBase ("10.1.2.0", "255.255.255.0"); address.Assign (staDevices); address.Assign (apDevices); 应用 所有应用程序要分别用apps.Start(), apps.Stop()设置应用的启动停止时间,注意服务器程序要比客户端早启动

UdpClient,UdpServer Udp应用,顾名思义,构建一个udp应用,客户端往服务器端发数据。可以设置数据包的大小,数量,发送间隔

在一个节点上安装udpserver

NodeContainer c; c.create(2); ... ... uint16_t port = 4000; UdpServerHelper server (port); ApplicationContainer apps = server.Install (c.Get (1)); apps.Start (Seconds (1.0)); apps.Stop (Seconds (10.0));

在一个节点上安装udpclient

UdpClientHelper client (serverAddress, port);//绑定服务器地址,地址用interface.GetAddress方法获取 client.SetAttribute ("MaxPackets", UintegerValue (320));//发送多少个包 client.SetAttribute ("Interval", TimeValue (0.05));//发送间隔 client.SetAttribute ("PacketSize", UintegerValue (1024));//包大小 apps = client.Install (n.Get (0));//安装到哪个节点 apps.Start (Seconds (2.0)); apps.Stop (Seconds (10.0)); OnOffApplication OnOff应用是个开关应用,开的时候产生流量,关的时候不产生流量。

BulkSendApplication PacketSink 移动模型 MobilityHelper 类用来设置节点移动模型 SetPositionAllocator

MobilityHelper mobility mobility.SetPositionAllocator()//设置节点初始位置 mobility.SetMobilityModel()  //设置节点运动方式

初始位置-网格布局 mobility.SetPositionAllocator ("ns3::GridPositionAllocator",                                  "MinX", DoubleValue (-100.0),                                  "MinY", DoubleValue (-100.0),                                  "DeltaX", DoubleValue (5.0),                                  "DeltaY", DoubleValue (20.0),                                  "GridWidth", UintegerValue (20),                                  "LayoutType", StringValue ("RowFirst")); GridPositionAllocator的属性以及默认值

“GridWidth”, “一行最多有几个节点”,UintegerValue (10), “MinX”, “网格布局起始处在x轴上的坐标.”,DoubleValue (1.0) “MinY”, “网格布局起始处在y轴上的坐标.”,DoubleValue (0.0) “DeltaX”, “x轴上节点间的距离.”,DoubleValue (1.0) “DeltaY”, “y轴上节点间的距离”, DoubleValue (1.0) “LayoutType”, “布局类型.”,EnumValue (ROW_FIRST) 如果有100个节点,GirdWidth值为10,则一行10个,共10行,Girdwidth为20,则一行20个,共5行

初始位置-随机布局 在一个圆内随机布局

mobility.SetPositionAllocator ("ns3::RandomDiscPositionAllocator",                              "X", StringValue ("100.0"),                              "Y", StringValue ("100.0"),                              "Rho", StringValue ("ns3::UniformRandomVariable[Min=0|Max=30]")); RandomDiscPositionAllocator属性以及默认值

“Theta”,“随机角度.”, StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=6.2830]”), “Rho”,“随机半径”, StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=200.0]”), “X”,“圆心x轴坐标”, DoubleValue (0.0), “Y”,“圆心y轴坐标”, DoubleValue (0.0) ns3::UniformRandomVariable[Min=0.0|Max=6.2830] 一个随机的值,0.0~6.2830

运动模型-固定不动 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 1 运动模型-随机运动 在一个2d场景随机运动,方向和速度都是随机的

mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",                          "Mode", StringValue ("Time"),                          "Time", StringValue ("2s"),                          "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),                          "Bounds", StringValue ("0|200|0|200")); RandomWalk2dMobilityModel模型的属性以及默认值

“Bounds”,“运动的范围”, RectangleValue (Rectangle (0.0, 100.0, 0.0, 100.0)) “Time”,“走多久换一次方向”,TimeValue (Seconds (1.0)) “Distance”,"走多远换一次方向"DoubleValue (1.0), “Mode”,“Time or Distance”,EnumValue (RandomWalk2dMobilityModel::MODE_DISTANCE) “Direction”,"随机方向 (radians)."StringValue (“ns3::UniformRandomVariable[Min=0.0|Max=6.283184]”) “Speed”“随机速度 (m/s).”,StringValue (“ns3::UniformRandomVariable[Min=2.0|Max=4.0]”) Mode 是选择根据运动时间来变方向还是根据运动距离来变方向

自定义运动 MobilityModel::SetPosition 方法用来控制模型的移动,

//在5秒后node节点会沿x轴,y轴,z轴方向分别移动1米, Ptr mob = node->GetObject (); Simulator::Schedule (Seconds (5.0), &MobilityModel::SetPosition, mob, Vector (1, 1, 1));  



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3