1.. include:: replace.txt
2
3.. _Mobility:
4
5Mobility
6--------
7
8.. heading hierarchy:
9   ------------- Chapter
10   ************* Section (#.#)
11   ============= Subsection (#.#.#)
12   ############# Paragraph (no number)
13
14The mobility support in |ns3| includes:
15
16- a set of mobility models which are used to track and maintain the *current* cartesian position and speed of an object.
17- a "course change notifier" trace source which can be used to register listeners to the course changes of a mobility model
18- a number of helper classes which are used to place nodes and setup mobility models (including parsers for some mobility definition formats).
19
20Model Description
21*****************
22
23The source code for mobility lives in the directory ``src/mobility``.
24
25Design
26======
27
28The design includes mobility models, position allocators, and helper
29functions.
30
31In |ns3|, `MobilityModel`` objects track the evolution of position
32with respect to a (cartesian) coordinate system.  The mobility model
33is typically aggregated to an ``ns3::Node`` object and queried using
34``GetObject<MobilityModel> ()``. The base class ``ns3::MobilityModel``
35is subclassed for different motion behaviors.
36
37The initial position of objects is typically set with a PositionAllocator.
38These types of objects will lay out the position on a notional canvas.
39Once the simulation starts, the position allocator may no longer be
40used, or it may be used to pick future mobility "waypoints" for such
41mobility models.
42
43Most users interact with the mobility system using mobility helper
44classes.  The MobilityHelper combines a mobility model and position
45allocator, and can be used with a node container to install a similar
46mobility capability on a set of nodes.
47
48We first describe the coordinate system and issues
49surrounding multiple coordinate systems.
50
51Coordinate system
52#################
53
54There are many possible coordinate systems and possible translations between
55them.  |ns3| uses the Cartesian coordinate system only, at present.
56
57The question has arisen as to how to use the mobility models (supporting
58Cartesian coordinates) with different coordinate systems.  This is possible
59if the user performs conversion between the |ns3| Cartesian and the
60other coordinate system.  One possible library to assist is
61the `proj4 <https://proj.org>`_ library for projections and reverse
62projections.
63
64If we support converting between coordinate systems, we must adopt a
65reference.  It has been suggested to use the geocentric Cartesian coordinate
66system as a reference.  Contributions are welcome in this regard.
67
68The question has arisen about adding a new mobility model whose motion
69is natively implemented in a different coordinate system (such as an
70orbital mobility model implemented using spherical coordinate system).
71We advise to create a subclass with the APIs desired
72(such as Get/SetSphericalPosition), and new position allocators, and
73implement the motion however desired, but must also support the conversion to
74cartesian (by supporting the cartesian Get/SetPosition).
75
76Coordinates
77###########
78
79The base class for a coordinate is called ``ns3::Vector``.  While
80positions are normally described as coordinates and not vectors in
81the literature, it is possible to reuse the same data structure to
82represent position (x,y,z) and velocity (magnitude and direction
83from the current position).  |ns3| uses class Vector for both.
84
85There are also some additional related structures used to support
86mobility models.
87
88- Rectangle
89- Box
90- Waypoint
91
92MobilityModel
93#############
94
95Describe base class
96
97- GetPosition ()
98- Position and Velocity attributes
99- GetDistanceFrom ()
100- CourseChangeNotification
101
102MobilityModel Subclasses
103########################
104
105- ConstantPosition
106- ConstantVelocity
107- ConstantAcceleration
108- GaussMarkov
109- Hierarchical
110- RandomDirection2D
111- RandomWalk2D
112- RandomWaypoint
113- SteadyStateRandomWaypoint
114- Waypoint
115
116PositionAllocator
117#################
118
119Position allocators usually used only at beginning, to lay out the nodes
120initial position.  However, some mobility models (e.g. RandomWaypoint) will
121use a position allocator to pick new waypoints.
122
123- ListPositionAllocator
124- GridPositionAllocator
125- RandomRectanglePositionAllocator
126- RandomBoxPositionAllocator
127- RandomDiscPositionAllocator
128- UniformDiscPositionAllocator
129
130Helper
131######
132
133A special mobility helper is provided that is mainly aimed at supporting
134the installation of mobility to a Node container (when using containers
135at the helper API level).  The MobilityHelper class encapsulates
136a MobilityModel factory object and a PositionAllocator used for
137initial node layout.
138
139Group mobility is also configurable via a GroupMobilityHelper object.
140Group mobility reuses the HierarchicalMobilityModel allowing one to
141define a reference (parent) mobility model and child (member) mobility
142models, with the position being the vector sum of the two mobility
143model positions (i.e., the child position is defined as an offset to
144the parent position).  In the GroupMobilityHelper, the parent mobility
145model is not associated with any node, and is used as the parent mobility
146model for all (distinct) child mobility models.  The reference point group
147mobility model [Camp2002]_ is the basis for this |ns3| model.
148
149ns-2 MobilityHelper
150###################
151
152The |ns2| mobility format is a widely used mobility trace format.  The
153documentation is available at: http://www.isi.edu/nsnam/ns/doc/node172.html
154
155Valid trace files use the following |ns2| statements:
156
157.. sourcecode:: bash
158
159   $node set X_ x1
160   $node set Y_ y1
161   $node set Z_ z1
162   $ns at $time $node setdest x2 y2 speed
163   $ns at $time $node set X_ x1
164   $ns at $time $node set Y_ Y1
165   $ns at $time $node set Z_ Z1
166
167In the above, the initial positions are set using the ``set`` statements.
168Also, this ``set`` can be specified for a future time, such as in the
169last three statements above.
170
171The command ``setdest`` instructs the simulation to start moving the
172specified node towards the coordinate (x2, y2) at the specified time.
173Note that the node may never get to the destination, but will
174proceed towards the destination at the specified speed until it
175either reaches the destination (where it will pause), is set to
176a new position (via ``set``), or sent on another course change
177(via ``setdest``).
178
179Note that in |ns3|, movement along the Z dimension is not supported.
180
181Some examples of external tools that can export in this format include:
182
183- `BonnMotion <http://net.cs.uni-bonn.de/wg/cs/applications/bonnmotion/>`_
184
185  - `Installation instructions <https://www.nsnam.org/wiki/HOWTO_use_ns-3_with_BonnMotion_mobility_generator_and_analysis_tool>`_ and
186  - `Documentation <https://sys.cs.uos.de/bonnmotion/doc/README.pdf>`_ for using BonnMotion with |ns3|
187
188- `SUMO <https://sourceforge.net/apps/mediawiki/sumo/index.php?title=Main_Page>`_
189- `TraNS <http://trans.epfl.ch/>`_
190- |ns2| `setdest <http://www.winlab.rutgers.edu/~zhibinwu/html/ns2_wireless_scene.htm>`_ utility
191
192A special Ns2MobilityHelper object can be used to parse these files
193and convert the statements into |ns3| mobility events.  The underlying
194ConstantVelocityMobilityModel is used to model these movements.
195
196See below for additional usage instructions on this helper.
197
198Scope and Limitations
199=====================
200
201- only cartesian coordinates are presently supported
202
203References
204==========
205
206.. [Camp2002] T. Camp, J. Boleng, V. Davies.  "A survey of mobility models for ad hoc network research",
207   in Wireless Communications and Mobile Computing, 2002: vol. 2, pp. 2483-2502.
208
209Usage
210*****
211
212Most |ns3| program authors typically interact with the mobility system
213only at configuration time.  However, various |ns3| objects interact
214with mobility objects repeatedly during runtime, such as a propagation
215model trying to determine the path loss between two mobile nodes.
216
217Helper
218======
219
220A typical usage pattern can be found in the ``third.cc`` program in the
221tutorial.
222
223First, the user instantiates a ``MobilityHelper`` object and sets some
224``Attributes`` controlling the "position allocator" functionality.
225
226.. sourcecode:: cpp
227
228  MobilityHelper mobility;
229
230  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
231    "MinX", DoubleValue (0.0),
232    "MinY", DoubleValue (0.0),
233    "DeltaX", DoubleValue (5.0),
234    "DeltaY", DoubleValue (10.0),
235    "GridWidth", UintegerValue (3),
236    "LayoutType", StringValue ("RowFirst"));
237
238This code tells the mobility helper to use a two-dimensional grid to initially
239place the nodes.  The first argument is an |ns3| TypeId specifying the
240type of mobility model; the remaining attribute/value pairs configure
241this position allocator.
242
243Next, the user typically sets the MobilityModel subclass; e.g.:
244
245.. sourcecode:: cpp
246
247  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
248    "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
249
250Once the helper is configured, it is typically passed a container, such as:
251
252.. sourcecode:: cpp
253
254  mobility.Install (wifiStaNodes);
255
256A MobilityHelper object may be reconfigured and reused for different
257NodeContainers during the configuration of an |ns3| scenario.
258
259Ns2MobilityHelper
260=================
261
262Two example programs are provided demonstrating the use of the
263|ns2| mobility helper:
264
265- ns2-mobility-trace.cc
266- bonnmotion-ns2-example.cc
267
268ns2-mobility-trace
269##################
270
271The ``ns2-mobility-trace.cc`` program is an example of loading an
272|ns2| trace file that specifies the movements of two nodes over 100
273seconds of simulation time.  It is paired with the file
274``default.ns_movements``.
275
276The program behaves as follows:
277
278- a Ns2MobilityHelper object is created, with the specified trace file.
279- A log file is created, using the log file name argument.
280- A node container is created with the number of nodes specified in the command line.  For this particular trace file, specify the value 2 for this argument.
281- the Install() method of Ns2MobilityHelper to set mobility to nodes. At this moment, the file is read line by line, and the movement is scheduled in the simulator.
282- A callback is configured, so each time a node changes its course a log message is printed.
283
284The example prints out messages generated by each read line from the ns2 movement trace file.   For each line, it shows if the line is correct, or of it has errors and in this case it will be ignored.
285
286Example usage:
287
288.. sourcecode:: bash
289
290  $ ./waf --run "ns2-mobility-trace \
291  --traceFile=src/mobility/examples/default.ns_movements \
292  --nodeNum=2 \
293  --duration=100.0 \
294  --logFile=ns2-mob.log"
295
296Sample log file output:
297
298.. sourcecode:: text
299
300  +0.0ns POS: x=150, y=93.986, z=0; VEL:0, y=50.4038, z=0
301  +0.0ns POS: x=195.418, y=150, z=0; VEL:50.1186, y=0, z=0
302  +104727357.0ns POS: x=200.667, y=150, z=0; VEL:50.1239, y=0, z=0
303  +204480076.0ns POS: x=205.667, y=150, z=0; VEL:0, y=0, z=0
304
305bonnmotion-ns2-example
306######################
307
308The ``bonnmotion-ns2-example.cc`` program, which models the movement of
309a single mobile node for 1000 seconds of simulation time, has a few
310associated files:
311
312- ``bonnmotion.ns_movements`` is the |ns2|-formatted mobility trace
313- ``bonnmotion.params`` is a BonnMotion-generated file with some metadata about the mobility trace
314- ``bonnmotion.ns_params`` is another BonnMotion-generated file with ns-2-related metadata.
315
316Neither of the latter two files is used by |ns3|, although they are generated
317as part of the BonnMotion process to output ns-2-compatible traces.
318
319The program ``bonnmotion-ns2-example.cc`` will output the following to stdout:
320
321.. sourcecode:: text
322
323  At 0.00 node 0: Position(329.82, 66.06, 0.00);   Speed(0.53, -0.22, 0.00)
324  At 100.00 node 0: Position(378.38, 45.59, 0.00);   Speed(0.00, 0.00, 0.00)
325  At 200.00 node 0: Position(304.52, 123.66, 0.00);   Speed(-0.92, 0.97, 0.00)
326  At 300.00 node 0: Position(274.16, 131.67, 0.00);   Speed(-0.53, -0.46, 0.00)
327  At 400.00 node 0: Position(202.11, 123.60, 0.00);   Speed(-0.98, 0.35, 0.00)
328  At 500.00 node 0: Position(104.60, 158.95, 0.00);   Speed(-0.98, 0.35, 0.00)
329  At 600.00 node 0: Position(31.92, 183.87, 0.00);   Speed(0.76, -0.51, 0.00)
330  At 700.00 node 0: Position(107.99, 132.43, 0.00);   Speed(0.76, -0.51, 0.00)
331  At 800.00 node 0: Position(184.06, 80.98, 0.00);   Speed(0.76, -0.51, 0.00)
332  At 900.00 node 0: Position(250.08, 41.76, 0.00);   Speed(0.60, -0.05, 0.00)
333
334The motion of the mobile node is sampled every 100 seconds, and its position
335and speed are printed out.  This output may be compared to the output of
336a similar |ns2| program (found in the |ns2| ``tcl/ex/`` directory of |ns2|)
337running from the same mobility trace.
338
339The next file is generated from |ns2| (users will have to download and
340install |ns2| and run this Tcl program to see this output).
341The output of the |ns2| ``bonnmotion-example.tcl`` program is shown below
342for comparison (file ``bonnmotion-example.tr``):
343
344.. sourcecode:: text
345
346  M 0.00000 0 (329.82, 66.06, 0.00), (378.38, 45.59), 0.57
347  M 100.00000 0 (378.38, 45.59, 0.00), (378.38, 45.59), 0.57
348  M 119.37150 0 (378.38, 45.59, 0.00), (286.69, 142.52), 1.33
349  M 200.00000 0 (304.52, 123.66, 0.00), (286.69, 142.52), 1.33
350  M 276.35353 0 (286.69, 142.52, 0.00), (246.32, 107.57), 0.70
351  M 300.00000 0 (274.16, 131.67, 0.00), (246.32, 107.57), 0.70
352  M 354.65589 0 (246.32, 107.57, 0.00), (27.38, 186.94), 1.04
353  M 400.00000 0 (202.11, 123.60, 0.00), (27.38, 186.94), 1.04
354  M 500.00000 0 (104.60, 158.95, 0.00), (27.38, 186.94), 1.04
355  M 594.03719 0 (27.38, 186.94, 0.00), (241.02, 42.45), 0.92
356  M 600.00000 0 (31.92, 183.87, 0.00), (241.02, 42.45), 0.92
357  M 700.00000 0 (107.99, 132.43, 0.00), (241.02, 42.45), 0.92
358  M 800.00000 0 (184.06, 80.98, 0.00), (241.02, 42.45), 0.92
359  M 884.77399 0 (241.02, 42.45, 0.00), (309.59, 37.22), 0.60
360  M 900.00000 0 (250.08, 41.76, 0.00), (309.59, 37.22), 0.60
361
362The output formatting is slightly different, and the course change
363times are additionally plotted, but it can be seen that the position
364vectors are the same between the two traces at intervals of 100 seconds.
365
366The mobility computations performed on the |ns2| trace file are slightly
367different in |ns2| and |ns3|, and floating-point arithmetic is used,
368so there is a chance that the position in |ns2| may be slightly
369different than the respective position when using the trace file
370in |ns3|.
371
372Use of Random Variables
373=======================
374
375A typical use case is to evaluate protocols on a mobile topology that
376involves some randomness in the motion or initial position allocation.
377To obtain random motion and positioning that is not affected by
378the configuration of the rest of the scenario, it is recommended to
379use the "AssignStreams" facility of the random number system.
380
381Class ``MobilityModel`` and class ``PositionAllocator`` both have public
382API to assign streams to underlying random variables:
383
384.. sourcecode:: cpp
385
386  /**
387   * Assign a fixed random variable stream number to the random variables
388   * used by this model. Return the number of streams (possibly zero) that
389   * have been assigned.
390   *
391   * \param stream first stream index to use
392   * \return the number of stream indices assigned by this model
393   */
394  int64_t AssignStreams (int64_t stream);
395
396The class ``MobilityHelper`` also provides this API.  The typical usage
397pattern when using the helper is:
398
399.. sourcecode:: cpp
400
401  int64_t streamIndex = /*some positive integer */
402  MobilityHelper mobility;
403  ... (configure mobility)
404  mobility.Install (wifiStaNodes);
405  int64_t streamsUsed = mobility.AssignStreams (wifiStaNodes, streamIndex);
406
407If AssignStreams is called before Install, it will not have any effect.
408
409Advanced Usage
410==============
411
412A number of external tools can be used to generate traces read by
413the Ns2MobilityHelper.
414
415ns-2 scengen
416############
417
418TBD
419
420BonnMotion
421##########
422
423http://net.cs.uni-bonn.de/wg/cs/applications/bonnmotion/
424
425SUMO
426####
427
428http://sourceforge.net/apps/mediawiki/sumo/index.php?title=Main_Page
429
430TraNS
431#####
432
433http://trans.epfl.ch/
434
435Examples
436========
437
438- main-random-topology.cc
439- main-random-walk.cc
440- main-grid-topology.cc
441- ns2-mobility-trace.cc
442- ns2-bonnmotion.cc
443
444reference-point-group-mobility-example.cc
445#########################################
446
447The reference point group mobility model ([Camp2002]_) is demonstrated
448in the example program `reference-point-group-mobility-example.cc`.
449This example runs a short simulation that illustrates a parent
450WaypointMobilityModel traversing a rectangular course within a bounding
451box, and three member nodes independently execute a two-dimensional
452random walk around the parent position, within a small bounding box.
453The example illustrates configuration using the GroupMobilityHelper
454and manual configuration without a helper; the configuration option
455is selectable by command-line argument.
456
457The example outputs two mobility trace files, a course change trace and
458a time-series trace of node position.  The latter trace file can be
459parsed by a Bash script (`reference-point-group-mobility-animate.sh`)
460to create PNG images at one-second intervals, which can then be combined
461using an image processing program such as ImageMagick to form a
462basic animated gif of the mobility.  The example and animation program
463files have further instructions on how to run them.
464
465Validation
466**********
467
468TBD
469