00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "ns3/simulator.h"
00021 #include <algorithm>
00022 #include <cmath>
00023 #include "random-direction-2d-mobility-model.h"
00024 #include "ns3/log.h"
00025
00026 NS_LOG_COMPONENT_DEFINE ("RandomDirection2dMobilityModel");
00027
00028 namespace ns3 {
00029
00030 const double RandomDirection2dMobilityModel::PI = 3.14159265358979323846;
00031
00032 NS_OBJECT_ENSURE_REGISTERED (RandomDirection2dMobilityModel);
00033
00034
00035 TypeId
00036 RandomDirection2dMobilityModel::GetTypeId (void)
00037 {
00038 static TypeId tid = TypeId ("ns3::RandomDirection2dMobilityModel")
00039 .SetParent<MobilityModel> ()
00040 .SetGroupName ("Mobility")
00041 .AddConstructor<RandomDirection2dMobilityModel> ()
00042 .AddAttribute ("Bounds", "The 2d bounding area",
00043 RectangleValue (Rectangle (-100, 100, -100, 100)),
00044 MakeRectangleAccessor (&RandomDirection2dMobilityModel::m_bounds),
00045 MakeRectangleChecker ())
00046 .AddAttribute ("Speed", "A random variable to control the speed (m/s).",
00047 RandomVariableValue (UniformVariable (1.0, 2.0)),
00048 MakeRandomVariableAccessor (&RandomDirection2dMobilityModel::m_speed),
00049 MakeRandomVariableChecker ())
00050 .AddAttribute ("Pause", "A random variable to control the pause (s).",
00051 RandomVariableValue (ConstantVariable (2.0)),
00052 MakeRandomVariableAccessor (&RandomDirection2dMobilityModel::m_pause),
00053 MakeRandomVariableChecker ())
00054 ;
00055 return tid;
00056 }
00057
00058
00059 RandomDirection2dMobilityModel::RandomDirection2dMobilityModel ()
00060 {
00061 m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this);
00062 }
00063 void
00064 RandomDirection2dMobilityModel::DoDispose (void)
00065 {
00066
00067 MobilityModel::DoDispose ();
00068 }
00069 void
00070 RandomDirection2dMobilityModel::Start (void)
00071 {
00072 double direction = UniformVariable::GetSingleValue (0, 2 * PI);
00073 SetDirectionAndSpeed (direction);
00074 }
00075
00076 void
00077 RandomDirection2dMobilityModel::BeginPause (void)
00078 {
00079 m_helper.Update ();
00080 m_helper.Pause ();
00081 Time pause = Seconds (m_pause.GetValue ());
00082 m_event = Simulator::Schedule (pause, &RandomDirection2dMobilityModel::ResetDirectionAndSpeed, this);
00083 NotifyCourseChange ();
00084 }
00085
00086 void
00087 RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction)
00088 {
00089 NS_LOG_FUNCTION_NOARGS ();
00090 m_helper.UpdateWithBounds (m_bounds);
00091 Vector position = m_helper.GetCurrentPosition ();
00092 double speed = m_speed.GetValue ();
00093 const Vector vector (std::cos (direction) * speed,
00094 std::sin (direction) * speed,
00095 0.0);
00096 m_helper.SetVelocity (vector);
00097 m_helper.Unpause ();
00098 Vector next = m_bounds.CalculateIntersection (position, vector);
00099 Time delay = Seconds (CalculateDistance (position, next) / speed);
00100 m_event = Simulator::Schedule (delay,
00101 &RandomDirection2dMobilityModel::BeginPause, this);
00102 NotifyCourseChange ();
00103 }
00104 void
00105 RandomDirection2dMobilityModel::ResetDirectionAndSpeed (void)
00106 {
00107 double direction = UniformVariable::GetSingleValue (0, PI);
00108
00109 m_helper.UpdateWithBounds (m_bounds);
00110 Vector position = m_helper.GetCurrentPosition ();
00111 switch (m_bounds.GetClosestSide (position))
00112 {
00113 case Rectangle::RIGHT:
00114 direction += PI / 2;
00115 break;
00116 case Rectangle::LEFT:
00117 direction += - PI / 2;
00118 break;
00119 case Rectangle::TOP:
00120 direction += PI;
00121 break;
00122 case Rectangle::BOTTOM:
00123 direction += 0.0;
00124 break;
00125 }
00126 SetDirectionAndSpeed (direction);
00127 }
00128 Vector
00129 RandomDirection2dMobilityModel::DoGetPosition (void) const
00130 {
00131 m_helper.UpdateWithBounds (m_bounds);
00132 return m_helper.GetCurrentPosition ();
00133 }
00134 void
00135 RandomDirection2dMobilityModel::DoSetPosition (const Vector &position)
00136 {
00137 m_helper.SetPosition (position);
00138 Simulator::Remove (m_event);
00139 m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this);
00140 }
00141 Vector
00142 RandomDirection2dMobilityModel::DoGetVelocity (void) const
00143 {
00144 return m_helper.GetVelocity ();
00145 }
00146
00147
00148
00149 }