Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Calculate Coverage Time in Ns3

To calculate coverage time in ns3, we need to find out the duration of a node that remains within the coverage area in an access point or in base station. This type of metrics mainly used in the situation where mobile having unstable network connection while the nodes are moving in and out of the coverage area.

The following steps will provide the instruction to analyse the coverage time in ns3.

Coverage Time analysis in ns-3

  1. Set Up ns3 Environment:
    • Make sure ns3 is installed on the system.
  2. Create a New ns3 Script:
    • Create a new script file in the scratch directory of ns3, e.g., coverage_time.cc.
  3. Include Necessary Headers:
    • Include the necessary ns3 headers in the script.
  4. Define Network Topology:
    • Set up a network topology with multiple nodes and an AP.
  5. Implement Coverage Time Calculation Logic:
    • Track the time each node remains within the coverage area of the AP.
  6. Run the Simulation:
    • Set the simulation time and run the simulation using Simulator::Run() and Simulator::Destroy().

Example Code:

#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 <vector>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“CoverageTime”);

class CoverageTimeMonitor

{

public:

CoverageTimeMonitor(NodeContainer &apNodes, NodeContainer &staNodes, double range)

: m_apNodes(apNodes), m_staNodes(staNodes), m_range(range)

{

m_coverageStartTime.assign(staNodes.GetN(), Seconds(0));

m_coverageDuration.assign(staNodes.GetN(), Seconds(0));

}

void CheckCoverage(Time interval)

{

Ptr<Node> apNode = m_apNodes.Get(0); // Assuming single AP for simplicity

Ptr<MobilityModel> apMobility = apNode->GetObject<MobilityModel>();

Vector apPosition = apMobility->GetPosition();

for (uint32_t i = 0; i < m_staNodes.GetN(); ++i)

{

Ptr<Node> staNode = m_staNodes.Get(i);

Ptr<MobilityModel> staMobility = staNode->GetObject<MobilityModel>();

Vector staPosition = staMobility->GetPosition();

double distance = CalculateDistance(apPosition, staPosition);

Time now = Simulator::Now();

if (distance <= m_range)

{

if (m_coverageStartTime[i] == Seconds(0))

{

m_coverageStartTime[i] = now;

}

}

else

{

if (m_coverageStartTime[i] != Seconds(0))

{

m_coverageDuration[i] += now – m_coverageStartTime[i];

m_coverageStartTime[i] = Seconds(0);

}

}

}

 

Simulator::Schedule(interval, &CoverageTimeMonitor::CheckCoverage, this, interval);

}

void ReportCoverageTime()

{

Time totalSimulationTime = Simulator::Now();

for (uint32_t i = 0; i < m_staNodes.GetN(); ++i)

{

if (m_coverageStartTime[i] != Seconds(0))

{

m_coverageDuration[i] += totalSimulationTime – m_coverageStartTime[i];

m_coverageStartTime[i] = Seconds(0);

}

std::cout << “Node ” << i << ” coverage time: ” << m_coverageDuration[i].GetSeconds() << ” seconds” << std::endl;

}

}

private:

NodeContainer &m_apNodes;

NodeContainer &m_staNodes;

double m_range;

std::vector<Time> m_coverageStartTime;

std::vector<Time> m_coverageDuration;

};

int main(int argc, char *argv[])

{

uint32_t nStas = 10;  // Number of stations

double range = 50.0;  // Coverage range of the AP

double totalTime = 10.0; // Total simulation time

double checkInterval = 0.1; // Interval to check coverage

CommandLine cmd;

cmd.AddValue(“nStas”, “Number of stations”, nStas);

cmd.AddValue(“range”, “Coverage range of the AP”, range);

cmd.AddValue(“totalTime”, “Total simulation time”, totalTime);

cmd.AddValue(“checkInterval”, “Interval to check coverage”, checkInterval);

cmd.Parse(argc, argv);

NodeContainer wifiStaNodes;

wifiStaNodes.Create(nStas);

NodeContainer wifiApNode;

wifiApNode.Create(1);

 

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

phy.SetChannel(channel.Create());

WifiHelper wifi = WifiHelper::Default();

wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);

WifiMacHelper mac;

Ssid ssid = Ssid(“ns-3-ssid”);

mac.SetType(“ns3::StaWifiMac”, “Ssid”, SsidValue(ssid), “ActiveProbing”, BooleanValue(false));

NetDeviceContainer staDevices = wifi.Install(phy, mac, wifiStaNodes);

mac.SetType(“ns3::ApWifiMac”, “Ssid”, SsidValue(ssid));

NetDeviceContainer apDevice = wifi.Install(phy, mac, wifiApNode);

MobilityHelper mobility;

Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();

positionAlloc->Add(Vector(0.0, 0.0, 0.0));  // Position of the AP

for (uint32_t i = 0; i < nStas; ++i)

{

positionAlloc->Add(Vector(rand() % 100 – 50, rand() % 100 – 50, 0.0));  // Random positions for STAs

}

mobility.SetPositionAllocator(positionAlloc);

mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

mobility.Install(wifiApNode);

mobility.SetMobilityModel(“ns3::RandomWalk2dMobilityModel”,

“Bounds”, RectangleValue(Rectangle(-50, 50, -50, 50)));

mobility.Install(wifiStaNodes);

InternetStackHelper stack;

stack.Install(wifiApNode);

stack.Install(wifiStaNodes);

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer staInterfaces = address.Assign(staDevices);

Ipv4InterfaceContainer apInterface = address.Assign(apDevice);

UdpEchoServerHelper echoServer(9);

ApplicationContainer serverApps = echoServer.Install(wifiApNode.Get(0));

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(totalTime));

UdpEchoClientHelper echoClient(apInterface.GetAddress(0), 9);

echoClient.SetAttribute(“MaxPackets”, UintegerValue(1));

echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));

ApplicationContainer clientApps = echoClient.Install(wifiStaNodes);

clientApps.Start(Seconds(2.0));

clientApps.Stop(Seconds(totalTime));

CoverageTimeMonitor monitor(wifiApNode, wifiStaNodes, range);

monitor.CheckCoverage(Seconds(checkInterval));

Simulator::Stop(Seconds(totalTime));

Simulator::Run();

monitor.ReportCoverageTime();

Simulator::Destroy();

return 0;

}

Explanation:

  1. Nodes and Links:
    • Created nodes for the access point (AP) and multiple stations (STA).
    • Configured the Wi-Fi channel, physical layer, and MAC layer for the AP and STA nodes.
  2. Mobility:
    • Set the AP node’s position to a constant location.
    • Set the STA nodes’ mobility to random walk within a bounded area.
  3. Applications:
    • Installed a UDP echo server on the AP node.
    • Installed a UDP echo client on the STA nodes to generate traffic.
  4. Coverage Time Calculation Logic:
    • Created a CoverageTimeMonitor class to track the time each STA node remains within the coverage area of the AP.
    • The CheckCoverage method checks if each STA node is within the specified range of the AP and tracks the coverage time.
    • The ReportCoverageTime method calculates and prints the total coverage time for each STA node.
  5. Running the Simulation:
    • The simulation runs with the STA nodes which are moving in the bounded area, and the coverage time is calculated and printed at the end.

At last, the coverage time is calculated by determining the duration between the coverage area access point or base station by using STA nodes to generate traffic.

Once you share with us your parameters we provide you with best network comparison analysis  for all STA nodes to generate traffic.