A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
reference-point-group-mobility-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Institute for the Wireless Internet of Things, Northeastern University,
3 * Boston, MA Copyright (c) 2021 University of Washington: for HierarchicalMobilityModel
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Michele Polese <michele.polese@gmail.com>
8 * Heavily edited by Tom Henderson (to reuse HierarchicalMobilityModel)
9 */
10
11/**
12 * This example shows how to use the ns3::HierarchicalMobilityModel
13 * to construct a Reference Point Group Mobility model (RPGM) model
14 * as described in "A survey of mobility models for ad hoc network
15 * research" by Tracy Camp, Jeff Boleng, and Vanessa Davies, Wireless
16 * Communications and Mobile Computing, 2002: vol. 2, pp. 2483-502.
17 *
18 * The HierarchicalMobilityModel is composed of two mobility models;
19 * a parent and a child. The position of the child is expressed
20 * in reference to the position of the parent. For group mobility,
21 * each node in the group installs the same parent mobility model
22 * and different child mobility models.
23 *
24 * There is no node associated with the parent (reference) model.
25 * Instead, all nodes are associated with a hierarchical mobility model
26 * containing both the parent and child models, and the position of
27 * the node is the vector sum of these parent and child positions.
28 *
29 * Standard ns-3 mobility model course change output is traced in
30 * 'reference-point-course-change.mob' file. This file only traces
31 * position when there is a course change. A second trace is produced
32 * by this example: a time-series of node positions sampled every second.
33 * This file is 'reference-point-time-series.mob' and can be plotted
34 * with the 'reference-point-group-mobility-animation.sh' program.
35 *
36 * There is a bit of randomness in the child mobility models (random
37 * walk within a 10m x 10m box surrounding the parent mobility position);
38 * slightly different output can be rendered by changing the ns-3 'RunNumber'
39 * value (see the documentation on ns-3 random variables).
40 *
41 * There is one program option: 'useHelper'. This is simply for code
42 * demonstration purposes; it selects the branch of code that is used
43 * to either configure the mobility using a helper object, or
44 * to directly configure using CreateObject<> () and handling of pointers.
45 * The traces generated should be the same.
46 */
47
48#include "ns3/core-module.h"
49#include "ns3/network-module.h"
50#include <ns3/mobility-module.h>
51
52#include <iostream>
53
54using namespace ns3;
55
56NS_LOG_COMPONENT_DEFINE("ReferencePointGroupMobilityExample");
57
58/// The time series file.
59std::ofstream g_timeSeries;
60
61/**
62 * Print the node position to the time series file.
63 *
64 * \param node The node.
65 */
66void
68{
69 if (!node)
70 {
71 return;
72 }
73 Ptr<MobilityModel> model = node->GetObject<MobilityModel>();
74 if (!model)
75 {
76 return;
77 }
78 NS_LOG_LOGIC("Node: " << node->GetId() << " Position: " << model->GetPosition());
79 g_timeSeries << Simulator::Now().GetSeconds() << " " << node->GetId() << " "
80 << model->GetPosition() << std::endl;
81}
82
83int
84main(int argc, char* argv[])
85{
86 Time simTime = Seconds(800);
87 uint32_t numPrints = 800;
88 bool useHelper = false;
89
90 CommandLine cmd(__FILE__);
91 cmd.AddValue("useHelper", "Whether to use helper code", useHelper);
92 cmd.Parse(argc, argv);
93
94 g_timeSeries.open("reference-point-time-series.mob");
95
97 n.Create(3);
98
99 // The primary mobility model is the WaypointMobilityModel defined within
100 // this bounding box:
101 //
102 // (0,50) (100,50)
103 // +-------------------------+
104 // | .(10,40) (90,40). |
105 // | |
106 // | |
107 // | .(10,10) (90,10). |
108 // | |
109 // +-------------------------+
110 // (0,0) (100,0)
111 //
112
113 // The reference (parent) mobility model starts at coordinate (10,10
114 // and walks clockwise to each waypoint, making two laps. The time
115 // to travel between each waypoint is 100s, so the velocity alternates
116 // between two values due to the rectangular path.
117 // No actual node is represented by the position of this mobility
118 // model; it forms the reference point from which the node's child
119 // mobility model position is offset.
120 //
122 waypointMm->AddWaypoint(Waypoint(Seconds(0), Vector(10, 10, 0)));
123 waypointMm->AddWaypoint(Waypoint(Seconds(100), Vector(10, 40, 0)));
124 waypointMm->AddWaypoint(Waypoint(Seconds(200), Vector(90, 40, 0)));
125 waypointMm->AddWaypoint(Waypoint(Seconds(300), Vector(90, 10, 0)));
126 waypointMm->AddWaypoint(Waypoint(Seconds(400), Vector(10, 10, 0)));
127 waypointMm->AddWaypoint(Waypoint(Seconds(500), Vector(10, 40, 0)));
128 waypointMm->AddWaypoint(Waypoint(Seconds(600), Vector(90, 40, 0)));
129 waypointMm->AddWaypoint(Waypoint(Seconds(700), Vector(90, 10, 0)));
130 waypointMm->AddWaypoint(Waypoint(Seconds(800), Vector(10, 10, 0)));
131
132 // Each HierarchicalMobilityModel contains the above model as the Parent,
133 // and a user defined model as the Child. Two MobilityModel objects are
134 // instantiated per node (one hierarchical, and one child model), and
135 // a single parent model is reused across all nodes.
136
137 // The program now branches into two: one using the low-level API, and
138 // one using the GroupMobilityHelper. Both branches result in equivalent
139 // configuration.
140
141 int64_t streamIndex = 1;
142 if (!useHelper)
143 {
144 // Assign random variable stream numbers on the parent and each child
145 streamIndex += waypointMm->AssignStreams(streamIndex);
146
147 // Mobility model for the first node (node 0)
149 hierarchical0->SetParent(waypointMm);
150
151 // Child Mobility model for the first node (node 0). This can be any
152 // other mobility model type; for this example, we reuse the random walk
153 // but with a small 10m x 10m bounding box.
155 // Position in reference to the original random walk
156 childRandomWalk0->SetAttribute("Bounds", RectangleValue(Rectangle(-5, 5, -5, 5)));
157 childRandomWalk0->SetAttribute("Speed",
158 StringValue("ns3::ConstantRandomVariable[Constant=0.1]"));
159 streamIndex += childRandomWalk0->AssignStreams(streamIndex);
160 hierarchical0->SetChild(childRandomWalk0);
161 n.Get(0)->AggregateObject(hierarchical0);
162 // Repeat for other two nodes
164 hierarchical1->SetParent(waypointMm); // Same parent as before
166 childRandomWalk1->SetAttribute("Bounds", RectangleValue(Rectangle(-5, 5, -5, 5)));
167 childRandomWalk1->SetAttribute("Speed",
168 StringValue("ns3::ConstantRandomVariable[Constant=0.1]"));
169 streamIndex += childRandomWalk1->AssignStreams(streamIndex);
170 hierarchical1->SetChild(childRandomWalk1);
171 n.Get(1)->AggregateObject(hierarchical1);
173 hierarchical2->SetParent(waypointMm); // Same parent as before
175 childRandomWalk2->SetAttribute("Bounds", RectangleValue(Rectangle(-5, 5, -5, 5)));
176 childRandomWalk2->SetAttribute("Speed",
177 StringValue("ns3::ConstantRandomVariable[Constant=0.1]"));
178 streamIndex += childRandomWalk2->AssignStreams(streamIndex);
179 hierarchical2->SetChild(childRandomWalk2);
180 n.Get(2)->AggregateObject(hierarchical2);
181 }
182 else
183 {
184 // This branch demonstrates an equivalent set of commands but using
185 // the GroupMobilityHelper
187
188 // The helper provides a method to set the reference mobility model
189 // for construction by an object factory, but in this case, since we
190 // are using the WaypointMobilityModel, which requires us to add
191 // waypoints directly on the object, we will just pass in the pointer.
192 group.SetReferenceMobilityModel(waypointMm);
193
194 // The WaypointMobilityModel does not need a position allocator
195 // (it can use its first waypoint as such), but in general, the
196 // GroupMobilityHelper can be configured to accept configuration for
197 // a PositionAllocator for the reference model. We skip that here.
198
199 // Next, configure the member mobility model
200 group.SetMemberMobilityModel("ns3::RandomWalk2dMobilityModel",
201 "Bounds",
202 RectangleValue(Rectangle(-5, 5, -5, 5)),
203 "Speed",
204 StringValue("ns3::ConstantRandomVariable[Constant=0.1]"));
205
206 // Again, we could call 'SetMemberPositionAllocator' and provide a
207 // position allocator here for the member nodes, but none is provided
208 // in this example, so they will start at time zero with the same
209 // position as the reference node.
210
211 // Install to all three nodes
212 group.Install(n);
213
214 // After installation, use the helper to make the equivalent
215 // stream assignments as above
216 group.AssignStreams(n, streamIndex);
217 }
218
219 // Note: The tracing methods are static methods declared on the
220 // MobilityHelper class, not on the GroupMobilityHelper class
221 AsciiTraceHelper ascii;
222 MobilityHelper::EnableAsciiAll(ascii.CreateFileStream("reference-point-course-change.mob"));
223
224 // Use a logging PrintPosition() to record time-series position
225 for (unsigned int i = 0; i < numPrints; i++)
226 {
227 for (auto nodeIt = n.Begin(); nodeIt != n.End(); ++nodeIt)
228 {
229 Simulator::Schedule(NanoSeconds(i * simTime.GetNanoSeconds() / numPrints),
231 (*nodeIt));
232 }
233 }
234
235 Simulator::Stop(simTime);
237 g_timeSeries.close();
239}
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Helper class used to assign positions and mobility models to nodes for a group mobility configuration...
void SetMemberMobilityModel(std::string type, Ts &&... args)
Configure the mobility model which will be installed as the member (child) mobility model during Grou...
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the mobility models on t...
void Install(Ptr< Node > node)
Install and configure a hierarchical mobility model to the given node, based on the configured refere...
void SetReferenceMobilityModel(Ptr< MobilityModel > mobility)
Set the reference mobility model which will be installed as the parent mobility model during GroupMob...
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
Keep track of the current position and velocity of an object.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition object.cc:298
Smart pointer class similar to boost::intrusive_ptr.
a 2d rectangle
Definition rectangle.h:24
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:407
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a (time, location) pair.
Definition waypoint.h:25
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ofstream g_timeSeries
The time series file.
void PrintPosition(Ptr< Node > node)
Print the node position to the time series file.