How to Implement VANET in ns2
To implement a Vehicular Ad-Hoc Network (VANET) in Network Simulator 2 (ns2) has needs to emulate the network of vehicles that interacts with each other and probably with roadside infrastructure. VANETs are a kind of Mobile Ad-Hoc Network (MANET) in which the nodes (vehicles) are highly mobile and have a particular movement patterns usually along roads. The given below are the procedures to implement a basic VANET in ns2:
Step-by-Step Implementation:
Step 1: Conceptualize the VANET Simulation
In a VANET, you have:
- Vehicles as mobile nodes: These signify the cars or other vehicles that move along predefined paths.
- Roadside Units (RSUs): Stationary nodes signify the communication points along the road.
- Communication: Vehicles interact with each other (Vehicle-to-Vehicle or V2V) and with RSUs (Vehicle-to-Infrastructure or V2I).
Step 2: Create the Tcl Script
The given below is an example Tcl script that emulates the simple VANET scenario with vehicles moving along a predefined path and interacting with each other and with RSUs.
Example Tcl Script for Simulating a VANET
# Create a simulator object
set ns [new Simulator]
# Define the topography object
set topo [new Topography]
$topo load_flatgrid 2000 2000 # Define a 2km x 2km area
# Create the General Operations Director (GOD) for wireless simulations
create-god 5 # Number of nodes (4 vehicles + 1 RSU)
# Configure the mobile nodes (vehicles) and RSU
$ns node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON
# Open trace and NAM files for recording the simulation
set tracefile [open vanet_out.tr w]
$ns trace-all $tracefile
set namfile [open vanet_out.nam w]
$ns namtrace-all-wireless $namfile 2000 2000
# Define a finish procedure to close files and end the simulation
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam vanet_out.nam &
exit 0
}
# Create vehicle nodes
set veh0 [$ns node]
set veh1 [$ns node]
set veh2 [$ns node]
set veh3 [$ns node]
# Create a Roadside Unit (RSU) node
set rsu [$ns node]
# Set initial positions for vehicles (starting points)
$veh0 set X_ 100.0
$veh0 set Y_ 1000.0
$veh0 set Z_ 0.0
$veh1 set X_ 200.0
$veh1 set Y_ 1000.0
$veh1 set Z_ 0.0
$veh2 set X_ 300.0
$veh2 set Y_ 1000.0
$veh2 set Z_ 0.0
$veh3 set X_ 400.0
$veh3 set Y_ 1000.0
$veh3 set Z_ 0.0
# Set the position of the RSU (stationary)
$rsu set X_ 1000.0
$rsu set Y_ 1000.0
$rsu set Z_ 0.0
# Define vehicle movement (along a straight road, for simplicity)
$ns at 1.0 “$veh0 setdest 1800.0 1000.0 20.0”
$ns at 1.0 “$veh1 setdest 1800.0 1000.0 25.0”
$ns at 1.0 “$veh2 setdest 1800.0 1000.0 30.0”
$ns at 1.0 “$veh3 setdest 1800.0 1000.0 35.0”
# Define traffic between vehicles and the RSU
# V2V Communication (Vehicle 0 to Vehicle 3)
set tcp_veh0 [new Agent/TCP]
$ns attach-agent $veh0 $tcp_veh0
set sink_veh3 [new Agent/TCPSink]
$ns attach-agent $veh3 $sink_veh3
$ns connect $tcp_veh0 $sink_veh3
set ftp_veh0 [new Application/FTP]
$ftp_veh0 attach-agent $tcp_veh0
$ftp_veh0 start
# V2I Communication (Vehicle 1 to RSU)
set tcp_veh1 [new Agent/TCP]
$ns attach-agent $veh1 $tcp_veh1
set sink_rsu [new Agent/TCPSink]
$ns attach-agent $rsu $sink_rsu
$ns connect $tcp_veh1 $sink_rsu
set ftp_veh1 [new Application/FTP]
$ftp_veh1 attach-agent $tcp_veh1
$ftp_veh1 start
# Schedule the end of the simulation
$ns at 40.0 “finish”
# Run the simulation
$ns run
Step 3: Run the Tcl Script
Save the script with a .tcl extension, for instance, vanet_simulation.tcl. Then, execute the script using the following command in terminal:
ns vanet_simulation.tcl
Step 4: Visualize the VANET Simulation
To visualize the network topology and traffic, open the generated NAM file using:
nam vanet_out.nam
Script Explanation
- Vehicles (veh0, veh1, veh2, veh3): These nodes signify the vehicles moving along a road. The positions and movements emulated the vehicles driving along a straight path.
- Roadside Unit (RSU): The RSU node is stationary and acts as an infrastructure point for vehicle-to-infrastructure communication.
- Vehicle Movements: The setdest commands to mimic the movement of vehicles together with predefined path (straight road) with diverse speeds.
- Traffic Definition:
- V2V Communication: TCP traffic between two vehicles (veh0 and veh3).
- V2I Communication: TCP traffic among a vehicle (veh1) and the RSU.
Customization
- Complex Mobility Models: Execute more realistic mobility patterns using tools such as Simulation of Urban Mobility (SUMO) and incorporates them with ns2 for more complex VANET scenarios.
- More Vehicles and RSUs: Add more vehicles and RSUs to emulate the larger and more complex VANET environments.
- Different Routing Protocols: Test with numerous routing protocols like DSR, TORA to assess their performance in a VANET scenario.
- Inter-Vehicle Communication: Mimic various kinds of communication such as broadcasting safety messages to reflect the variations of data exchanged in VANETs.
- Network Performance Metrics: Execute the scripts to gather and evaluate the parameters such as throughput, delay, and packet delivery ratio.
Limitations
- Simplified Mobility: The example uses a basic straight-line movement, which doesn’t capture the complexity of real-world vehicular movement patterns.
- No Integration with Traffic Simulation: This sample does not combined with traffic simulators like SUMO, which can create more realistic vehicle movements and traffic scenarios.
- Lack of VANET-Specific Protocols: ns2 does not support directly for VANET-specific protocols such as Dedicated Short-Range Communications (DSRC). The simulation uses general-purpose ad-hoc routing protocols such as AODV.
In this page, we gathers the information on how to execute and deploy the vehicular ad-hoc network using the tool of ns2 simulation and we plan to provide the more information about the vehicular ad-hoc network.
Our developers have structured the implementation of VANET in ns2, utilizing top-notch tools and resources to ensure high-quality results for your projects. Feel free to reach out to us for further guidance or to have your project analyzed by our experts!