WSN中LEACH協(xié)議源碼分析

上傳人:細(xì)水****9 文檔編號(hào):61709259 上傳時(shí)間:2022-03-12 格式:DOC 頁(yè)數(shù):10 大?。?2KB
收藏 版權(quán)申訴 舉報(bào) 下載
WSN中LEACH協(xié)議源碼分析_第1頁(yè)
第1頁(yè) / 共10頁(yè)
WSN中LEACH協(xié)議源碼分析_第2頁(yè)
第2頁(yè) / 共10頁(yè)
WSN中LEACH協(xié)議源碼分析_第3頁(yè)
第3頁(yè) / 共10頁(yè)

下載文檔到電腦,查找使用更方便

0 積分

下載資源

還剩頁(yè)未讀,繼續(xù)閱讀

資源描述:

《WSN中LEACH協(xié)議源碼分析》由會(huì)員分享,可在線閱讀,更多相關(guān)《WSN中LEACH協(xié)議源碼分析(10頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。

1、WSN中LEACH協(xié)議源碼分析分析(一)首先對(duì)wireless.tcl進(jìn)行分析,先對(duì)默認(rèn)的腳本選項(xiàng)進(jìn)行初始化: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/OmniAntennaset opt(x) 0 ;# X dimension of the to

2、pographyset opt(y) 0 ;# Y dimension of the topographyset opt(cp) set opt(sc) ./mobility/scene/scen-670x670-50-600-20-2 ;# 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(tr) out.tr ;# trace fileset opt(rp

3、) dsdv ;# routing protocol scriptset opt(lm) on ;# log movement在這個(gè)wireless.tcl中設(shè)置了一些全局變量:# Initialize Global Variables#set ns_ new Simulatorset chan new $opt(chan)set prop new $opt(prop)set topo new Topographyset tracefd open $opt(tr) w$topo load_flatgrid $opt(x) $opt(y)$prop topography $topo這些初始化將在

4、后面的使用中用到,該文件最重要的是創(chuàng)建leach節(jié)點(diǎn):創(chuàng)建方法如下: elseif string compare $opt(rp) leach = 0 for set i 0 $i $opt(nn) incr i leach-create-mobile-node $i如果路由協(xié)議是leach協(xié)議,則在Uamps.tcl中調(diào)用leach-create-mobile-node方法創(chuàng)建leach節(jié)點(diǎn)。將在第二小節(jié)講如何創(chuàng)建leach節(jié)點(diǎn)。for set i 0 $i $begin_sleep_ set idle_energy expr $opt(Pidle) * expr $ns_ now - $b

5、egin_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 set begin_idle_ 0分析(六)在start中調(diào)用下面這個(gè)方法。Application/LEACH instproc decideClusterHead global ns_ chan opt node_ $self instvar ali

6、ve_ TDMAschedule_ $self instvar begin_idle_ begin_sleep_ # Check the alive status of the node. 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 $se

7、lf node set netif_(0) set alive_ 0 set opt(nn_) expr $opt(nn_) - 1 set ISalive $self node set netif_(0) set alive_ #從網(wǎng)絡(luò)接口netif中查看當(dāng)前節(jié)點(diǎn)狀 況如果節(jié)點(diǎn)存活,但是節(jié)點(diǎn)能量耗光,則$chan removeif $self node set netif_(0)將節(jié)點(diǎn)信道中移出,并將節(jié)點(diǎn)設(shè)置為死亡。節(jié)點(diǎn)的總數(shù)目減少一個(gè)。if $opt(eq_energy) = 1 # # Pi(t) = k / (N - k mod(r,N/k) # where k is the expe

8、cted number of clusters per round # 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) / expr $nn - $opt(num_cluster

9、s) * $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 cluster-head for this round. if $self hasbeenClusterHead? set thresh 0 else # # Pi(t) = Ei(t) / E

10、total(t) * k # where k is the expected number of clusters per round, # Ei(t) is the nodes current energy, and Etotal(t) is the total # 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 cou

