AODV simulation for a Sensor network 802.15.4
The reason to write this topic is many people asked me how to
simulate sensor networks.
Obviously, authors of 802.15.4/Zigbee protocol
developers on NS2 have given a sample examples. But, these examples do
not run correctly, and give some kind of unknown error (at least I don’t
know what errors mean).
Therefore, I have decided to test AODV using
802.15.4 MAC/PHY. Thus, if my tests work, I hope you can test your own
routing protocols using this source code.
Alright, the TCL file is fairly simple. I briefly explain what means
what. We first set simulation environment. We are going to deploy 500
nodes, in 1000×500 sqm area, simulation time is 500 seconds. And we are
using 802.15.4 MAC/PHY and interface queue is 100. We also set simulator
and files to trace the simulation.
2 | set val(chan) Channel/WirelessChannel ; |
3 | set val(prop) Propagation/TwoRayGround ; |
4 | set val(netif) Phy/WirelessPhy/802_15_4 ; |
5 | set val(mac) Mac/802_15_4 ; |
6 | set val(ifq) Queue/DropTail/PriQueue ; |
8 | set val(ant) Antenna/OmniAntenna ; |
15 | set val(energymodel) EnergyModel ; |
16 | set val(initialenergy) 100 ; |
19 | set tracefd [open trace-aodv-802-15-4.tr w] |
20 | set namtrace [open nam-aodv-802-15-4.nam w] |
23 | $ns namtrace-all-wireless $namtrace $val(x) $val(y) |
Let’s set radio transmission range to 40 meters, but this does not
mean exactly 40 meters. The code below filters packet with receiving
signal strength above “40 meters”.
1 | set dist(5m) 7.69113e-06 |
2 | set dist(9m) 2.37381e-06 |
3 | set dist(10m) 1.92278e-06 |
4 | set dist(11m) 1.58908e-06 |
5 | set dist(12m) 1.33527e-06 |
6 | set dist(13m) 1.13774e-06 |
7 | set dist(14m) 9.81011e-07 |
8 | set dist(15m) 8.54570e-07 |
9 | set dist(16m) 7.51087e-07 |
10 | set dist(20m) 4.80696e-07 |
11 | set dist(25m) 3.07645e-07 |
12 | set dist(30m) 2.13643e-07 |
13 | set dist(35m) 1.56962e-07 |
14 | set dist(40m) 1.20174e-07 |
15 | Phy/WirelessPhy set CSThresh_ $dist(40m) |
16 | Phy/WirelessPhy set RXThresh_ $dist(40m) |
And lets set topography as flat, deploy nodes randomly in an area of 1000 x 500 sqm.
2 | set topo [new Topography] |
3 | $topo load_flatgrid $val(x) $val(y) |
8 | $ns node-config -adhocRouting $val(rp) |
16 | -channel [new $val(chan)] |
22 | -energyModel $val(energymodel) |
23 | -initialEnergy $val(initialenergy) |
32 | for {set i 0} {$i < $val(nn) } { incr i } { |
33 | set mnode_($i) [$ns node] |
36 | for {set i 1} {$i < $val(nn) } { incr i } { |
37 | $mnode_($i) set X_ [ expr {$val(x) * rand()} ] |
38 | $mnode_($i) set Y_ [ expr {$val(y) * rand()} ] |
And we are goig to deploy sink node in the center of area, i.e. at [500, 250].
2 | $mnode_(0) set X_ [ expr {$val(x)/2} ] |
3 | $mnode_(0) set Y_ [ expr {$val(y)/2} ] |
The code below is useful how big the nodes are going to be shown in
NAM (network animator), thus it does not have meaning in real
simulation.
1 | for {set i 0} {$i < $val(nn)} { incr i } { |
2 | $ns initial_node_pos $mnode_($i) 10 |
Finally, we have deployed nodes, and remained important thing is
establish connection. We are going to use UDP protocol with CBR
(constant bit rate, interval (interval_) is set to 2 seconds)
3 | $ns attach-agent $mnode_(10) $udp |
5 | set sink [new Agent/Null] |
6 | $ns attach-agent $mnode_(0) $sink |
12 | set cbr [new Application/Traffic/CBR] |
15 | $cbr set packet_size_ 50 |
20 | $ns at 5.0 "$cbr start" |
21 | $ns at [expr $val(stop) - 5] "$cbr stop" |
24 | for {set i 0} {$i < $val(nn) } { incr i } { |
25 | $ns at $val(stop) "$mnode_($i) reset;" |
29 | $ns at $val(stop) "$ns nam-end-wireless $val(stop)" |
30 | $ns at $val(stop) "stop" |
31 | $ns at [expr $val(stop) + 0.01] "puts "end simulation"; $ns halt" |
33 | global ns tracefd namtrace |
We have finished writing sample AODV TCL script, we can run it
Download whole source code here : aodv_802_15_4.tcl . If you find any problem with that, leave comment here. If you want to test your own routing protocol simply change $val(rp) AODV with your own.
No comments:
Post a Comment