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 
32 namespace ns3 {
33 
34 NS_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 }
46 void
48 {
49  m_position = allocator;
50 }
51 
52 void
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 
78 void
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 
102 void
104 {
106  m_mobilityStack.push_back (mobility);
107 }
108 
109 void
111 {
112  Ptr<MobilityModel> mobility = Names::Find<MobilityModel> (referenceName);
113  m_mobilityStack.push_back (mobility);
114 }
115 
116 void
118 {
119  m_mobilityStack.pop_back ();
120 }
121 
122 
123 std::string
125 {
126  return m_mobility.GetTypeId ().GetName ();
127 }
128 
129 void
131 {
132  Ptr<Object> object = node;
133  Ptr<MobilityModel> model = object->GetObject<MobilityModel> ();
134  if (model == 0)
135  {
136  model = m_mobility.Create ()->GetObject<MobilityModel> ();
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 
162 void
163 MobilityHelper::Install (std::string nodeName) const
164 {
165  Ptr<Node> node = Names::Find<Node> (nodeName);
166  Install (node);
167 }
168 void
170 {
171  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
172  {
173  Install (*i);
174  }
175 }
176 
177 void
179 {
181 }
188 static double
189 DoRound (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 }
208 void
210 {
211  std::ostream* os = stream->GetStream ();
212  Ptr<Node> node = mobility->GetObject<Node> ();
213  *os << "now=" << Simulator::Now ()
214  << " node=" << node->GetId ();
215  Vector pos = mobility->GetPosition ();
216  pos.x = DoRound (pos.x);
217  pos.y = DoRound (pos.y);
218  pos.z = DoRound (pos.z);
219  Vector vel = mobility->GetVelocity ();
220  vel.x = DoRound (vel.x);
221  vel.y = DoRound (vel.y);
222  vel.z = DoRound (vel.z);
223  std::streamsize saved_precision = os->precision ();
224  std::ios::fmtflags saved_flags = os->flags ();
225  os->precision (3);
226  os->setf (std::ios::fixed,std::ios::floatfield);
227  *os << " pos=" << pos.x << ":" << pos.y << ":" << pos.z
228  << " vel=" << vel.x << ":" << vel.y << ":" << vel.z
229  << std::endl;
230  os->flags (saved_flags);
231  os->precision (saved_precision);
232 }
233 
234 void
236 {
237  std::ostringstream oss;
238  oss << "/NodeList/" << nodeid << "/$ns3::MobilityModel/CourseChange";
241 }
242 void
244 {
245  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
246  {
247  EnableAscii (stream, (*i)->GetId ());
248  }
249 }
250 void
252 {
254 }
255 int64_t
257 {
258  int64_t currentStream = stream;
259  Ptr<Node> node;
261  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
262  {
263  node = (*i);
264  mobility = node->GetObject<MobilityModel> ();
265  if (mobility)
266  {
267  currentStream += mobility->AssignStreams (currentStream);
268  }
269  }
270  return (currentStream - stream);
271 }
272 
273 double
275 {
277  double distSq = 0.0;
278 
279  Ptr<MobilityModel> rxPosition = n1->GetObject<MobilityModel> ();
280  NS_ASSERT (rxPosition != 0);
281 
282  Ptr<MobilityModel> txPosition = n2->GetObject<MobilityModel> ();
283  NS_ASSERT (txPosition != 0);
284 
285  double dist = rxPosition -> GetDistanceFrom (txPosition);
286  distSq = dist * dist;
287 
288  return distSq;
289 }
290 
291 } // namespace ns3
void InstallAll(void)
Perform the work of MobilityHelper::Install on all nodes which exist in the simulation.
std::string GetName(void) const
Get the name.
Definition: type-id.cc:977
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
uint32_t GetId(void) const
Definition: node.cc:109
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Hold variables of type string.
Definition: string.h:41
static double GetDistanceSquaredBetween(Ptr< Node > n1, Ptr< Node > n2)
Hold a value for an Attribute.
Definition: attribute.h:68
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1703
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
#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
~MobilityHelper()
Destroy a Mobility Helper.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the mobility models (inc...
void PushReferenceMobilityModel(Ptr< Object > reference)
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
mobility
Definition: third.py:108
Ptr< PositionAllocator > m_position
Position allocator for use in hierarchical mobility model.
Keep track of the current position and velocity of an object.
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...
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
static void CourseChanged(Ptr< OutputStreamWrapper > stream, Ptr< const MobilityModel > mobility)
Output course change events from mobility model to output stream.
static void EnableAscii(Ptr< OutputStreamWrapper > stream, uint32_t nodeid)
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
bool ConnectWithoutContextFailSafe(std::string path, const CallbackBase &cb)
This function will attempt to find all trace sources which match the input path and will then connect...
Definition: config.cc:907
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ObjectFactory m_mobility
Object factory to create mobility objects.
keep track of a set of node pointers.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
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())
std::string GetMobilityModelType(void) const
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
void SetPosition(const Vector &position)
TypeId GetTypeId(void) const
Get the TypeId which will be created by this ObjectFactory.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
static NodeContainer GetGlobal(void)
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
std::vector< Ptr< MobilityModel > > m_mobilityStack
Internal stack of mobility models.
Instantiate subclasses of ns3::Object.
MobilityHelper()
Construct a Mobility Helper which is used to make life easier when working with mobility models...
A network Node.
Definition: node.h:56
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
void PopReferenceMobilityModel(void)
Remove the top item from the top of the stack of "reference mobility models".
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Allocate a set of positions.