1.正確安裝sumo,我這裏使用的是sumo-0.32.0版本,查看sumo版本的命令是sumo。
sumo
2.取openStreetMap下載osm地圖文件
3.選好大致區域後,點擊導出按鈕,然後點擊手動選擇不同區域,確定選擇的區域。選擇好區域後點擊導出按鈕,瀏覽器自動下載osm文件
4.將下載好的osm拷貝到一個新的目錄中,我這裏給osm文件重命名爲sanTaiZi.osm。然後從命令行cd進這個目錄並執行如下命令 ,使用工具netconvert根據osm文件轉化出sumo使用的路網文件。出現警告不要緊,有下面的success就好。
netconvert --osm-files sanTaiZi.osm -o sanTaiZi.net.xml
5.cd進sumo的主目錄,即環境變量裏配置的SUMO_HOME裏面,然後再cd進入data/typemap目錄裏,把這裏面的osmPolyconvert.typ.xml複製到之前存放osm文件的位置。
6.cd到存放osm文件的目錄。使用下面的命令,生成.poly.xml文件,注意把相應的文件名換成你自己的文件名,後綴保持一致。
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的配置文件,注意更換標籤內部的三個文件名
<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仿真。延遲調在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文件,複製裏面的代碼到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文件名,方便以後的數據分析
- 添加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循環裏添加$ns_ initial_node_pos $node_($i) 20
- 刪掉對node(1)和node(0)位置設置的代碼和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