Monday 7 January 2013

Tcl script to create the dynamic number of nodes and its initial location




#-------Event scheduler object creation--------#
set ns [new Simulator]
#creating the trace file and nam file
set tracefd [open wireless3.tr w]
set namtrace [open wireless3.nam w]   
$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)]
# configure the nodes
        $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 < $val(nn) } {incr i } {
            $node_($i) color gold
            $ns at 0.0 "$node_($i) color gold"
      }
## Provide initial location of mobilenodes..
            for {set i 0} {$i < $val(nn) } { incr i } {
                  set xx [expr rand()*600]
                  set yy [expr rand()*600]
                  $node_($i) set X_ $xx
                  $node_($i) set Y_ $yy
                 
            }
     
# 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
}
# Telling nodes when the simulation ends
for {set i 0} {$i < $val(nn) } { incr i } {
    $ns at $val(stop) "$node_($i) reset";
}
# ending nam and the simulation 
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 10.01 "puts \"end simulation\" ; $ns halt"
#stop procedure...
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec nam wireless3.nam &
}
$ns run

# snapshot of the program output



1 comment:

  1. this code is not working. gives following error:
    num_nodes is set 0
    warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
    can't read "node_(0)": no such variable
    while executing
    "$node_(0) set X_ 27.0"
    (file "wirelessrandom.tcl" line 65)

    ReplyDelete