A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
random-waypoint-mobility-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
20
21#include "position-allocator.h"
22
23#include "ns3/pointer.h"
24#include "ns3/random-variable-stream.h"
25#include "ns3/simulator.h"
26#include "ns3/string.h"
27
28#include <cmath>
29
30namespace ns3
31{
32
33NS_OBJECT_ENSURE_REGISTERED(RandomWaypointMobilityModel);
34
35TypeId
37{
38 static TypeId tid =
39 TypeId("ns3::RandomWaypointMobilityModel")
41 .SetGroupName("Mobility")
42 .AddConstructor<RandomWaypointMobilityModel>()
43 .AddAttribute("Speed",
44 "A random variable used to pick the speed of a random waypoint model.",
45 StringValue("ns3::UniformRandomVariable[Min=0.3|Max=0.7]"),
47 MakePointerChecker<RandomVariableStream>())
48 .AddAttribute("Pause",
49 "A random variable used to pick the pause of a random waypoint model.",
50 StringValue("ns3::ConstantRandomVariable[Constant=2.0]"),
52 MakePointerChecker<RandomVariableStream>())
53 .AddAttribute("PositionAllocator",
54 "The position model used to pick a destination point.",
57 MakePointerChecker<PositionAllocator>());
58
59 return tid;
60}
61
62void
64{
66 Vector m_current = m_helper.GetCurrentPosition();
67 NS_ASSERT_MSG(m_position, "No position allocator added before using this model");
68 Vector destination = m_position->GetNext();
69 Vector delta = destination - m_current;
70 double distance = delta.GetLength();
71 double speed = m_speed->GetValue();
72
73 NS_ASSERT_MSG(speed > 0, "Speed must be strictly positive.");
74
75 // Note: the following two lines are needed to prevent corner cases where
76 // the distance is null (and the Velocity is undefined).
77 double k = distance ? speed / distance : 0;
78 Time travelDelay = distance ? Seconds(distance / speed) : Time(0);
79
80 m_helper.SetVelocity(k * delta);
83 m_event =
86}
87
88void
90{
93}
94
95void
97{
100 Time pause = Seconds(m_pause->GetValue());
103}
104
105Vector
107{
110}
111
112void
114{
115 m_helper.SetPosition(position);
116 m_event.Cancel();
118}
119
120Vector
122{
123 return m_helper.GetVelocity();
124}
125
126int64_t
128{
129 int64_t positionStreamsAllocated;
130 m_speed->SetStream(stream);
131 m_pause->SetStream(stream + 1);
132 NS_ASSERT_MSG(m_position, "No position allocator added before using this model");
133 positionStreamsAllocated = m_position->AssignStreams(stream + 2);
134 return (2 + positionStreamsAllocated);
135}
136
137} // namespace ns3
Vector GetCurrentPosition() const
Get current position vector.
Vector GetVelocity() const
Get velocity; if paused, will return a zero vector.
void Update() const
Update position, if not paused, from last position and time of last update.
void Unpause()
Resume mobility from current position at current velocity.
void SetPosition(const Vector &position)
Set position vector.
void SetVelocity(const Vector &vel)
Set new velocity vector.
void Pause()
Pause mobility at current position.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
Keep track of the current position and velocity of an object.
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:451
AttributeValue implementation for Pointer.
Definition: pointer.h:48
virtual double GetValue()=0
Get the next random value drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
void BeginWalk()
Get next position, begin moving towards it, schedule future pause event.
ConstantVelocityHelper m_helper
helper for velocity computations
void DoInitializePrivate()
Begin current pause event, schedule future walk event.
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
Ptr< PositionAllocator > m_position
pointer to position allocator
Ptr< RandomVariableStream > m_speed
random variable to generate speeds
void DoInitialize() override
Initialize() implementation.
static TypeId GetTypeId()
Register this type with the TypeId system.
Ptr< RandomVariableStream > m_pause
random variable to generate pauses
EventId m_event
event ID of next scheduled event
void DoSetPosition(const Vector &position) override
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.