A Discrete-Event Network Simulator
API
mobility-helper.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2008 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20#include "ns3/mobility-helper.h"
21#include "ns3/mobility-model.h"
22#include "ns3/position-allocator.h"
23#include "ns3/hierarchical-mobility-model.h"
24#include "ns3/log.h"
25#include "ns3/pointer.h"
26#include "ns3/config.h"
27#include "ns3/simulator.h"
28#include "ns3/names.h"
29#include "ns3/string.h"
30#include <iostream>
31
32namespace ns3 {
33
34NS_LOG_COMPONENT_DEFINE ("MobilityHelper");
35
37{
38 m_position = CreateObjectWithAttributes<RandomRectanglePositionAllocator>
39 ("X", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"),
40 "Y", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
41 m_mobility.SetTypeId ("ns3::ConstantPositionMobilityModel");
42}
44{
45}
46void
48{
49 m_position = allocator;
50}
51
52void
54 std::string n1, const AttributeValue &v1,
55 std::string n2, const AttributeValue &v2,
56 std::string n3, const AttributeValue &v3,
57 std::string n4, const AttributeValue &v4,
58 std::string n5, const AttributeValue &v5,
59 std::string n6, const AttributeValue &v6,
60 std::string n7, const AttributeValue &v7,
61 std::string n8, const AttributeValue &v8,
62 std::string n9, const AttributeValue &v9)
63{
64 ObjectFactory pos;
65 pos.SetTypeId (type);
66 pos.Set (n1, v1);
67 pos.Set (n2, v2);
68 pos.Set (n3, v3);
69 pos.Set (n4, v4);
70 pos.Set (n5, v5);
71 pos.Set (n6, v6);
72 pos.Set (n7, v7);
73 pos.Set (n8, v8);
74 pos.Set (n9, v9);
76}
77
78void
80 std::string n1, const AttributeValue &v1,
81 std::string n2, const AttributeValue &v2,
82 std::string n3, const AttributeValue &v3,
83 std::string n4, const AttributeValue &v4,
84 std::string n5, const AttributeValue &v5,
85 std::string n6, const AttributeValue &v6,
86 std::string n7, const AttributeValue &v7,
87 std::string n8, const AttributeValue &v8,
88 std::string n9, const AttributeValue &v9)
89{
90 m_mobility.SetTypeId (type);
91 m_mobility.Set (n1, v1);
92 m_mobility.Set (n2, v2);
93 m_mobility.Set (n3, v3);
94 m_mobility.Set (n4, v4);
95 m_mobility.Set (n5, v5);
96 m_mobility.Set (n6, v6);
97 m_mobility.Set (n7, v7);
98 m_mobility.Set (n8, v8);
99 m_mobility.Set (n9, v9);
100}
101
102void
104{
106 m_mobilityStack.push_back (mobility);
107}
108
109void
111{
112 Ptr<MobilityModel> mobility = Names::Find<MobilityModel> (referenceName);
113 m_mobilityStack.push_back (mobility);
114}
115
116void
118{
119 m_mobilityStack.pop_back ();
120}
121
122
123std::string
125{
126 return m_mobility.GetTypeId ().GetName ();
127}
128
129void
131{
132 Ptr<Object> object = node;
133 Ptr<MobilityModel> model = object->GetObject<MobilityModel> ();
134 if (model == 0)
135 {
137 if (model == 0)
138 {
139 NS_FATAL_ERROR ("The requested mobility model is not a mobility model: \""<<
140 m_mobility.GetTypeId ().GetName ()<<"\"");
141 }
142 if (m_mobilityStack.empty ())
143 {
144 NS_LOG_DEBUG ("node="<<object<<", mob="<<model);
145 object->AggregateObject (model);
146 }
147 else
148 {
149 // we need to setup a hierarchical mobility model
150 Ptr<MobilityModel> parent = m_mobilityStack.back ();
151 Ptr<MobilityModel> hierarchical =
152 CreateObjectWithAttributes<HierarchicalMobilityModel> ("Child", PointerValue (model),
153 "Parent", PointerValue (parent));
154 object->AggregateObject (hierarchical);
155 NS_LOG_DEBUG ("node="<<object<<", mob="<<hierarchical);
156 }
157 }
158 Vector position = m_position->GetNext ();
159 model->SetPosition (position);
160}
161
162void
163MobilityHelper::Install (std::string nodeName) const
164{
165 Ptr<Node> node = Names::Find<Node> (nodeName);
166 Install (node);
167}
168void
170{
171 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
172 {
173 Install (*i);
174 }
175}
176
177void
179{
181}
188static double
189DoRound (double v)
190{
191 if (v <= 1e-4 && v >= -1e-4)
192 {
193 return 0.0;
194 }
195 else if (v <= 1e-3 && v >= 0)
196 {
197 return 1e-3;
198 }
199 else if (v >= -1e-3 && v <= 0)
200 {
201 return -1e-3;
202 }
203 else
204 {
205 return v;
206 }
207}
208void
210{
211 std::ostream* os = stream->GetStream ();
212 Vector pos = mobility->GetPosition ();
213 Vector vel = mobility->GetVelocity ();
214 *os << "now=" << Simulator::Now ()
215 << " node=" << mobility->GetObject<Node> ()->GetId ();
216 pos.x = DoRound (pos.x);
217 pos.y = DoRound (pos.y);
218 pos.z = DoRound (pos.z);
219 vel.x = DoRound (vel.x);
220 vel.y = DoRound (vel.y);
221 vel.z = DoRound (vel.z);
222 std::streamsize saved_precision = os->precision ();
223 std::ios::fmtflags saved_flags = os->flags ();
224 os->precision (3);
225 os->setf (std::ios::fixed,std::ios::floatfield);
226 *os << " pos=" << pos.x << ":" << pos.y << ":" << pos.z
227 << " vel=" << vel.x << ":" << vel.y << ":" << vel.z
228 << std::endl;
229 os->flags (saved_flags);
230 os->precision (saved_precision);
231}
232
233void
235{
236 std::ostringstream oss;
237 oss << "/NodeList/" << nodeid << "/$ns3::MobilityModel/CourseChange";
240}
241void
243{
244 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
245 {
246 EnableAscii (stream, (*i)->GetId ());
247 }
248}
249void
251{
253}
254int64_t
256{
257 int64_t currentStream = stream;
258 Ptr<Node> node;
260 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
261 {
262 node = (*i);
263 mobility = node->GetObject<MobilityModel> ();
264 if (mobility)
265 {
266 currentStream += mobility->AssignStreams (currentStream);
267 }
268 }
269 return (currentStream - stream);
270}
271
272double
274{
276 double distSq = 0.0;
277
278 Ptr<MobilityModel> rxPosition = n1->GetObject<MobilityModel> ();
279 NS_ASSERT (rxPosition != 0);
280
281 Ptr<MobilityModel> txPosition = n2->GetObject<MobilityModel> ();
282 NS_ASSERT (txPosition != 0);
283
284 double dist = rxPosition -> GetDistanceFrom (txPosition);
285 distSq = dist * dist;
286
287 return distSq;
288}
289
290} // namespace ns3
Hold a value for an Attribute.
Definition: attribute.h:69
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
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...
std::string GetMobilityModelType(void) const
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
static double GetDistanceSquaredBetween(Ptr< Node > n1, Ptr< Node > n2)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
ObjectFactory m_mobility
Object factory to create mobility objects.
static void CourseChanged(Ptr< OutputStreamWrapper > stream, Ptr< const MobilityModel > mobility)
Output course change events from mobility model to output stream.
void InstallAll(void)
Perform the work of MobilityHelper::Install on all nodes which exist in the simulation.
~MobilityHelper()
Destroy a Mobility Helper.
void PopReferenceMobilityModel(void)
Remove the top item from the top of the stack of "reference mobility models".
Ptr< PositionAllocator > m_position
Position allocator for use in hierarchical mobility model.
void PushReferenceMobilityModel(Ptr< Object > reference)
MobilityHelper()
Construct a Mobility Helper which is used to make life easier when working with mobility models.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
static void EnableAscii(Ptr< OutputStreamWrapper > stream, uint32_t nodeid)
std::vector< Ptr< MobilityModel > > m_mobilityStack
Internal stack of mobility models.
Keep track of the current position and velocity of an object.
void SetPosition(const Vector &position)
keep track of a set of node pointers.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
static NodeContainer GetGlobal(void)
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
A network Node.
Definition: node.h:57
Instantiate subclasses of ns3::Object.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
TypeId GetTypeId(void) const
Get the TypeId which will be created by this ObjectFactory.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Allocate a set of positions.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Hold variables of type string.
Definition: string.h:41
std::string GetName(void) const
Get the name.
Definition: type-id.cc:976
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
bool ConnectWithoutContextFailSafe(std::string path, const CallbackBase &cb)
Definition: config.cc:909
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1709
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static double DoRound(double v)
Utility function that rounds |1e-4| < input value < |1e-3| up to +/- 1e-3 and value <= |1e-4| to zero...
mobility
Definition: third.py:107