In Vehicular Ad-hoc Networks (VANETs), it is a one of the types of mobile ad-hoc network that enables the interaction among vehicles and roadside infrastructure; it also offers the modules and supports the simulation in VANET.
Here are the sample projects using ns3 tool for VANET execution:
Example 1: Basic VANET Communication
The given below illustrates the simple communication among the vehicles using WiFi (802.11p) in a VANET.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
#include “ns3/vanetmobility-helper.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“VanetExample”);
int main (int argc, char *argv[])
{
// Configure command line parameters
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (10); // Create 10 vehicle nodes
// Configure the wireless channel and PHY layer
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
// Configure the WiFi standard and MAC layer for 802.11p (WAVE)
Wifi80211pHelper wifi80211p = Wifi80211pHelper::Default ();
NqosWaveMacHelper wifiMac = NqosWaveMacHelper::Default ();
NetDeviceContainer devices = wifi80211p.Install (phy, wifiMac, nodes);
// Install the internet stack
InternetStackHelper stack;
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Configure mobility
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (10.0),
“DeltaY”, DoubleValue (10.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantVelocityMobilityModel”);
mobility.Install (nodes);
for (uint32_t i = 0; i < nodes.GetN (); ++i)
{
Ptr<ConstantVelocityMobilityModel> cvmm = nodes.Get (i)->GetObject<ConstantVelocityMobilityModel> ();
cvmm->SetVelocity (Vector (20.0, 0.0, 0.0)); // Set velocity (e.g., 20 m/s)
}
// Set up applications (e.g., UDP echo server and client)
uint16_t port = 9; // Port number for applications
// Install UDP Echo Server on the first vehicle
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Install UDP Echo Clients on other vehicles to communicate with the first vehicle
for (uint32_t i = 1; i < nodes.GetN (); ++i)
{
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), port);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (i));
clientApps.Start (Seconds (2.0 + i)); // Stagger start times
clientApps.Stop (Seconds (10.0));
}
// Run simulation
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Example 2: VANET with Traffic Lights
Here, we provide the sample demonstration on how to simulate the VANET where vehicles communicate with traffic lights to optimize traffic flow.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
#include “ns3/vanetmobility-helper.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“VanetTrafficLightExample”);
int main (int argc, char *argv[])
{
// Configure command line parameters
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer vehicleNodes, trafficLightNodes;
vehicleNodes.Create (10); // Create 10 vehicle nodes
trafficLightNodes.Create (2); // Create 2 traffic light nodes
// Configure the wireless channel and PHY layer
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
// Configure the WiFi standard and MAC layer for 802.11p (WAVE)
Wifi80211pHelper wifi80211p = Wifi80211pHelper::Default ();
NqosWaveMacHelper wifiMac = NqosWaveMacHelper::Default ();
NetDeviceContainer vehicleDevices = wifi80211p.Install (phy, wifiMac, vehicleNodes);
NetDeviceContainer trafficLightDevices = wifi80211p.Install (phy, wifiMac, trafficLightNodes);
// Install the internet stack
InternetStackHelper stack;
stack.Install (vehicleNodes);
stack.Install (trafficLightNodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer vehicleInterfaces = address.Assign (vehicleDevices);
address.SetBase (“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer trafficLightInterfaces = address.Assign (trafficLightDevices);
// Configure mobility for vehicles
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (10.0),
“DeltaY”, DoubleValue (10.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantVelocityMobilityModel”);
mobility.Install (vehicleNodes);
for (uint32_t i = 0; i < vehicleNodes.GetN (); ++i)
{
Ptr<ConstantVelocityMobilityModel> cvmm = vehicleNodes.Get (i)->GetObject<ConstantVelocityMobilityModel> ();
cvmm->SetVelocity (Vector (20.0, 0.0, 0.0)); // Set velocity (e.g., 20 m/s)
}
// Configure mobility for traffic lights
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (trafficLightNodes);
trafficLightNodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (50.0, 50.0, 0.0));
trafficLightNodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (150.0, 50.0, 0.0));
// Set up applications (e.g., UDP echo server and client)
uint16_t port = 9; // Port number for applications
// Install UDP Echo Server on the first traffic light
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApps = echoServer.Install (trafficLightNodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Install UDP Echo Clients on vehicles to communicate with the first traffic light
for (uint32_t i = 0; i < vehicleNodes.GetN (); ++i)
{
UdpEchoClientHelper echoClient (trafficLightInterfaces.GetAddress (0), port);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (vehicleNodes.Get (i));
clientApps.Start (Seconds (2.0 + i)); // Stagger start times
clientApps.Stop (Seconds (10.0));
}
// Run simulation
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Example 3: VANET with Roadside Units (RSUs)
The shown below are the sample simulation in VANET where vehicles communicate with roadside units (RSUs) to get traffic information.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
#include “ns3/vanetmobility-helper.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“VanetRsuExample”);
int main (int argc, char *argv[])
{
// Configure command line parameters
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer vehicleNodes, rsuNodes;
vehicleNodes.Create (10); // Create 10 vehicle nodes
rsuNodes.Create (2); // Create 2 RSU nodes
// Configure the wireless channel and PHY layer
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
// Configure the WiFi standard and MAC layer for 802.11p (WAVE)
Wifi80211pHelper wifi80211p = Wifi80211pHelper::Default ();
NqosWaveMacHelper wifiMac = NqosWaveMacHelper::Default ();
NetDeviceContainer vehicleDevices = wifi80211p.Install (phy, wifiMac, vehicleNodes);
NetDeviceContainer rsuDevices = wifi80211p.Install (phy, wifiMac, rsuNodes);
// Install the internet stack
InternetStackHelper stack;
stack.Install (vehicleNodes);
stack.Install (rsuNodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer vehicleInterfaces = address.Assign (vehicleDevices);
address.SetBase (“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer rsuInterfaces = address.Assign (rsuDevices);
// Configure mobility for vehicles
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (10.0),
“DeltaY”, DoubleValue (10.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantVelocityMobilityModel”);
mobility.Install (vehicleNodes);
for (uint32_t i = 0; i < vehicleNodes.GetN (); ++i)
{
Ptr<ConstantVelocityMobilityModel> cvmm = vehicleNodes.Get (i)->GetObject<ConstantVelocityMobilityModel> ();
cvmm->SetVelocity (Vector (20.0, 0.0, 0.0)); // Set velocity (e.g., 20 m/s)
}
// Configure mobility for RSUs
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (rsuNodes);
rsuNodes.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (50.0, 50.0, 0.0));
rsuNodes.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (150.0, 50.0, 0.0));
// Set up applications (e.g., UDP echo server and client)
uint16_t port = 9; // Port number for applications
// Install UDP Echo Server on the first RSU
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApps = echoServer.Install (rsuNodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Install UDP Echo Clients on vehicles to communicate with the first RSU
for (uint32_t i = 0; i < vehicleNodes.GetN (); ++i)
{
UdpEchoClientHelper echoClient (rsuInterfaces.GetAddress (0), port);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (vehicleNodes.Get (i));
clientApps.Start (Seconds (2.0 + i)); // Stagger start times
clientApps.Stop (Seconds (10.0));
}
// Run simulation
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Explanation of the Examples
- Basic VANET Communication: Establishes basic communication among vehicles using WiFi (802.11p). Each vehicle is set up with a constant velocity mobility model, and UDP echo applications are used to simulate communication between the vehicles.
- VANET with Traffic Lights: Simulates a VANET where vehicles communicate with traffic lights to optimize traffic flow. Traffic lights are set up as static nodes, and vehicles are configured to communicate with the traffic lights using UDP echo applications.
- VANET with Roadside Units (RSUs): Simulates a VANET where vehicles communicate with roadside units (RSUs) to get traffic information. RSUs are set up as static nodes, and vehicles are configured to communicate with the RSUs using UDP echo applications.
In the end, we had clearly learned how the example of VANET environment simulates in ns3 tool. We will give further outline information for VANET implementation examples.
Vehicular Ad-hoc Networks (VANETs), thesis and all types of project support are provided by us so feel free to address with us all your reasech issues.