http://www.pixnet.net/NS2lab

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

創作者介紹

『網路節點模擬』 研究日誌

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


留言列表 (8)

發表留言
  • pbyohong
  • 您好:
    我專題也是作這方面的,偶然發現您的部落格
    我想請問一下 ,這行指令是什麼意思呢
    $cbr0 set random_ false

    剛碰NS2的我,指令都看不懂>"<
  • NS2lab
  • 因為是CBR(Constant Bit Rate),所以在模擬過程我們讓他隨機關閉,這樣可以比較接近實際狀況
  • pbyohong
  • NSG2 Parameters設置問題

    大大~請問您:NSG2寫模擬無線網路裡面的Parameters-->Channel是設定802.11b的設置,那裡面勾選的地方代表什麼意思呢?
    Antenna/OmniAntenna set Gt_ 1 ;#Transmit antenna gain
    Antenna/OmniAntenna set Gr_ 1 ;#Receive antenna gain
    Phy/WirelessPhy set L_ 1.0 ;#System Loss Factor
    Phy/WirelessPhy set freq_ 2.472e9 ;#channel
    Phy/WirelessPhy set bandwidth_ 11Mb ;#Data Rate
    Phy/WirelessPhy set Pt_ 0.031622777 ;#Transmit Power
    Phy/WirelessPhy set CPThresh_ 10.0 ;#Collision Threshold
    Phy/WirelessPhy set CSThresh_ 5.011872e-12 ;#Carrier Sense Power
    Phy/WirelessPhy set RXThresh_ 5.82587e-09 ;#Receive Power Threshold
    Mac/802_11 set dataRate_ 11Mb ;#Rate for Data Frames
    Mac/802_11 set basicRate_ 1Mb ;#Rate for Control Frames
  • NS2lab
  • 您想問的是每個參數代表的意義嗎?
    後面就有註解囉
    Antenna是天線以及包括Mac和Phy的參數設定
    如果想知道CSThresh_和RXThresh_傳輸範圍的設定
    建議您閱讀設定傳輸範圍那篇
  • JoJo
  • 請問大大~你有試過在同一tcl黨內,設定同一種傳輸技術其BS(or AP)不同的覆蓋範圍嗎?Ex.在同一無線網路拓普內有兩個BS,802.16BS_1的覆蓋範圍250m,802.16_BS_2的覆蓋範圍1000m.
  • 傳輸範圍設定,這個之前我們也有弄過…那時想法也是想跟你一樣,去設定多個覆蓋範圍
    不過似乎當『節點創立』之後,他就會『固定其傳輸範圍的設定』了,之後如果又想修改範圍好像都沒效果,
    而且在執行模擬的時候 ns2 也會顯示一個傳輸範圍的值,所以我推測它是不能設定多個覆蓋範圍的。
    希望有幫助到您^_^

    NS2lab 於 2008/03/17 22:10 回覆

  • JoJo
  • 謝謝您的回覆^^,但是我在看NIST所release出來的WiMAX範例,其內有展示802.11與802.16 heterogenous網路模擬,我看它的範例感覺也是分別設覆蓋範圍,然後再分別用node config-指令,但是我依照他的作法自己試著設置兩種覆蓋範圍的WiMAX BS卻感覺沒有成功 囧,所以想看看您那邊是否有方法可以解決此問題^^.
  • specific
  • 请问这个程序要在什么软件来运行呢,有具体的版本要求吗?刚接触这个,请见谅!
  • 恩,可以建議你從我們最前面貼的網誌開始看起!
    這個腳本是要用NS-2這個軟體去跑,我們網誌有放上使用以及安裝的教學!!

    NS2lab 於 2008/07/30 15:30 回覆

  • 802.11e+noah
  • 請問您有試過 noah在802.11e的實驗下 可以使用嗎?