2007.08.31

老師這次是要我們回去多找個例子來做個分析,於是我去柯志亨老師的網站上找到一個對於 wireless-ping 的程式碼,馬上就下載來測試一番,不過這個程式碼沒有 NAM 的模擬,於是我有自己加了一些東西進去。

程式碼如下:

# A simple example for wireless simulation
#================================================================
# Define options
#================================================================
  set val(chan)   Channel/WirelessChannel      ;# channel type
  set val(prop)    Propagation/TwoRayGround    # radio-propagation model
# set val(prop)   Propagation/RFMGroundProp ;# radio-propagation model
# set val(prop)   Propagation/SimpleProp         # radio-propagation model
  set val(netif)     Phy/WirelessPhy                     # network interface type
  set val(mac)     Mac/802_11                            ;# MAC type
# set val(mac)    Mac/Tdma                               ;# 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)   50                                           ;# max packet in ifq
  set val(nn)        3                                             ;# number of mobilenodes
  set val(rp)         DSDV                                      ;# routing protocol
# set val(rp)        AODV                                      ;# routing protocol
# set val(rp)        DSR                                       ;# routing protocol
  set val(batterymodel)     Battery/Simple          ;# battery model
  set val(batterymonitor)   "on"                          ;# 電力控制顯示??
  set val(initialenergy)      36                             ;# Initial battery capacity
  set val(radiomodel)       Radio/Simple           ;# generic radio hardware
  set val(receivepower)    0.5                            ;# Receiving Power
  set val(transmitpower)  0.5                            ;# Transmitting Power
  set val(idlepower)          0.05                          ;# Idle Power

#================================================================
# Other Settings
#================================================================

  LL set mindelay_    50us   ;# Link Layer 的延遲
  LL set delay_          25us   ;# Link Layer 的延遲
  LL set bandwidth_  0          ;# not used & 待研究

  Queue/DropTail/PriQueue set Prefer_Routing_Protocols 1    ;#待研究

# unity gain, omni-directional antennas
# set up the antennas to be centered in the node and 1.5 meters above it
  Antenna/OmniAntenna set X_ 0       ;#待研究
  Antenna/OmniAntenna set Y_ 0       ;#待研究
  Antenna/OmniAntenna set Z_ 1.5    ;#待研究
  Antenna/OmniAntenna set Gt_ 1.0  ;#待研究
  Antenna/OmniAntenna set Gr_ 1.0  ;#待研究

# Initialize the SharedMedia for a transmission range of 20 m for TwoRay Ground model
  Phy/WirelessPhy set CPThresh_ 10.0                            ;#待研究
  Phy/WirelessPhy set CSThresh_ 1.559e-11                   ;#待研究
# -- Phy/WirelessPhy set RXThresh_ 4.80696e-07 -- #    ;#待研究
  Phy/WirelessPhy set Rb_ 2*1e6                                     ;#待研究
  Phy/WirelessPhy set Pt_ 0.2818                                     ;# transmit power
  Phy/WirelessPhy set freq_ 914e+6                                 ;# frequency
  Phy/WirelessPhy set L_ 1.0


#================================================================
# Main Program
#================================================================
# remove-all-packet-headers
# add-packet-header DSDV Agent/Ping Mac/802_11 Mac/Tdma

  set ns_ [new Simulator]
  set tracefd [open WLping.tr w]
  $ns_ trace-all $tracefd  ;# open  the trace

# Open the NAM trace file
  set namfile [open out.nam w]
  $ns_ namtrace-all $namfile ;# open the nam
  $ns_ namtrace-all-wireless $namfile 100 100

# set up topography object
  set topo [new Topography] ;# set topo
  $topo load_flatgrid 100 100 ;# range

  create-god $val(nn)  ;# set god

# Create channel
  set chan_1 [new $val(chan)]

