src/mobility/random-waypoint-mobility-model.cc
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Fri, 11 Jul 2008 18:39:19 +0100
changeset 3416 926ccb845111
parent 2965 4b28e9740e3b
child 3782 a2375aed06f3
permissions -rw-r--r--
Rescan APIs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1660
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     2
/*
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     3
 * Copyright (c) 2007 INRIA
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     4
 *
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     7
 * published by the Free Software Foundation;
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     8
 *
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    12
 * GNU General Public License for more details.
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    13
 *
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    14
 * You should have received a copy of the GNU General Public License
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    15
 * along with this program; if not, write to the Free Software
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    17
 *
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
4a4621b4e3ab Adapt to StaticSpeedHelper change
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1648
diff changeset
    19
 */
1648
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    20
#include <cmath>
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
#include "ns3/simulator.h"
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    22
#include "ns3/random-variable.h"
2927
73b47ce1d805 get rid of implicit conversion of Attribute to/from Ptr<>. Replace this with an explicit Pointer class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    23
#include "ns3/pointer.h"
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
#include "random-waypoint-mobility-model.h"
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    25
#include "position-allocator.h"
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
namespace ns3 {
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
2249
3a1da26d61dc replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2232
diff changeset
    29
NS_OBJECT_ENSURE_REGISTERED (RandomWaypointMobilityModel);
3a1da26d61dc replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2232
diff changeset
    30
2250
18f432098389 InterfaceId -> TypeId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2249
diff changeset
    31
TypeId
2251
04963d8cca51 iid (void) -> GetTypeId (void)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2250
diff changeset
    32
RandomWaypointMobilityModel::GetTypeId (void)
2249
3a1da26d61dc replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2232
diff changeset
    33
{
2602
d9262bff6df2 add back support for introspected doxygen.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2553
diff changeset
    34
  static TypeId tid = TypeId ("ns3::RandomWaypointMobilityModel")
2249
3a1da26d61dc replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2232
diff changeset
    35
    .SetParent<MobilityModel> ()
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    36
    .SetGroupName ("Mobility")
2249
3a1da26d61dc replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2232
diff changeset
    37
    .AddConstructor<RandomWaypointMobilityModel> ()
2543
401bfe8c0176 kill old code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2503
diff changeset
    38
    .AddAttribute ("Speed",
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    39
                   "A random variable used to pick the speed of a random waypoint model.",
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2927
diff changeset
    40
                   RandomVariableValue (UniformVariable (0.3, 0.7)),
2435
3128175f5866 ParamSpec -> Accessor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2427
diff changeset
    41
                   MakeRandomVariableAccessor (&RandomWaypointMobilityModel::m_speed),
2427
9245ec163111 split checker from ParamSpec.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    42
                   MakeRandomVariableChecker ())
2543
401bfe8c0176 kill old code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2503
diff changeset
    43
    .AddAttribute ("Pause",
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    44
                   "A random variable used to pick the pause of a random waypoint model.",
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2927
diff changeset
    45
                   RandomVariableValue (ConstantVariable (2.0)),
2435
3128175f5866 ParamSpec -> Accessor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2427
diff changeset
    46
                   MakeRandomVariableAccessor (&RandomWaypointMobilityModel::m_pause),
2427
9245ec163111 split checker from ParamSpec.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    47
                   MakeRandomVariableChecker ())
2543
401bfe8c0176 kill old code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2503
diff changeset
    48
    .AddAttribute ("Position",
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    49
                   "The position model used to pick a destination point.",
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2927
diff changeset
    50
                   PointerValue (),
2927
73b47ce1d805 get rid of implicit conversion of Attribute to/from Ptr<>. Replace this with an explicit Pointer class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    51
                   MakePointerAccessor (&RandomWaypointMobilityModel::m_position),
73b47ce1d805 get rid of implicit conversion of Attribute to/from Ptr<>. Replace this with an explicit Pointer class.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    52
                   MakePointerChecker<PositionAllocator> ());
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    53
  
2252
80595448707a iid -> tid
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2251
diff changeset
    54
  return tid;
2249
3a1da26d61dc replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2232
diff changeset
    55
}
3a1da26d61dc replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2232
diff changeset
    56
1601
84805e29dd21 add default value support to random waypoint
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1600
diff changeset
    57
RandomWaypointMobilityModel::RandomWaypointMobilityModel ()
1603
e54bfec07c72 make sure we schedule the start event correctly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1602
diff changeset
    58
{
e54bfec07c72 make sure we schedule the start event correctly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1602
diff changeset
    59
  Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this);
e54bfec07c72 make sure we schedule the start event correctly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1602
diff changeset
    60
}
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    61
1648
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    62
void
1663
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    63
RandomWaypointMobilityModel::BeginWalk (void)
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    64
{
1817
8b0520433350 replace Position and Speed by Vector
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1816
diff changeset
    65
  Vector m_current = m_helper.GetCurrentPosition ();
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    66
  Vector destination = m_position->GetNext ();
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    67
  double speed = m_speed.GetValue ();
1648
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    68
  double dx = (destination.x - m_current.x);
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    69
  double dy = (destination.y - m_current.y);
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    70
  double dz = (destination.z - m_current.z);
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    71
  double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    72
1817
8b0520433350 replace Position and Speed by Vector
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1816
diff changeset
    73
  m_helper.Reset (Vector (k*dx, k*dy, k*dz));
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
  Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
1648
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    75
  m_event = Simulator::Schedule (travelDelay,
1663
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    76
				 &RandomWaypointMobilityModel::Start, this);
1648
abe82df90582 Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1647
diff changeset
    77
  NotifyCourseChange ();
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    78
}
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    79
1663
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    80
void
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    81
RandomWaypointMobilityModel::Start (void)
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    82
{
2399
fd9d94d518d2 convert Mobility subsystem to Value framework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2383
diff changeset
    83
  Time pause = Seconds (m_pause.GetValue ());
1663
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    84
  m_helper.Pause ();
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    85
  NotifyCourseChange ();
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    86
  m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::BeginWalk, this);
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    87
}
ea3848cdbdaf According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1660
diff changeset
    88
