ImageVerifierCode 换一换
格式:DOC , 页数:12 ,大小:144.04KB ,
资源ID:2243713      下载积分:8 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/2243713.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(WSN中LEACH协议源码分析.doc)为本站上传会员【精***】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

WSN中LEACH协议源码分析.doc

1、WSN中LEACH协议源码分析 ———————————————————————————————— 作者: ———————————————————————————————— 日期: 12 个人收集整理 勿做商业用途 WSN中LEACH协议

2、源码分析 分析(一) 首先对wireless。tcl进行分析,先对默认的脚本选项进行初始化: set opt(chan)Channel/WirelessChannel set opt(prop) Propagation/TwoRayGround set opt(netif)Phy/WirelessPhy set opt(mac) Mac/802_11 set opt(ifq) Queue/DropTail/PriQueue set opt(ll) LL set opt(ant) Antenna/OmniAntenna set opt(x) 0 ;#

3、 X dimension of the topography set opt(y) 0 ;# Y dimension of the topography set opt(cp) "” set opt(sc) ”。。/mobility/scene/scen-670x670—50—600—20—2” ;# scenario file set opt(ifqlen) 50;# max packet in if set opt(nn) 51 ;# number of nodes set opt(seed) 0.0 set opt(stop) 10.0 ;# simula

4、tion time set opt(tr) out。tr ;# trace file set opt(rp) dsdv ;# routing protocol script set opt(lm) "on” ;# log movement 在这个wireless.tcl中设置了一些全局变量: # # Initialize Global Variables # set ns_ [new Simulator] set chan [new $opt(chan)] set prop