# configure node
  $ns_ node-config \
      -adhocRouting    $val(rp) \       ;# DSDV or AODV or DSR
      -llType                 $val(ll) \        ;# LL
      -macType            $val(mac) \    ;# Mac/802_11
      -ifqType               $val(ifq) \       ;# Queue/DropTail/PriQueue 
      -ifqLen                 $val(ifqlen) \ ;# 50
      -antType              $val(ant) \     ;# Antenna/OmniAntenna
      -propType            $val(prop) \   ;# Propagation/TwoRayGround
      -phyType              $val(netif) \   ;# Phy/WirelessPhy
      -channel              $chan_1 \      ;# 管道
      -topoInstance      $topo \           ;# topograph
      -agentTrace         ON \               ;# agent紀錄檔
      -routerTrace        ON \               ;# router紀錄檔
      -macTrace           ON \               ;# mac紀錄檔
      -movementTrace ON \               ;# movement紀錄檔
      -energyModel       "EnergyModel" \           ;# 電力的模組
      -initialEnergy       $val(initialenergy) \      ;# 基本node電力
      -rxPower               $val(receivepower) \   ;# 接收消耗的電力
      -txPower               $val(transmitpower) \  ;# 傳送消耗的電力
      -idlePower           $val(idlepower)            ;# 閒置消耗的電力

# Generating nodes
  for {set i 0} {$i < $val(nn) } {incr i} {
     set node_($i) [$ns_ node]
  }

# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
  $node_(0) set X_ 94.85
  $node_(0) set Y_ 12.75
  $node_(0) set Z_ 0.0
  $ns_ initial_node_pos $node_(0) 20

  $node_(1) set X_ 60.79
  $node_(1) set Y_ 92.33
  $node_(1) set Z_ 0.0
  $ns_ initial_node_pos $node_(1) 20

  $node_(2) set X_ 41.86
  $node_(2) set Y_ 10.13
  $node_(2) set Z_ 0.0
  $ns_ initial_node_pos $node_(2) 20

# Create two ping agents and attach them to the nodes n0 and n2
  set p0 [new Agent/Ping]
  $ns_ attach-agent $node_(0) $p0

  set p1 [new Agent/Ping]
  $ns_ attach-agent $node_(2) $p1

# Connect the two agents
  $ns_ connect $p0 $p1

# Create udp agents and attach them to the nodes n1 and n2
  set udp0 [new Agent/UDP]
  $ns_ attach-agent $node_(1) $udp0

  set null0 [new Agent/Null]
  $ns_ attach-agent $node_(2) $null0

# Connect the two agents
  $ns_ connect $udp0 $null0

# Setup a CBR Application over UDP connection
  set cbr0 [new Application/Traffic/CBR]
  $cbr0 attach-agent $udp0
  $cbr0 set type_ CBR
  $cbr0 set rate_ 1mb
  $cbr0 set packet_size_ 1000
  $cbr0 set random_ false

  $ns_ at 0.5 "$cbr0 start"
  $ns_ at 9.0 "$cbr0 stop"

proc finish {} {
    global ns_ tracefd namfile
    $ns_ flush-trace
    close $tracefd
    close $namfile
    exec nam out.nam &
    exit 0
}

# Define a 'recv' function for the class 'Agent/Ping'
  Agent/Ping instproc recv {from rtt} {
     $self instvar node_
     puts "node [$node_ id] received ping answer from $from with round-trip-time $rtt ms."
  }

# set the "Ping" time
  for {set i 0} {$i<3} {incr i} {
     $ns_ at [expr $i+0.3] "$p0 send"
     $ns_ at [expr $i+0.5] "$p1 send"
     $ns_ at [expr $i+0.7] "$p0 send"
     $ns_ at [expr $i+0.9] "$p1 send"
  }

  $ns_ at 10.0 "finish"

# Run the Simulation
  puts "Starting Simulation..."
  $ns_ run
 

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

arrow
arrow
    全站熱搜

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