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
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::RandomDirection2dMobilityModel::RandomDirection2dMobilityModel
RandomDirection2dMobilityModel()
Definition: random-direction-2d-mobility-model.cc:58
ns3::RandomDirection2dMobilityModel::BeginPause
void BeginPause(void)
Pause, cancel currently scheduled event, schedule end of pause event.
Definition: random-direction-2d-mobility-model.cc:84
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
ns3::RandomDirection2dMobilityModel::DoInitialize
virtual void DoInitialize(void)
Initialize() implementation.
Definition: random-direction-2d-mobility-model.cc:70
ns3::RandomDirection2dMobilityModel::GetTypeId
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Definition: random-direction-2d-mobility-model.cc:36
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::RandomDirection2dMobilityModel::m_helper
ConstantVelocityHelper m_helper
helper for velocity computations
Definition: random-direction-2d-mobility-model.h:84
ns3::RandomDirection2dMobilityModel
Random direction mobility model.
Definition: random-direction-2d-mobility-model.h:45
ns3::RandomDirection2dMobilityModel::DoInitializePrivate
void DoInitializePrivate(void)
Sets a new random direction and calls SetDirectionAndSpeed.
Definition: random-direction-2d-mobility-model.cc:77
ns3::Rectangle::LEFT
@ LEFT
Definition: rectangle.h:42
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:95
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:557
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::RandomDirection2dMobilityModel::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: random-direction-2d-mobility-model.cc:64
ns3::RandomDirection2dMobilityModel::DoGetVelocity
virtual Vector DoGetVelocity(void) const
Definition: random-direction-2d-mobility-model.cc:151
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::RandomDirection2dMobilityModel::m_pause
Ptr< RandomVariableStream > m_pause
a random variable to control pause
Definition: random-direction-2d-mobility-model.h:82
ns3::ConstantVelocityHelper::GetCurrentPosition
Vector GetCurrentPosition(void) const
Get current position vector.
Definition: constant-velocity-helper.cc:59
random-direction-2d-mobility-model.h
ns3::EventId::Cancel
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
ns3::RandomDirection2dMobilityModel::DoAssignStreams
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
Definition: random-direction-2d-mobility-model.cc:156
ns3::Rectangle::CalculateIntersection
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:177
ns3::CalculateDistance
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:105
ns3::RandomDirection2dMobilityModel::m_bounds
Rectangle m_bounds
the 2D bounding area
Definition: random-direction-2d-mobility-model.h:80
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
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::RandomDirection2dMobilityModel::m_direction
Ptr< UniformRandomVariable > m_direction
rv to control direction
Definition: random-direction-2d-mobility-model.h:79
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
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
ns3::RandomDirection2dMobilityModel::SetDirectionAndSpeed
void SetDirectionAndSpeed(double direction)
Set new velocity and direction, and schedule next pause event
Definition: random-direction-2d-mobility-model.cc:95
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::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::RandomDirection2dMobilityModel::m_event
EventId m_event
event ID of next scheduled event
Definition: random-direction-2d-mobility-model.h:83
ns3::Rectangle::GetClosestSide
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
ns3::ConstantVelocityHelper::SetPosition
void SetPosition(const Vector &position)
Set position vector.
Definition: constant-velocity-helper.cc:50
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::RandomDirection2dMobilityModel::m_speed
Ptr< RandomVariableStream > m_speed
a random variable to control speed
Definition: random-direction-2d-mobility-model.h:81
ns3::ConstantVelocityHelper::Pause
void Pause(void)
Pause mobility at current position.
Definition: constant-velocity-helper.cc:122
ns3::UniformRandomVariable::GetValue
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
Definition: random-variable-stream.cc:182
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::Simulator::ScheduleNow
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:588
ns3::RandomDirection2dMobilityModel::DoSetPosition
virtual void DoSetPosition(const Vector &position)
Definition: random-direction-2d-mobility-model.cc:144
ns3::ConstantVelocityHelper::GetVelocity
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
Definition: constant-velocity-helper.cc:66
ns3::RandomDirection2dMobilityModel::DoGetPosition
virtual Vector DoGetPosition(void) const
Definition: random-direction-2d-mobility-model.cc:138
ns3::Rectangle::BOTTOM
@ BOTTOM
Definition: rectangle.h:44
ns3::RandomDirection2dMobilityModel::ResetDirectionAndSpeed
void ResetDirectionAndSpeed(void)
Set a new direction and speed.
Definition: random-direction-2d-mobility-model.cc:114