5、[new $opt(prop)] set topo [new Topography] set tracefd [open $opt(tr) w] $topo load_flatgrid $opt(x) $opt(y) $prop topography $topo 这些初始化将在后面的使用中用到,该文件最重要的是创建leach节点:创建方法如下: } elseif { [string compare $opt(rp) "leach”] == 0} { for {set i 0} {$i < $opt(nn) } {incr i} { leach—create—m

6、obile-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 "$node_($i) reset"; //完成后,重置节点的应用 } $ns_ at $opt(stop)。00000001 ”puts \"NS EXITING。。。\" ; $ns_ halt" if { $opt(sc)

7、 == ”” } { 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。tcl进行了简要的分析,接下来对Uamps.tcl脚本进行分析。 set opt(Efriss_a

8、mp) [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 ht^2 hr^2) set opt(Etwo_ray_amp) [expr 1.1 * $opt(RXThresh) / \ [expr $opt(bw) * $opt(Gt) * $opt(G

9、r) * \ $opt(ht) * $opt(ht) * $opt(ht) * $opt(ht)]] set opt(EXcvr) 50e—9 ;# Energy for radio circuitry set opt(e_bf) 5e-9 ;# Beamforming energy (J/bit) set opt(Esense) 0 ;# Sensing energy (J/bit) set o

10、pt(thresh_energy) 0。00 ;# Threshold for power adaptation set opt(Pidle) 0 ;# Idle power (W) set opt(Psleep) 0 ;# Sleep power (W) set initialized 0 set rng_ [new RNG]#用于产生随机数 首先往opt数组里面添加一些变量,并对这些变量进行初化。opt(Psleep) ,

11、opt(Pidle), opt(thresh_energy)在ns—leach。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.

12、*.txt]” catch ”exec 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" 如果没有初始化过,则将以前的跟踪文件删除,接着回

13、到创建leach的函数中,创建节点: if {$id != $opt(nn_)} { puts —nonewline "$id ” #important set node_($id) [new MobileNode/ResourceAwareNode] } else { puts "($opt(nn_) == BS)” set node_($id) [new MobileNode/ResourceAwareNode $BS_NODE] } 如果不是簇头节点则将$opt(nn_)-1个节点设置为一般节点

14、将$opt(nn_)设置为BS节点.newMobileNode/ResourceAwareNode函数是在ns—ranode.tcl中。分析完这个我们接下来分析newMobileNode/ResourceAwareNode这个函数。 set node $node_($id) if {$id != $opt(nn_)} { # Set initial node energy。 if {$opt(eq_energy) == 1} { $node set-energy $opt(init_energy) $opt(thres

15、h_energy) } else { 由于eq-energy在leach—test中给定,将eq—energy=1;则每个节点都会对能量进行初始化,设置节点的初始能量和门槛能量(个人理解,死亡能量)。 set high_e_nodes [list 97 19 12 87 8 22 83 55 34 72] if {[lsearch $high_e_nodes $id] == —1} { set E 2 } else { set E 200 } $nod

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

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

18、opt(nn_)设置为bs。现在对ns—ranode。tcl进行分析。好了我们现在就开始分析这个脚本。 Class MobileNode/ResourceAwareNode —superclass Node/MobileNode MobileNode/ResourceAwareNode instproc init args set bs_node [lindex $args 0] eval $self next [lreplace $args 0 0] args由参数传递进来,若节点为簇头节点则bs_node=1,调用父类初始化函数。 set ResourceMana

19、ger_ [new ResourceManager] $ResourceManager_ Register [new Resource/NeighborResource] set energy [new Resource/Energy] $ResourceManager_ Register $energy ResourceManager在ns—resouce-manager.tcl中定义的。Resource/NeighborResource在ns—neighbor—resource中对能量进行管理。 # Create a new agent and a

20、ttach it to the node if {$bs_node == 1} { set agent [new Agent/BSAgent] } else { set agent [new Agent/RCAgent] } set rca_agent_ $agent 普通节点的应用为RCAgent,BS的应用为BSAGgent. 并将应用attch到node上。 下面两段看不明白,但是这两段不影响看程序。 set haslist [find_haslist [

21、self id]] if {$bs_node == 1} { set rca [new $opt(bsapp)] } else { set rca [new $opt(rcapp) $opt(mtype) $wantslist $haslist] } $ns_ attach—agent $self $agent $rca attach-agent $agent set rca_app_ $rca 将bs节点的应用设置为bsapp,普通节点的应用设置为rcaap

22、p,并将节点的服务设置为不同的服务。bsapp在ns—app。tcl中。$opt(rcapp)定义在leach。tcl中,opt(mtype)定义在leach。tcl中。 set opt(rcapp) ”Application/LEACH" ;# Application type set opt(ra_adv) [TxTime [expr $opt(hdr_size) + 4]] ;# Total time (s) for CH ADVs

23、 ;# Assume max 4(nn*%) CHs set opt(ra_adv_total) [expr $opt(ra_adv)*($opt(num_clusters)*4 + 1)] ;# RA Time (s) for nodes' join reqs set opt(ra_join) [expr 0。01 * $opt(nn_)]

24、 ;# Buffer time for join req xmittal set 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) [ex

25、pr 0。005 + [TxTime [expr $opt(nn_)*4+$opt(hdr_size)]]] ;# Overhead time for cluster set-up set opt(start_xmit) [expr $opt(ra_adv_total) + $opt(ra_join) + $opt(xmit_sch)] 一般节点的应用为Application/LEACH,最终sink节点的应用为Application/BSApp。 通过前3次的分析得出,在分析1中定义变量,在分析2

26、中创建leach节点,在分析3中将节点的应用绑定在节点上。 下面将分析leach的主要协议ns-leach.tcl脚本。 分析(四) 完成前面3个脚本的分析,最后进行ns—leach.tcl脚本的分析.首先我们看下这个脚本要使用的有哪些功能。 Application/LEACH instproc init args {} 对leach进行初始化,即构造函数。 下面是leach的一些方法 Application/LEACH instproc start {} {} Application/LEACH instproc getRandomNumber {llim ulim} {}

27、得到随机数 Application/LEACH instproc node {} {} Application/LEACH instproc nodeID {} {} Application/LEACH instproc mac {} {} Application/LEACH instproc getX {} {} Application/LEACH instproc getY {} {} Application/LEACH instproc getER {} {} Application/LEACH instproc GoToSleep {} {} 节点进行休眠 Appli

