|
|
| 1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2008 INESC Porto |
| 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: Gustavo Carneiro <gjc@inescporto.pt> |
| 20 |
*/ |
| 21 |
#include <cmath> |
| 22 |
#include "ns3/simulator.h" |
| 23 |
#include "ns3/random-variable.h" |
| 24 |
#include "ns3/random-variable-default-value.h" |
| 25 |
#include "ns3/type-id-default-value.h" |
| 26 |
#include "waypoint-list-mobility-model.h" |
| 27 |
#include "random-position.h" |
| 28 |
|
| 29 |
namespace ns3 { |
| 30 |
|
| 31 |
NS_OBJECT_ENSURE_REGISTERED (WaypointListMobilityModel); |
| 32 |
|
| 33 |
static RandomVariableDefaultValue |
| 34 |
g_speed ("WaypointListSpeed", |
| 35 |
"A random variable used to pick the speed of a random waypoint model.", |
| 36 |
"Uniform:10:20"); |
| 37 |
|
| 38 |
|
| 39 |
WaypointListMobilityModelParameters::WaypointListMobilityModelParameters () |
| 40 |
: m_speed (g_speed.Get ()) |
| 41 |
{ |
| 42 |
} |
| 43 |
WaypointListMobilityModelParameters::WaypointListMobilityModelParameters (const RandomVariable &speed, |
| 44 |
const std::vector<Vector> &waypoints) |
| 45 |
: m_speed (speed), |
| 46 |
m_waypoints (waypoints) |
| 47 |
{ |
| 48 |
} |
| 49 |
void |
| 50 |
WaypointListMobilityModelParameters::SetWaypoints (const std::vector<Vector> &waypoints) |
| 51 |
{ |
| 52 |
m_waypoints = waypoints; |
| 53 |
} |
| 54 |
void |
| 55 |
WaypointListMobilityModelParameters::SetSpeed (const RandomVariable &speed) |
| 56 |
{ |
| 57 |
m_speed = speed; |
| 58 |
} |
| 59 |
|
| 60 |
Ptr<WaypointListMobilityModelParameters> |
| 61 |
WaypointListMobilityModelParameters::GetCurrent (void) |
| 62 |
{ |
| 63 |
static Ptr<WaypointListMobilityModelParameters> parameters = 0; |
| 64 |
if (parameters == 0 || |
| 65 |
g_speed.IsDirty ()) |
| 66 |
{ |
| 67 |
parameters = CreateObject<WaypointListMobilityModelParameters> (); |
| 68 |
} |
| 69 |
return parameters; |
| 70 |
} |
| 71 |
|
| 72 |
TypeId |
| 73 |
WaypointListMobilityModel::GetTypeId (void) |
| 74 |
{ |
| 75 |
static TypeId tid = TypeId ("WaypointListMobilityModel") |
| 76 |
.SetParent<MobilityModel> () |
| 77 |
.AddConstructor<WaypointListMobilityModel> () |
| 78 |
.AddConstructor<WaypointListMobilityModel,Ptr<WaypointListMobilityModelParameters> > (); |
| 79 |
return tid; |
| 80 |
} |
| 81 |
|
| 82 |
WaypointListMobilityModel::WaypointListMobilityModel () |
| 83 |
: m_parameters (WaypointListMobilityModelParameters::GetCurrent ()) |
| 84 |
{ |
| 85 |
Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this); |
| 86 |
} |
| 87 |
|
| 88 |
WaypointListMobilityModel::WaypointListMobilityModel (Ptr<WaypointListMobilityModelParameters> parameters) |
| 89 |
: m_parameters (parameters) |
| 90 |
{ |
| 91 |
Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this); |
| 92 |
NotifyCourseChange (); |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
WaypointListMobilityModel::BeginWalk (void) |
| 97 |
{ |
| 98 |
if (m_nextWaypoint == m_parameters->m_waypoints.end ()) |
| 99 |
{ |
| 100 |
// list of waypoints is empty; nothing to do |
| 101 |
return; |
| 102 |
} |
| 103 |
Vector destination = *m_nextWaypoint; |
| 104 |
NextWaypoint (); |
| 105 |
Vector current = GetPosition (); |
| 106 |
m_walkStartPoint = current; |
| 107 |
m_walkStartTime = Simulator::Now (); |
| 108 |
double speed = m_parameters->m_speed.GetValue (); |
| 109 |
double dx = (destination.x - current.x); |
| 110 |
double dy = (destination.y - current.y); |
| 111 |
double dz = (destination.z - current.z); |
| 112 |
double sq = dx*dx + dy*dy + dz*dz; |
| 113 |
if (sq == 0) |
| 114 |
{ |
| 115 |
m_velocity = Vector (0, 0, 0); |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
double k = speed / std::sqrt (sq); |
| 120 |
m_velocity = Vector (k*dx, k*dy, k*dz); |
| 121 |
} |
| 122 |
Time travelDelay = Seconds (CalculateDistance (destination, current) / speed); |
| 123 |
m_walkEndEvent = Simulator::Schedule |
| 124 |
(travelDelay, &WaypointListMobilityModel::BeginWalk, this); |
| 125 |
NotifyCourseChange (); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
/// prepare next waypoint |
| 130 |
void |
| 131 |
WaypointListMobilityModel::NextWaypoint (void) |
| 132 |
{ |
| 133 |
m_nextWaypoint++; |
| 134 |
if (m_nextWaypoint == m_parameters->m_waypoints.end ()) |
| 135 |
{ |
| 136 |
// cycle back to the first waypoint |
| 137 |
m_nextWaypoint = m_parameters->m_waypoints.begin (); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
WaypointListMobilityModel::Start (void) |
| 143 |
{ |
| 144 |
if (m_nextWaypoint == m_parameters->m_waypoints.end ()) |
| 145 |
{ |
| 146 |
NS_FATAL_ERROR("list of waypoints is empty; nothing to do"); |
| 147 |
return; |
| 148 |
} |
| 149 |
m_nextWaypoint = m_parameters->m_waypoints.begin (); |
| 150 |
m_velocity = Vector (0, 0, 0); |
| 151 |
m_walkStartTime = Simulator::Now (); |
| 152 |
m_walkStartPoint = *m_nextWaypoint; |
| 153 |
BeginWalk (); |
| 154 |
} |
| 155 |
|
| 156 |
Vector |
| 157 |
WaypointListMobilityModel::DoGetPosition (void) const |
| 158 |
{ |
| 159 |
double dt = (Simulator::Now () - m_walkStartTime).GetSeconds (); |
| 160 |
return Vector (m_walkStartPoint.x + dt*m_velocity.x, |
| 161 |
m_walkStartPoint.y + dt*m_velocity.y, |
| 162 |
m_walkStartPoint.z + dt*m_velocity.z); |
| 163 |
} |
| 164 |
void |
| 165 |
WaypointListMobilityModel::DoSetPosition (const Vector &position) |
| 166 |
{ |
| 167 |
NS_FATAL_ERROR ("Cannot change the position WaypointListMobilityModel models, only the waypoints"); |
| 168 |
} |
| 169 |
Vector |
| 170 |
WaypointListMobilityModel::DoGetVelocity (void) const |
| 171 |
{ |
| 172 |
return m_velocity; |
| 173 |
} |
| 174 |
|
| 175 |
WaypointListMobilityModel::~WaypointListMobilityModel () |
| 176 |
{ |
| 177 |
m_walkEndEvent.Cancel (); |
| 178 |
} |
| 179 |
|
| 180 |
} // namespace ns3 |