A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
random-walk-2d-mobility-model.cc
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  */
20 #include "random-walk-2d-mobility-model.h"
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 TypeId
36 RandomWalk2dMobilityModel::GetTypeId (void)
37 {
38  static TypeId tid = TypeId ("ns3::RandomWalk2dMobilityModel")
39  .SetParent<MobilityModel> ()
40  .SetGroupName ("Mobility")
41  .AddConstructor<RandomWalk2dMobilityModel> ()
42  .AddAttribute ("Bounds",
43  "Bounds of the area to cruise.",
44  RectangleValue (Rectangle (0.0, 0.0, 100.0, 100.0)),
45  MakeRectangleAccessor (&RandomWalk2dMobilityModel::m_bounds),
46  MakeRectangleChecker ())
47  .AddAttribute ("Time",
48  "Change current direction and speed after moving for this delay.",
49  TimeValue (Seconds (1.0)),
50  MakeTimeAccessor (&RandomWalk2dMobilityModel::m_modeTime),
51  MakeTimeChecker ())
52  .AddAttribute ("Distance",
53  "Change current direction and speed after moving for this distance.",
54  DoubleValue (1.0),
55  MakeDoubleAccessor (&RandomWalk2dMobilityModel::m_modeDistance),
56  MakeDoubleChecker<double> ())
57  .AddAttribute ("Mode",
58  "The mode indicates the condition used to "
59  "change the current speed and direction",
60  EnumValue (RandomWalk2dMobilityModel::MODE_DISTANCE),
61  MakeEnumAccessor (&RandomWalk2dMobilityModel::m_mode),
62  MakeEnumChecker (RandomWalk2dMobilityModel::MODE_DISTANCE, "Distance",
63  RandomWalk2dMobilityModel::MODE_TIME, "Time"))
64  .AddAttribute ("Direction",
65  "A random variable used to pick the direction (gradients).",
66  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
67  MakePointerAccessor (&RandomWalk2dMobilityModel::m_direction),
68  MakePointerChecker<RandomVariableStream> ())
69  .AddAttribute ("Speed",
70  "A random variable used to pick the speed (m/s).",
71  StringValue ("ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
72  MakePointerAccessor (&RandomWalk2dMobilityModel::m_speed),
73  MakePointerChecker<RandomVariableStream> ());
74  return tid;
75 }
76 
77 void
79 {
80  DoInitializePrivate ();
82 }
83 
84 void
85 RandomWalk2dMobilityModel::DoInitializePrivate (void)
86 {
87  m_helper.Update ();
88  double speed = m_speed->GetValue ();
89  double direction = m_direction->GetValue ();
90  Vector vector (std::cos (direction) * speed,
91  std::sin (direction) * speed,
92  0.0);
93  m_helper.SetVelocity (vector);
94  m_helper.Unpause ();
95 
96  Time delayLeft;
97  if (m_mode == RandomWalk2dMobilityModel::MODE_TIME)
98  {
99  delayLeft = m_modeTime;
100  }
101  else
102  {
103  delayLeft = Seconds (m_modeDistance / speed);
104  }
105  DoWalk (delayLeft);
106 }
107 
108 void
109 RandomWalk2dMobilityModel::DoWalk (Time delayLeft)
110 {
111  Vector position = m_helper.GetCurrentPosition ();
112  Vector speed = m_helper.GetVelocity ();
113  Vector nextPosition = position;
114  nextPosition.x += speed.x * delayLeft.GetSeconds ();
115  nextPosition.y += speed.y * delayLeft.GetSeconds ();
116  m_event.Cancel ();
117  if (m_bounds.IsInside (nextPosition))
118  {
119  m_event = Simulator::Schedule (delayLeft, &RandomWalk2dMobilityModel::DoInitializePrivate, this);
120  }
121  else
122  {
123  nextPosition = m_bounds.CalculateIntersection (position, speed);
124  Time delay = Seconds ((nextPosition.x - position.x) / speed.x);
125  m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Rebound, this,
126  delayLeft - delay);
127  }
129 }
130 
131 void
132 RandomWalk2dMobilityModel::Rebound (Time delayLeft)
133 {
134  m_helper.UpdateWithBounds (m_bounds);
135  Vector position = m_helper.GetCurrentPosition ();
136  Vector speed = m_helper.GetVelocity ();
137  switch (m_bounds.GetClosestSide (position))
138  {
139  case Rectangle::RIGHT:
140  case Rectangle::LEFT:
141  speed.x = -speed.x;
142  break;
143  case Rectangle::TOP:
144  case Rectangle::BOTTOM:
145  speed.y = -speed.y;
146  break;
147  }
148  m_helper.SetVelocity (speed);
149  m_helper.Unpause ();
150  DoWalk (delayLeft);
151 }
152 
153 void
155 {
156  // chain up
158 }
159 Vector
161 {
162  m_helper.UpdateWithBounds (m_bounds);
163  return m_helper.GetCurrentPosition ();
164 }
165 void
167 {
168  NS_ASSERT (m_bounds.IsInside (position));
169  m_helper.SetPosition (position);
170  Simulator::Remove (m_event);
171  m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::DoInitializePrivate, this);
172 }
173 Vector
175 {
176  return m_helper.GetVelocity ();
177 }
178 int64_t
180 {
181  m_speed->SetStream (stream);
182  m_direction->SetStream (stream + 1);
183  return 2;
184 }
185 
186 
187 } // namespace ns3
double x
Definition: vector.h:49
keep track of time unit.
Definition: nstime.h:149
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:89
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void DoDispose(void)
Definition: object.cc:335
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
a 3d vector
Definition: vector.h:31
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
virtual void DoSetPosition(const Vector &position)
bool IsInside(const Vector &position) const
Definition: rectangle.cc:48
void NotifyCourseChange(void) const
static void Remove(const EventId &id)
Definition: simulator.cc:257
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Definition: simulator.h:981
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
void Cancel(void)
Definition: event-id.cc:47
virtual void DoInitialize(void)
Definition: object.cc:342