A Discrete-Event Network Simulator
API
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 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("RandomWalk2d");
32 
33 NS_OBJECT_ENSURE_REGISTERED (RandomWalk2dMobilityModel);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::RandomWalk2dMobilityModel")
40  .SetGroupName ("Mobility")
41  .AddConstructor<RandomWalk2dMobilityModel> ()
42  .AddAttribute ("Bounds",
43  "Bounds of the area to cruise.",
44  RectangleValue (Rectangle (0.0, 100.0, 0.0, 100.0)),
47  .AddAttribute ("Time",
48  "Change current direction and speed after moving for this delay.",
49  TimeValue (Seconds (1.0)),
51  MakeTimeChecker ())
52  .AddAttribute ("Distance",
53  "Change current direction and speed after moving for this distance.",
54  DoubleValue (1.0),
56  MakeDoubleChecker<double> ())
57  .AddAttribute ("Mode",
58  "The mode indicates the condition used to "
59  "change the current speed and direction",
64  .AddAttribute ("Direction",
65  "A random variable used to pick the direction (radians).",
66  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
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]"),
73  MakePointerChecker<RandomVariableStream> ());
74  return tid;
75 }
76 
77 void
79 {
82 }
83 
84 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;
98  {
99  delayLeft = m_modeTime;
100  }
101  else
102  {
103  delayLeft = Seconds (m_modeDistance / speed);
104  }
105  DoWalk (delayLeft);
106 }
107 
108 void
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  {
120  }
121  else
122  {
123  nextPosition = m_bounds.CalculateIntersection (position, speed);
124  Time delay = Seconds ((nextPosition.x - position.x) / speed.x);
126  delayLeft - delay);
127  }
129 }
130 
131 void
133 {
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 {
163  return m_helper.GetCurrentPosition ();
164 }
165 void
167 {
168  NS_ASSERT (m_bounds.IsInside (position));
169  m_helper.SetPosition (position);
170  m_event.Cancel ();
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
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::Rectangle::RIGHT
@ RIGHT
Definition: rectangle.h:41
ns3::ConstantVelocityHelper::SetVelocity
void SetVelocity(const Vector &vel)
Set new velocity vector.
Definition: constant-velocity-helper.cc:72
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#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
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeEnumChecker
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:161
ns3::RandomWalk2dMobilityModel::Rebound
void Rebound(Time timeLeft)
Performs the rebound of the node if it reaches a boundary.
Definition: random-walk-2d-mobility-model.cc:132
ns3::Rectangle::LEFT
@ LEFT
Definition: rectangle.h:42
ns3::RandomWalk2dMobilityModel::MODE_TIME
@ MODE_TIME
Definition: random-walk-2d-mobility-model.h:57
ns3::RectangleValue
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
ns3::MobilityModel::NotifyCourseChange
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
Definition: mobility-model.cc:108
ns3::RandomWalk2dMobilityModel::m_speed
Ptr< RandomVariableStream > m_speed
rv for picking speed
Definition: random-walk-2d-mobility-model.h:88
ns3::Rectangle::TOP
@ TOP
Definition: rectangle.h:43
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::EnumValue
Hold variables of type enum.
Definition: enum.h:55
ns3::RandomWalk2dMobilityModel::DoAssignStreams
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
Definition: random-walk-2d-mobility-model.cc:179
ns3::MakeRectangleAccessor
Ptr< const AttributeAccessor > MakeRectangleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: rectangle.h:97
ns3::RandomWalk2dMobilityModel::m_modeTime
Time m_modeTime
Change current direction and speed after this delay.
Definition: random-walk-2d-mobility-model.h:87
ns3::RandomWalk2dMobilityModel::GetTypeId
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Definition: random-walk-2d-mobility-model.cc:36
ns3::RandomWalk2dMobilityModel::m_event
EventId m_event
stored event ID
Definition: random-walk-2d-mobility-model.h:84
ns3::ConstantVelocityHelper::GetCurrentPosition
Vector GetCurrentPosition(void) const
Get current position vector.
Definition: constant-velocity-helper.cc:59
ns3::EventId::Cancel
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
ns3::RandomWalk2dMobilityModel::m_helper
ConstantVelocityHelper m_helper
helper for this object
Definition: random-walk-2d-mobility-model.h:83
ns3::Rectangle::CalculateIntersection
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:177
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::RandomWalk2dMobilityModel::DoInitialize
virtual void DoInitialize(void)
Initialize() implementation.
Definition: random-walk-2d-mobility-model.cc:78
ns3::RandomWalk2dMobilityModel::MODE_DISTANCE
@ MODE_DISTANCE
Definition: random-walk-2d-mobility-model.h:56
ns3::ConstantVelocityHelper::Unpause
void Unpause(void)
Resume mobility from current position at current velocity.
Definition: constant-velocity-helper.cc:129
ns3::Object::DoInitialize
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
ns3::Rectangle
a 2d rectangle
Definition: rectangle.h:35
ns3::MakePointerAccessor
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: pointer.h:227
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::MakeDoubleAccessor
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: double.h:42
ns3::RandomWalk2dMobilityModel::DoGetPosition
virtual Vector DoGetPosition(void) const
Definition: random-walk-2d-mobility-model.cc:160
random-walk-2d-mobility-model.h
ns3::RandomWalk2dMobilityModel::DoWalk
void DoWalk(Time timeLeft)
Walk according to position and velocity, until distance is reached, time is reached,...
Definition: random-walk-2d-mobility-model.cc:109
ns3::RandomWalk2dMobilityModel::DoSetPosition
virtual void DoSetPosition(const Vector &position)
Definition: random-walk-2d-mobility-model.cc:166
ns3::RandomWalk2dMobilityModel::DoInitializePrivate
void DoInitializePrivate(void)
Perform initialization of the object before MobilityModel::DoInitialize ()
Definition: random-walk-2d-mobility-model.cc:85
ns3::RandomWalk2dMobilityModel::DoGetVelocity
virtual Vector DoGetVelocity(void) const
Definition: random-walk-2d-mobility-model.cc:174
ns3::RandomVariableStream::GetValue
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
ns3::ConstantVelocityHelper::UpdateWithBounds
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
Definition: constant-velocity-helper.cc:98
ns3::MakeRectangleChecker
Ptr< const AttributeChecker > MakeRectangleChecker(void)
Definition: rectangle.cc:213
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::RandomWalk2dMobilityModel::m_bounds
Rectangle m_bounds
Bounds of the area to cruise.
Definition: random-walk-2d-mobility-model.h:90
ns3::ConstantVelocityHelper::Update
void Update(void) const
Update position, if not paused, from last position and time of last update.
Definition: constant-velocity-helper.cc:80
ns3::MakeEnumAccessor
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: enum.h:203
ns3::Rectangle::GetClosestSide
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::ConstantVelocityHelper::SetPosition
void SetPosition(const Vector &position)
Set position vector.
Definition: constant-velocity-helper.cc:50
ns3::RandomWalk2dMobilityModel::m_direction
Ptr< RandomVariableStream > m_direction
rv for picking direction
Definition: random-walk-2d-mobility-model.h:89
ns3::MobilityModel
Keep track of the current position and velocity of an object.
Definition: mobility-model.h:40
ns3::RandomVariableStream::SetStream
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Definition: random-variable-stream.cc:100
ns3::RandomWalk2dMobilityModel::m_modeDistance
double m_modeDistance
Change direction and speed after this distance.
Definition: random-walk-2d-mobility-model.h:86
ns3::Rectangle::IsInside
bool IsInside(const Vector &position) const
Definition: rectangle.cc:48
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::RandomWalk2dMobilityModel::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: random-walk-2d-mobility-model.cc:154
ns3::Simulator::ScheduleNow
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
ns3::RandomWalk2dMobilityModel::m_mode
enum Mode m_mode
whether in time or distance mode
Definition: random-walk-2d-mobility-model.h:85
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
ns3::MakeTimeAccessor
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1354
ns3::ConstantVelocityHelper::GetVelocity
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
Definition: constant-velocity-helper.cc:66
ns3::Rectangle::BOTTOM
@ BOTTOM
Definition: rectangle.h:44
ns3::RandomWalk2dMobilityModel
2D random walk mobility model.
Definition: random-walk-2d-mobility-model.h:47