Skip to content

A Simple VANET Protocol Simulation using TCL level functions of ns-2

Generally, under ns-2, the VENET protocols are designed as application layer agents. In this project, we will see one such simple design.

The following are the main parts of one such  VANET Simulation Script

Defining Some Important Simulation Parameters

set RSU_BEACON_PORT 42 ;#the broadcast port
set RSU_BROADCAST_ADDRESS -1 ;#the broadcast address


set RSU_BROADCAST_INTERVAL 5 ;#in Seconds
set RSU_BROADCAST_MESSAGE_SIZE 10 ;# in bytes Max 1500 - it may in the format #RSUID#
set RSUBeaconStartTime 5 ;# Start sending RSU beacons after 5 seconds

set SizeOfRSUnode 40
set SizeOfVechicleNode 30
set TraficStartTime 6 ;# time from which the vehicles starts to move
set TraficJitterSeed 400 ;# we may increase this to increase the distance between vehicles

set DistanceBetweenRSUs 400 ;#in meters - it Should be less than the tx range of the nodes

set DataContactTime 50 ;#in Seconds

set TotalNumberOfNodes [expr $NumberOfRSUs + $NumberOfVehicles]

set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# 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) $TotalNumberOfNodes ;# total number of nodes in the scenario (RSUs + Vehicles)
set val(rp) AODV ;# routing protocol

set val(x) [expr $NumberOfRSUs * $DistanceBetweenRSUs + $DistanceBetweenRSUs ] ;# X dimension of topography
set val(y) 200 ;# Y dimension of topography 
set val(stop) 500 ;# time of simulation end


 

Set Default Node Configuration/Parameters

 

$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 ON \
-movementTrace ON

 

Designing an RSU Beacon Protocol by using the simple Message Passing Agent of ns2

 

# all the nodes in which the the RSUBeaconProtocol is running will listen for beacons 
# and call this recv fuction if a RSU Beacon message is received
# All the vehicle nodes will call this function on the event of receiving a becon
Agent/RSUBeaconProtocol instproc recv {source sport size data} {
$self instvar node_ ConnectionFlag_ PreviouslyConnectedRSUID_
global ns RSU_BROADCAST_ADDRESS TransportProtocol sink TrafficGenerator DataContactTime Node CreateNam
set rng [new RNG]
$rng seed 0

# extract message ID from message
set message_id $data
set ThisNodeID [$node_ node-addr]
if {$CreateNam==1} {
$ns trace-annotate "At [$ns now] : Vehicle-Node $ThisNodeID Received a Beacon with message #$message_id#"
puts "At [$ns now] : Vehicle-Node $ThisNodeID Received a Beacon with message #$message_id#"
}
$ns at [expr [$ns now] ] "$node_ color red"
$ns at [expr [$ns now] + 0.1] "$node_ color brown"

#This segment of code is a complex part of work. 
#here the automatic tcp connection/disconnection between a RSU and a vechicle will happen.
if {$ConnectionFlag_==0 } {
# if {$source!=$PreviouslyConnectedRSUID_} {
$ns attach-agent $Node($source) $sink($source)
$ns attach-agent $node_ $TransportProtocol($ThisNodeID)

$ns connect $TransportProtocol($ThisNodeID) $sink($source) 
$self set PreviouslyConnectedRSUID_ $source
$ns at [expr [$ns now] + 0.2] "$node_ color yellow"
$TrafficGenerator($ThisNodeID) start

if {$CreateNam==1} {
$ns trace-annotate "At [$ns now] : Vehicle-Node $ThisNodeID Connected with RSU-$source and Starts pushing data*******DATA*******"
puts "At [$ns now] : Vehicle-Node $ThisNodeID Connected with RSU-$source and Starts pushing data*******DATA*******"
}
$self set ConnectionFlag_ 1

$ns at [expr [$ns now] + $DataContactTime] "$TrafficGenerator($ThisNodeID) stop"

$ns at [expr [$ns now] + $DataContactTime + 0.1] "$ns detach-agent $node_ $TransportProtocol($ThisNodeID)"
$ns at [expr [$ns now] + $DataContactTime + 0.1] "$ns detach-agent $Node($source) $sink($source)"
$ns at [expr [$ns now] + $DataContactTime + 0.1] "$TransportProtocol($ThisNodeID) reset"
$ns at [expr [$ns now] + $DataContactTime + 0.1] "$sink($source) reset"


$ns at [expr [$ns now] + $DataContactTime + 0.11] "$self set ConnectionFlag_ 0"
$ns at [expr [$ns now] + $DataContactTime + 0.11] "$node_ color brown"
if {$CreateNam==1} {
$ns at [expr [$ns now] + $DataContactTime + 0.11] "$ns trace-annotate \"At [$ns now] : Vehicle-Node $ThisNodeID disconnects from RSU-$source and Stops pushing data ...........STOPPED\" "
$ns at [expr [$ns now] + $DataContactTime + 0.11] "puts \"At [$ns now] : Vehicle-Node $ThisNodeID disconnects from RSU-$source and Stops pushing data ...........STOPPED\" "
}
# }
} else {
$ns at [expr [$ns now] + 0.2] "$node_ color yellow"
}


}

