A Discrete-Event Network Simulator
API
random-direction-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) 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 "ns3/simulator.h"
21 #include <algorithm>
22 #include <cmath>
23 #include "ns3/log.h"
24 #include "ns3/string.h"
25 #include "ns3/pointer.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("RandomDirection2dMobilityModel");
31 
32 NS_OBJECT_ENSURE_REGISTERED (RandomDirection2dMobilityModel);
33 
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::RandomDirection2dMobilityModel")
40  .SetGroupName ("Mobility")
41  .AddConstructor<RandomDirection2dMobilityModel> ()
42  .AddAttribute ("Bounds", "The 2d bounding area",
43  RectangleValue (Rectangle (-100, 100, -100, 100)),
46  .AddAttribute ("Speed", "A random variable to control the speed (m/s).",
47  StringValue ("ns3::UniformRandomVariable[Min=1.0|Max=2.0]"),
49  MakePointerChecker<RandomVariableStream> ())
50  .AddAttribute ("Pause", "A random variable to control the pause (s).",
51  StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"),
53  MakePointerChecker<RandomVariableStream> ())
54  ;
55  return tid;
56 }
57 
59 {
60  m_direction = CreateObject <UniformRandomVariable> ();
61 }
62 
63 void
65 {
66  // chain up.
68 }
69 void
71 {
74 }
75 
76 void
78 {
79  double direction = m_direction->GetValue (0, 2 * M_PI);
80  SetDirectionAndSpeed (direction);
81 }
82 
83 void
85 {
86  m_helper.Update ();
87  m_helper.Pause ();
88  Time pause = Seconds (m_pause->GetValue ());
89  m_event.Cancel ();
92 }
93 
94 void
96 {
99  Vector position = m_helper.GetCurrentPosition ();
100  double speed = m_speed->GetValue ();
101  const Vector vector (std::cos (direction) * speed,
102  std::sin (direction) * speed,
103  0.0);
104  m_helper.SetVelocity (vector);
105  m_helper.Unpause ();
106  Vector next = m_bounds.CalculateIntersection (position, vector);
107  Time delay = Seconds (CalculateDistance (position, next) / speed);
108  m_event.Cancel ();
109  m_event = Simulator::Schedule (delay,
112 }
113 void
115 {
116  double direction = m_direction->GetValue (0, M_PI);
117 
119  Vector position = m_helper.GetCurrentPosition ();
120  switch (m_bounds.GetClosestSide (position))
121  {
122  case Rectangle::RIGHT:
123  direction += M_PI / 2;
124  break;
125  case Rectangle::LEFT:
126  direction += -M_PI / 2;
127  break;
128  case Rectangle::TOP:
129  direction += M_PI;
130  break;
131  case Rectangle::BOTTOM:
132  direction += 0.0;
133  break;
134  }
135  SetDirectionAndSpeed (direction);
136 }
137 Vector
139 {
141  return m_helper.GetCurrentPosition ();
142 }
143 void
145 {
146  m_helper.SetPosition (position);
147  m_event.Cancel ();
149 }
150 Vector
152 {
153  return m_helper.GetVelocity ();
154 }
155 int64_t
157 {
158  m_direction->SetStream (stream);
159  m_speed->SetStream (stream + 1);
160  m_pause->SetStream (stream + 2);
161  return 3;
162 }
163 
164 
165 
166 } // namespace ns3
void Unpause(void)
Resume mobility from current position at current velocity.
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
void SetPosition(const Vector &position)
Set position vector.
void Pause(void)
Pause mobility at current position.
void SetVelocity(const Vector &vel)
Set new velocity vector.
void Update(void) const
Update position, if not paused, from last position and time of last update.
Vector GetCurrentPosition(void) const
Get current position vector.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
Keep track of the current position and velocity of an object.
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
static TypeId GetTypeId(void)
Register this type with the TypeId system.
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
void SetDirectionAndSpeed(double direction)
Set new velocity and direction, and schedule next pause event
EventId m_event
event ID of next scheduled event
virtual void DoDispose(void)
Destructor implementation.
ConstantVelocityHelper m_helper
helper for velocity computations
void DoInitializePrivate(void)
Sets a new random direction and calls SetDirectionAndSpeed.
void BeginPause(void)
Pause, cancel currently scheduled event, schedule end of pause event.
Ptr< RandomVariableStream > m_speed
a random variable to control speed
void ResetDirectionAndSpeed(void)
Set a new direction and speed.
virtual void DoInitialize(void)
Initialize() implementation.
Ptr< UniformRandomVariable > m_direction
rv to control direction
virtual void DoSetPosition(const Vector &position)
Ptr< RandomVariableStream > m_pause
a random variable to control pause
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
a 2d rectangle
Definition: rectangle.h:35
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:177
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
Hold variables of type string.
Definition: string.h:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
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
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
Ptr< const AttributeChecker > MakeRectangleChecker(void)
Definition: rectangle.cc:213
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:105