change direction and velocity of nodes in NS3

851 views
Skip to first unread message

Hiba Yousef

unread,
Feb 19, 2018, 12:07:46 PM2/19/18
to ns-3-users
Dear all,
Since I am quite new is NS3,  please help me setting the following sinareo:
I am setting a wifi node with constant velocity :
 ``   MobilityHelper mobility;

  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                                 "MinX", DoubleValue (0.0),
                                 "MinY", DoubleValue (0.0),
                                 "DeltaX", DoubleValue (5.0),
                                 "DeltaY", DoubleValue (10.0),
                                 "GridWidth", UintegerValue (3),
                                 "LayoutType", StringValue ("RowFirst"));

  mobility.SetMobilityModel ("ns3::ConstantVelocityMobilityModel");
  mobility.Install (wifiStaNodes);

  Ptr<ConstantVelocityMobilityModel> mob = wifiStaNodes.Get(0)-> GetObject<ConstantVelocityMobilityModel>();
            mob->SetVelocity(Vector(20, 0.0, 0.0));  ``

I am using NS3 in realtime emulation, I want this wifi node to keep moving towards and backwards after some distance with different speed (for all the realtime session).
To emulate a mobile node.
Can you help me please.
Thanks in advance !

pdbarnes

unread,
Feb 19, 2018, 1:53:32 PM2/19/18
to ns-3-users
Sounds like you could use a WaypointMobilityModel and dynamically add new waypoints, using the CourseChangeCallback. This Callback will be invoked at the time of the Waypoint. Your function hooked to that callback can add a new Waypoint,

Peter

Hiba Yousef

unread,
Feb 20, 2018, 4:00:50 AM2/20/18
to ns-3-users
I am trying with that, thanks so much

Hiba Yousef

unread,
Feb 20, 2018, 10:35:53 AM2/20/18
to ns-3-users
What I am thinking to do now is to keep increasing the simulation time and change the velocity every 3seconds for example. Can You please tell me how to keep changing the time inside the main time ?! In this case I just get tNow = 0;
and doesnt update with the simulation .
Thanks in advance.

double tNow = 0.001*Simulator::Now().GetMilliSeconds();
  double tNxt = 0;
  double constVelocity1 = 25;
  double constVelocity2 = -10 ;

if (tNow >= tNxt) {
      tNxt = tNow + 3.0;
      if (int (tNxt)%2 > 0) {
          Simulator::Schedule (Seconds (tNxt), &SetVelocity, wifiStaNodes.Get (0),Vector (constVelocity2,0.0,0.0));
      }
      else {
          Simulator::Schedule (Seconds (tNxt), &SetVelocity, wifiStaNodes.Get (0),Vector (constVelocity1,0.0,0.0));
      }
  }


On Tuesday, February 20, 2018 at 10:00:50 AM UTC+1, Hiba Yousef wrote:

pdbarnes

unread,
Feb 21, 2018, 9:21:55 AM2/21/18
to ns-3-users
You need to refactor in to a function UpdateVelocity(Ptr<Node> node0)
which does two things:

1. Set the new velocity:
node0->SetVelocity (Vector (...));

2. Schedule itself for the next update time:
Schedule (Seconds (3), UpdateVelocity, node0);

Notice that you don’t compute the absolute time; you just schedule for 3s in the future.

Now, how do you get this to run the first time? You schedule the first execution of this function in your main, before launching the simulator with Simulator::Run(). This call looks almost like step 2 above:

Schedule (Seconds (3), UpdateVelocity, wifiStaNodes.Get (0));

P

Hiba Yousef

unread,
Feb 21, 2018, 10:35:47 AM2/21/18
to ns-3-users
That was what I need, I added this function :

static void
UpdateVelocity(Ptr<Node> node0) {

    Ptr <ConstantVelocityMobilityModel> mobility = node0 -> GetObject<ConstantVelocityMobilityModel>();
    Vector velocity = mobility -> GetVelocity() ;
    if (velocity.x > 0 )
        mobility->SetVelocity (Vector (-20,0.0,0.0));
    else
        mobility->SetVelocity (Vector (+20,0.0,0.0));

    Simulator::Schedule (Seconds (3), UpdateVelocity, node0);
}

I can now calculate the time to reach exactly each point and repkace it with. Thanks so much for your help.
Reply all
Reply to author
Forward
0 new messages