Doing Onehop Broadcasts

 

###################################################################################################

# this function actually broadcasts becons (one hop broadcast only)
###################################################################################################
Agent/RSUBeaconProtocol instproc BroadcastRSUBeacon { message_id } {
$self instvar node_
global ns MESSAGE_PORT RSU_BROADCAST_ADDRESS RSU_BEACON_PORT RSU_BROADCAST_MESSAGE_SIZE RSU_BROADCAST_INTERVAL CreateNam
if {$CreateNam==1} {
$ns trace-annotate "At [$ns now] : RSU-Node [$node_ node-addr] Broadcasting Beacon with message #$message_id#"
puts "At [$ns now] : RSU-Node [$node_ node-addr] Broadcasting Beacon with message #$message_id#"
}
$self sendto $RSU_BROADCAST_MESSAGE_SIZE $message_id $RSU_BROADCAST_ADDRESS $RSU_BEACON_PORT

}


 

 

Periodic Broadcast Handling

 

###################################################################################################
# All the RSU units will call this function periodically (in an automated manner) to generate becons
###################################################################################################
Agent/RSUBeaconProtocol instproc ScheduleRSUBeacon { attime message_id } {
$self instvar node_
global ns MESSAGE_PORT RSU_BROADCAST_ADDRESS RSU_BEACON_PORT RSU_BROADCAST_MESSAGE_SIZE RSU_BROADCAST_INTERVAL CreateNam
set rng [new RNG]
if {$CreateNam==1} {
$ns trace-annotate "At [$ns now] : RSU-Node [$node_ node-addr] Sceduling Next Beacon with message #$message_id#"
puts "At [$ns now] : RSU-Node [$node_ node-addr] Sceduling Next Beacon with message #$message_id#"
}
set RSUBroadcastJitter .01 ;# [expr 0.1/ [$rng integer 100 ] ] ;# this jitter is used to maintain a small randomness in communication (in milli/micro seconds]

$ns at [expr [$ns now]+ $RSUBroadcastJitter ] "$self BroadcastRSUBeacon $message_id" 
$ns at [expr [$ns now]+ $RSUBroadcastJitter ] "$node_ color red"
$ns at [expr [$ns now]+ $RSUBroadcastJitter + 0.1] "$node_ color blue"

set NextBeaconTime [ expr [$ns now]+ $RSU_BROADCAST_INTERVAL ]
$ns at $NextBeaconTime "$self ScheduleRSUBeacon $NextBeaconTime $message_id"

}
###################################################################################################

 

 

The RSU Side Protocol Design

 

#create RSUs and place them appropriately
set rng [new RNG]


