For doing research in wireless sensor networks, I began to learn ns-2 recently. Building network in Matlab is limited to some extend. Statistic collection of variables in Matlab becomes difficult. Some of my experiences in NS2 are recorded here to prevent me from forgetting and it may useful for others. The version of ns-2 discussed here is ns-allinone-2.31. If any comments, very welcome to let me know.
1. Enable the debugging of ns-2 with gdb/ddd completely, including the otcl, tcl parts
The following three steps are needed before executing ./install:
1a) Edit file: <allinone>/install
a) Locate the part for "Build Tcl<Version>", and
add option '--enable-symbols' to the ./configure command, so that it becomes
./configure --enable-gcc --enable-symbols ...
b) Locate the part for "Build Tk<Version>", and
add option '--enable-symbols' to the ./configure command, so that it becomes
./configure --enable-gcc --enable-symbols ...
c) Locate the part for "Build Otcl<Version>", and
add option '--enable-debug' to the ./configure command, so that it becomes
./configure --enable-debug ...
d) Locate the part for "Build tclcl<Version>", and
add option '--enable-debug' to the ./configure command, so that it becomes
./configure --enable-debug ...
e) Locate the part for "Build ns<Version>", and
add option '--enable-debug' to the ./configure command, so that it becomes
./configure --enable-debug ...
2a) Edit file: <allinone>/<tcl8>/unix/configure
Search for DBGX and replace DBGX=g with DBGX="".
Otherwise, the debuggable library produced has a name with a trailing "g".
By default, ns-2 will not link this library.
3a) Edit file: <allinone>/<ns>/configure
Comment the line: #V_CCOPT="$V_CCOPT -Wall -Werror"
Otherwise, warnings will be treated as errors when <ns> is compiled.
If you don't perform these three steps when you first install ns-2, don't worry. You just perform these steps and execute ./install again. The later installation will clean the previous installation as its first step and then start from scratch.
2. Where to find the documentation about Otcl?
In the "otcl" directory of the <allinone> distribution, there is a directory named "doc", which contains the authoritative documentation on otcl. It's faster to look at them than
to browse the web.
3. The steps that ns-2 execute to create a wireless node.
1) $ns node-config
set up variables to specify the type of nodes to be created
2) $ns node
call create-wireless-node
3) $ns create-wireless-node
a) call create-node-instance, which invokes the constructor of Node/MobileNode
this constructor "init" does the following:
calls the constructor "init" of Node
$node mk-default-classifier
$node set reg_module_([$mod module-name]) $mod
$node install-entry $self $classifier_
initializes X_, Y_, Z_, arptable_, nifs_ to 0 or ""
b) create the routing agent
c) attach the routing agent to the node
d) add the wireless interface to the node
4. A small bug in ns-lib.tcl
In the procedure "Simulator instproc node-config", the command
warn "Please use -channel as shown in tcl/ex/wireless-mitf.tcl"
should appear in the next elseif body: "elseif {[info exists channel_]}"
Currently, this bug doesn't affect the correctness of simulation.
5. A small bug in common/mobilenode.cc
int MobileNode::command(int argc, const char*const* argv)
There is a conflict between objects WiredPhy and WirelessPhy in the implementation of "addif". Currently, this bug doesn't affect the correctness of simulation.
6. The steps of setting up dynamic links
1). Set up bidirectional dynamic links, with each command creating a new rtmodel
$ns rtmodel model-name params elements ;# in rtglib/dynamics.tcl
e.g. $ns rtmodel Deterministic { 0.1 1 1 } $n0 $n1
set ret [eval new rtModel/$dist $self]
# add link_(0:1) and link_(1:0), if exist, to "links_($i)"
eval $ret set-elements $args
eval $ret set-parms $parms
set trace [$self get-ns-traceall]
if {$trace != ""} {
$ret trace $self $trace ;# calling trace-dynamics for all links_($i)
}
lappend rtModel_ $ret
2). Simulator instproc run {}
$self rtmodel-configure ;# in rtglib/dynamics.tcl
for every rtModel/$dist, call
rtModel/$dist configure
3). rtModel/$dist configure ;# in rtglib/dynamics.tcl
for every $links_($i), call
$links_($i) dynamic ;# creating the DynamicLink object for this link $self set-first-event
7. How to configure the data rate of MAC layer?
1) tcl/lan/ns-mac.tcl
Mac/802_11 set basicRate_ 1Mb ;# set this to 0 if want to use bandwidth_
Mac/802_11 set dataRate_ 1Mb ;# for both control and data pkts
2) tcl/lib/ns-default.tcl
Mac/802_11 set PLCPDataRate_ 1.0e6 ;# 1Mbps
Both files are sourced by ns-lib.tcl
8. How to configure different communcation range?
1) To set communication radius, you have to set the receiving threshold value, RXThresh_.The value of RXThresh_ can be obtained by running the program ~ns-2.xx/indep-utils/propagation/threshold.cc (compile using g++ and the option –lm) with propagation model and desired radius parameters as an example:
$ threshold -m TwoRayGround 100 ;# Two-ray model with comm. range = 100m
Receiving threshold RXThresh_ is: 1.42681e-08 ;# This output from threshold.cc
2) Then use above value in your tcl script along with all default parameters as
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
Phy/WirelessPhy set CPThresh_ 10.0
Phy/WirelessPhy set CSThresh_ 1.559e-11
Phy/WirelessPhy set RXThresh_ 1.42681e-08 ;# This value is different for different comm. ranges
Phy/WirelessPhy set bandwidth_ 2e6
Phy/WirelessPhy set Pt_ 0.28183815
Phy/WirelessPhy set freq_ 914e+6
Phy/WirelessPhy set L_ 1.0
9. How to simulate the random correlated traffic when an event triggers many nearby nodes at a time?
1) A Random Correlated Event traffic Model (RCET) is developed for ns2 in paper reference [2] at Here.
(a) Modify the God.cc/.h files to define one function which will take event location (Xe,Ye), event radius Rc, for example:
int God::getEventReports(double x, double y, double radius){
double radiussquare = radius * radius;
int reportingNode = 1; // skip node 0 (the dumb node)
for(int i = 1; i < num_nodes; i++) {
if( (x-mb_node[i]->X())*(x-mb_node[i]->X()) + (y-mb_node[i]->Y())*(y-mb_node[i]->Y()) < radiussquare ){
// in the range, store
reportingNode = reportingNode +1;
sensor_tra_generator_[i]->sendmsg( pktsize, NULL, NULL );
// call agent function here for generating one packet from respective node only
}
//reportingNode = 1; //not in the range [default value]
}
return reportingNode;
}
(b) Then define another function (i.e.,sensor_tra_generator_[i]) which will get above output as an input for generating udp/tcp packets only from given reporting nodes by reference pointer in God file (you need to define pointer variable for udp/tcp agent for activating itself.