收藏 分销(赏)

WSN中LEACH协议源码分析.doc

上传人:精*** 文档编号:2243713 上传时间:2024-05-23 格式:DOC 页数:12 大小:144.04KB
下载 相关 举报
WSN中LEACH协议源码分析.doc_第1页
第1页 / 共12页
WSN中LEACH协议源码分析.doc_第2页
第2页 / 共12页
WSN中LEACH协议源码分析.doc_第3页
第3页 / 共12页
WSN中LEACH协议源码分析.doc_第4页
第4页 / 共12页
WSN中LEACH协议源码分析.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

1、WSN中LEACH协议源码分析 作者: 日期:12 个人收集整理 勿做商业用途WSN中LEACH协议源码分析分析(一)首先对wireless。tcl进行分析,先对默认的脚本选项进行初始化:set opt(chan)Channel/WirelessChannelset opt(prop) Propagation/TwoRayGroundset opt(netif)Phy/WirelessPhyset opt(mac) Mac/802_11set opt(ifq) Queue/DropTail/PriQueueset opt(ll) LLset opt(ant) Antenna/OmniAnten

2、naset opt(x) 0 ; X dimension of the topographyset opt(y) 0 ;# Y dimension of the topographyset opt(cp) ”set opt(sc) ”。/mobility/scene/scen-670x67050600202” ;# scenario fileset opt(ifqlen)50; max packet in ifset opt(nn) 51 ;# number of nodesset opt(seed) 0.0set opt(stop) 10.0 ; simulation timeset opt

3、(tr) out。tr ;# trace fileset opt(rp) dsdv ; routing protocol scriptset opt(lm) on” ; log movement在这个wireless.tcl中设置了一些全局变量: Initialize Global Variablesset ns_ new Simulatorset chan new $opt(chan)set prop new $opt(prop)set topo new Topographyset tracefd open opt(tr) wtopo load_flatgrid $opt(x) opt(y)

4、prop topography topo这些初始化将在后面的使用中用到,该文件最重要的是创建leach节点:创建方法如下: elseif string compare opt(rp) leach” = 0 for set i 0 $i $opt(nn) incr i leachcreatemobile-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

5、node_($i) reset; /完成后,重置节点的应用$ns_ at opt(stop)。00000001 ”puts NS EXITING。 ; $ns_ haltif $opt(sc) = ” puts ”* NOTE: no scenario file specified.” set opt(sc) ”none” else puts Loading scenario file。.source opt(sc)puts Load complete。.”ns在什么时候结束simulation,并告诉ns加载sc场景文件。最后ns_ run则ns就开始运行了。分析(二)上节对wireless

6、。tcl进行了简要的分析,接下来对Uamps.tcl脚本进行分析。set opt(Efriss_amp) expr expr 1。1 opt(RXThresh) 16 $PI $PI / expr opt(bw) opt(Gt) * opt(Gr) l * l Etwo_ray_amp = RXThresh / (Rb Gt Gr ht2 hr2)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)set opt(E

7、Xcvr) 50e9 ;# Energy for radio circuitryset opt(e_bf) 5e-9 ;# Beamforming energy (J/bit)set opt(Esense) 0 ;# Sensing energy (J/bit)set opt(thresh_energy) 0。00 ;# Threshold for power adaptationset opt(Pidle) 0 ;# Idle power (W)set opt(Psleep) 0 ;# Sleep power (W)set initialized 0set rng_ new RNG用于产生随

8、机数 首先往opt数组里面添加一些变量,并对这些变量进行初化。opt(Psleep) ,opt(Pidle), opt(thresh_energy)在nsleach。tcl中使用到,这个是计算单位时间空闲所消耗的能量和休眠所消耗的能量。这个脚本主要是创建leach节点:if $initialized = 0 remove old trace sens_init set initialized 1 Remove old trace files. catch ”eval exec rm glob nocomplain opt(dirname)/TDMAschedule.txt” catch ”ex

9、ec rm opt(dirname)/opt(filename).energy catch ”exec rm $opt(dirname)/opt(filename).data catch ”exec rm $opt(dirname)/$opt(filename).alive catch ”exec rm opt(dirname)/startup.energy” catch ”exec rm opt(dirname)/init.energy如果没有初始化过,则将以前的跟踪文件删除,接着回到创建leach的函数中,创建节点:if id != opt(nn_) puts nonewline id ”

