A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
random-walk-2d-mobility-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006,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 "ns3/double.h"
22#include "ns3/enum.h"
23#include "ns3/log.h"
24#include "ns3/pointer.h"
25#include "ns3/simulator.h"
26#include "ns3/string.h"
27
28#include <cmath>
29
30namespace ns3
31{
32
33NS_LOG_COMPONENT_DEFINE("RandomWalk2d");
34
35NS_OBJECT_ENSURE_REGISTERED(RandomWalk2dMobilityModel);
36
37TypeId
39{
40 static TypeId tid =
41 TypeId("ns3::RandomWalk2dMobilityModel")
43 .SetGroupName("Mobility")
44 .AddConstructor<RandomWalk2dMobilityModel>()
45 .AddAttribute("Bounds",
46 "Bounds of the area to cruise.",
47 RectangleValue(Rectangle(0.0, 100.0, 0.0, 100.0)),
48 MakeRectangleAccessor(&RandomWalk2dMobilityModel::m_bounds),
49 MakeRectangleChecker())
50 .AddAttribute("Time",
51 "Change current direction and speed after moving for this delay.",
52 TimeValue(Seconds(1.0)),
55 .AddAttribute("Distance",
56 "Change current direction and speed after moving for this distance.",
57 DoubleValue(1.0),
59 MakeDoubleChecker<double>())
60 .AddAttribute("Mode",
61 "The mode indicates the condition used to "
62 "change the current speed and direction",
66 "Distance",
68 "Time"))
69 .AddAttribute("Direction",
70 "A random variable used to pick the direction (radians).",
71 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
73 MakePointerChecker<RandomVariableStream>())
74 .AddAttribute("Speed",
75 "A random variable used to pick the speed (m/s).",
76 StringValue("ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
78 MakePointerChecker<RandomVariableStream>());
79 return tid;
80}
81
82void
84{
87}
88
89void
91{
93 double speed = m_speed->GetValue();
94 double direction = m_direction->GetValue();
95 Vector vector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
96 m_helper.SetVelocity(vector);
98
99 Time delayLeft;
101 {
102 delayLeft = m_modeTime;
103 }
104 else
105 {
106 delayLeft = Seconds(m_modeDistance / speed);
107 }
108 DoWalk(delayLeft);
109}
110
111void
113{
114 Vector position = m_helper.GetCurrentPosition();
115 Vector speed = m_helper.GetVelocity();
116 Vector nextPosition = position;
117 nextPosition.x += speed.x * delayLeft.GetSeconds();
118 nextPosition.y += speed.y * delayLeft.GetSeconds();
119 m_event.Cancel();
120 if (m_bounds.IsInside(nextPosition))
121 {
122 m_event =
124 }
125 else
126 {
127 nextPosition = m_bounds.CalculateIntersection(position, speed);
128 Time delay = Seconds((nextPosition.x - position.x) / speed.x);
131 this,
132 delayLeft - delay);
133 }
135}
136
137void
139{
141 Vector position = m_helper.GetCurrentPosition();
142 Vector speed = m_helper.GetVelocity();
143 switch (m_bounds.GetClosestSide(position))
144 {
145 case Rectangle::RIGHT:
146 case Rectangle::LEFT:
147 speed.x = -speed.x;
148 break;
149 case Rectangle::TOP:
151 speed.y = -speed.y;
152 break;
153 }
154 m_helper.SetVelocity(speed);
156 DoWalk(delayLeft);
157}
158
159void
161{
162 // chain up
164}
165
166Vector
168{
171}
172
173void
175{
176 NS_ASSERT(m_bounds.IsInside(position));
177 m_helper.SetPosition(position);
178 m_event.Cancel();
180}
181
182Vector
184{
185 return m_helper.GetVelocity();
186}
187
188int64_t
190{
191 m_speed->SetStream(stream);
192 m_direction->SetStream(stream + 1);
193 return 2;
194}
195
196} // 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 UpdateWithBounds(const Rectangle &rectangle) 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.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:56
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:360
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
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.
Ptr< RandomVariableStream > m_speed
rv for picking speed
static TypeId GetTypeId()
Register this type with the TypeId system.
void DoInitialize() override
Initialize() implementation.
double m_modeDistance
Change direction and speed after this distance.
void DoInitializePrivate()
Perform initialization of the object before MobilityModel::DoInitialize ()
void DoWalk(Time timeLeft)
Walk according to position and velocity, until distance is reached, time is reached,...
void Rebound(Time timeLeft)
Performs the rebound of the node if it reaches a boundary.
ConstantVelocityHelper m_helper
helper for this object
Time m_modeTime
Change current direction and speed after this delay.
Rectangle m_bounds
Bounds of the area to cruise.
void DoSetPosition(const Vector &position) override
void DoDispose() override
Destructor implementation.
Ptr< RandomVariableStream > m_direction
rv for picking direction
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
Mode m_mode
whether in time or distance mode
a 2d rectangle
Definition: rectangle.h:35
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
bool IsInside(const Vector &position) const
Definition: rectangle.cc:49
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:178
AttributeValue implementation for Rectangle.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition: enum.h:205
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1444
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1424
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:163