To Implement a Vehicular Ad-Hoc Network (VANET) in ns-3 by setting up a network of vehicles that can communicate with each other using wireless communication protocols. This guide we will help you to create a basic VANET simulation in ns-3 using WiFi or WiFi 802.11p (WAVE) for communication.
Prerequisites
- ns-3 installed on your system.
- Basic understanding of ns-3 and C++ programming.
Step-by-step to Implement a VANET in ns-3
- Install ns-3: Ensure you have ns-3 installed. You can download it from the official website and follow the installation instructions.
wget https://www.nsnam.org/release/ns-allinone-3.xx.tar.bz2
tar -xjf ns-allinone-3.xx.tar.bz2
cd ns-allinone-3.xx
./build.py –enable-examples –enable-tests
Include Necessary Headers: Include the required headers for network modules, mobility, internet, and WAVE.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/mobility-module.h”
#include “ns3/internet-module.h”
#include “ns3/wave-module.h”
Create Network Topology: Create nodes representing vehicles.
NodeContainer vehicles;
vehicles.Create(10); // Create 10 vehicle nodes
Set Up Mobility Model: Configure the mobility model to simulate vehicle movement.
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, DoubleValue(0.0),
“MinY”, DoubleValue(0.0),
“DeltaX”, DoubleValue(5.0),
“DeltaY”, DoubleValue(10.0),
“GridWidth”, UintegerValue(5),
“LayoutType”, StringValue(“RowFirst”));
mobility.SetMobilityModel(“ns3::ConstantVelocityMobilityModel”);
mobility.Install(vehicles);
for (uint32_t i = 0; i < vehicles.GetN(); ++i) {
Ptr<ConstantVelocityMobilityModel>mob=vehicles.Get(i)->GetObject<ConstantVelocityMobilityModel>();
mob->SetVelocity(Vector(20, 0, 0)); // Set constant speed for each vehicle
}
Install WAVE Devices: Install WAVE devices on the vehicles for communication.
YansWifiChannelHelper waveChannel = YansWifiChannelHelper::Default();
YansWifiPhyHelper wavePhy = YansWifiPhyHelper::Default();
wavePhy.SetChannel(waveChannel.Create());
NqosWaveMacHelper waveMac = NqosWaveMacHelper::Default();
WaveHelper waveHelper = WaveHelper::Default();
NetDeviceContainer waveDevices = waveHelper.Install(wavePhy, waveMac, vehicles);
Install Internet Stack: Install the internet stack on the vehicle nodes.
InternetStackHelper internet;
internet.Install(vehicles);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(waveDevices);
Create Applications: Create and install applications to simulate data exchange between vehicles.
uint16_t port = 9; // Port number for the application
OnOffHelperonoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(interfaces.GetAddress(1), port)));
onoff.SetConstantRate(DataRate(“500kb/s”));
ApplicationContainer app = onoff.Install(vehicles.Get(0));
app.Start(Seconds(1.0));
app.Stop(Seconds(10.0));
PacketSinkHelpersink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
app = sink.Install(vehicles.Get(1));
app.Start(Seconds(0.0));
app.Stop(Seconds(10.0));
Run the Simulation: Finally, run the simulation and clean up.
Simulator::Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
Example Complete Script
Below is an example complete script for setting up a basic VANET in ns-3:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/mobility-module.h”
#include “ns3/internet-module.h”
#include “ns3/wave-module.h”
using namespace ns3;
int main(int argc, char *argv[])
{
NodeContainer vehicles;
vehicles.Create(10); // Create 10 vehicle nodes
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, DoubleValue(0.0),
“MinY”, DoubleValue(0.0),
“DeltaX”, DoubleValue(5.0),
“DeltaY”, DoubleValue(10.0),
“GridWidth”, UintegerValue(5),
“LayoutType”, StringValue(“RowFirst”));
mobility.SetMobilityModel(“ns3::ConstantVelocityMobilityModel”);
mobility.Install(vehicles);
for (uint32_t i = 0; i < vehicles.GetN(); ++i) {
Ptr<ConstantVelocityMobilityModel>mob=vehicles.Get(i)->GetObject<ConstantVelocityMobilityModel>();
mob->SetVelocity(Vector(20, 0, 0)); // Set constant speed for each vehicle
}
YansWifiChannelHelper waveChannel = YansWifiChannelHelper::Default();
YansWifiPhyHelper wavePhy = YansWifiPhyHelper::Default();
wavePhy.SetChannel(waveChannel.Create());
NqosWaveMacHelper waveMac = NqosWaveMacHelper::Default();
WaveHelper waveHelper = WaveHelper::Default();
NetDeviceContainer waveDevices = waveHelper.Install(wavePhy, waveMac, vehicles);
InternetStackHelper internet;
internet.Install(vehicles);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(waveDevices);
uint16_t port = 9; // Port number for the application
OnOffHelperonoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(interfaces.GetAddress(1), port)));
onoff.SetConstantRate(DataRate(“500kb/s”));
ApplicationContainer app = onoff.Install(vehicles.Get(0));
app.Start(Seconds(1.0));
app.Stop(Seconds(10.0));
PacketSinkHelpersink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
app = sink.Install(vehicles.Get(1));
app.Start(Seconds(0.0));
app.Stop(Seconds(10.0));
Simulator::Stop(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Running the Script
Compile and run the script using the following commands:
./waf configure –enable-examples
./waf build
./waf –run <script-name>
Atlast, implementing a Vehicular Ad-Hoc Network (VANET) in ns-3 is described by step by step. And further we support all kind of advanced VANET projects.