10、 #important set node_($id) new MobileNode/ResourceAwareNode else puts ($opt(nn_) = BS)” set node_($id) new MobileNode/ResourceAwareNode BS_NODE 如果不是簇头节点则将opt(nn_)-1个节点设置为一般节点,将opt(nn_)设置为BS节点.newMobileNode/ResourceAwareNode函数是在nsranode.tcl中。分析完这个我们接下来分析newMobileNode/ResourceAwareNode这个函数。set node $n

11、ode_($id) if id != $opt(nn_) # Set initial node energy。 if $opt(eq_energy) = 1 node set-energy opt(init_energy) opt(thresh_energy) else 由于eq-energy在leachtest中给定,将eqenergy=1;则每个节点都会对能量进行初始化,设置节点的初始能量和门槛能量(个人理解,死亡能量)。set high_e_nodes list 97 19 12 87 8 22 83 55 34 72 if lsearch $high_e_nodes id = 1 se

12、t E 2 else set E 200 $node set-energy E $opt(thresh_energy) set initf open ”$opt(dirname)/init。energy a puts $initf ”$idtE close initf将不属于list中的能量初始化能量设置为2,将属于list中的能量设置为200。并将初始化能量写到init.energy中,将节点id和节点初始能量写进去。 else # Base station has an infinite amount of energy。 node setenergy 50000 $opt(thresh_

13、energy) 节点为簇头节点,则将节点的初始化能量设置为50000,能量无限.到此为止,创建节点完成并将每个节点的能量初始化完成。下一节将分析ns-ranode.tcl脚本。区分普通节点和簇头节点的不同。接下来是配置节点信道和跟踪文件:$ns_ at 0.0 node_(id) startapp ns在0的时候启动应用,应用在nsranode。tcl中分析。分析(三)对前面两个脚本进行分析后,已经创建好节点和设置好节点的初始能量,将opt(nn_)-1个节点设置为普通节点,而将opt(nn_)设置为bs。现在对nsranode。tcl进行分析。好了我们现在就开始分析这个脚本。Class Mo

14、bileNode/ResourceAwareNode superclass Node/MobileNodeMobileNode/ResourceAwareNode instproc init argsset bs_node lindex $args 0 eval self next lreplace args 0 0 args由参数传递进来,若节点为簇头节点则bs_node=1,调用父类初始化函数。 set ResourceManager_ new ResourceManager ResourceManager_ Register new Resource/NeighborResource s

15、et energy new Resource/EnergyResourceManager_ Register energyResourceManager在nsresouce-manager.tcl中定义的。Resource/NeighborResource在nsneighborresource中对能量进行管理。 Create a new agent and attach it to the node if bs_node = 1 set agent new Agent/BSAgent else set agent new Agent/RCAgent set rca_agent_ $agent

16、普通节点的应用为RCAgent,BS的应用为BSAGgent. 并将应用attch到node上。 下面两段看不明白,但是这两段不影响看程序。 set haslist find_haslist $self id if bs_node = 1 set rca new opt(bsapp) else set rca new opt(rcapp) $opt(mtype) wantslist haslist ns_ attachagent $self agent rca attach-agent $agent set rca_app_ $rca将bs节点的应用设置为bsapp,普通节点的应用设置为rca

17、app,并将节点的服务设置为不同的服务。bsapp在nsapp。tcl中。opt(rcapp)定义在leach。tcl中,opt(mtype)定义在leach。tcl中。set opt(rcapp) ”Application/LEACH ;# Application typeset opt(ra_adv) TxTime expr opt(hdr_size) + 4 ; Total time (s) for CH ADVs ; Assume max 4(nn*%) CHsset opt(ra_adv_total) expr $opt(ra_adv)($opt(num_clusters)4 + 1

18、) ; RA Time (s) for nodes join reqsset opt(ra_join) expr 0。01 * $opt(nn_) ;# Buffer time for join req xmittalset opt(ra_delay) TxTime expr opt(hdr_size) + 4 ;# Maximum time required to transmit ;# a schedule (n nodes in 1 cluster)set opt(xmit_sch) expr 0。005 + TxTime expr opt(nn_)4+$opt(hdr_size) ;#

