Sonu Mishra
Sonu Mishra

Reputation: 1779

NS-3: How to change node position during simulation?

I am trying to model in NS-3 some nodes moving with constant vertical velocity inside a rectangle.

Problem: I have to add the feature that, when a node goes beyond y=2500, its x-position abruptly changes to its x-position + 257. This is what I have tried so far:

NodeContainer nodes;
nodes.Create (25);
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                             "MinX", DoubleValue (0.0),
                             "MinY", DoubleValue (0.0),
                             "DeltaX", DoubleValue (500),
                             "DeltaY", DoubleValue (500),
                             "GridWidth", UintegerValue (5),
                             "LayoutType", StringValue ("RowFirst"));

mobility.SetMobilityModel ("ns3::ConstantVelocityMobilityModel");
mobility.Install (nodes);
for (uint n=0 ; n < nodes.GetN() ; n++)
 {
    Ptr<ConstantVelocityMobilityModel> mob = nodes.Get(n)->GetObject<ConstantVelocityMobilityModel>();
    mob->SetVelocity(Vector(0, 10, 0));        
 }
for (uint n=0 ; n < satellites.GetN() ; n++)
 {
    Ptr<ConstantVelocityMobilityModel> cvMob = satellites.Get(n)->GetObject<ConstantVelocityMobilityModel>();
    Ptr<MobilityModel> mob = satellites.Get(n)->GetObject<MobilityModel>();
    Vector m_position = mob->GetPosition();
    Vector m_velocity = mob->GetVelocity();
    if (m_position.y > 2500) 
        {
           m_position.x += 257;
           m_velocity.y *= -1;
           cvMob->SetVelocity(m_velocity);
           mob->SetPosition(m_position);
        }
 }

This last for loop is not working at all! How should I implement the feature into the current script?

PS: Since I am new to NS-3, I do not want to risk modifying any NS-3 source file.

Upvotes: 2

Views: 4563

Answers (1)

Konstantinos
Konstantinos

Reputation: 556

what you need to do is to move the content of your second loop in a 'scheduled' method (e.g. CheckBound).

void CheckBound(Ptr< ConstantVelocityMobilityModel > mob ){
    Vector m_position = mob->GetPosition();
    Vector m_velocity = mob->GetVelocity();
    if (m_position.y > 2500) 
        {
           m_position.x += 257;
           m_velocity.y *= -1;
           mob->SetVelocity(m_velocity);
           mob->SetPosition(m_position);
        }
}

Then in your main you need to estimate when this has to be called. Doing simple math calculations, we estimate the Dy which is the distance to your 2500 limit and the time it is required Dt = Dy/speed. e.g.

for (uint n=0 ; n < satellites.GetN() ; n++)
 {
    Ptr<ConstantVelocityMobilityModel> cvMob = satellites.Get(n)->GetObject<ConstantVelocityMobilityModel>();
    Vector m_position = cvMob->GetPosition();
    Vector m_speed = cvMob->GetVelocity();
    double Dy = 2500 - m_position.y;
    double Dt = Dy/m_speed.y; 
    Simulator::Schedule(Seconds(Dt), MakeCallback(&CheckBound, cvMob));
 }

Upvotes: 1

Related Questions