A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
random-direction-2d-mobility-model.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2007 INRIA
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
#include "ns3/simulator.h"
21
#include <algorithm>
22
#include <cmath>
23
#include "ns3/log.h"
24
#include "ns3/string.h"
25
#include "ns3/pointer.h"
26
#include "
random-direction-2d-mobility-model.h
"
27
28
NS_LOG_COMPONENT_DEFINE
(
"RandomDirection2dMobilityModel"
);
29
30
namespace
ns3 {
31
32
const
double
RandomDirection2dMobilityModel::PI
= 3.14159265358979323846;
33
34
NS_OBJECT_ENSURE_REGISTERED
(RandomDirection2dMobilityModel);
35
36
37
TypeId
38
RandomDirection2dMobilityModel::GetTypeId
(
void
)
39
{
40
static
TypeId
tid =
TypeId
(
"ns3::RandomDirection2dMobilityModel"
)
41
.
SetParent
<
MobilityModel
> ()
42
.SetGroupName (
"Mobility"
)
43
.AddConstructor<
RandomDirection2dMobilityModel
> ()
44
.AddAttribute (
"Bounds"
,
"The 2d bounding area"
,
45
RectangleValue
(
Rectangle
(-100, 100, -100, 100)),
46
MakeRectangleAccessor (&
RandomDirection2dMobilityModel::m_bounds
),
47
MakeRectangleChecker ())
48
.AddAttribute (
"Speed"
,
"A random variable to control the speed (m/s)."
,
49
StringValue
(
"ns3::UniformRandomVariable[Min=1.0|Max=2.0]"
),
50
MakePointerAccessor (&
RandomDirection2dMobilityModel::m_speed
),
51
MakePointerChecker<RandomVariableStream> ())
52
.AddAttribute (
"Pause"
,
"A random variable to control the pause (s)."
,
53
StringValue
(
"ns3::ConstantRandomVariable[Constant=2.0]"
),
54
MakePointerAccessor (&
RandomDirection2dMobilityModel::m_pause
),
55
MakePointerChecker<RandomVariableStream> ())
56
;
57
return
tid;
58
}
59
60
RandomDirection2dMobilityModel::RandomDirection2dMobilityModel
()
61
{
62
m_direction
= CreateObject <UniformRandomVariable> ();
63
}
64
65
void
66
RandomDirection2dMobilityModel::DoDispose
(
void
)
67
{
68
// chain up.
69
MobilityModel::DoDispose
();
70
}
71
void
72
RandomDirection2dMobilityModel::DoInitialize
(
void
)
73
{
74
DoInitializePrivate
();
75
MobilityModel::DoInitialize
();
76
}
77
78
void
79
RandomDirection2dMobilityModel::DoInitializePrivate
(
void
)
80
{
81
double
direction =
m_direction
->
GetValue
(0, 2 *
PI
);
82
SetDirectionAndSpeed
(direction);
83
}
84
85
void
86
RandomDirection2dMobilityModel::BeginPause
(
void
)
87
{
88
m_helper
.
Update
();
89
m_helper
.
Pause
();
90
Time
pause =
Seconds
(
m_pause
->
GetValue
());
91
m_event
.
Cancel
();
92
m_event
=
Simulator::Schedule
(pause, &
RandomDirection2dMobilityModel::ResetDirectionAndSpeed
,
this
);
93
NotifyCourseChange
();
94
}
95
96
void
97
RandomDirection2dMobilityModel::SetDirectionAndSpeed
(
double
direction)
98
{
99
NS_LOG_FUNCTION_NOARGS
();
100
m_helper
.
UpdateWithBounds
(
m_bounds
);
101
Vector
position =
m_helper
.
GetCurrentPosition
();
102
double
speed =
m_speed
->
GetValue
();
103
const
Vector
vector (std::cos (direction) * speed,
104
std::sin (direction) * speed,
105
0.0);
106
m_helper
.
SetVelocity
(vector);
107
m_helper
.
Unpause
();
108
Vector
next =
m_bounds
.
CalculateIntersection
(position, vector);
109
Time
delay =
Seconds
(
CalculateDistance
(position, next) / speed);
110
m_event
.
Cancel
();
111
m_event
=
Simulator::Schedule
(delay,
112
&
RandomDirection2dMobilityModel::BeginPause
,
this
);
113
NotifyCourseChange
();
114
}
115
void
116
RandomDirection2dMobilityModel::ResetDirectionAndSpeed
(
void
)
117
{
118
double
direction =
m_direction
->
GetValue
(0,
PI
);
119
120
m_helper
.
UpdateWithBounds
(
m_bounds
);
121
Vector
position =
m_helper
.
GetCurrentPosition
();
122
switch
(
m_bounds
.
GetClosestSide
(position))
123
{
124
case
Rectangle::RIGHT
:
125
direction +=
PI
/ 2;
126
break
;
127
case
Rectangle::LEFT
:
128
direction += -
PI
/ 2;
129
break
;
130
case
Rectangle::TOP
:
131
direction +=
PI
;
132
break
;
133
case
Rectangle::BOTTOM
:
134
direction += 0.0;
135
break
;
136
}
137
SetDirectionAndSpeed
(direction);
138
}
139
Vector
140
RandomDirection2dMobilityModel::DoGetPosition
(
void
)
const
141
{
142
m_helper
.
UpdateWithBounds
(
m_bounds
);
143
return
m_helper
.
GetCurrentPosition
();
144
}
145
void
146
RandomDirection2dMobilityModel::DoSetPosition
(
const
Vector
&position)
147
{
148
m_helper
.
SetPosition
(position);
149
Simulator::Remove
(
m_event
);
150
m_event
.
Cancel
();
151
m_event
=
Simulator::ScheduleNow
(&
RandomDirection2dMobilityModel::DoInitializePrivate
,
this
);
152
}
153
Vector
154
RandomDirection2dMobilityModel::DoGetVelocity
(
void
)
const
155
{
156
return
m_helper
.
GetVelocity
();
157
}
158
int64_t
159
RandomDirection2dMobilityModel::DoAssignStreams
(int64_t stream)
160
{
161
m_direction
->
SetStream
(stream);
162
m_speed
->
SetStream
(stream + 1);
163
m_pause
->
SetStream
(stream + 2);
164
return
3;
165
}
166
167
168
169
}
// namespace ns3
src
mobility
model
random-direction-2d-mobility-model.cc
Generated on Tue May 14 2013 11:08:29 for ns-3 by
1.8.1.2