A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
random-walk-2d-mobility-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006,2007 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  */
21 #include "ns3/enum.h"
22 #include "ns3/double.h"
23 #include "ns3/string.h"
24 #include "ns3/pointer.h"
25 #include "ns3/simulator.h"
26 #include "ns3/log.h"
27 #include <cmath>
28 
29 NS_LOG_COMPONENT_DEFINE ("RandomWalk2d");
30 
31 namespace ns3 {
32 
33 NS_OBJECT_ENSURE_REGISTERED (RandomWalk2dMobilityModel)
34  ;
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::RandomWalk2dMobilityModel")
41  .SetGroupName ("Mobility")
42  .AddConstructor<RandomWalk2dMobilityModel> ()
43  .AddAttribute ("Bounds",
44  "Bounds of the area to cruise.",
45  RectangleValue (Rectangle (0.0, 0.0, 100.0, 100.0)),
46  MakeRectangleAccessor (&RandomWalk2dMobilityModel::m_bounds),
47  MakeRectangleChecker ())
48  .AddAttribute ("Time",
49  "Change current direction and speed after moving for this delay.",
50  TimeValue (Seconds (1.0)),
51  MakeTimeAccessor (&RandomWalk2dMobilityModel::m_modeTime),
52  MakeTimeChecker ())
53  .AddAttribute ("Distance",
54  "Change current direction and speed after moving for this distance.",
55  DoubleValue (1.0),
56  MakeDoubleAccessor (&RandomWalk2dMobilityModel::m_modeDistance),
57  MakeDoubleChecker<double> ())
58  .AddAttribute ("Mode",
59  "The mode indicates the condition used to "
60  "change the current speed and direction",
65  .AddAttribute ("Direction",
66  "A random variable used to pick the direction (gradients).",
67  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
68  MakePointerAccessor (&RandomWalk2dMobilityModel::m_direction),
69  MakePointerChecker<RandomVariableStream> ())
70  .AddAttribute ("Speed",
71  "A random variable used to pick the speed (m/s).",
72  StringValue ("ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
73  MakePointerAccessor (&RandomWalk2dMobilityModel::m_speed),
74  MakePointerChecker<RandomVariableStream> ());
75  return tid;
76 }
77 
78 void
80 {
83 }
84 
85 void
87 {
88  m_helper.Update ();
89  double speed = m_speed->GetValue ();
90  double direction = m_direction->GetValue ();
91  Vector vector (std::cos (direction) * speed,
92  std::sin (direction) * speed,
93  0.0);
94  m_helper.SetVelocity (vector);
95  m_helper.Unpause ();
96 
97  Time delayLeft;
99  {
100  delayLeft = m_modeTime;
101  }
102  else
103  {
104  delayLeft = Seconds (m_modeDistance / speed);
105  }
106  DoWalk (delayLeft);
107 }
108 
109 void
111 {
112  Vector position = m_helper.GetCurrentPosition ();
113  Vector speed = m_helper.GetVelocity ();
114  Vector nextPosition = position;
115  nextPosition.x += speed.x * delayLeft.GetSeconds ();
116  nextPosition.y += speed.y * delayLeft.GetSeconds ();
117  m_event.Cancel ();
118  if (m_bounds.IsInside (nextPosition))
119  {
121  }
122  else
123  {
124  nextPosition = m_bounds.CalculateIntersection (position, speed);
125  Time delay = Seconds ((nextPosition.x - position.x) / speed.x);
127  delayLeft - delay);
128  }
130 }
131 
132 void
134 {
136  Vector position = m_helper.GetCurrentPosition ();
137  Vector speed = m_helper.GetVelocity ();
138  switch (m_bounds.GetClosestSide (position))
139  {
140  case Rectangle::RIGHT:
141  case Rectangle::LEFT:
142  speed.x = -speed.x;
143  break;
144  case Rectangle::TOP:
145  case Rectangle::BOTTOM:
146  speed.y = -speed.y;
147  break;
148  }
149  m_helper.SetVelocity (speed);
150  m_helper.Unpause ();
151  DoWalk (delayLeft);
152 }
153 
154 void
156 {
157  // chain up
159 }
160 Vector
162 {
164  return m_helper.GetCurrentPosition ();
165 }
166 void
168 {
169  NS_ASSERT (m_bounds.IsInside (position));
170  m_helper.SetPosition (position);
173 }
174 Vector
176 {
177  return m_helper.GetVelocity ();
178 }
179 int64_t
181 {
182  m_speed->SetStream (stream);
183  m_direction->SetStream (stream + 1);
184  return 2;
185 }
186 
187 
188 } // namespace ns3
NS_LOG_COMPONENT_DEFINE("RandomWalk2d")
double x
x coordinate of vector
Definition: vector.h:49
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Definition: enum.cc:178
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:89
hold variables of type string
Definition: string.h:19
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
a 3d vector
Definition: vector.h:31
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
hold objects of type ns3::Rectangle
Keep track of the current position and velocity of an object.
2D random walk mobility model.
double GetSeconds(void) const
Definition: nstime.h:274
hold variables of type 'enum'
Definition: enum.h:37
virtual void DoSetPosition(const Vector &position)
hold objects of type ns3::Time
Definition: nstime.h:961
bool IsInside(const Vector &position) const
Definition: rectangle.cc:48
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void SetVelocity(const Vector &vel)
double y
y coordinate of vector
Definition: vector.h:53
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition: enum.h:118
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
void UpdateWithBounds(const Rectangle &rectangle) const
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
void SetPosition(const Vector &position)
Hold a floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:49
a 2d rectangle
Definition: rectangle.h:33
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:343
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.