00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <cmath>
00021 #include "ns3/simulator.h"
00022 #include "ns3/random-variable.h"
00023 #include "ns3/pointer.h"
00024 #include "random-waypoint-mobility-model.h"
00025 #include "position-allocator.h"
00026
00027 namespace ns3 {
00028
00029 NS_OBJECT_ENSURE_REGISTERED (RandomWaypointMobilityModel);
00030
00031 TypeId
00032 RandomWaypointMobilityModel::GetTypeId (void)
00033 {
00034 static TypeId tid = TypeId ("ns3::RandomWaypointMobilityModel")
00035 .SetParent<MobilityModel> ()
00036 .SetGroupName ("Mobility")
00037 .AddConstructor<RandomWaypointMobilityModel> ()
00038 .AddAttribute ("Speed",
00039 "A random variable used to pick the speed of a random waypoint model.",
00040 RandomVariableValue (UniformVariable (0.3, 0.7)),
00041 MakeRandomVariableAccessor (&RandomWaypointMobilityModel::m_speed),
00042 MakeRandomVariableChecker ())
00043 .AddAttribute ("Pause",
00044 "A random variable used to pick the pause of a random waypoint model.",
00045 RandomVariableValue (ConstantVariable (2.0)),
00046 MakeRandomVariableAccessor (&RandomWaypointMobilityModel::m_pause),
00047 MakeRandomVariableChecker ())
00048 .AddAttribute ("Position",
00049 "The position model used to pick a destination point.",
00050 PointerValue (),
00051 MakePointerAccessor (&RandomWaypointMobilityModel::m_position),
00052 MakePointerChecker<PositionAllocator> ());
00053
00054 return tid;
00055 }
00056
00057 RandomWaypointMobilityModel::RandomWaypointMobilityModel ()
00058 {
00059 m_event = Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this);
00060 }
00061
00062 void
00063 RandomWaypointMobilityModel::BeginWalk (void)
00064 {
00065 m_helper.Update ();
00066 Vector m_current = m_helper.GetCurrentPosition ();
00067 Vector destination = m_position->GetNext ();
00068 double speed = m_speed.GetValue ();
00069 double dx = (destination.x - m_current.x);
00070 double dy = (destination.y - m_current.y);
00071 double dz = (destination.z - m_current.z);
00072 double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz);
00073
00074 m_helper.SetVelocity (Vector (k*dx, k*dy, k*dz));
00075 m_helper.Unpause ();
00076 Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed);
00077 m_event = Simulator::Schedule (travelDelay,
00078 &RandomWaypointMobilityModel::Start, this);
00079 NotifyCourseChange ();
00080 }
00081
00082 void
00083 RandomWaypointMobilityModel::Start (void)
00084 {
00085 m_helper.Update ();
00086 m_helper.Pause ();
00087 Time pause = Seconds (m_pause.GetValue ());
00088 m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::BeginWalk, this);
00089 NotifyCourseChange ();
00090 }
00091
00092 Vector
00093 RandomWaypointMobilityModel::DoGetPosition (void) const
00094 {
00095 m_helper.Update ();
00096 return m_helper.GetCurrentPosition ();
00097 }
00098 void
00099 RandomWaypointMobilityModel::DoSetPosition (const Vector &position)
00100 {
00101 m_helper.SetPosition (position);
00102 Simulator::Remove (m_event);
00103 m_event = Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this);
00104 }
00105 Vector
00106 RandomWaypointMobilityModel::DoGetVelocity (void) const
00107 {
00108 return m_helper.GetVelocity ();
00109 }
00110
00111
00112 }