Monday 7 January 2013

Tcl script to give mobility to nodes communication



$ns run


       set ns [new Simulator]
## Create a trace file and nam file..

      set tracefd [open wireless2.tr w]

      set namtrace [open wireless2.nam w]   
## Trace the nam and trace details from the main simulation..

      $ns trace-all $tracefd

      $ns namtrace-all-wireless $namtrace $val(x) $val(y)
## set up topography object..

      set topo [new Topography]
      $topo load_flatgrid $val(x) $val(y)
      set god_ [create-god $val(nn)]
## Color Descriptions..
      $ns color 1 dodgerblue
      $ns color 2 blue
      $ns color 3 cyan
      $ns color 4 green
      $ns color 5 yellow
      $ns color 6 black
      $ns color 7 magenta
      $ns color 8 gold
      $ns color 9 red
## Setting The Distance Variables..
# For model 'TwoRayGround'
      set dist(5m)  7.69113e-06
      set dist(9m)  2.37381e-06
      set dist(10m) 1.92278e-06
      set dist(11m) 1.58908e-06
      set dist(12m) 1.33527e-06
      set dist(13m) 1.13774e-06
      set dist(25m) 3.07645e-07
      set dist(30m) 2.13643e-07
      set dist(35m) 1.56962e-07
      set dist(40m) 1.56962e-10
      set dist(45m) 1.56962e-11
      set dist(50m) 1.20174e-13
      #Phy/WirelessPhy set CSThresh_ $dist(50m)
      #Phy/WirelessPhy set RXThresh_ $dist(50m)

## Setting node config event with set of inputs..

 
        $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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
     

## Creating node objects..               
      for {set i 0} {$i < $val(nn) } { incr i } {
            set node_($i) [$ns node]     
      }
      for {set i 0} {$i < 4 } {incr i } {
            $node_($i) color yellow
            $ns at 0.0 "$node_($i) color yellow"
      }
      for {set i 4} {$i < 10 } {incr i } {
            $node_($i) color red
            $ns at 3.0 "$node_($i) color red"
      }
      for {set i 10} {$i < 15 } {incr i } {
            $node_($i) color blue
            $ns at 5.0 "$node_($i) color blue"
      }
## Provide initial location of mobilenodes..

      $node_(0) set X_ 27.0
      $node_(0) set Y_ 260.0
      $node_(0) set Z_ 0.0
      $node_(1) set X_ 137.0

      $node_(1) set Y_ 348.0
      $node_(1) set Z_ 0.0
      $node_(2) set X_ 294.0

      $node_(2) set Y_ 235.0
      $node_(2) set Z_ 0.0
      $node_(3) set X_ 414.0

      $node_(3) set Y_ 342.0
      $node_(3) set Z_ 0.0
      $node_(4) set X_ 562.0

      $node_(4) set Y_ 267.0
      $node_(4) set Z_ 0.0
      $node_(5) set X_ 279.0

      $node_(5) set Y_ 447.0
      $node_(5) set Z_ 0.0
      $node_(6) set X_ -128.0
      $node_(6) set Y_ 260.0
      $node_(6) set Z_ 0.0
      $node_(7) set X_ 727.0

      $node_(7) set Y_ 269.0
      $node_(7) set Z_ 0.0
     
      $node_(8) set X_ 130.0
      $node_(8) set Y_ 126.0
      $node_(8) set Z_ 0.0
      $node_(9) set X_ 318.0

      $node_(9) set Y_ 45.0
      $node_(9) set Z_ 0.0
      $node_(10) set X_ 505.0

      $node_(10) set Y_ 446.0
      $node_(10) set Z_ 0.0
      $node_(11) set X_ 421.0

      $node_(11) set Y_ 158.0
      $node_(11) set Z_ 0.0
      $node_(12) set X_ 72.0

      $node_(12) set Y_ 397.0
      $node_(12) set Z_ 0.0
      if {$val(nn) >12} {

            for {set i 13} {$i < $val(nn) } { incr i } {
                  set xx [expr rand()*600]
                  set yy [expr rand()*500]
                  $node_($i) set X_ $xx
                  $node_($i) set Y_ $yy
                  $node_($i) set Z_ 0.0
            }
      }
## Define node initial position in nam..
      for {set i 0} {$i < $val(nn)} { incr i } {
      # 30 defines the node size for nam..
            $ns initial_node_pos $node_($i) 30
      }
## Stop procedure..
$ns at 0.0 "destination"
proc destination {} {
      global ns val node_
      set time 1.0
      set now [$ns now]
      for {set i 0} {$i<$val(nn)} {incr i} {
            set xx [expr rand()*600]
            set yy [expr rand()*500]
            $ns at $now "$node_($i) setdest $xx $yy 20.0"
      }
      $ns at [expr $now+$time] "destination"
}
$ns at $val(stop) "stop"
#stop procedure:

proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
    puts "running nam..."
    exec nam wireless2.nam &
    exit 0
}
$ns run


1 comment:

  1. tôi đã thử đoạn code trên nhưng không chạy, hãy giải thích cho tôi

    ReplyDelete