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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
9
10#include "ns3/double.h"
11#include "ns3/enum.h"
12#include "ns3/log.h"
13#include "ns3/pointer.h"
14#include "ns3/simulator.h"
15#include "ns3/string.h"
16
17#include <cmath>
18
19namespace ns3
20{
21
22NS_LOG_COMPONENT_DEFINE("RandomWalk2d");
23
25
28{
29 static TypeId tid =
30 TypeId("ns3::RandomWalk2dMobilityModel")
32 .SetGroupName("Mobility")
33 .AddConstructor<RandomWalk2dMobilityModel>()
34 .AddAttribute("Bounds",
35 "Bounds of the area to cruise.",
36 RectangleValue(Rectangle(0.0, 100.0, 0.0, 100.0)),
39 .AddAttribute("Time",
40 "Change current direction and speed after moving for this delay.",
44 .AddAttribute("Distance",
45 "Change current direction and speed after moving for this distance.",
46 DoubleValue(1.0),
49 .AddAttribute("Mode",
50 "The mode indicates the condition used to "
51 "change the current speed and direction",
56 "Distance",
58 "Time"))
59 .AddAttribute("Direction",
60 "A random variable used to pick the direction (radians).",
61 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
64 .AddAttribute("Speed",
65 "A random variable used to pick the speed (m/s).",
66 StringValue("ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
69 return tid;
70}
71
76
77void
84
85// Set new velocity and distance to travel, and call DoWalk() to initiate movement
86void
88{
89 NS_LOG_FUNCTION(this);
90 m_helper.Update();
91 Vector position = m_helper.GetCurrentPosition();
92
93 double speed = m_speed->GetValue();
94 double direction = m_direction->GetValue();
95 Vector velocity(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
96 if (m_bounds.IsOnTheBorder(position))
97 {
98 switch (m_bounds.GetClosestSideOrCorner(position))
99 {
101 velocity.x = -1 * std::abs(velocity.x);
102 break;
104 velocity.x = std::abs(velocity.x);
105 break;
107 velocity.y = -1 * std::abs(velocity.y);
108 break;
110 velocity.y = std::abs(velocity.y);
111 break;
113 velocity.x = -1 * std::abs(velocity.x);
114 velocity.y = -1 * std::abs(velocity.y);
115 break;
117 velocity.x = std::abs(velocity.x);
118 velocity.y = -1 * std::abs(velocity.y);
119 break;
121 velocity.x = -1 * std::abs(velocity.x);
122 velocity.y = std::abs(velocity.y);
123 break;
125 velocity.x = std::abs(velocity.x);
126 velocity.y = std::abs(velocity.y);
127 break;
128 }
129 }
130 NS_LOG_INFO("Setting new velocity to " << velocity);
131 m_helper.SetVelocity(velocity);
132 m_helper.Unpause();
133
134 Time delayLeft;
136 {
137 delayLeft = m_modeTime;
138 }
139 else
140 {
141 delayLeft = Seconds(m_modeDistance / speed);
142 }
143 NS_LOG_INFO("Setting delayLeft for DoWalk() to " << delayLeft.As(Time::S));
144 DoWalk(delayLeft);
145}
146
147// Notify course change and schedule event for either a wall rebound or a new velocity
148void
150{
151 NS_LOG_FUNCTION(this << delayLeft.As(Time::S));
152 Vector position = m_helper.GetCurrentPosition();
153 Vector velocity = m_helper.GetVelocity();
154 Vector nextPosition = position;
155 nextPosition.x += velocity.x * delayLeft.GetSeconds();
156 nextPosition.y += velocity.y * delayLeft.GetSeconds();
157 m_event.Cancel();
158 if (m_bounds.IsInside(nextPosition))
159 {
160 NS_LOG_INFO("Scheduling new velocity in " << delayLeft.As(Time::S));
161 m_event = Simulator::Schedule(delayLeft,
163 this);
164 }
165 else
166 {
167 nextPosition = m_bounds.CalculateIntersection(position, velocity);
168 double delaySeconds = std::numeric_limits<double>::max();
169 if (velocity.x != 0)
170 {
171 delaySeconds =
172 std::min(delaySeconds, std::abs((nextPosition.x - position.x) / velocity.x));
173 }
174 else if (velocity.y != 0)
175 {
176 delaySeconds =
177 std::min(delaySeconds, std::abs((nextPosition.y - position.y) / velocity.y));
178 }
179 else
180 {
181 NS_ABORT_MSG("RandomWalk2dMobilityModel::DoWalk: unable to calculate the rebound time "
182 "(the node is stationary).");
183 }
184 Time delay = Seconds(delaySeconds);
185 NS_LOG_INFO("Scheduling a rebound event in " << (delayLeft - delay).As(Time::S));
188 this,
189 delayLeft - delay);
190 }
192}
193
194// Set a velocity from the previous velocity, and start motion with remaining time
195void
197{
198 NS_LOG_FUNCTION(this << delayLeft.As(Time::S));
199 m_helper.UpdateWithBounds(m_bounds);
200 Vector position = m_helper.GetCurrentPosition();
201 Vector velocity = m_helper.GetVelocity();
202 switch (m_bounds.GetClosestSideOrCorner(position))
203 {
206 velocity.x = -velocity.x;
207 break;
210 velocity.y = -velocity.y;
211 break;
216 velocity.x = -velocity.x;
217 velocity.y = -velocity.y;
218 break;
219 }
220 m_helper.SetVelocity(velocity);
221 m_helper.Unpause();
222 NS_LOG_INFO("Rebounding with new velocity " << velocity);
223 DoWalk(delayLeft);
224}
225
226void
233
234Vector
236{
237 m_helper.UpdateWithBounds(m_bounds);
238 return m_helper.GetCurrentPosition();
239}
240
241void
243{
244 NS_LOG_FUNCTION(this << position);
245 NS_ASSERT(m_bounds.IsInside(position));
246 m_helper.SetPosition(position);
247 m_event.Cancel();
248 m_event =
250}
251
252Vector
254{
255 return m_helper.GetVelocity();
256}
257
258int64_t
260{
261 NS_LOG_FUNCTION(this << stream);
262 m_speed->SetStream(stream);
263 m_direction->SetStream(stream + 1);
264 return 2;
265}
266
267} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Hold variables of type enum.
Definition enum.h:52
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
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
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 DrawRandomVelocityAndDistance()
Draw a new random velocity and distance to travel, and call DoWalk()
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:24
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
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:403
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
@ S
second
Definition nstime.h:105
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(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
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_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
#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.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1433
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition enum.h:179
Ptr< const AttributeChecker > MakeRectangleChecker()
Definition rectangle.cc:173
Ptr< const AttributeAccessor > MakeRectangleAccessor(T1 a1)
Definition rectangle.h:114
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition enum.h:221
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1453