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
22NS_OBJECT_ENSURE_REGISTERED(RandomWaypointMobilityModel);
23
24TypeId
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{
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);
77 m_event =
80}
81
82void
88
89void
98
99Vector
105
106void
113
114Vector
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
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:44
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:440
AttributeValue implementation for Pointer.
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:560
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
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:48
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:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.