20 #include "random-walk-2d-mobility-model.h"
22 #include "ns3/double.h"
23 #include "ns3/string.h"
24 #include "ns3/pointer.h"
25 #include "ns3/simulator.h"
33 NS_OBJECT_ENSURE_REGISTERED (RandomWalk2dMobilityModel);
36 RandomWalk2dMobilityModel::GetTypeId (
void)
38 static TypeId tid = TypeId (
"ns3::RandomWalk2dMobilityModel")
39 .SetParent<MobilityModel> ()
40 .SetGroupName (
"Mobility")
41 .AddConstructor<RandomWalk2dMobilityModel> ()
42 .AddAttribute (
"Bounds",
43 "Bounds of the area to cruise.",
44 RectangleValue (Rectangle (0.0, 0.0, 100.0, 100.0)),
45 MakeRectangleAccessor (&RandomWalk2dMobilityModel::m_bounds),
46 MakeRectangleChecker ())
47 .AddAttribute (
"Time",
48 "Change current direction and speed after moving for this delay.",
50 MakeTimeAccessor (&RandomWalk2dMobilityModel::m_modeTime),
52 .AddAttribute (
"Distance",
53 "Change current direction and speed after moving for this distance.",
55 MakeDoubleAccessor (&RandomWalk2dMobilityModel::m_modeDistance),
56 MakeDoubleChecker<double> ())
57 .AddAttribute (
"Mode",
58 "The mode indicates the condition used to "
59 "change the current speed and direction",
60 EnumValue (RandomWalk2dMobilityModel::MODE_DISTANCE),
61 MakeEnumAccessor (&RandomWalk2dMobilityModel::m_mode),
62 MakeEnumChecker (RandomWalk2dMobilityModel::MODE_DISTANCE,
"Distance",
63 RandomWalk2dMobilityModel::MODE_TIME,
"Time"))
64 .AddAttribute (
"Direction",
65 "A random variable used to pick the direction (gradients).",
66 StringValue (
"ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
67 MakePointerAccessor (&RandomWalk2dMobilityModel::m_direction),
68 MakePointerChecker<RandomVariableStream> ())
69 .AddAttribute (
"Speed",
70 "A random variable used to pick the speed (m/s).",
71 StringValue (
"ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
72 MakePointerAccessor (&RandomWalk2dMobilityModel::m_speed),
73 MakePointerChecker<RandomVariableStream> ());
80 DoInitializePrivate ();
85 RandomWalk2dMobilityModel::DoInitializePrivate (
void)
89 double direction = m_direction->
GetValue ();
90 Vector vector (std::cos (direction) * speed,
91 std::sin (direction) * speed,
93 m_helper.SetVelocity (vector);
97 if (m_mode == RandomWalk2dMobilityModel::MODE_TIME)
99 delayLeft = m_modeTime;
103 delayLeft =
Seconds (m_modeDistance / speed);
109 RandomWalk2dMobilityModel::DoWalk (Time delayLeft)
111 Vector position = m_helper.GetCurrentPosition ();
112 Vector speed = m_helper.GetVelocity ();
113 Vector nextPosition = position;
114 nextPosition.
x += speed.x * delayLeft.GetSeconds ();
115 nextPosition.y += speed.y * delayLeft.GetSeconds ();
117 if (m_bounds.
IsInside (nextPosition))
119 m_event =
Simulator::Schedule (delayLeft, &RandomWalk2dMobilityModel::DoInitializePrivate,
this);
124 Time delay =
Seconds ((nextPosition.x - position.x) / speed.x);
132 RandomWalk2dMobilityModel::Rebound (Time delayLeft)
134 m_helper.UpdateWithBounds (m_bounds);
135 Vector position = m_helper.GetCurrentPosition ();
136 Vector speed = m_helper.GetVelocity ();
139 case Rectangle::RIGHT:
140 case Rectangle::LEFT:
144 case Rectangle::BOTTOM:
148 m_helper.SetVelocity (speed);
162 m_helper.UpdateWithBounds (m_bounds);
163 return m_helper.GetCurrentPosition ();
169 m_helper.SetPosition (position);
176 return m_helper.GetVelocity ();
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
virtual Vector DoGetPosition(void) const
Vector CalculateIntersection(const Vector ¤t, const Vector &speed) const
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void DoDispose(void)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
virtual Vector DoGetVelocity(void) const
virtual void DoSetPosition(const Vector &position)
bool IsInside(const Vector &position) const
void NotifyCourseChange(void) const
static void Remove(const EventId &id)
virtual void DoDispose(void)
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
virtual void DoInitialize(void)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Side GetClosestSide(const Vector &position) const
virtual void DoInitialize(void)
virtual int64_t DoAssignStreams(int64_t)