NS2相关学习——在ns中模拟无线场景
之前學習的都是有線場景下的NS2相關應用,現在開始,終于要學習無線啦!無線是我研究的重點,要好好學習呀!
在本節中,我們將學習使用ns中提供的移動無線仿真模型。 該部分由兩部分組成。 在第一小節中,我們討論如何創建和運行一個簡單的2節點無線網絡仿真。 在第二部分中,我們將擴展我們的示例(在第1小節中),創建一個比較復雜的無線方案。
1、創建簡單的無線場景
我們將模擬一個非常簡單的2個節點無線場景。 拓撲由兩個移動節點(node_(0)和node_(1))組成。 在這個例子中邊界定義為500mX500m的區域。 節點最初在邊界的兩個相對端開始相對移動。在兩個移動終端之間建立TCP連接。 節點之間的數據包在彼此的聽覺范圍內進行交換。 當它們離開時,數據包開始下降。
首先,我們創建一個tcl腳本,命名為simple-wireless.tcl。
移動節點由網絡組件組成,網絡組件包括鏈路層(LL),接口隊列(IfQ),MAC層,無線信道節點發送和接收信號(有關這些網絡組件的詳細信息參見請參見ns手冊第15章第1節)。?在無線模擬開始時,我們需要為每個這些網絡組件定義類型。此外,我們需要定義其他參數,如天線類型,無線電傳播模型,移動終端使用的自組織路由協議的類型等。
下面我們開始我們的腳本——simple-wireless.tcl,這個腳本列出了上面說的那些參數,具有一定的參考意義。
# ====================================================================== # Define options # ====================================================================== set val(chan) Channel/WirelessChannel ;# channel type 通道類型 set val(prop) Propagation/TwoRayGround ;# radio-propagation model 無線傳播模型 set val(ant) Antenna/OmniAntenna ;# Antenna type 天線類型 set val(ll) LL ;# Link layer type 鏈路層類型 set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type 接口隊列類型 set val(ifqlen) 50 ;# max packet in ifq Ifq的大小 set val(netif) Phy/WirelessPhy ;# network interface type 網絡接口類型 set val(mac) Mac/802_11 ;# MAC type MAC類型 set val(rp) DSDV ;# ad-hoc routing protocol ad-hoc路由協議 set val(nn) 2 ;# number of mobilenodes 移動節點的數目接下來我們來到程序的主要部分,首先創建模擬器的一個實例 set ns_ [new Simulator]接下來創建一個拓撲對象,跟蹤拓撲邊界內的移動節點的移動。
set topo [new Topography]
我們之前提到,移動節點在500mX500m的拓撲內移動。 我們提供邊界的x和y坐標(x = 500,y = 500):
$topo load_flatgrid 500 500整個區域被分解為網格,網格分辨率的默認值為1.可以將不同的值作為第三個參數傳遞給load_flatgrid {}。
下面我們創造god對象(可以理解為一個很厲害的具有上帝視角的對象) create-god $val(nn) god(通用運營總監)是用于存儲關于全局環境、網絡、節點的所有信息,但是對于模擬參與者來說是透明的。目前,god對象存儲了所有移動節點的數量,一個節點到達另外一個節點最小跳數表。因為在模擬運行期間計算下一跳信息相當耗時,所以下一跳信息通常在模擬開始之前從運動模式文件中加載到god對象中。?然而,為了保持這個例子簡單,我們避免使用運動模式文件,因此不能向上帝提供下一跳信息。 運動模式文件的使用和向上帝饋送下一跳信息將在下一小節的示例中顯示。 程序create-god在?ns / tcl / mobility / com.tcl中定義,它只允許在仿真期間創建God對象的單個全局實例。 除了評估功能之外,上帝的對象是由移動節點中的MAC對象內部調用的。 所以即使我們不能利用god的評估目的(如本例),我們仍然需要創造god。 接下來,我們創建移動節點。 節點創建API已被修改,在這里我們將使用新的API來創建移動代碼。
首先,我們需要在創建節點之前配置節點。 節點配置API可以包括定義尋址類型(平面/層次等),自適應路由協議的類型,鏈路層,MAC層,IfQ等。配置API可以定義如下:
(parameter examples) # $ns_ node-config -addressingType flat or hierarchical or expanded # -adhocRouting DSDV or DSR or TORA ###設置移動節點所需要的路由協議 # -llType LL ###設置移動節點的邏輯鏈路層 # -macType Mac/802_11 ###設置移動節點的MAC層 # -propType "Propagation/TwoRayGround" ###設置移動節點的無線信號傳輸模型 # -ifqType "Queue/DropTail/PriQueue" ###設置移動節點的隊列類型 # -ifqLen 50 ####設置移動節點的隊列長度 # -phyType "Phy/WirelessPhy" ####設置移動節點的物理層模型 # -antType "Antenna/OmniAntenna" ####設置移動節點的天線類型 # -channelType "Channel/WirelessChannel" ####設置移動節點的無線信道類型 # -topoInstance $topo ####設置移動節點的拓撲對象 # -energyModel "EnergyModel" ###設置節點的能量模型 # -initialEnergy (in Joules) ###設置節點初始能量,單位是J(焦耳) # -rxPower (in W) ###設置節點的接收功率,單位是瓦(w) # -txPower (in W) ###設置節點的發送功率,單位是瓦(w) # -agentTrace ON or OFF ###是否打開應用層的trace # -routerTrace ON or OFF ###是否打開路由層的trace # -macTrace ON or OFF ###是否打開mac層的trace # -movementTrace ON or OFF ###是否打開節點位置和移動信息的trace
用于創建移動節點的配置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
然后我們創建兩個移動節點
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 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)處開始。
接下來產生節點移動
# # 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”表示在時刻50.0s時,node1以15m / s的速度開始向目標移動(x = 25,y = 20)。 該API用于改變移動節點的移動方向和速度。
接下來,在兩個節點之間建立數據流。 # 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是源節點 $ns_ attach-agent $node_(1) $sink ###node1是目標節點 $ns_ connect $tcp $sink ###吧源和目標連接起來 set ftp [new Application/FTP] ###使用的應用層協議是ftp $ftp attach-agent $tcp ###把應用層協議和傳輸層協議連接起來 $ns_ at 10.0 "$ftp start" 這將在兩個兩個節點之間建立起來一個TCP連接。
然后,我們需要在模擬結束時定義停止時間,并告訴移動節點復位,實際上重置其內部網絡組件,
# # 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 }在時刻150.0s時模擬停止,節點在此時被重置。在150.0002s時調用“$ ns_ halt”。 調用stop {}來清除跟蹤并關閉跟蹤文件。
最后執行“開始模擬”的命令。 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腳本同一個目錄文件下,產生了一個trace文件。
trace文件中,AgentTraces在其第5個字段中標有AGT,RouterTrace和MACTrack標有RTR。
鏈接:http://www.isi.edu/nsnam/ns/tutorial/
總結
以上是生活随笔為你收集整理的NS2相关学习——在ns中模拟无线场景的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何让nRF52840 dongle化身
- 下一篇: 【坑】云相关