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);
148  m_event.Cancel ();
150 }
151 Vector
153 {
154  return m_helper.GetVelocity ();
155 }
156 int64_t
158 {
159  m_direction->SetStream (stream);
160  m_speed->SetStream (stream + 1);
161  m_pause->SetStream (stream + 2);
162  return 3;
163 }
164 
165 
166 
167 } // namespace ns3
Ptr< UniformRandomVariable > m_direction
rv to control direction
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Hold variables of type string.
Definition: string.h:41
Vector GetCurrentPosition(void) const
Get current position vector.
Ptr< RandomVariableStream > m_speed
a random variable to control speed
void ResetDirectionAndSpeed(void)
Set a new direction and speed.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
ConstantVelocityHelper m_helper
helper for velocity computations
virtual void DoInitialize(void)
Initialize() implementation.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
void DoInitializePrivate(void)
Sets a new random direction and calls SetDirectionAndSpeed.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
Keep track of the current position and velocity of an object.
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:220
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
void Update(void) const
Update position, if not paused, from last position and time of last update.
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:89
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:92
virtual void DoSetPosition(const Vector &position)
virtual void DoDispose(void)
Destructor implementation.
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
void SetVelocity(const Vector &vel)
Set new velocity vector.
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1570
EventId m_event
event ID of next scheduled event
static TypeId GetTypeId(void)
Register this type with the TypeId system.
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDirectionAndSpeed(double direction)
Set new velocity and direction, and schedule next pause event
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
void Unpause(void)
Resume mobility from current position at current velocity.
Ptr< RandomVariableStream > m_pause
a random variable to control pause
Ptr< const AttributeChecker > MakeRectangleChecker(void)
Definition: rectangle.cc:125
void BeginPause(void)
Pause, cancel currently scheduled event, schedule end of pause event.
void SetPosition(const Vector &position)
Set position vector.
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
a unique identifier for an interface.
Definition: type-id.h:58
void Pause(void)
Pause mobility at current position.
a 2d rectangle
Definition: rectangle.h:34
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915