1.正確安裝sumo功戚,我這里使用的是sumo-0.32.0版本落包,查看sumo版本的命令是sumo。
sumo
2.取openStreetMap下載osm地圖文件
3.選好大致區(qū)域后径玖,點擊導(dǎo)出按鈕扬卷,然后點擊手動選擇不同區(qū)域,確定選擇的區(qū)域辆毡。選擇好區(qū)域后點擊導(dǎo)出按鈕菜秦,瀏覽器自動下載osm文件
4.將下載好的osm拷貝到一個新的目錄中,我這里給osm文件重命名為sanTaiZi.osm舶掖。然后從命令行cd進這個目錄并執(zhí)行如下命令 球昨,使用工具netconvert根據(jù)osm文件轉(zhuǎn)化出sumo使用的路網(wǎng)文件。出現(xiàn)警告不要緊眨攘,有下面的success就好主慰。
netconvert --osm-files sanTaiZi.osm -o sanTaiZi.net.xml
5.cd進sumo的主目錄嚣州,即環(huán)境變量里配置的SUMO_HOME里面,然后再cd進入data/typemap目錄里共螺,把這里面的osmPolyconvert.typ.xml復(fù)制到之前存放osm文件的位置该肴。
6.cd到存放osm文件的目錄。使用下面的命令藐不,生成.poly.xml文件匀哄,注意把相應(yīng)的文件名換成你自己的文件名,后綴保持一致雏蛮。
polyconvert --osm-files sanTaiZi.osm --net-file sanTaiZi.net.xml --type-file osmPolyconvert.typ.xml -o sanTaiZi.poly.xml
7.使用sumo帶的工具生成.rou.xml文件涎嚼,命令如下:
python /home/lzx/Downloads/sumo-0.32.0/tools/randomTrips.py -n sanTaiZi.net.xml -r sanTaiZi.rou.xml -e 50 -l
8.編寫sumo的配置文件,注意更換<input>標簽內(nèi)部的三個文件名
<configuration>
<input>
<net-file value="sanTaiZi.net.xml"/>
<route-files value="sanTaiZi.rou.xml"/>
<additional-files value="sanTaiZi.poly.xml"/>
</input>
<time>
<begin value="0"/>
<end value="100"/>
<step-length value="0.1"/>
</time>
</configuration>
9.在終端使用下面的命令運行sumo-gui仿真挑秉。延遲調(diào)在100到200之間法梯,效果比較好。
sumo-gui sanTaiZi.sumocfg
10.使用下面的命令犀概,生成連接NS2用的配置文件立哑。
sumo -c sanTaiZi.sumocfg --fcd-output sanTaiZi.sumo.xml
11.使用下面的命令生成ns2仿真用的腳本文件,注意sumo的根目錄換成自己的姻灶。
python ~/Downloads/sumo-0.32.0/tools/traceExporter.py --fcd-input sanTaiZi.sumo.xml --ns2config-output sanTaiZi.tcl --ns2mobility-output mobility.tcl --ns2activity-output activity.tcl
12.cd進ns2的根目錄刁憋,/home/lzx/Documents/ns-allinone-2.35/ns-2.35/tcl/ex,然后進入ex目錄木蹬,用文本編輯器
打開simple-wireless.tcl文件,復(fù)制里面的代碼到sanTaiZi.tcl里面若皱,即上一步生成的腳本文件镊叁。
13.修改腳本文件
- 刪除opt(min-x),opt(min-y)
- val(rp)屬性改成AODV
- 刪除opt(start)和opt(stop)
- 刪除這兩行
set opt(mf) $opt(config-path)
append opt(mf) /mobility.tcl
- 把val(nn)的值改成opt(nn)的值
- 刪除這兩行
set opt(af) $opt(config-path)
append opt(af) /activity.tcl
- 更改.tr文件名走触,方便以后的數(shù)據(jù)分析
- 添加nam文件
set namf [open sanTaiZi.nam w]
$ns_ namtrace-all-wireless $namf $opt(x) $opt(y)
- 給ope(x)和opt(y)向上取整晦譬,例如4942.85改為4943
- 把$topo load_flatgrid 500 500改成$topo load_flatgrid $opt(x) $opt(y)
- 把-movementTrace OFF改成-movementTrace ON
- 在第一個for循環(huán)里添加$ns_ initial_node_pos $node_($i) 20
- 刪掉對node(1)和node(0)位置設(shè)置的代碼和set tcp上面的代碼,添加source mobility.tcl
- $ns_ attach-agent node_(1) $sink改成$ns_ attach-agent $node_(22) $sink
- 把下面的150都改成100
14.保存退出互广,使用下面的命令運行ns2仿真
ns sanTaiZi.tcl
15.使用下面的命令運行ns2仿真界面
nam sanTaiZi.nam
完整的ns2腳本代碼
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) 45 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set opt(x) 4943
set opt(y) 21754
# ======================================================================
# Main Program
# ======================================================================
set ns_ [new Simulator]
set tracefd [open sanTaiZi.tr w]
$ns_ trace-all $tracefd
set namf [open sanTaiZi.nam w]
$ns_ namtrace-all-wireless $namf $opt(x) $opt(y)
# set up topography object
set topo [new Topography]
$topo load_flatgrid $opt(x) $opt(y)
#
# 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 ON
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0 ;# disable random motion
$ns_ initial_node_pos $node_($i) 20
}
source mobility.tcl
set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(22) $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 100.0 "$node_($i) reset";
}
$ns_ at 100.0 "stop"
$ns_ at 100.01 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
puts "Starting Simulation..."
$ns_ run