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)),
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",
64 MakeEnumAccessor<RandomWalk2dMobilityModel::Mode>(
67 "Distance",
69 "Time"))
70 .AddAttribute("Direction",
71 "A random variable used to pick the direction (radians).",
72 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
74 MakePointerChecker<RandomVariableStream>())
75 .AddAttribute("Speed",
76 "A random variable used to pick the speed (m/s).",
77 StringValue("ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
79 MakePointerChecker<RandomVariableStream>());
80 return tid;
81}
82
83void
85{
88}
89
90void
92{
94 Vector position = m_helper.GetCurrentPosition();
95
96 double speed = m_speed->GetValue();
97 double direction = m_direction->GetValue();
98 Vector velocity(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
99 if (m_bounds.IsOnTheBorder(position))
100 {
101 switch (m_bounds.GetClosestSideOrCorner(position))
102 {
104 velocity.x = -1 * std::abs(velocity.x);
105 break;
107 velocity.x = std::abs(velocity.x);
108 break;
110 velocity.y = -1 * std::abs(velocity.y);
111 break;
113 velocity.y = std::abs(velocity.y);
114 break;
116 velocity.x = -1 * std::abs(velocity.x);
117 velocity.y = -1 * std::abs(velocity.y);
118 break;
120 velocity.x = std::abs(velocity.x);
121 velocity.y = -1 * std::abs(velocity.y);
122 break;
124 velocity.x = -1 * std::abs(velocity.x);
125 velocity.y = std::abs(velocity.y);
126 break;
128 velocity.x = std::abs(velocity.x);
129 velocity.y = std::abs(velocity.y);
130 break;
131 }
132 }
133 m_helper.SetVelocity(velocity);
135
136 Time delayLeft;
138 {
139 delayLeft = m_modeTime;
140 }
141 else
142 {
143 delayLeft = Seconds(m_modeDistance / speed);
144 }
145 DoWalk(delayLeft);
146}
147
148void
150{
151 Vector position = m_helper.GetCurrentPosition();
152 Vector velocity = m_helper.GetVelocity();
153 Vector nextPosition = position;
154 nextPosition.x += velocity.x * delayLeft.GetSeconds();
155 nextPosition.y += velocity.y * delayLeft.GetSeconds();
156 m_event.Cancel();
157 if (m_bounds.IsInside(nextPosition))
158 {
159 m_event =
161 }
162 else
163 {
164 nextPosition = m_bounds.CalculateIntersection(position, velocity);
165 double delaySeconds = std::numeric_limits<double>::max();
166 if (velocity.x != 0)
167 {
168 delaySeconds =
169 std::min(delaySeconds, std::abs((nextPosition.x - position.x) / velocity.x));
170 }
171 else if (velocity.y != 0)
172 {
173 delaySeconds =
174 std::min(delaySeconds, std::abs((nextPosition.y - position.y) / velocity.y));
175 }
176 else
177 {
178 NS_ABORT_MSG("RandomWalk2dMobilityModel::DoWalk: unable to calculate the rebound time "
179 "(the node is stationary).");
180 }
181 Time delay = Seconds(delaySeconds);
184 this,
185 delayLeft - delay);
186 }
188}
189
190void
192{
194 Vector position = m_helper.GetCurrentPosition();
195 Vector velocity = m_helper.GetVelocity();
196 switch (m_bounds.GetClosestSideOrCorner(position))
197 {
200 velocity.x = -velocity.x;
201 break;
204 velocity.y = -velocity.y;
205 break;
210 velocity.x = -velocity.x;
211 velocity.y = -velocity.y;
212 break;
213 }
214 m_helper.SetVelocity(velocity);
216 DoWalk(delayLeft);
217}
218
219void
221{
222 // chain up
224}
225
226Vector
228{
231}
232
233void
235{
236 NS_ASSERT(m_bounds.IsInside(position));
237 m_helper.SetPosition(position);
238 m_event.Cancel();
240}
241
242Vector
244{
245 return m_helper.GetVelocity();
246}
247
248int64_t
250{
251 m_speed->SetStream(stream);
252 m_direction->SetStream(stream + 1);
253 return 2;
254}
255
256} // 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:62
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
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
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 GetClosestSideOrCorner(const Vector &position) const
Definition: rectangle.cc:64
bool IsInside(const Vector &position) const
Definition: rectangle.cc:50
bool IsOnTheBorder(const Vector &position) const
Definition: rectangle.cc:57
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:152
AttributeValue implementation for Rectangle.
Definition: rectangle.h:125
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
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
AttributeValue implementation for Time.
Definition: nstime.h:1406
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(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 > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
Ptr< const AttributeAccessor > MakeRectangleAccessor(T1 a1)
Definition: rectangle.h:125
Ptr< const AttributeChecker > MakeRectangleChecker()
Definition: rectangle.cc:187
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1427
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1407
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#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:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:189