sumo连接ns2

1.正确安装sumo,我这里使用的是sumo-0.32.0版本,查看sumo版本的命令是sumo。

sumo

2.取openStreetMap下载osm地图文件
2.png
3.选好大致区域后,点击导出按钮,然后点击手动选择不同区域,确定选择的区域。选择好区域后点击导出按钮,浏览器自动下载osm文件
4.png
4.将下载好的osm拷贝到一个新的目录中,我这里给osm文件重命名为sanTaiZi.osm。然后从命令行cd进这个目录并执行如下命令 ,使用工具netconvert根据osm文件转化出sumo使用的路网文件。出现警告不要紧,有下面的success就好。

netconvert --osm-files sanTaiZi.osm -o sanTaiZi.net.xml 

5.png
5.cd进sumo的主目录,即环境变量里配置的SUMO_HOME里面,然后再cd进入data/typemap目录里,把这里面的osmPolyconvert.typ.xml复制到之前存放osm文件的位置。
6.png
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.png
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.png
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

9.png
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里面,即上一步生成的脚本文件。
10.png
11.png
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

12.png

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章