28、cation/LEACH instproc WakeUp {} {}节点醒来 Application/LEACH instproc setCode code {}{} Application/LEACH instproc checkAlive {} {} 节点是否存活 Application/LEACH instproc isClusterHead? {} {} 判断是否是簇头节点 Application/LEACH instproc hasbeenClusterHead? {} {} 判断是否成为过簇头节点 Application/LEACH instproc hasnot

29、beenClusterHead {} {}hasbeench=0不是簇头节点 Application/LEACH instproc setClusterHead {} {} 设置为簇头节点 Application/LEACH instproc unsetClusterHead {} {} 设置为非簇头节点 Application/LEACH instproc decideClusterHead {} {} 决定簇头节点,非常重要 Application/LEACH instproc advertiseClusterHead {} {} 广播簇头节点 Application/LE

30、ACH 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 {n

31、odeID} {} Application/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

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

33、 { [$self mac] set node_num_ [$self nodeID] $self decideClusterHead $self checkAlive } 在这个方法中会调用decideClusterHead和checkAlive方法。 Application/LEACH instproc GoToSleep {} { global opt ns_ $self instvar begin_idle_ begin_sleep_ [[$self node] set netif_(0)] set sleep_ 1 # If

34、node has been awake, remove idle energy (e.g。, the amount of energy # dissipated while the node is in the idle state)。 Otherwise, 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_ 〉 $b

35、egin_sleep_} { set idle_energy [expr $opt(Pidle) * [expr [$ns_ now] — $begin_idle_]] [$self getER] remove $idle_energy } else { set sleep_energy [expr $opt(Psleep) * [expr [$ns_ now] - $begin_sleep_]] [$self getER] remove $sleep_energy } set begin_sleep_ [$ns_ now] se

36、t 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. If the node has run out of # energy, it no lon

37、ger 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] se

38、t ISalive [[[$self node] set netif_(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 roun

39、d # N is the total number of sensor nodes in the network # and r is the number of 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)) /

40、\ [expr $nn - $opt(num_clusters) * $round_]] # Whenever round_ is 0, all nodes are eligible to be cluster-head。 if {$round_ == 0} { $self hasnotbeenClusterHead } } # If node has been cluster—head in this group of rounds, it will not # act as a cl

41、uster-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 node’s current energy, and Etotal(t) is the total # energy from

42、 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 i

43、d} { set app [$node_($id) set rca_app_] set 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=

44、k / (N — k mod(r,N/k)),每个节点在一个1/p中都要成为簇头节点一次。p=簇头节点占所有节点的比例,在r=0的时候每个节点都有机会吃呢更为簇头节点.如果节点成为过簇头节点则thresh=0,则这个节点在1/p轮后才可以成为簇头节点。 if {[$self getRandomNumber 0 1] 〈 $thresh} { puts "$nodeID: *******************************************” puts "$nodeID: Is a cluster head at time [$ns_ now]”

45、 $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 unsetClusterHead

46、 } 如果thresh〉getRandomNumber,则节点成为簇头节点。然后调用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 chI

47、D [$self nodeID] set 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 ca

48、n hear。 $self send $mac_dst $link_dst $ADV_CH $msg $datasize $opt(max_dist) $code_ 将该节点设置为簇头节点,设置当前节点所处的簇号。然后发送数据,广播该节点为簇头信息到全局网络。 在$ns_ at [expr $now_ + $opt(ra_adv_total)] "$self findBestCluster"调用findBestCluster方法。 分析(七) 当簇头发出了一个ADV类型的包时,其他的节点会接收这个包,并会将发送这个包的簇头的节点号按顺序先后记录在clusterChoices_中,还会

49、计算每个簇头到接收节点的距离并记录在clusterDist_中。这样可以方便每个节点选簇的时候进行比较.具体的实现在ns-leach.tcl中的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 myADVn

50、um_] # 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(

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

关于我们      便捷服务       自信AI       AI导航        抽奖活动

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

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

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

关注我们 :微信公众号    抖音    微博    LOFTER 

客服