A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
random-waypoint-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 <cmath>
21 #include "ns3/simulator.h"
22 #include "ns3/random-variable-stream.h"
23 #include "ns3/pointer.h"
24 #include "ns3/string.h"
26 #include "position-allocator.h"
27 
28 namespace ns3 {
29 
30 NS_OBJECT_ENSURE_REGISTERED (RandomWaypointMobilityModel)
31  ;
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::RandomWaypointMobilityModel")
38  .SetGroupName ("Mobility")
39  .AddConstructor<RandomWaypointMobilityModel> ()
40  .AddAttribute ("Speed",
41  "A random variable used to pick the speed of a random waypoint model.",
42  StringValue ("ns3::UniformRandomVariable[Min=0.3|Max=0.7]"),
43  MakePointerAccessor (&RandomWaypointMobilityModel::m_speed),
44  MakePointerChecker<RandomVariableStream> ())
45  .AddAttribute ("Pause",
46  "A random variable used to pick the pause of a random waypoint model.",
47  StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"),
48  MakePointerAccessor (&RandomWaypointMobilityModel::m_pause),
49  MakePointerChecker<RandomVariableStream> ())
50  .AddAttribute ("PositionAllocator",
51  "The position model used to pick a destination point.",
52  PointerValue (),
53  MakePointerAccessor (&RandomWaypointMobilityModel::m_position),
54  MakePointerChecker<PositionAllocator> ());
55 
56  return tid;
57 }
58 
59 void
61 {
62  m_helper.Update ();
63  Vector m_current = m_helper.GetCurrentPosition ();
64  NS_ASSERT_MSG (m_position, "No position allocator added before using this model");
65  Vector destination = m_position->GetNext ();
66  double speed = m_speed->GetValue ();
67  double dx = (destination.x - m_current.x);
68  double dy = (destination.y - m_current.y);
69  double dz = (destination.z - m_current.z);
70  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
71 
72  m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
73  m_helper.Unpause ();
74  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
75  m_event.Cancel ();
76  m_event = Simulator::Schedule (travelDelay,
79 }
80 
81 void
83 {
86 }
87 
88 void
90 {
91  m_helper.Update ();
92  m_helper.Pause ();
93  Time pause = Seconds (m_pause->GetValue ());
96 }
97 
98 Vector
100 {
101  m_helper.Update ();
102  return m_helper.GetCurrentPosition ();
103 }
104 void
106 {
107  m_helper.SetPosition (position);
110 }
111 Vector
113 {
114  return m_helper.GetVelocity ();
115 }
116 int64_t
118 {
119  int64_t positionStreamsAllocated;
120  m_speed->SetStream (stream);
121  m_pause->SetStream (stream + 1);
122  NS_ASSERT_MSG (m_position, "No position allocator added before using this model");
123  positionStreamsAllocated = m_position->AssignStreams (stream + 2);
124  return (2 + positionStreamsAllocated);
125 }
126 
127 
128 } // namespace ns3
double x
x coordinate of vector
Definition: vector.h:49
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
hold variables of type string
Definition: string.h:19
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
a 3d vector
Definition: vector.h:31
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
Keep track of the current position and velocity of an object.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:71
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
hold objects of type Ptr
Definition: pointer.h:33
void SetVelocity(const Vector &vel)
double y
y coordinate of vector
Definition: vector.h:53
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
virtual void DoSetPosition(const Vector &position)
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
void SetPosition(const Vector &position)
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:343
double z
z coordinate of vector
Definition: vector.h:57