A Discrete-Event Network Simulator
API
random-walk-2d-mobility-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006,2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
20 
21 #include "ns3/double.h"
22 #include "ns3/enum.h"
23 #include "ns3/log.h"
24 #include "ns3/pointer.h"
25 #include "ns3/simulator.h"
26 #include "ns3/string.h"
27 
28 #include <cmath>
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("RandomWalk2d");
34 
35 NS_OBJECT_ENSURE_REGISTERED(RandomWalk2dMobilityModel);
36 
37 TypeId
39 {
40  static TypeId tid =
41  TypeId("ns3::RandomWalk2dMobilityModel")
43  .SetGroupName("Mobility")
44  .AddConstructor<RandomWalk2dMobilityModel>()
45  .AddAttribute("Bounds",
46  "Bounds of the area to cruise.",
47  RectangleValue(Rectangle(0.0, 100.0, 0.0, 100.0)),
48  MakeRectangleAccessor(&RandomWalk2dMobilityModel::m_bounds),
49  MakeRectangleChecker())
50  .AddAttribute("Time",
51  "Change current direction and speed after moving for this delay.",
52  TimeValue(Seconds(1.0)),
55  .AddAttribute("Distance",
56  "Change current direction and speed after moving for this distance.",
57  DoubleValue(1.0),
59  MakeDoubleChecker<double>())
60  .AddAttribute("Mode",
61  "The mode indicates the condition used to "
62  "change the current speed and direction",
64  MakeEnumAccessor<RandomWalk2dMobilityModel::Mode>(
67  "Distance",
69  "Time"))
70  .AddAttribute("Direction",
71  "A random variable used to pick the direction (radians).",
72  StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
74  MakePointerChecker<RandomVariableStream>())
75  .AddAttribute("Speed",
76  "A random variable used to pick the speed (m/s).",
77  StringValue("ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
79  MakePointerChecker<RandomVariableStream>());
80  return tid;
81 }
82 
83 void
85 {
88 }
89 
90 void
92 {
93  m_helper.Update();
94  Vector position = m_helper.GetCurrentPosition();
95 
96  double speed = m_speed->GetValue();
97  double direction = 0;
98  if (!m_bounds.IsOnTheBorder(position))
99  {
100  direction = m_direction->GetValue();
101  }
102  else
103  {
104  Ptr<UniformRandomVariable> directionOverride = CreateObject<UniformRandomVariable>();
105  switch (m_bounds.GetClosestSideOrCorner(position))
106  {
108  direction = directionOverride->GetValue(M_PI_2, M_PI + M_PI_2);
109  break;
110  case Rectangle::LEFTSIDE:
111  direction = directionOverride->GetValue(-M_PI_2, M_PI - M_PI_2);
112  break;
113  case Rectangle::TOPSIDE:
114  direction = directionOverride->GetValue(M_PI, 2 * M_PI);
115  break;
117  direction = directionOverride->GetValue(0, M_PI);
118  break;
120  direction = directionOverride->GetValue(M_PI, M_PI + M_PI_2);
121  break;
123  direction = directionOverride->GetValue(M_PI + M_PI_2, 2 * M_PI);
124  break;
126  direction = directionOverride->GetValue(0, M_PI_2);
127  direction += M_PI / 2;
128  break;
130  direction = directionOverride->GetValue(M_PI_2, M_PI);
131  break;
132  }
133  }
134  Vector vector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
135  m_helper.SetVelocity(vector);
136  m_helper.Unpause();
137 
138  Time delayLeft;
140  {
141  delayLeft = m_modeTime;
142  }
143  else
144  {
145  delayLeft = Seconds(m_modeDistance / speed);
146  }
147  DoWalk(delayLeft);
148 }
149 
150 void
152 {
153  Vector position = m_helper.GetCurrentPosition();
154  Vector speed = m_helper.GetVelocity();
155  Vector nextPosition = position;
156  nextPosition.x += speed.x * delayLeft.GetSeconds();
157  nextPosition.y += speed.y * delayLeft.GetSeconds();
158  m_event.Cancel();
159  if (m_bounds.IsInside(nextPosition))
160  {
161  m_event =
163  }
164  else
165  {
166  nextPosition = m_bounds.CalculateIntersection(position, speed);
167  double delaySeconds = std::numeric_limits<double>::max();
168  if (speed.x != 0)
169  {
170  delaySeconds =
171  std::min(delaySeconds, std::abs((nextPosition.x - position.x) / speed.x));
172  }
173  else if (speed.y != 0)
174  {
175  delaySeconds =
176  std::min(delaySeconds, std::abs((nextPosition.y - position.y) / speed.y));
177  }
178  else
179  {
180  NS_ABORT_MSG("RandomWalk2dMobilityModel::DoWalk: unable to calculate the rebound time "
181  "(the node is stationary).");
182  }
183  Time delay = Seconds(delaySeconds);
186  this,
187  delayLeft - delay);
188  }
190 }
191 
192 void
194 {
196  Vector position = m_helper.GetCurrentPosition();
197  Vector speed = m_helper.GetVelocity();
198  switch (m_bounds.GetClosestSideOrCorner(position))
199  {
201  case Rectangle::LEFTSIDE:
202  speed.x = -speed.x;
203  break;
204  case Rectangle::TOPSIDE:
206  speed.y = -speed.y;
207  break;
212  auto temp = speed.x;
213  speed.x = -speed.y;
214  speed.y = -temp;
215  break;
216  }
217  m_helper.SetVelocity(speed);
218  m_helper.Unpause();
219  DoWalk(delayLeft);
220 }
221 
222 void
224 {
225  // chain up
227 }
228 
229 Vector
231 {
233  return m_helper.GetCurrentPosition();
234 }
235 
236 void
238 {
239  NS_ASSERT(m_bounds.IsInside(position));
240  m_helper.SetPosition(position);
241  m_event.Cancel();
243 }
244 
245 Vector
247 {
248  return m_helper.GetVelocity();
249 }
250 
251 int64_t
253 {
254  m_speed->SetStream(stream);
255  m_direction->SetStream(stream + 1);
256  return 2;
257 }
258 
259 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
Vector GetCurrentPosition() const
Get current position vector.
Vector GetVelocity() const
Get velocity; if paused, will return a zero vector.
void Update() const
Update position, if not paused, from last position and time of last update.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
void Unpause()
Resume mobility from current position at current velocity.
void SetPosition(const Vector &position)
Set position vector.
void SetVelocity(const Vector &vel)
Set new velocity vector.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:62
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
Keep track of the current position and velocity of an object.
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:359
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
virtual double GetValue()=0
Get the next random value drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Ptr< RandomVariableStream > m_speed
rv for picking speed
static TypeId GetTypeId()
Register this type with the TypeId system.
void DoInitialize() override
Initialize() implementation.
double m_modeDistance
Change direction and speed after this distance.
void DoInitializePrivate()
Perform initialization of the object before MobilityModel::DoInitialize ()
void DoWalk(Time timeLeft)
Walk according to position and velocity, until distance is reached, time is reached,...
void Rebound(Time timeLeft)
Performs the rebound of the node if it reaches a boundary.
ConstantVelocityHelper m_helper
helper for this object
Time m_modeTime
Change current direction and speed after this delay.
Rectangle m_bounds
Bounds of the area to cruise.
void DoSetPosition(const Vector &position) override
void DoDispose() override
Destructor implementation.
Ptr< RandomVariableStream > m_direction
rv for picking direction
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
Mode m_mode
whether in time or distance mode
a 2d rectangle
Definition: rectangle.h:35
Side GetClosestSideOrCorner(const Vector &position) const
Definition: rectangle.cc:64
bool IsInside(const Vector &position) const
Definition: rectangle.cc:50
bool IsOnTheBorder(const Vector &position) const
Definition: rectangle.cc:57
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:152
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:194
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43