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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
9
10#include "position-allocator.h"
11
12#include "ns3/pointer.h"
13#include "ns3/random-variable-stream.h"
14#include "ns3/simulator.h"
15#include "ns3/string.h"
16
17#include <cmath>
18
19namespace ns3
20{
21
23
26{
27 static TypeId tid =
28 TypeId("ns3::RandomWaypointMobilityModel")
30 .SetGroupName("Mobility")
31 .AddConstructor<RandomWaypointMobilityModel>()
32 .AddAttribute("Speed",
33 "A random variable used to pick the speed of a random waypoint model.",
34 StringValue("ns3::UniformRandomVariable[Min=0.3|Max=0.7]"),
37 .AddAttribute("Pause",
38 "A random variable used to pick the pause of a random waypoint model.",
39 StringValue("ns3::ConstantRandomVariable[Constant=2.0]"),
42 .AddAttribute("PositionAllocator",
43 "The position model used to pick a destination point.",
47
48 return tid;
49}
50
55
56void
58{
59 m_helper.Update();
60 Vector m_current = m_helper.GetCurrentPosition();
61 NS_ASSERT_MSG(m_position, "No position allocator added before using this model");
62 Vector destination = m_position->GetNext();
63 Vector delta = destination - m_current;
64 double distance = delta.GetLength();
65 double speed = m_speed->GetValue();
66
67 NS_ASSERT_MSG(speed > 0, "Speed must be strictly positive.");
68
69 // Note: the following two lines are needed to prevent corner cases where
70 // the distance is null (and the Velocity is undefined).
71 double k = distance ? speed / distance : 0;
72 Time travelDelay = distance ? Seconds(distance / speed) : Time(0);
73
74 m_helper.SetVelocity(k * delta);
75 m_helper.Unpause();
76 m_event.Cancel();
77 m_event =
80}
81
82void
88
89void
98
99Vector
101{
102 m_helper.Update();
103 return m_helper.GetCurrentPosition();
104}
105
106void
108{
109 m_helper.SetPosition(position);
110 m_event.Cancel();
112}
113
114Vector
116{
117 return m_helper.GetVelocity();
118}
119
120int64_t
122{
123 int64_t positionStreamsAllocated;
124 m_speed->SetStream(stream);
125 m_pause->SetStream(stream + 1);
126 NS_ASSERT_MSG(m_position, "No position allocator added before using this model");
127 positionStreamsAllocated = m_position->AssignStreams(stream + 2);
128 return (2 + positionStreamsAllocated);
129}
130
131} // namespace ns3
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:440
AttributeValue implementation for Pointer.
Definition pointer.h:37
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:561
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#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:75
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:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
Every class exported by the ns3 library is enclosed in the ns3 namespace.