1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2007 INRIA |
|
4 * All rights reserved. |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License version 2 as |
|
8 * published by the Free Software Foundation; |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 * |
|
19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
20 */ |
|
21 #include "ns3/random-variable-default-value.h" |
|
22 #include "ns3/rectangle-default-value.h" |
|
23 #include "ns3/simulator.h" |
|
24 #include <algorithm> |
|
25 #include <cmath> |
|
26 #include "random-direction-mobility-model.h" |
|
27 |
|
28 namespace ns3 { |
|
29 |
|
30 const double RandomDirectionMobilityModel::PI = 3.1415; |
|
31 const InterfaceId RandomDirectionMobilityModel::iid = |
|
32 MakeInterfaceId ("RandomDirectionMobilityModel", MobilityModel::iid); |
|
33 const ClassId RandomDirectionMobilityModel::cid = |
|
34 MakeClassId<RandomDirectionMobilityModel> ("RandomDirectionMobilityModel", |
|
35 RandomDirectionMobilityModel::iid); |
|
36 |
|
37 |
|
38 static RandomVariableDefaultValue |
|
39 g_speedVariable ("RandomDirectionSpeed", |
|
40 "A random variable to control the speed of a RandomDirection mobility model.", |
|
41 "Uniform:1:2"); |
|
42 |
|
43 static RandomVariableDefaultValue |
|
44 g_pauseVariable ("RandomDirectionPause", |
|
45 "A random variable to control the duration " |
|
46 "of the pause of a RandomDiretion mobility model.", |
|
47 "Constant:2"); |
|
48 |
|
49 static RectangleDefaultValue |
|
50 g_bounds ("RandomDirectionArea", |
|
51 "The bounding area for the RandomDirection model.", |
|
52 -100, 100, -100, 100); |
|
53 |
|
54 |
|
55 RandomDirectionParameters::RandomDirectionParameters () |
|
56 : m_bounds (g_bounds.GetValue ()), |
|
57 m_speedVariable (g_speedVariable.GetCopy ()), |
|
58 m_pauseVariable (g_pauseVariable.GetCopy ()) |
|
59 |
|
60 {} |
|
61 RandomDirectionParameters::RandomDirectionParameters (const Rectangle &bounds, |
|
62 const RandomVariable &speedVariable, |
|
63 const RandomVariable &pauseVariable) |
|
64 : m_bounds (bounds), |
|
65 m_speedVariable (speedVariable.Copy ()), |
|
66 m_pauseVariable (pauseVariable.Copy ()) |
|
67 {} |
|
68 |
|
69 RandomDirectionParameters::~RandomDirectionParameters () |
|
70 { |
|
71 delete m_speedVariable; |
|
72 delete m_pauseVariable; |
|
73 m_speedVariable = 0; |
|
74 m_pauseVariable = 0; |
|
75 } |
|
76 |
|
77 void |
|
78 RandomDirectionParameters::SetSpeed (const RandomVariable &speedVariable) |
|
79 { |
|
80 delete m_speedVariable; |
|
81 m_speedVariable = speedVariable.Copy (); |
|
82 } |
|
83 void |
|
84 RandomDirectionParameters::SetPause (const RandomVariable &pauseVariable) |
|
85 { |
|
86 delete m_pauseVariable; |
|
87 m_pauseVariable = pauseVariable.Copy (); |
|
88 } |
|
89 void |
|
90 RandomDirectionParameters::SetBounds (const Rectangle &bounds) |
|
91 { |
|
92 m_bounds = bounds; |
|
93 } |
|
94 |
|
95 Ptr<RandomDirectionParameters> |
|
96 RandomDirectionParameters::GetCurrent (void) |
|
97 { |
|
98 static Ptr<RandomDirectionParameters> parameters = 0; |
|
99 if (parameters == 0 || |
|
100 g_bounds.IsDirty () || |
|
101 g_speedVariable.IsDirty () || |
|
102 g_pauseVariable.IsDirty ()) |
|
103 { |
|
104 parameters = Create<RandomDirectionParameters> (); |
|
105 g_bounds.ClearDirtyFlag (); |
|
106 g_speedVariable.ClearDirtyFlag (); |
|
107 g_pauseVariable.ClearDirtyFlag (); |
|
108 } |
|
109 return parameters; |
|
110 } |
|
111 |
|
112 |
|
113 RandomDirectionMobilityModel::RandomDirectionMobilityModel () |
|
114 : m_parameters (RandomDirectionParameters::GetCurrent ()) |
|
115 { |
|
116 SetInterfaceId (RandomDirectionMobilityModel::iid); |
|
117 m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); |
|
118 } |
|
119 RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr<RandomDirectionParameters> parameters) |
|
120 : m_parameters (parameters) |
|
121 { |
|
122 SetInterfaceId (RandomDirectionMobilityModel::iid); |
|
123 m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); |
|
124 } |
|
125 void |
|
126 RandomDirectionMobilityModel::DoDispose (void) |
|
127 { |
|
128 m_parameters = 0; |
|
129 // chain up. |
|
130 MobilityModel::DoDispose (); |
|
131 } |
|
132 void |
|
133 RandomDirectionMobilityModel::Start (void) |
|
134 { |
|
135 double direction = UniformVariable::GetSingleValue (0, 2 * PI); |
|
136 SetDirectionAndSpeed (direction); |
|
137 } |
|
138 void |
|
139 RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction) |
|
140 { |
|
141 double speed = m_parameters->m_speedVariable->GetValue (); |
|
142 const Speed vector (std::cos (direction) * speed, |
|
143 std::sin (direction) * speed, |
|
144 0.0); |
|
145 Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); |
|
146 Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); |
|
147 m_helper.Reset (vector, pause); |
|
148 Position next = m_parameters->m_bounds.CalculateIntersection (position, vector); |
|
149 Time delay = Seconds (CalculateDistance (position, next) / speed); |
|
150 m_event = Simulator::Schedule (delay + pause, |
|
151 &RandomDirectionMobilityModel::ResetDirectionAndSpeed, this); |
|
152 NotifyCourseChange (); |
|
153 } |
|
154 void |
|
155 RandomDirectionMobilityModel::ResetDirectionAndSpeed (void) |
|
156 { |
|
157 double direction = UniformVariable::GetSingleValue (0, PI); |
|
158 |
|
159 Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); |
|
160 switch (m_parameters->m_bounds.GetClosestSide (position)) |
|
161 { |
|
162 case Rectangle::RIGHT: |
|
163 direction += PI / 2; |
|
164 break; |
|
165 case Rectangle::LEFT: |
|
166 direction += - PI / 2; |
|
167 break; |
|
168 case Rectangle::TOP: |
|
169 direction += PI; |
|
170 break; |
|
171 case Rectangle::BOTTOM: |
|
172 direction += 0.0; |
|
173 break; |
|
174 } |
|
175 SetDirectionAndSpeed (direction); |
|
176 } |
|
177 Position |
|
178 RandomDirectionMobilityModel::DoGet (void) const |
|
179 { |
|
180 return m_helper.GetCurrentPosition (m_parameters->m_bounds); |
|
181 } |
|
182 void |
|
183 RandomDirectionMobilityModel::DoSet (const Position &position) |
|
184 { |
|
185 m_helper.InitializePosition (position); |
|
186 Simulator::Remove (m_event); |
|
187 m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); |
|
188 } |
|
189 Speed |
|
190 RandomDirectionMobilityModel::DoGetSpeed (void) const |
|
191 { |
|
192 return m_helper.GetSpeed (); |
|
193 } |
|
194 |
|
195 |
|
196 |
|
197 } // namespace ns3 |
|