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 double speed = m_speed->GetValue();
70 double dx = (destination.x - m_current.x);
71 double dy = (destination.y - m_current.y);
72 double dz = (destination.z - m_current.z);
73 double k = speed / std::sqrt(dx * dx + dy * dy + dz * dz);
74
75 m_helper.SetVelocity(Vector(k * dx, k * dy, k * dz));
77 Time travelDelay = Seconds(CalculateDistance(destination, m_current) / speed);
79 m_event =
82}
83
84void
86{
89}
90
91void
93{
96 Time pause = Seconds(m_pause->GetValue());
99}
100
101Vector
103{
106}
107
108void
110{
111 m_helper.SetPosition(position);
112 m_event.Cancel();
114}
115
116Vector
118{
119 return m_helper.GetVelocity();
120}
121
122int64_t
124{
125 int64_t positionStreamsAllocated;
126 m_speed->SetStream(stream);
127 m_pause->SetStream(stream + 1);
128 NS_ASSERT_MSG(m_position, "No position allocator added before using this model");
129 positionStreamsAllocated = m_position->AssignStreams(stream + 2);
130 return (2 + positionStreamsAllocated);
131}
132
133} // 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.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:109