for {set i 0} {$i < $NumberOfRSUs } { incr i } {
set Node($i) [$ns node]
set RSUBeaconProtocolAgent($i) [new Agent/RSUBeaconProtocol]
$Node($i) attach $RSUBeaconProtocolAgent($i) $RSU_BEACON_PORT

set sink($i) [new Agent/TCPSink]

$ns at 0.0 "$Node($i) label RSU$i"
$Node($i) color blue
$ns at 0.0 "$Node($i) color blue"

$Node($i) set X_ [expr [ expr $i + 1 ] * $DistanceBetweenRSUs] 
$Node($i) set Y_ 150
$Node($i) set Z_ 0
$ns initial_node_pos $Node($i) $SizeOfRSUnode

set RSUBroadcastJitter .01 ;# [expr 0.1 / [expr [$rng integer 100 ] + 1] ] ;# this jitter is used to maintain a small randomness in communication (in milli/micro seconds]

set RSUBeaconStartJitter [$rng integer 5] ;# this jitter is used to start the RSUBeacon with some random gaps in seconds


set BeaconTime [expr [$ns now]+ $RSUBroadcastJitter + 0.01]

$ns at [expr $RSUBeaconStartTime + $RSUBeaconStartJitter ] "$RSUBeaconProtocolAgent($i) ScheduleRSUBeacon $BeaconTime [$Node($i) node-addr]"


}
###################################################################################################

 

 

Simulate Some Road Network and Traffic Scenario

###################################################################################################
#initially, create all the vehicles at a same location - will be move them on the road

for {set i $NumberOfRSUs} {$i < $TotalNumberOfNodes } { incr i } {
set Node($i) [$ns node] 

set RSUBeaconProtocolAgent($i) [new Agent/RSUBeaconProtocol]
$RSUBeaconProtocolAgent($i) set ConnectionFlag_ 0
$RSUBeaconProtocolAgent($i) set PreviouslyConnectedRSUID_ -1

$Node($i) attach $RSUBeaconProtocolAgent($i) $RSU_BEACON_PORT

$Node($i) color gray
$ns at 0.0 "$Node($i) color gray"


$Node($i) set X_ 50 ;#[expr [$rng integer 100] +30 ]
$Node($i) set Y_ 50
$Node($i) set Z_ 0
$ns initial_node_pos $Node($i) $SizeOfVechicleNode ;# # Define initial size in nam

set DestinationXLocation [expr $val(x) -50 ] 

set TraficJitter [$rng integer $TraficJitterSeed] 
#converting the vehicle speed from km/hour in to m/s.
set SpeedinM [expr $SpeedofVechicleNodes * 1000 / 60 / 60 ]

$ns at [expr $TraficStartTime + $TraficJitter ] "$Node($i) setdest $DestinationXLocation 50 $SpeedinM"

$ns at [expr $TraficStartTime + $TraficJitter ] "$Node($NumberOfRSUs) label S=$SpeedinM.m/s"

$ns at [expr $TraficStartTime + $TraficJitter ] "$Node($i) color brown"

# Set a TransportProtocol connection
set TransportProtocol($i) [new Agent/TCP]
#$TransportProtocol($i) set interval_ 0.1
$TransportProtocol($i) set class_ $i



switch $TrafficType {
"ftp" { 

# FTP traffic generator produces maximum available bit rate

set TrafficGenerator($i) [new Application/FTP]
}

"cbr" { 
# CBR traffic generator will produce constant bit rate 
set TrafficGenerator($i) [new Application/Traffic/CBR]
$TrafficGenerator($i) set packetSize_ 256 
$TrafficGenerator($i) set rate_ 100k 
}
"vbr" { 
# Create a Variable Bit Rate traffic source 
set TrafficGenerator($i) [new Application/Traffic/Exponential] 
$TrafficGenerator($i) set packetSize_ 256
$TrafficGenerator($i) set rate_ 100k 
#$TrafficGenerator($i) set burst_time_ 150ms 
}
}

$TrafficGenerator($i) attach-agent $TransportProtocol($i)

} 
###################################################################################################


 

Terminal Outputs During Running the Simulation

The following are the Partial console output of the simulation for parameters (the whole output will be several pages)

-------------------------- Some Important Simulation Parameters----------------------------------
Number Of RSUs : 4
Number Of Vehicles : 5
Speed of Vechicles : 20
TrafficType : cbr
-------------------------------------------------------------------------------------------------

