2007.12.06

針對專題的第一個目標,直接傳送 (direct transmission),也就是每個節點本身都會直接傳送資料到 Base ,雖然目
標是希望做出都不去經過其他節點,不過似乎礙於 routing 的關係,會有一些節點會去 via 其他節點,不過這種情形
發生的情況並不多,所以並不是很影響到整個模擬的進行。

程式寫法是每個節點的傳送、接收範圍設定很大,以至於可以涵蓋到 Base 的位置,這樣路由建立的時候就會直接
跟 Base 做連線以及傳送資料;接著再設定每個節點的傳輸消耗電量,以『距離』當參數下去設定傳送消耗的電量
,而傳送消耗的電量單位,目前是自己內定的,並且是以每一百公尺為一個單位下去設定。


# This script is created by NSG2 beta1
# <http://wushoupong.googlepages.com/nsg>

#===================================
#     Simulation parameters setup
#===================================
  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) 100                        ;# max packet in ifq
  set val(nn)     101                        ;# number of mobilenodes
  set val(rp)     AODV                       ;# routing protocol
  set val(x)      500                        ;# X dimension of topography
  set val(y)      500                        ;# Y dimension of topography
  set val(stop)   100.0                      ;# time of simulation end
  set val(batterymodel)   Battery/Simple     ;# battery model
  set val(batterymonitor) "on"
  set val(initialenergy)  5                  ;# Initial battery capacity

# 傳送、接收範圍 1000 單位
  Phy/WirelessPhy set freq_ 9.14e+08
  Phy/WirelessPhy set RXThresh_ 1.42681e-12
  Phy/WirelessPhy set CSThresh_ 1.42681e-12
  Phy/WirelessPhy set Pt_ 0.28183815

#===================================
#        Initialization       
#===================================
# Create a ns simulator
  set ns [new Simulator]

# Setup topography object
  set topo [new Topography]
  $topo load_flatgrid $val(x) $val(y)
  create-god $val(nn)

# Open the NS trace file
  set tracefile [open out.tr w]
  $ns use-newtrace
  $ns trace-all $tracefile

# Open the NAM trace file
  set namfile [open out.nam w]
  $ns namtrace-all $namfile
  $ns namtrace-all-wireless $namfile $val(x) $val(y)
  set chan [new $val(chan)];#Create wireless channel

#===================================
#        Node-Config       
#===================================
#     以每一百範圍為50個單位
  proc Nodeconfig { range } {
     global ns val topo chan
     $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) \
                     -channel       $chan \
                     -topoInstance  $topo \
                     -agentTrace    ON \
                     -routerTrace   ON \
                     -macTrace      ON \
                     -movementTrace ON \
                     -energyModel   "EnergyModel" \
                     -initialEnergy $val(initialenergy) \
                     -sleepPower    0 \
                     -txPower       [expr ($range/100)*50] \
                     -rxPower       1.0 \
                     -idlePower     0.001
  }

#===================================
#        Nodes Definition       
#===================================
  Nodeconfig 0          ;# Base 設定傳送消耗電量為零

  set BB [$ns node]
  $BB set X_ 0
  $BB set Y_ 0
  $BB set Z_ 0
  $ns initial_node_pos $BB 50

#===================================
#   Create (val(nn)-1) random nodes
#===================================
  set rng [new RNG]
  $rng seed 0

  for {set i 0} {$i < $val(nn)-1} {incr i} {
    set XX [$rng uniform 0 500]     ;# 隨機的 X 座標
    set YY [$rng uniform 0 500]     ;# 隨機的 Y 座標

    set s [expr $XX*$XX+$YY*$YY]    ;# 計算(x,y)到(0,0)的長度
    set g [expr $s/2]
    for {set j 0} {$j<10} {incr j} {
      set r [expr $s/$g]
      set g [expr ($g+$r)/2]       ;# g = sqrt(s)
    }
  
    #===================================
    #     Mobile node parameter setup
    #===================================
    Nodeconfig $g                   ;# 根據 node 到 base 的距離去設定傳送會消耗的電量
 
    set n($i) [$ns node]            ;# 建立 node
    $n($i) set X_ $XX
    $n($i) set Y_ $YY
    $n($i) set Z_ 0.0
    $ns initial_node_pos $n($i) 20
  }

#===================================
#     set Agent && Application
#===================================
  for {set k 0} {$k < $val(nn)-1} {incr k} {
    set udp($k) [new Agent/UDP]
    set null($k) [new Agent/Null]
 
    $ns attach-agent $n($k) $udp($k)
    $ns attach-agent $BB $null($k)
 
    $ns connect $udp($k) $null($k)
 
    set cbr($k) [new Application/Traffic/CBR]
    $cbr($k) attach-agent $udp($k)
    $cbr($k) set type_ CBR
    $cbr($k) set rate_ 1mb
    $cbr($k) set packet_size_ 2000
    $cbr($k) set random_ false
 
        #===================================
        #        Random Variable
        #===================================
        set rng [new RNG]
        $rng seed 0
        # ======== Uniform Distribution ========
        set RVstart [new RandomVariable/Uniform]
        $RVstart use-rng $rng
        $RVstart set min_ 0
        $RVstart set max_ 1
        set RVend [new RandomVariable/Uniform]
        $RVend use-rng $rng
        $RVend set min_ 50
        $RVend set max_ 60
        # ======== Uniform Distribution ========

    #===================================
    #       Applications Definition
    #===================================
    set startT($k) [ expr [$RVstart value] ]
    set endT($k) [ expr [$RVend value] ]
    $ns at $startT($k) "$cbr($k) start"
    $ns at $endT($k) "$cbr($k) stop"
  }

#===================================
#        Termination       
#===================================
# Define a 'finish' procedure
  proc finish {} {
      global ns tracefile namfile
      $ns flush-trace
      close $tracefile
      close $namfile
      exec nam out.nam &
      exit 0
  }

  for {set i 0} {$i < $val(nn)-1 } { incr i } {
      $ns at $val(stop) "\$n($i) reset"
  }
  $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  $ns at $val(stop) "finish"
  $ns at $val(stop) "puts \"done\" ; $ns halt"
  $ns run

歡迎對於 NS2 有研究或見解的同學、朋友可以一起討論  by Yo PM 07:16

arrow
arrow
    全站熱搜

    NS2lab 發表在 痞客邦 留言(5) 人氣()