11、ld 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_ 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 上面是對(duì)thresh的計(jì)算,當(dāng)(N - k m

12、od(r,N/k)1,則將thresh設(shè)置為1,否則節(jié)點(diǎn)thresh=k / (N - k mod(r,N/k),每個(gè)節(jié)點(diǎn)在一個(gè)1/p中都要成為簇頭節(jié)點(diǎn)一次。p=簇頭節(jié)點(diǎn)占所有節(jié)點(diǎn)的比例,在r=0的時(shí)候每個(gè)節(jié)點(diǎn)都有機(jī)會(huì)吃呢更為簇頭節(jié)點(diǎn)。如果節(jié)點(diǎn)成為過(guò)簇頭節(jié)點(diǎn)則thresh=0,則這個(gè)節(jié)點(diǎn)在1/p輪后才可以成為簇頭節(jié)點(diǎn)。 if $self getRandomNumber 0 1 getRandomNumber,則節(jié)點(diǎn)成為簇頭節(jié)點(diǎn)。然后調(diào)用advertiseClusterHead方法。set next_change_time_ expr $now_ + $opt(ch_change) $ns_ a

13、t $next_change_time_ $self decideClusterHead $ns_ at expr $now_ + $opt(ra_adv_total) $self findBestCluster當(dāng)節(jié)點(diǎn)成為簇頭節(jié)點(diǎn),則節(jié)點(diǎn)調(diào)用 advertiseClusterHead方法。 set chID $self nodeID set currentCH_ $chID pp Cluster Head $currentCH_ broadcasting ADV at time $ns_ now set mac_dst $MAC_BROADCAST set link_dst $LINK_BRO

14、ADCAST 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 $datasize $opt(max_dist) $code_將該節(jié)點(diǎn)設(shè)置為簇頭節(jié)點(diǎn),設(shè)置當(dāng)前節(jié)點(diǎn)所處的簇號(hào)。然后發(fā)送數(shù)據(jù),廣播該節(jié)點(diǎn)為簇頭信息到全局網(wǎng)絡(luò)。在$ns_ at expr $now_ + $opt(ra_adv_total) $sel

15、f findBestCluster調(diào)用findBestCluster方法。分析(七)當(dāng)簇頭發(fā)出了一個(gè)ADV類(lèi)型的包時(shí),其他的節(jié)點(diǎn)會(huì)接收這個(gè)包,并會(huì)將發(fā)送這個(gè)包的簇頭的節(jié)點(diǎn)號(hào)按順序先后記錄在clusterChoices_中,還會(huì)計(jì)算每個(gè)簇頭到接收節(jié)點(diǎn)的距離并記錄在clusterDist_中。這樣可以方便每個(gè)節(jié)點(diǎn)選簇的時(shí)候進(jìn)行比較。具體的實(shí)現(xiàn)在ns-leach.tcl中的recvADV_CH函數(shù)中。findBestCluster if $self isClusterHead? # If node is CH, determine code and create a TDMA schedule. s

16、et 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

17、$ns_ at expr $now_ + $opt(ra_adv_total) + $opt(ra_join) $self createSchedule 如果這個(gè)節(jié)點(diǎn)是簇頭節(jié)點(diǎn),設(shè)置為當(dāng)前的簇頭, set numCodesAvail expr 2 * $opt(spreading) - 1 set ClusterCode expr int(fmod($myADVnum_, $numCodesAvail) + 1這兩句沒(méi)看懂在一個(gè)隨機(jī)時(shí)間后調(diào)用createSchedule,節(jié)點(diǎn)根據(jù)自己收到的clusterChoices,選擇最近的簇頭作為簇頭節(jié)點(diǎn)。選擇好簇頭節(jié)點(diǎn)就informClusterHea

18、d。發(fā)送完數(shù)據(jù)就進(jìn)入休眠狀態(tài)。Application/LEACH instproc createSchedule global ns_ opt ADV_SCH MAC_BROADCAST BYTES_ID $self instvar clusterNodes_ TDMAschedule_ $self instvar dist_ code_ now_ beginningE_ set numNodes llength $clusterNodes_ set chID $self nodeID if $numNodes = 0 set xmitOrder puts Warning! There are

19、 no nodes in this cluster ($chID)! $self SendMyDataToBS else # Set the TDMA schedule and send it to all nodes in the cluster. set xmitOrder $clusterNodes_ set msg list $xmitOrder set spreading_factor $opt(spreading) set datasize expr $spreading_factor * $BYTES_ID * llength $xmitOrder pp $chID sendin

20、g TDMA schedule: $xmitOrder at time $ns_ now pp Packet size is $datasize. set mac_dst $MAC_BROADCAST set link_dst $chID $self send $mac_dst $link_dst $ADV_SCH $msg $datasize $dist_ $code_ set TDMAschedule_ $xmitOrder set outf open $opt(dirname)/TDMAschedule.$now_.txt a puts $outf $chIDt$TDMAschedule

21、_ close $outf set outf open $opt(dirname)/startup.energy a puts $outf $ns_ nowt$chIDtexpr $beginningE_ - $self getER query close $outf在recvADV_SCH給每個(gè)節(jié)點(diǎn)一個(gè)時(shí)間間隙發(fā)送數(shù)據(jù) set frame_time_ expr expr 5 + llength join $order * $opt(ss_slot_time) set xmitTime_ expr $opt(ss_slot_time) * $ind set end_frm_time_ expr $frame_time_ - $xmitTime_ set xmitat expr $ns_ now + $xmitTime_ pp $nodeID scheduled to transmit at $xmitat. It is now $ns_ now. if expr $xmitat + $end_frm_time_ expr $next_change_time_ - 10 * $opt(ss_slot_time) $ns_ at $xmitat $self sendData具體實(shí)現(xiàn)方法看不太懂。

展開(kāi)閱讀全文
溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!