当前位置:文档之家› WSN中LEACH协议源码分析报告

WSN中LEACH协议源码分析报告

WSN中LEACH协议源码分析分析(一)首先对wireless.tcl进行分析,先对默认的脚本选项进行初始化:set opt(chan)Channel/\VirelessChannelset opt(prop) Propagatioii/TwoRayGroundset opt(netif)PhyAVirelessPhyset opt(mac) Mac/802_l 1set opt(ifq) Qucuc/DropTail/PriQueueset opt(ll) LLset opt(ant) Antenna/OmniAntennaset opt(x) 0 。

# X dimension of the topographyset opt(y) 0。

# Y dimension of the topographyset opt(cp),H,set opt(sc) N../mobility/scene/scen-670x670-50-600-20-2u。

# scenario file set opt(ifqlen)50o # max packet in ifset opt(nn) 51。

# number of nodesset opt(secd) 0.0set opt(stop) 10.0 o # simulation timeset opt(tr) out.tr。

# trace fileset opt(rp) dsdv 。

# routing protocol scriptset opt(lm) M on H。

# log movement在这个wireless.tcl中设置了一些全局变呈::##Initialize Global Variables#set ns_ [new Simulator]set chan [new $opt(chan)]set prop [new $opt(prop)]set topo [newTopography]set tracefd [open Sopt(tr) w]Stopo Ioad_flatgrid $opt(x) $opt(y)Sprop topography Stopo这些初始化将在后而的使用中用到,该文件最重要的是创建leach 17点:创建方法如下:} elseif { [string compare Sopt(rp) M leach,,]==0} {for {set i 0} {$i < $opt(nn) } {incr i} {leach-create-mobile-node $i}如果路由协议是leach协议,则在Uamps.tcl中调用leach-create-mobile-node方法创建leach节点。

将在第二小节讲如何创建leach节点。

for {set i 0} {$i < $opt(nn) } {incr i} {$ns_ at $opt(stop).000000001 M Snode_($i) reset”。

〃完成后,重宜右点的应用}$ns_ at Sopt(stop).00000001 M puts VNS EXITING..'”。

$ns_ halt” if { $opt(sc) ==} {puts H *** NOTE: no scenario file specified/1set opt(sc) u none°)else {puts ”Loading scenario file../* source Sopt(sc)puts ”Load complete...,r }ns 在什么时候结束simulation,并告诉ns 加载sc 场景文件。

最后$ns_ run 则ns 就开始运 行了。

分析(-)上节对wireless.tcl 进行了简要的分析,接下来对Uanips.tcl 脚本进行分析。

setopKEfriss^amp) [expr [expr 1.1 * $opt(RXThrcsh) * 16 * $PI * $PI] /\[expr $opt(bw) * Sopt(Gt) * $opt(Gr) * $1 * $1]]# Etwo_ray_amp = RXThresh /(Rb Gt Gr ht A 2 hr A 2) set opt(Etwo_ray_amp) [expr 1.1 ♦ $opt(RXThresh)/\[expr $opt(bw) * $opt(Gt) * $opt(Gr) ♦ \$opt(ht) * $opt(ht) * $opt(ht) * $opt(ht)]] 50e-9 o #Energy for radio circuitry5e-9 o # Beamforming energy (J/bit) 0o # Sensing energy (J/bit)o # Threshold for power adaptation o # Idle power (W) a # Sleep power (W)set initialized 0set rng_ [new RNG]#用于产生随机数首先往opt 数组里而添加一些变量,并对这些变量进行初化。

opt(Psleep) , opt(Pidle),opt(thresh_energy)^t ns-lcach.tcl 中使用到,这个是讣算单位时间空闲所消耗的能呈:和休眠所 消耗的能量。

这个脚本主要是创建leach 节点:if {Sinitialized = O} {#remove old trace sensjnitset initialized 1 )# Remove old trace files.catch H eval exec rm [glob -nocomplain Sopt(dirname)/TDMAschedule.*.txt]M catch H exec rm $opt(dirname)/$opt(filename).energy H catch "exec rm $opt(dirnaine)/$opt(fiIename).data° catch "exec rm $opt(dirnanie)/$opt(filename).alive H catch ”exec rm $opt(dirname)/startup.energy H catch "exec rm $opt(dirnaine)/init.energy°set opt(EXcvr) set opt(e_bf) set opt(Esense)set opt(thresh_energy) 0.00 set opt(Pidle) 0 set opt(Psleep)如果没有初始化过,则将以前的跟踪文件删除,接着回到创建leach的函数中,创建节点:if {Sid != $opt(nn_)} {puts -nonewline H$id ”importantset node_($id) [new MobileNodc/ResourceAwareNode]} else {puts "($opt(nn_) = BS)Hset nodc_($id) [new MobileNodc/ResourceAwareNode SBS_NODE]}如果不是簇头节点则将$opt(nn_)・l个肖点设置为一般盯点,将$opt(nn」设宜为BS节点。

newMobileNodc/RcsourceAwarcNode函数是在ns-ranode.tcl中。

分析完这个我们接下来分析ncwMobileNode/ResourceAwareNode 这个函数。

set node $nodc_($id)if {$id != $opt(nn_)} {# Set initial node energy.if {$opt(eq_energy) == 1} {Snode set-energy $opt(init_energy) $opt(thresh_energy) } else {由于eq-energy在leach-test中给定,将eq-encrgy=l:则每个节点都会对能量进行初始化, 设置节点的初始能量和门槛能量(个人理解,死亡能量)。

set high.e.nodes (list 97 19 12 87 8 22 83 55 34 72]if {[lsearch $high_e_nodcs Sid] == -1} {set E2} else {set E 200}Snode set-energy $E Sopt(thresh_energy)set initf [open M$opt(dirname)/init.energy H a]puts Sinitf H$id\t$E Mclose Sinitf将不属于list中的能量初始化能量设巻为2,将属于list中的能量设置为200.并将初始化能量写到init.energy中,将节点id和节点初始能量写进去。

else {# Base station has an infinite amount of energy.$node set-energy 50000 $opt(thresh_energy)}节点为簇头节点,则将节点的初始化能量设宜为50000,能量无限。

到此为止,创建肖点完成并将每个节点的能量初始化完成。

下一节将分析ns-ranode.tcl脚札区分普通节点和簇头节点的不同。

接下来是配置右点信道和跟踪文件:$ns_ at 0.0 ”Snodc_($id) start-app Nns 在 0 的时候启动应用,应用在 ns-ranode.tcl 中分析。

分析(三)对前而两个脚本进行分析后,已经创建好节点和设置好节点的初始能崑将opt(nnj-l 个节点设置为普通i'j 点,而将opt(nn_)设置为bs 。

现在对ns-ranode.tcl 进行分析。

好了我们 现在就开始分析这个脚本。

Class MobileNodc/ResourceAwareNode -superclass Node/MobileNode MobileNode/ResourccAwareNode instproc init argsset bs_nodc [lindex $args 0]eval $self next [Ireplace Sargs 0 0]args 由参数传递进来,若节点为簇头节点则bs_nodc=l, i 周用父类初始化函数* set ResourceManager_ [new ResourceManager]$RcsourceManager_ Register |new Resource/NeighborResource] set energy [new Resource/Energy] $ResourceManager_ Register SenergyResourceManager 在 ns-resouce-manager.tcl 中立 义的© Resource/NeighborResource 在 ns-neighbor-resource 中对能量进行管理。

相关主题