NS2相关学习——在ns中模拟无线场景
之前學(xué)習(xí)的都是有線場(chǎng)景下的NS2相關(guān)應(yīng)用,現(xiàn)在開始,終于要學(xué)習(xí)無(wú)線啦!無(wú)線是我研究的重點(diǎn),要好好學(xué)習(xí)呀!
在本節(jié)中,我們將學(xué)習(xí)使用ns中提供的移動(dòng)無(wú)線仿真模型。 該部分由兩部分組成。 在第一小節(jié)中,我們討論如何創(chuàng)建和運(yùn)行一個(gè)簡(jiǎn)單的2節(jié)點(diǎn)無(wú)線網(wǎng)絡(luò)仿真。 在第二部分中,我們將擴(kuò)展我們的示例(在第1小節(jié)中),創(chuàng)建一個(gè)比較復(fù)雜的無(wú)線方案。
1、創(chuàng)建簡(jiǎn)單的無(wú)線場(chǎng)景
我們將模擬一個(gè)非常簡(jiǎn)單的2個(gè)節(jié)點(diǎn)無(wú)線場(chǎng)景。 拓?fù)溆蓛蓚€(gè)移動(dòng)節(jié)點(diǎn)(node_(0)和node_(1))組成。 在這個(gè)例子中邊界定義為500mX500m的區(qū)域。 節(jié)點(diǎn)最初在邊界的兩個(gè)相對(duì)端開始相對(duì)移動(dòng)。在兩個(gè)移動(dòng)終端之間建立TCP連接。 節(jié)點(diǎn)之間的數(shù)據(jù)包在彼此的聽覺(jué)范圍內(nèi)進(jìn)行交換。 當(dāng)它們離開時(shí),數(shù)據(jù)包開始下降。
首先,我們創(chuàng)建一個(gè)tcl腳本,命名為simple-wireless.tcl。
移動(dòng)節(jié)點(diǎn)由網(wǎng)絡(luò)組件組成,網(wǎng)絡(luò)組件包括鏈路層(LL),接口隊(duì)列(IfQ),MAC層,無(wú)線信道節(jié)點(diǎn)發(fā)送和接收信號(hào)(有關(guān)這些網(wǎng)絡(luò)組件的詳細(xì)信息參見(jiàn)請(qǐng)參見(jiàn)ns手冊(cè)第15章第1節(jié))。?在無(wú)線模擬開始時(shí),我們需要為每個(gè)這些網(wǎng)絡(luò)組件定義類型。此外,我們需要定義其他參數(shù),如天線類型,無(wú)線電傳播模型,移動(dòng)終端使用的自組織路由協(xié)議的類型等。
下面我們開始我們的腳本——simple-wireless.tcl,這個(gè)腳本列出了上面說(shuō)的那些參數(shù),具有一定的參考意義。
# ====================================================================== # Define options # ====================================================================== set val(chan) Channel/WirelessChannel ;# channel type 通道類型 set val(prop) Propagation/TwoRayGround ;# radio-propagation model 無(wú)線傳播模型 set val(ant) Antenna/OmniAntenna ;# Antenna type 天線類型 set val(ll) LL ;# Link layer type 鏈路層類型 set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type 接口隊(duì)列類型 set val(ifqlen) 50 ;# max packet in ifq Ifq的大小 set val(netif) Phy/WirelessPhy ;# network interface type 網(wǎng)絡(luò)接口類型 set val(mac) Mac/802_11 ;# MAC type MAC類型 set val(rp) DSDV ;# ad-hoc routing protocol ad-hoc路由協(xié)議 set val(nn) 2 ;# number of mobilenodes 移動(dòng)節(jié)點(diǎn)的數(shù)目接下來(lái)我們來(lái)到程序的主要部分,首先創(chuàng)建模擬器的一個(gè)實(shí)例 set ns_ [new Simulator]接下來(lái)創(chuàng)建一個(gè)拓?fù)鋵?duì)象,跟蹤拓?fù)溥吔鐑?nèi)的移動(dòng)節(jié)點(diǎn)的移動(dòng)。
set topo [new Topography]
我們之前提到,移動(dòng)節(jié)點(diǎn)在500mX500m的拓?fù)鋬?nèi)移動(dòng)。 我們提供邊界的x和y坐標(biāo)(x = 500,y = 500):
$topo load_flatgrid 500 500整個(gè)區(qū)域被分解為網(wǎng)格,網(wǎng)格分辨率的默認(rèn)值為1.可以將不同的值作為第三個(gè)參數(shù)傳遞給load_flatgrid {}。
下面我們創(chuàng)造god對(duì)象(可以理解為一個(gè)很厲害的具有上帝視角的對(duì)象) create-god $val(nn) god(通用運(yùn)營(yíng)總監(jiān))是用于存儲(chǔ)關(guān)于全局環(huán)境、網(wǎng)絡(luò)、節(jié)點(diǎn)的所有信息,但是對(duì)于模擬參與者來(lái)說(shuō)是透明的。目前,god對(duì)象存儲(chǔ)了所有移動(dòng)節(jié)點(diǎn)的數(shù)量,一個(gè)節(jié)點(diǎn)到達(dá)另外一個(gè)節(jié)點(diǎn)最小跳數(shù)表。因?yàn)樵谀M運(yùn)行期間計(jì)算下一跳信息相當(dāng)耗時(shí),所以下一跳信息通常在模擬開始之前從運(yùn)動(dòng)模式文件中加載到god對(duì)象中。?然而,為了保持這個(gè)例子簡(jiǎn)單,我們避免使用運(yùn)動(dòng)模式文件,因此不能向上帝提供下一跳信息。 運(yùn)動(dòng)模式文件的使用和向上帝饋送下一跳信息將在下一小節(jié)的示例中顯示。 程序create-god在?ns / tcl / mobility / com.tcl中定義,它只允許在仿真期間創(chuàng)建God對(duì)象的單個(gè)全局實(shí)例。 除了評(píng)估功能之外,上帝的對(duì)象是由移動(dòng)節(jié)點(diǎn)中的MAC對(duì)象內(nèi)部調(diào)用的。 所以即使我們不能利用god的評(píng)估目的(如本例),我們?nèi)匀恍枰獎(jiǎng)?chuàng)造god。 接下來(lái),我們創(chuàng)建移動(dòng)節(jié)點(diǎn)。 節(jié)點(diǎn)創(chuàng)建API已被修改,在這里我們將使用新的API來(lái)創(chuàng)建移動(dòng)代碼。
首先,我們需要在創(chuàng)建節(jié)點(diǎn)之前配置節(jié)點(diǎn)。 節(jié)點(diǎn)配置API可以包括定義尋址類型(平面/層次等),自適應(yīng)路由協(xié)議的類型,鏈路層,MAC層,IfQ等。配置API可以定義如下:
(parameter examples) # $ns_ node-config -addressingType flat or hierarchical or expanded # -adhocRouting DSDV or DSR or TORA ###設(shè)置移動(dòng)節(jié)點(diǎn)所需要的路由協(xié)議 # -llType LL ###設(shè)置移動(dòng)節(jié)點(diǎn)的邏輯鏈路層 # -macType Mac/802_11 ###設(shè)置移動(dòng)節(jié)點(diǎn)的MAC層 # -propType "Propagation/TwoRayGround" ###設(shè)置移動(dòng)節(jié)點(diǎn)的無(wú)線信號(hào)傳輸模型 # -ifqType "Queue/DropTail/PriQueue" ###設(shè)置移動(dòng)節(jié)點(diǎn)的隊(duì)列類型 # -ifqLen 50 ####設(shè)置移動(dòng)節(jié)點(diǎn)的隊(duì)列長(zhǎng)度 # -phyType "Phy/WirelessPhy" ####設(shè)置移動(dòng)節(jié)點(diǎn)的物理層模型 # -antType "Antenna/OmniAntenna" ####設(shè)置移動(dòng)節(jié)點(diǎn)的天線類型 # -channelType "Channel/WirelessChannel" ####設(shè)置移動(dòng)節(jié)點(diǎn)的無(wú)線信道類型 # -topoInstance $topo ####設(shè)置移動(dòng)節(jié)點(diǎn)的拓?fù)鋵?duì)象 # -energyModel "EnergyModel" ###設(shè)置節(jié)點(diǎn)的能量模型 # -initialEnergy (in Joules) ###設(shè)置節(jié)點(diǎn)初始能量,單位是J(焦耳) # -rxPower (in W) ###設(shè)置節(jié)點(diǎn)的接收功率,單位是瓦(w) # -txPower (in W) ###設(shè)置節(jié)點(diǎn)的發(fā)送功率,單位是瓦(w) # -agentTrace ON or OFF ###是否打開應(yīng)用層的trace # -routerTrace ON or OFF ###是否打開路由層的trace # -macTrace ON or OFF ###是否打開mac層的trace # -movementTrace ON or OFF ###是否打開節(jié)點(diǎn)位置和移動(dòng)信息的trace
用于創(chuàng)建移動(dòng)節(jié)點(diǎn)的配置API如下所示: # Configure nodes$ns_ node-config -adhocRouting $val(rp) \-llType $val(ll) \-macType $val(mac) \-ifqType $val(ifq) \-ifqLen $val(ifqlen) \-antType $val(ant) \-propType $val(prop) \-phyType $val(netif) \-topoInstance $topo \-channelType $val(chan) \-agentTrace ON \-routerTrace ON \-macTrace OFF \-movementTrace OFF
然后我們創(chuàng)建兩個(gè)移動(dòng)節(jié)點(diǎn)
for {set i 0} {$i < $val(nn) } {incr i} {set node_($i) [$ns_ node ]$node_($i) random-motion 0 ;# disable random motion 這個(gè)節(jié)點(diǎn)不能亂動(dòng)}
下面給移動(dòng)節(jié)點(diǎn)他們開始的位置 # # Provide initial (X,Y, for now Z=0) co-ordinates for node_(0) and node_(1) # $node_(0) set X_ 5.0 $node_(0) set Y_ 2.0 $node_(0) set Z_ 0.0$node_(1) set X_ 390.0 $node_(1) set Y_ 385.0 $node_(1) set Z_ 0.0Node0的起始位置為(5,2),而Node1在位置(390,385)處開始。
接下來(lái)產(chǎn)生節(jié)點(diǎn)移動(dòng)
# # Node_(1) starts to move towards node_(0) # $ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0" $ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"# Node_(1) then starts to move away from node_(0) $ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"$ ns_ at 50.0“$ node_(1)setdest 25.0 20.0 15.0”表示在時(shí)刻50.0s時(shí),node1以15m / s的速度開始向目標(biāo)移動(dòng)(x = 25,y = 20)。 該API用于改變移動(dòng)節(jié)點(diǎn)的移動(dòng)方向和速度。
接下來(lái),在兩個(gè)節(jié)點(diǎn)之間建立數(shù)據(jù)流。 # TCP connections between node_(0) and node_(1)set tcp [new Agent/TCP] $tcp set class_ 2 set sink [new Agent/TCPSink] $ns_ attach-agent $node_(0) $tcp ###node0是源節(jié)點(diǎn) $ns_ attach-agent $node_(1) $sink ###node1是目標(biāo)節(jié)點(diǎn) $ns_ connect $tcp $sink ###吧源和目標(biāo)連接起來(lái) set ftp [new Application/FTP] ###使用的應(yīng)用層協(xié)議是ftp $ftp attach-agent $tcp ###把應(yīng)用層協(xié)議和傳輸層協(xié)議連接起來(lái) $ns_ at 10.0 "$ftp start" 這將在兩個(gè)兩個(gè)節(jié)點(diǎn)之間建立起來(lái)一個(gè)TCP連接。
然后,我們需要在模擬結(jié)束時(shí)定義停止時(shí)間,并告訴移動(dòng)節(jié)點(diǎn)復(fù)位,實(shí)際上重置其內(nèi)部網(wǎng)絡(luò)組件,
# # Tell nodes when the simulation ends # for {set i 0} {$i < $val(nn) } {incr i} {$ns_ at 150.0 "$node_($i) reset"; } $ns_ at 150.0001 "stop" $ns_ at 150.0002 "puts \"NS EXITING...\" ; $ns_ halt" proc stop {} {global ns_ tracefdclose $tracefd }在時(shí)刻150.0s時(shí)模擬停止,節(jié)點(diǎn)在此時(shí)被重置。在150.0002s時(shí)調(diào)用“$ ns_ halt”。 調(diào)用stop {}來(lái)清除跟蹤并關(guān)閉跟蹤文件。
最后執(zhí)行“開始模擬”的命令。 puts "Starting Simulation..." $ns_ run
保存文件simple-wireless.tcl。
完整的腳本如下所示:# Copyright (c) 1997 Regents of the University of California. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. All advertising materials mentioning features or use of this software # must display the following acknowledgement: # This product includes software developed by the Computer Systems # Engineering Group at Lawrence Berkeley Laboratory. # 4. Neither the name of the University nor of the Laboratory may be used # to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # simple-wireless.tcl # A simple example for wireless simulation# ====================================================================== # Define options # ====================================================================== set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set val(netif) Phy/WirelessPhy ;# network interface type set val(mac) Mac/802_11 ;# MAC type set val(ifq) Queue/DropTail/PriQueue ;# interface queue type set val(ll) LL ;# link layer type set val(ant) Antenna/OmniAntenna ;# antenna model set val(ifqlen) 50 ;# max packet in ifq set val(nn) 2 ;# number of mobilenodes set val(rp) DSDV ;# routing protocol# ====================================================================== # Main Program # ======================================================================# # Initialize Global Variables # set ns_ [new Simulator] set tracefd [open simple.tr w] $ns_ trace-all $tracefd# set up topography object set topo [new Topography]$topo load_flatgrid 500 500# # Create God # create-god $val(nn)# # Create the specified number of mobilenodes [$val(nn)] and "attach" them # to the channel. # Here two nodes are created : node(0) and node(1)# configure node$ns_ node-config -adhocRouting $val(rp) \-llType $val(ll) \-macType $val(mac) \-ifqType $val(ifq) \-ifqLen $val(ifqlen) \-antType $val(ant) \-propType $val(prop) \-phyType $val(netif) \-channelType $val(chan) \-topoInstance $topo \-agentTrace ON \-routerTrace ON \-macTrace OFF \-movementTrace OFF for {set i 0} {$i < $val(nn) } {incr i} {set node_($i) [$ns_ node] $node_($i) random-motion 0 ;# disable random motion}# # Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes # $node_(0) set X_ 5.0 $node_(0) set Y_ 2.0 $node_(0) set Z_ 0.0$node_(1) set X_ 390.0 $node_(1) set Y_ 385.0 $node_(1) set Z_ 0.0# # Now produce some simple node movements # Node_(1) starts to move towards node_(0) # $ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0" $ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"# Node_(1) then starts to move away from node_(0) $ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0" # Setup traffic flow between nodes # TCP connections between node_(0) and node_(1)set tcp [new Agent/TCP] $tcp set class_ 2 set sink [new Agent/TCPSink] $ns_ attach-agent $node_(0) $tcp $ns_ attach-agent $node_(1) $sink $ns_ connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns_ at 10.0 "$ftp start" # # Tell nodes when the simulation ends # for {set i 0} {$i < $val(nn) } {incr i} {$ns_ at 150.0 "$node_($i) reset"; } $ns_ at 150.0 "stop" $ns_ at 150.01 "puts \"NS EXITING...\" ; $ns_ halt" proc stop {} {global ns_ tracefd$ns_ flush-traceclose $tracefd }puts "Starting Simulation..." $ns_ run
在與tcl腳本同一個(gè)目錄文件下,產(chǎn)生了一個(gè)trace文件。
trace文件中,AgentTraces在其第5個(gè)字段中標(biāo)有AGT,RouterTrace和MACTrack標(biāo)有RTR。
鏈接:http://www.isi.edu/nsnam/ns/tutorial/
總結(jié)
以上是生活随笔為你收集整理的NS2相关学习——在ns中模拟无线场景的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何让nRF52840 dongle化身
- 下一篇: 【坑】云相关