Creating event trace file for Trace Analysis
Creating NAM trace file for visualization
At 6 : RSU-Node 2 Sceduling Next Beacon with message #2#
At 6.0099999999999998 : RSU-Node 2 Broadcasting Beacon with message #2#
At 7 : RSU-Node 1 Sceduling Next Beacon with message #1#
At 7 : RSU-Node 3 Sceduling Next Beacon with message #3#
At 7.0099999999999998 : RSU-Node 1 Broadcasting Beacon with message #1#
At 7.0099999999999998 : RSU-Node 3 Broadcasting Beacon with message #3#
At 8 : RSU-Node 0 Sceduling Next Beacon with message #0#
At 8.0099999999999998 : RSU-Node 0 Broadcasting Beacon with message #0#
At 11 : RSU-Node 2 Sceduling Next Beacon with message #2#
At 11.01 : RSU-Node 2 Broadcasting Beacon with message #2#
At 12 : RSU-Node 1 Sceduling Next Beacon with message #1#
At 12 : RSU-Node 3 Sceduling Next Beacon with message #3#
At 12.01 : RSU-Node 1 Broadcasting Beacon with message #1#
At 12.01 : RSU-Node 3 Broadcasting Beacon with message #3#
At 13 : RSU-Node 0 Sceduling Next Beacon with message #0#
At 13.01 : RSU-Node 0 Broadcasting Beacon with message #0#
At 16 : RSU-Node 2 Sceduling Next Beacon with message #2#
At 16.010000000000002 : RSU-Node 2 Broadcasting Beacon with message #2#
At 17 : RSU-Node 1 Sceduling Next Beacon with message #1#
At 17 : RSU-Node 3 Sceduling Next Beacon with message #3#
At 17.010000000000002 : RSU-Node 1 Broadcasting Beacon with message #1#
At 17.010000000000002 : RSU-Node 3 Broadcasting Beacon with message #3#
At 18 : RSU-Node 0 Sceduling Next Beacon with message #0#
At 18.010000000000002 : RSU-Node 0 Broadcasting Beacon with message #0#
At 21 : RSU-Node 2 Sceduling Next Beacon with message #2#
At 21.010000000000002 : RSU-Node 2 Broadcasting Beacon with message #2#
At 22 : RSU-Node 1 Sceduling Next Beacon with message #1#

....................................

The Actual output will be very long

.....................................

...........STOPPED
At 498 : RSU-Node 0 Sceduling Next Beacon with message #0#
At 498.00999999999999 : RSU-Node 0 Broadcasting Beacon with message #0#
end simulation
num_nodes is set 9
channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
highestAntennaZ_ = 1.5, distCST_ = 550.0



 

The NAM outputs

The VAN Scenario at startup (gray nodes shows inactive nodes – blue nodes are the RSUs)

 

A vehicle starts to move from left (brown vehicle is a moving vehicle)


The RSU will start sending beacons (at the instant of sending a beacon, the RSU node will be in red also, the node which are receiving the beacons also change color to red and become yellow if it will be with in the range of another RSU – this will happen again whenever the vehicle reaches another RSU.

A vehicle is within the range and starts sending data to RSU

 

The Vehicle moved and established connection with another RSU and resume sending data

 

The Graphical Outputs of Some Preliminary Analysis.

Note :  For Getting these kinds of graphs, we have to run the simulation for different parameters and store the generated trace file outputs and then use suitable trace analysis scripts for different metrics and do an elaborate trace analysis. (this part is not explained in this article) – A lot of shell scripts and awk scripts will automate the iterative running, trace analysis and preparation of graphs. 

 

Comparison of Performance with CBR,VBR and FTP Traffic

 

Performance in terms of End to End Delay

 

If we compare cbr, vbr and ftp, the vbr gives lower end to end delay than cbr.

Performance in terms of Throughput 

In terms of throughput, ftp produces higher throughput than cbr and vbr.

 

Summary 

A simple VANET protocol can be designed only using the tcl level features available in ns2. In this way we can simulate any Application layer protocol under ns2.

But there are more advanced ways to simulate realistic VANETS.

For example, we can simulate realistic VANET using SUMO as explained in the following article :

 

Creating Random Road Network, Traffic Flows, Signals in SUMO

Similarly, we can simulate realistic VANET using ns-3 as explained in the following article :

 

Installing ms-van3t – A Multi-Stack VANET Framework for ns-3

 

Chek some othe related article from the following link:

Confusions in doing VANET simulations using DSRC/WAVE under ns-3

VANET Mobility Trace file Generation using Mobisim for ns-2 and ns-3 Simulations

Vehicular Adhoc Network (VANET) Simulation With Veins + Omnet + SUMO

 

 

 

 

Leave a Reply

WhatsApp Discuss Through WhatsApp