How to set up a moving node by constant velocity model

1,173 views
Skip to first unread message

曹以喆

unread,
Apr 6, 2015, 3:53:31 PM4/6/15
to ns-3-...@googlegroups.com
Hi, all. I want to set up a node that moves between two positions back and forth at a constant speed.
I have no idea how to use the constant velocity model to implement this. Can anyone help me?

Thanks!

曹以喆

unread,
Apr 6, 2015, 4:31:29 PM4/6/15
to ns-3-...@googlegroups.com
  I want to create a node moving between (1500,0) and (1500,1200) at a speed of 20m/s.
  This is what I have tried, 
  
  Ptr<Node> c2 = CreateObject<Node>();
  MobilityHelper mobility_sink;
  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  positionAlloc->Add (Vector (1500.0, 0.0, 0.0));
  positionAlloc->Add (Vector (1500.0, 1200.0, 0.0));
  mobility_sink.SetPositionAllocator (positionAlloc);
  mobility_sink.SetMobilityModel ("ns3::ConstantVelocityMobilityModel");
  mobility_sink.Install (c2);
  c2 -> GetObject<ConstantVelocityMobilityModel>() -> SetVelocity(Vector(0.0, 20.0, 0.0));  

  But it seems that it can only go one direction. How to achieve going back and forth continuously?

Konstantinos

unread,
Apr 6, 2015, 4:56:37 PM4/6/15
to ns-3-...@googlegroups.com
Hi,

Please read the ns-3 documentation.
The Position Allocator that you added the two points is only used to set initial positions of the nodes.
The constant velocity model, as you found out sets a constant velocity of a node towards one direction (defined by the speed vector).

Now what you need to implement is using the contant mobility and callback framework of NS-3, identify when the node reaches the second point, 'revert' the speed and vice versa.
A pseudocode:

1) Start ConstantMobility from A towards B with speed V.
2) Schedule an event at time = (calculate when we reach B) to SetVelocity = -V towards A
3) When that event fires, it means you have reached B, so you need to schedule again another event similar to (2) for the time that the node will reach A.

----

Another option, is to use the waypoint mobility model and put the waypoints but again for this you need to pre-calculate the timing for each waypoint based on your desired velocity.


Regards,
K.

Tommaso Pecorella

unread,
Apr 6, 2015, 5:05:11 PM4/6/15
to ns-3-...@googlegroups.com
Hi,

this problem has been discussed many times before. Please search the forum.

As a general hint, you'll have to do some math. No trace or event will be actually fired when the node will reach a specified point. What you have to do is to do it "manually" through a loop. I.e., start the movement, you know where the node is, its speed, you calc when it will be in the target position and you schedule an event for that time. When the event is fired, you reverse the direction and you fire the same event. Simple.

Cheers,

T.

Hiba Yousef

unread,
Feb 20, 2018, 12:04:04 PM2/20/18
to ns-3-users
I have the same problem, can you please clarify more with an example since I am quite new with NS3 and I want to use that in emulation. how to keep revert the direction in background in real time ?!!
Thanks in advance
Reply all
Reply to author
Forward
0 new messages