1817
8b0520433350 replace Position and Speed by Vector
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1816
diff changeset
    89
Vector
1816
d7c9b9da78f4 rename MobilityModel::Get/Set to GetPosition/SetPosition
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1663
diff changeset
    90
RandomWaypointMobilityModel::DoGetPosition (void) const
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    91
{
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    92
  return m_helper.GetCurrentPosition ();
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    93
}
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    94
void 
1817
8b0520433350 replace Position and Speed by Vector
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1816
diff changeset
    95
RandomWaypointMobilityModel::DoSetPosition (const Vector &position)
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    96
{
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    97
  m_helper.InitializePosition (position);
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    98
  Simulator::Remove (m_event);
1603
e54bfec07c72 make sure we schedule the start event correctly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1602
diff changeset
    99
  Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this);
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   100
}
1817
8b0520433350 replace Position and Speed by Vector
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1816
diff changeset
   101
Vector
1818
fbdc8361dc77 GetSpeed -> GetVelocity
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1817
diff changeset
   102
RandomWaypointMobilityModel::DoGetVelocity (void) const
1624
6e7d48a8c120 add MobilityModel::GetSpeed method and implement it in all mobility models
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1603
diff changeset
   103
{
1818
fbdc8361dc77 GetSpeed -> GetVelocity
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1817
diff changeset
   104
  return m_helper.GetVelocity ();
1624
6e7d48a8c120 add MobilityModel::GetSpeed method and implement it in all mobility models
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1603
diff changeset
   105
}
1600
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   106
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   107
1a1101c83922 add a 3d random waypoint model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   108
} // namespace ns3