A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
random-waypoint-mobility-model.cc
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"
25 #include "random-waypoint-mobility-model.h"
26 #include "position-allocator.h"
27 
28 namespace ns3 {
29 
30 NS_OBJECT_ENSURE_REGISTERED (RandomWaypointMobilityModel);
31 
32 TypeId
33 RandomWaypointMobilityModel::GetTypeId (void)
34 {
35  static TypeId tid = TypeId ("ns3::RandomWaypointMobilityModel")
36  .SetParent<MobilityModel> ()
37  .SetGroupName ("Mobility")
38  .AddConstructor<RandomWaypointMobilityModel> ()
39  .AddAttribute ("Speed",
40  "A random variable used to pick the speed of a random waypoint model.",
41  StringValue ("ns3::UniformRandomVariable[Min=0.3|Max=0.7]"),
42  MakePointerAccessor (&RandomWaypointMobilityModel::m_speed),
43  MakePointerChecker<RandomVariableStream> ())
44  .AddAttribute ("Pause",
45  "A random variable used to pick the pause of a random waypoint model.",
46  StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"),
47  MakePointerAccessor (&RandomWaypointMobilityModel::m_pause),
48  MakePointerChecker<RandomVariableStream> ())
49  .AddAttribute ("PositionAllocator",
50  "The position model used to pick a destination point.",
51  PointerValue (),
52  MakePointerAccessor (&RandomWaypointMobilityModel::m_position),
53  MakePointerChecker<PositionAllocator> ());
54 
55  return tid;
56 }
57 
58 void
59 RandomWaypointMobilityModel::BeginWalk (void)
60 {
61  m_helper.Update ();
62  Vector m_current = m_helper.GetCurrentPosition ();
63  NS_ASSERT_MSG (m_position, "No position allocator added before using this model");
64  Vector destination = m_position->GetNext ();
65  double speed = m_speed->GetValue ();
66  double dx = (destination.x - m_current.x);
67  double dy = (destination.y - m_current.y);
68  double dz = (destination.z - m_current.z);
69  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
70 
71  m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
72  m_helper.Unpause ();
73  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
74  m_event.Cancel ();
75  m_event = Simulator::Schedule (travelDelay,
76  &RandomWaypointMobilityModel::DoInitializePrivate, this);
78 }
79 
80 void
82 {
83  DoInitializePrivate ();
85 }
86 
87 void
88 RandomWaypointMobilityModel::DoInitializePrivate (void)
89 {
90  m_helper.Update ();
91  m_helper.Pause ();
92  Time pause = Seconds (m_pause->GetValue ());
93  m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::BeginWalk, this);
95 }
96 
97 Vector
99 {
100  m_helper.Update ();
101  return m_helper.GetCurrentPosition ();
102 }
103 void
105 {
106  m_helper.SetPosition (position);
107  Simulator::Remove (m_event);
108  m_event = Simulator::ScheduleNow (&RandomWaypointMobilityModel::DoInitializePrivate, this);
109 }
110 Vector
112 {
113  return m_helper.GetVelocity ();
114 }
115 int64_t
117 {
118  int64_t positionStreamsAllocated;
119  m_speed->SetStream (stream);
120  m_pause->SetStream (stream + 1);
121  NS_ASSERT_MSG (m_position, "No position allocator added before using this model");
122  positionStreamsAllocated = m_position->AssignStreams (stream + 2);
123  return (2 + positionStreamsAllocated);
124 }
125 
126 
127 } // namespace ns3
keep track of time unit.
Definition: nstime.h:149
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
a 3d vector
Definition: vector.h:31
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:71
void NotifyCourseChange(void) const
static void Remove(const EventId &id)
Definition: simulator.cc:257
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Definition: simulator.h:981
virtual void DoSetPosition(const Vector &position)
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
void Cancel(void)
Definition: event-id.cc:47
virtual void DoInitialize(void)
Definition: object.cc:342