19、 Overhead time for cluster set-upset opt(start_xmit) expr opt(ra_adv_total) + $opt(ra_join) + opt(xmit_sch)一般节点的应用为Application/LEACH,最终sink节点的应用为Application/BSApp。通过前3次的分析得出,在分析1中定义变量,在分析2中创建leach节点,在分析3中将节点的应用绑定在节点上。下面将分析leach的主要协议ns-leach.tcl脚本。分析(四)完成前面3个脚本的分析,最后进行nsleach.tcl脚本的分析.首先我们看下这个脚本要使用的有

20、哪些功能。Application/LEACH instproc init args 对leach进行初始化,即构造函数。下面是leach的一些方法Application/LEACH instproc start Application/LEACH instproc getRandomNumber llim ulim 得到随机数Application/LEACH instproc node Application/LEACH instproc nodeID Application/LEACH instproc mac Application/LEACH instproc getX Applica

21、tion/LEACH instproc getY Application/LEACH instproc getER Application/LEACH instproc GoToSleep 节点进行休眠Application/LEACH instproc WakeUp 节点醒来Application/LEACH instproc setCode code Application/LEACH instproc checkAlive 节点是否存活Application/LEACH instproc isClusterHead? 判断是否是簇头节点Application/LEACH instproc

22、 hasbeenClusterHead? 判断是否成为过簇头节点Application/LEACH instproc hasnotbeenClusterHead hasbeench=0不是簇头节点Application/LEACH instproc setClusterHead 设置为簇头节点Application/LEACH instproc unsetClusterHead 设置为非簇头节点Application/LEACH instproc decideClusterHead 决定簇头节点,非常重要Application/LEACH instproc advertiseClusterHe

23、ad 广播簇头节点Application/LEACH instproc findBestCluster 找到最佳簇Application/LEACH instproc informClusterHead 通知簇头节点Application/LEACH instproc createSchedule 创建TDMA调度接收功能:Application/LEACH instproc recv args Application/LEACH instproc recvADV_CH msg Application/LEACH instproc recvJOIN_REQ nodeID Application

24、/LEACH instproc recvADV_SCH order Application/LEACH instproc recvDATA msg 发送功能:Application/LEACH instproc sendData Application/LEACH instproc send mac_dst link_dst type msgdata_size dist code Application/LEACH instproc send_now mac_dst link_dst type msg data_size dist code Application/LEACH instproc

25、 SendDataToBS Application/LEACH instproc SendMyDataToBS 分析(五)由leach 分析三可知,一般节点的应用为Application/LEACH。则现在就是如何选择簇头节点和设置门槛值.我们接下来分析leach 分析4中红色的方法。在leach 分析3中创建一个Application/LEACH对象则就会自动调用start方法。Application/LEACH instproc start self mac set node_num_ self nodeID $self decideClusterHead $self checkAlive

26、 在这个方法中会调用decideClusterHead和checkAlive方法。Application/LEACH instproc GoToSleep global opt ns_ self instvar begin_idle_ begin_sleep_ $self node set netif_(0) set sleep_ 1 # If node has been awake, remove idle energy (e.g。, the amount of energy # dissipated while the node is in the idle state)。 Otherwi

27、se, the node has been asleep and must remove sleep energy (e.g。, the amount of energy dissipated while the node is in the sleep state). if begin_idle_ begin_sleep_ set idle_energy expr opt(Pidle) * expr $ns_ now $begin_idle_ $self getER remove idle_energy else set sleep_energy expr opt(Psleep) * exp

28、r $ns_ now - begin_sleep_ $self getER remove $sleep_energy set begin_sleep_ ns_ now set begin_idle_ 0分析(六)在start中调用下面这个方法.Application/LEACH instproc decideClusterHead global ns_ chan opt node_ $self instvar alive_ TDMAschedule_ self instvar begin_idle_ begin_sleep_ Check the alive status of the node

29、. If the node has run out of energy, it no longer functions in the network。 set ISalive self node set netif_(0) set alive_ if alive_ = 1 if $ISalive = 0 puts Node self nodeID is DEAD! chan removeif $self node set netif_(0) set alive_ 0 set opt(nn_) expr opt(nn_) - 1 set ISalive $self node set netif_

30、(0) set alive_ 从网络接口netif中查看当前节点状 况如果节点存活,但是节点能量耗光,则chan removeif self node set netif_(0)将节点信道中移出,并将节点设置为死亡.节点的总数目减少一个.if opt(eq_energy) = 1 # Pi(t) = k / (N k mod(r,N/k) where k is the expected number of clusters per round N is the total number of sensor nodes in the network # and r is the number o

31、f rounds that have already passed. # set nn $opt(nn_) if expr nn opt(num_clusters) round_ 1 set thresh 1 else set thresh expr double(opt(num_clusters) / expr $nn - $opt(num_clusters) $round_ Whenever round_ is 0, all nodes are eligible to be cluster-head。 if round_ = 0 $self hasnotbeenClusterHead #

32、If node has been clusterhead in this group of rounds, it will not # act as a cluster-head for this round. if self hasbeenClusterHead? set thresh 0 else # Pi(t) = Ei(t) / Etotal(t) * k # where k is the expected number of clusters per round, Ei(t) is the nodes current energy, and Etotal(t) is the tota

33、l # energy from all nodes in the network. set Etotal 0 # Note! In a real network, would need a routing protocol to get this information. Alternatively, each node could estimate Etotal(t) from the energy of nodes in its cluster。 for set id 0 id expr $opt(nn)1 incr id set app $node_(id) set rca_app_ s

34、et E app getER query set Etotal expr Etotal + $E set E $self getER query set thresh expr double(expr E * opt(num_clusters) / Etotal 上面是对thresh的计算,当(N - k mod(r,N/k)1,则将thresh设置为1,否则节点thresh=k / (N k mod(r,N/k),每个节点在一个1/p中都要成为簇头节点一次。p=簇头节点占所有节点的比例,在r=0的时候每个节点都有机会吃呢更为簇头节点.如果节点成为过簇头节点则thresh=0,则这个节点在1/

35、p轮后才可以成为簇头节点。 if $self getRandomNumber 0 1 thresh puts nodeID: *” puts nodeID: Is a cluster head at time ns_ now” self setClusterHead set random_access $self getRandomNumber 0 opt(ra_adv) opt(ra_adv) in leach。tcl ns_ at expr now_ + random_access self advertiseClusterHead else puts ”nodeID: * self un

36、setClusterHead 如果threshgetRandomNumber,则节点成为簇头节点。然后调用advertiseClusterHead方法。set next_change_time_ expr $now_ + opt(ch_change) ns_ at next_change_time_ self decideClusterHead $ns_ at expr $now_ + opt(ra_adv_total) ”$self findBestCluster当节点成为簇头节点,则节点调用 advertiseClusterHead方法。 set chID $self nodeID set

37、 currentCH_ $chID pp ”Cluster Head $currentCH_ broadcasting ADV at time ns_ now set mac_dst MAC_BROADCAST set link_dst $LINK_BROADCAST set msg list currentCH_ set datasize expr $BYTES_ID * llength $msg Send beacons opt(max_dist) meters so all nodes can hear。 $self send mac_dst link_dst $ADV_CH msg $

38、datasize opt(max_dist) $code_将该节点设置为簇头节点,设置当前节点所处的簇号。然后发送数据,广播该节点为簇头信息到全局网络。在$ns_ at expr $now_ + opt(ra_adv_total) self findBestCluster调用findBestCluster方法。分析(七)当簇头发出了一个ADV类型的包时,其他的节点会接收这个包,并会将发送这个包的簇头的节点号按顺序先后记录在clusterChoices_中,还会计算每个簇头到接收节点的距离并记录在clusterDist_中。这样可以方便每个节点选簇的时候进行比较.具体的实现在ns-leach.t

39、cl中的recvADV_CH函数中.findBestCluster if $self isClusterHead? If node is CH, determine code and create a TDMA schedule。 set dist_ $opt(max_dist) set currentCH_ $nodeID set myADVnum_ self mac set myADVnum_ # There are opt(spreading) - 1 codes available b/c need 1 code for communication with the base station. set numCodesAvail expr 2 $opt(spreading) 1 set ClusterCode expr int(fmod(myADVnum_, numCodesAvail) + 1 ns_ at expr $now_ + opt(ra_adv_total) + $opt(

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 应用文书 > 合同范本

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服