split useful code out of random direction mobility model
This commit is contained in:
185
src/node/mobility-model-helper.cc
Normal file
185
src/node/mobility-model-helper.cc
Normal file
@@ -0,0 +1,185 @@
|
||||
#include <cmath>
|
||||
#include "ns3/simulator.h"
|
||||
#include "mobility-model-helper.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
Position2D::Position2D (double _x, double _y)
|
||||
: x (_x),
|
||||
y (_y)
|
||||
{}
|
||||
Position2D::Position2D ()
|
||||
: x (0.0),
|
||||
y (0.0)
|
||||
{}
|
||||
|
||||
Speed2D::Speed2D (double _dx, double _dy)
|
||||
: dx (_dx),
|
||||
dy (_dy)
|
||||
{}
|
||||
|
||||
Speed2D::Speed2D ()
|
||||
: dx (0.0),
|
||||
dy (0.0)
|
||||
{}
|
||||
|
||||
MobilityModelHelper::MobilityModelHelper ()
|
||||
{}
|
||||
void
|
||||
MobilityModelHelper::InitializePosition (const struct Position2D &position)
|
||||
{
|
||||
m_position = position;
|
||||
m_speed.dx = 0.0;
|
||||
m_speed.dy = 0.0;
|
||||
m_lastUpdate = Simulator::Now ();
|
||||
m_pauseEnd = Simulator::Now ();
|
||||
}
|
||||
void
|
||||
MobilityModelHelper::Update (const struct AreaBounds &bounds) const
|
||||
{
|
||||
Time now = Simulator::Now ();
|
||||
if (m_pauseEnd > now)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Time last = std::max (now, m_pauseEnd);
|
||||
if (m_lastUpdate >= last)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Time deltaTime = now - last;
|
||||
m_lastUpdate = now;
|
||||
double deltaS = deltaTime.GetSeconds ();
|
||||
m_position.x += m_speed.dx * deltaS;
|
||||
m_position.y += m_speed.dy * deltaS;
|
||||
m_position.x = std::min (m_position.x, bounds.xMax);
|
||||
m_position.y = std::min (m_position.y, bounds.yMax);
|
||||
m_position.x = std::max (m_position.x, bounds.xMin);
|
||||
m_position.y = std::max (m_position.y, bounds.yMin);
|
||||
}
|
||||
Position2D
|
||||
MobilityModelHelper::CalculateIntersection (const struct AreaBounds &bounds) const
|
||||
{
|
||||
double xMaxY = m_position.y + (bounds.xMax - m_position.x) / m_speed.dx * m_speed.dy;
|
||||
double xMinY = m_position.y + (bounds.xMin - m_position.x) / m_speed.dx * m_speed.dy;
|
||||
double yMaxX = m_position.x + (bounds.yMax - m_position.y) / m_speed.dy * m_speed.dx;
|
||||
double yMinX = m_position.x + (bounds.yMin - m_position.y) / m_speed.dy * m_speed.dx;
|
||||
bool xMaxOk = xMaxY <= bounds.yMax && xMaxY >= bounds.yMin;
|
||||
bool xMinOk = xMinY <= bounds.yMax && xMinY >= bounds.yMin;
|
||||
bool yMaxOk = yMaxX <= bounds.xMax && yMaxX >= bounds.xMin;
|
||||
bool yMinOk = yMinX <= bounds.xMax && yMinX >= bounds.xMin;
|
||||
if (xMaxOk && m_speed.dx >= 0)
|
||||
{
|
||||
return Position2D (bounds.xMax, xMaxY);
|
||||
}
|
||||
else if (xMinOk && m_speed.dx <= 0)
|
||||
{
|
||||
return Position2D (bounds.xMin, xMinY);
|
||||
}
|
||||
else if (yMaxOk && m_speed.dy >= 0)
|
||||
{
|
||||
return Position2D (yMaxX, bounds.yMax);
|
||||
}
|
||||
else if (yMinOk && m_speed.dy <= 0)
|
||||
{
|
||||
return Position2D (yMinX, bounds.yMin);
|
||||
}
|
||||
else
|
||||
{
|
||||
NS_ASSERT (false);
|
||||
// quiet compiler
|
||||
return Position2D (0.0, 0.0);
|
||||
}
|
||||
}
|
||||
double
|
||||
MobilityModelHelper::Distance (const Position2D &a, const Position2D &b) const
|
||||
{
|
||||
double dx = a.x - b.x;
|
||||
double dy = a.y - b.y;
|
||||
return sqrt (dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
Time
|
||||
MobilityModelHelper::Reset (const struct AreaBounds &bounds,
|
||||
double direction,
|
||||
double speed,
|
||||
const Time &maxMovementDelay,
|
||||
const Time &pauseDelay)
|
||||
{
|
||||
Update (bounds);
|
||||
m_pauseEnd = Simulator::Now () + pauseDelay;
|
||||
m_speed.dx = std::cos (direction) * speed;
|
||||
m_speed.dy = std::sin (direction) * speed;
|
||||
Position2D intersection = CalculateIntersection (bounds);
|
||||
Time seconds = Seconds (Distance (intersection, m_position) / speed);
|
||||
seconds = std::min (seconds, maxMovementDelay);
|
||||
return seconds;
|
||||
}
|
||||
Time
|
||||
MobilityModelHelper::Reset (const struct AreaBounds &bounds,
|
||||
double direction,
|
||||
double speed,
|
||||
double maxDistance,
|
||||
const Time &pauseDelay)
|
||||
{
|
||||
Update (bounds);
|
||||
m_pauseEnd = Simulator::Now () + pauseDelay;
|
||||
m_speed.dx = std::cos (direction) * speed;
|
||||
m_speed.dy = std::sin (direction) * speed;
|
||||
Position2D intersection = CalculateIntersection (bounds);
|
||||
double distance = Distance (intersection, m_position);
|
||||
distance = std::min (distance, maxDistance);
|
||||
double seconds = distance / speed;
|
||||
return Seconds (seconds);
|
||||
}
|
||||
Time
|
||||
MobilityModelHelper::Reset (const struct AreaBounds &bounds,
|
||||
double direction,
|
||||
double speed,
|
||||
const Time &pauseDelay)
|
||||
{
|
||||
Update (bounds);
|
||||
m_pauseEnd = Simulator::Now () + pauseDelay;
|
||||
m_speed.dx = std::cos (direction) * speed;
|
||||
m_speed.dy = std::sin (direction) * speed;
|
||||
Position2D intersection = CalculateIntersection (bounds);
|
||||
double seconds = Distance (intersection, m_position) / speed;
|
||||
return Seconds (seconds);
|
||||
}
|
||||
|
||||
Position2D
|
||||
MobilityModelHelper::GetCurrentPosition (const struct AreaBounds &bounds) const
|
||||
{
|
||||
Update (bounds);
|
||||
return m_position;
|
||||
}
|
||||
|
||||
Speed2D
|
||||
MobilityModelHelper::GetSpeed (void) const
|
||||
{
|
||||
return m_speed;
|
||||
}
|
||||
|
||||
enum MobilityModelHelper::Side
|
||||
MobilityModelHelper::GetSide (const struct AreaBounds &bounds) const
|
||||
{
|
||||
if (m_position.x == bounds.xMin)
|
||||
{
|
||||
return MobilityModelHelper::LEFT;
|
||||
}
|
||||
if (m_position.x == bounds.xMax)
|
||||
{
|
||||
return MobilityModelHelper::RIGHT;
|
||||
}
|
||||
if (m_position.y == bounds.yMin)
|
||||
{
|
||||
return MobilityModelHelper::BOTTOM;
|
||||
}
|
||||
if (m_position.y == bounds.yMax)
|
||||
{
|
||||
return MobilityModelHelper::TOP;
|
||||
}
|
||||
return MobilityModelHelper::NONE;
|
||||
}
|
||||
|
||||
} // namespace ns3
|
||||
80
src/node/mobility-model-helper.h
Normal file
80
src/node/mobility-model-helper.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef MOBILITY_MODEL_HELPER_H
|
||||
#define MOBILITY_MODEL_HELPER_H
|
||||
|
||||
#include "ns3/nstime.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
class Position2D
|
||||
{
|
||||
public:
|
||||
Position2D (double _x, double _y);
|
||||
Position2D ();
|
||||
double x;
|
||||
double y;
|
||||
};
|
||||
|
||||
struct AreaBounds
|
||||
{
|
||||
double xMin;
|
||||
double xMax;
|
||||
double yMin;
|
||||
double yMax;
|
||||
};
|
||||
|
||||
class Speed2D
|
||||
{
|
||||
public:
|
||||
Speed2D (double _dx, double _dy);
|
||||
Speed2D ();
|
||||
double dx;
|
||||
double dy;
|
||||
};
|
||||
|
||||
class MobilityModelHelper
|
||||
{
|
||||
public:
|
||||
enum Side {
|
||||
LEFT,
|
||||
RIGHT,
|
||||
TOP,
|
||||
BOTTOM,
|
||||
NONE
|
||||
};
|
||||
MobilityModelHelper ();
|
||||
void InitializePosition (const struct Position2D &position);
|
||||
Time Reset (const struct AreaBounds &bounds,
|
||||
double direction,
|
||||
double speed,
|
||||
const Time &maxMovementDelay,
|
||||
const Time &pauseDelay);
|
||||
Time Reset (const struct AreaBounds &bounds,
|
||||
double direction,
|
||||
double speed,
|
||||
double maxDistance,
|
||||
const Time &pauseDelay);
|
||||
// move in specified direction until
|
||||
// we hit the area bounds.
|
||||
// return delay until we reach the bounds.
|
||||
Time Reset (const struct AreaBounds &bounds,
|
||||
double direction,
|
||||
double speed,
|
||||
const Time &pauseDelay);
|
||||
|
||||
Position2D GetCurrentPosition (const struct AreaBounds &bounds) const;
|
||||
Speed2D GetSpeed (void) const;
|
||||
enum MobilityModelHelper::Side GetSide (const struct AreaBounds &bounds) const;
|
||||
|
||||
private:
|
||||
double Distance (const Position2D &a, const Position2D &b) const;
|
||||
void Update (const struct AreaBounds &bounds) const;
|
||||
Position2D CalculateIntersection (const struct AreaBounds &bounds) const;
|
||||
mutable Position2D m_position;
|
||||
Speed2D m_speed;
|
||||
mutable Time m_lastUpdate;
|
||||
Time m_pauseEnd;
|
||||
};
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
#endif /* MOBILITY_MODEL_HELPER_H */
|
||||
@@ -52,31 +52,34 @@ static RectangleDefaultValue
|
||||
|
||||
|
||||
RandomDirectionParameters::RandomDirectionParameters ()
|
||||
: m_xMin (g_rectangle.GetMinX ()),
|
||||
m_xMax (g_rectangle.GetMaxX ()),
|
||||
m_yMin (g_rectangle.GetMinY ()),
|
||||
m_yMax (g_rectangle.GetMaxY ()),
|
||||
m_speedVariable (g_speedVariable.GetCopy ()),
|
||||
: m_speedVariable (g_speedVariable.GetCopy ()),
|
||||
m_pauseVariable (g_pauseVariable.GetCopy ())
|
||||
{}
|
||||
{
|
||||
m_bounds.xMin = g_rectangle.GetMinX ();
|
||||
m_bounds.xMax = g_rectangle.GetMaxX ();
|
||||
m_bounds.yMin = g_rectangle.GetMinY ();
|
||||
m_bounds.yMax = g_rectangle.GetMaxY ();
|
||||
}
|
||||
RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax)
|
||||
: m_xMin (xMin),
|
||||
m_xMax (xMax),
|
||||
m_yMin (yMin),
|
||||
m_yMax (yMax),
|
||||
m_speedVariable (g_speedVariable.GetCopy ()),
|
||||
: m_speedVariable (g_speedVariable.GetCopy ()),
|
||||
m_pauseVariable (g_pauseVariable.GetCopy ())
|
||||
{}
|
||||
{
|
||||
m_bounds.xMin = xMin;
|
||||
m_bounds.xMax = xMax;
|
||||
m_bounds.yMin = yMin;
|
||||
m_bounds.yMax = yMax;
|
||||
}
|
||||
RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax,
|
||||
const RandomVariable &speedVariable,
|
||||
const RandomVariable &pauseVariable)
|
||||
: m_xMin (xMin),
|
||||
m_xMax (xMax),
|
||||
m_yMin (yMin),
|
||||
m_yMax (yMax),
|
||||
m_speedVariable (speedVariable.Copy ()),
|
||||
: m_speedVariable (speedVariable.Copy ()),
|
||||
m_pauseVariable (pauseVariable.Copy ())
|
||||
{}
|
||||
{
|
||||
m_bounds.xMin = xMin;
|
||||
m_bounds.xMax = xMax;
|
||||
m_bounds.yMin = yMin;
|
||||
m_bounds.yMax = yMax;
|
||||
}
|
||||
|
||||
RandomDirectionParameters::~RandomDirectionParameters ()
|
||||
{
|
||||
@@ -101,20 +104,20 @@ RandomDirectionParameters::SetPause (const RandomVariable &pauseVariable)
|
||||
void
|
||||
RandomDirectionParameters::SetBounds (double xMin, double xMax, double yMin, double yMax)
|
||||
{
|
||||
m_xMin = xMin;
|
||||
m_yMin = yMin;
|
||||
m_xMax = xMax;
|
||||
m_yMax = yMax;
|
||||
m_bounds.xMin = xMin;
|
||||
m_bounds.yMin = yMin;
|
||||
m_bounds.xMax = xMax;
|
||||
m_bounds.yMax = yMax;
|
||||
}
|
||||
|
||||
Ptr<RandomDirectionParameters>
|
||||
RandomDirectionMobilityModel::GetDefaultParameters (void)
|
||||
{
|
||||
static Ptr<RandomDirectionParameters> parameters = Create<RandomDirectionParameters> ();
|
||||
if (parameters->m_xMin != g_rectangle.GetMinX () ||
|
||||
parameters->m_yMin != g_rectangle.GetMinY () ||
|
||||
parameters->m_xMax != g_rectangle.GetMaxX () ||
|
||||
parameters->m_yMax != g_rectangle.GetMaxY () ||
|
||||
if (parameters->m_bounds.xMin != g_rectangle.GetMinX () ||
|
||||
parameters->m_bounds.yMin != g_rectangle.GetMinY () ||
|
||||
parameters->m_bounds.xMax != g_rectangle.GetMaxX () ||
|
||||
parameters->m_bounds.yMax != g_rectangle.GetMaxY () ||
|
||||
g_speedVariable.IsDirty () ||
|
||||
g_pauseVariable.IsDirty ())
|
||||
{
|
||||
@@ -127,66 +130,29 @@ RandomDirectionMobilityModel::GetDefaultParameters (void)
|
||||
|
||||
|
||||
RandomDirectionMobilityModel::RandomDirectionMobilityModel ()
|
||||
: m_parameters (GetDefaultParameters ()),
|
||||
m_x (0.0),
|
||||
m_y (0.0),
|
||||
m_dx (0.0),
|
||||
m_dy (0.0),
|
||||
m_prevTime (Simulator::Now ()),
|
||||
m_pauseStart (Simulator::Now ())
|
||||
: m_parameters (GetDefaultParameters ())
|
||||
{
|
||||
SetInterfaceId (RandomDirectionMobilityModel::iid);
|
||||
InitializeDirectionAndSpeed ();
|
||||
}
|
||||
bool
|
||||
RandomDirectionMobilityModel::CheckMobilityModel (void) const
|
||||
{
|
||||
return
|
||||
m_x <= m_parameters->m_xMax &&
|
||||
m_x >= m_parameters->m_xMin &&
|
||||
m_y <= m_parameters->m_yMax &&
|
||||
m_y >= m_parameters->m_yMin;
|
||||
}
|
||||
|
||||
RandomDirectionMobilityModel::RandomDirectionMobilityModel (double x, double y)
|
||||
: m_parameters (GetDefaultParameters ()),
|
||||
m_x (x),
|
||||
m_y (y),
|
||||
m_dx (0.0),
|
||||
m_dy (0.0),
|
||||
m_prevTime (Simulator::Now ()),
|
||||
m_pauseStart (Simulator::Now ())
|
||||
: m_parameters (GetDefaultParameters ())
|
||||
{
|
||||
SetInterfaceId (RandomDirectionMobilityModel::iid);
|
||||
NS_ASSERT (CheckMobilityModel ());
|
||||
InitializeDirectionAndSpeed ();
|
||||
}
|
||||
RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr<RandomDirectionParameters> parameters)
|
||||
: m_parameters (parameters),
|
||||
m_x (0.0),
|
||||
m_y (0.0),
|
||||
m_dx (0.0),
|
||||
m_dy (0.0),
|
||||
m_prevTime (Simulator::Now ()),
|
||||
m_pauseStart (Simulator::Now ())
|
||||
: m_parameters (parameters)
|
||||
{
|
||||
SetInterfaceId (RandomDirectionMobilityModel::iid);
|
||||
InitializeDirectionAndSpeed ();
|
||||
NS_ASSERT (CheckMobilityModel ());
|
||||
}
|
||||
RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr<RandomDirectionParameters> parameters,
|
||||
double x, double y)
|
||||
: m_parameters (parameters),
|
||||
m_x (x),
|
||||
m_y (y),
|
||||
m_dx (0.0),
|
||||
m_dy (0.0),
|
||||
m_prevTime (Simulator::Now ()),
|
||||
m_pauseStart (Simulator::Now ())
|
||||
double x, double y)
|
||||
: m_parameters (parameters)
|
||||
{
|
||||
SetInterfaceId (RandomDirectionMobilityModel::iid);
|
||||
InitializeDirectionAndSpeed ();
|
||||
NS_ASSERT (CheckMobilityModel ());
|
||||
}
|
||||
void
|
||||
RandomDirectionMobilityModel::DoDispose (void)
|
||||
@@ -195,52 +161,6 @@ RandomDirectionMobilityModel::DoDispose (void)
|
||||
// chain up.
|
||||
MobilityModel::DoDispose ();
|
||||
}
|
||||
enum RandomDirectionMobilityModel::Side
|
||||
RandomDirectionMobilityModel::CalculateIntersection (double &x, double &y)
|
||||
{
|
||||
double xMin = m_parameters->m_xMin;
|
||||
double xMax = m_parameters->m_xMax;
|
||||
double yMin = m_parameters->m_yMin;
|
||||
double yMax = m_parameters->m_yMax;
|
||||
double xMaxY = m_y + (xMax - m_x) / m_dx * m_dy;
|
||||
double xMinY = m_y + (xMin - m_x) / m_dx * m_dy;
|
||||
double yMaxX = m_x + (yMax - m_y) / m_dy * m_dx;
|
||||
double yMinX = m_x + (yMin - m_y) / m_dy * m_dx;
|
||||
bool xMaxOk = xMaxY <= yMax && xMaxY >= yMin;
|
||||
bool xMinOk = xMinY <= yMax && xMinY >= yMin;
|
||||
bool yMaxOk = yMaxX <= xMax && yMaxX >= xMin;
|
||||
bool yMinOk = yMinX <= xMax && yMinX >= xMin;
|
||||
if (xMaxOk && m_dx >= 0)
|
||||
{
|
||||
x = xMax;
|
||||
y = xMaxY;
|
||||
return RandomDirectionMobilityModel::RIGHT;
|
||||
}
|
||||
else if (xMinOk && m_dx <= 0)
|
||||
{
|
||||
x = xMin;
|
||||
y = xMinY;
|
||||
return RandomDirectionMobilityModel::LEFT;
|
||||
}
|
||||
else if (yMaxOk && m_dy >= 0)
|
||||
{
|
||||
x = yMaxX;
|
||||
y = yMax;
|
||||
return RandomDirectionMobilityModel::TOP;
|
||||
}
|
||||
else if (yMinOk && m_dy <= 0)
|
||||
{
|
||||
x = yMinX;
|
||||
y = yMin;
|
||||
return RandomDirectionMobilityModel::BOTTOM;
|
||||
}
|
||||
else
|
||||
{
|
||||
NS_ASSERT (false);
|
||||
// quiet compiler
|
||||
return RandomDirectionMobilityModel::RIGHT;
|
||||
}
|
||||
}
|
||||
void
|
||||
RandomDirectionMobilityModel::InitializeDirectionAndSpeed (void)
|
||||
{
|
||||
@@ -251,82 +171,50 @@ void
|
||||
RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction)
|
||||
{
|
||||
double speed = m_parameters->m_speedVariable->GetValue ();
|
||||
m_dx = std::cos (direction) * speed;
|
||||
m_dy = std::sin (direction) * speed;
|
||||
double x, y;
|
||||
m_side = CalculateIntersection (x, y);
|
||||
double deltaX = x - m_x;
|
||||
double deltaY = y - m_y;
|
||||
double distance = sqrt (deltaX * deltaX + deltaY * deltaY);
|
||||
double seconds = distance / speed;
|
||||
double pause = m_parameters->m_pauseVariable->GetValue ();
|
||||
m_pauseStart = Simulator::Now () + Seconds (seconds);
|
||||
m_prevTime = Simulator::Now ();
|
||||
m_event = Simulator::Schedule (Seconds (seconds + pause),
|
||||
Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ());
|
||||
Time delay = m_helper.Reset (m_parameters->m_bounds,
|
||||
direction, speed,
|
||||
pause);
|
||||
m_event = Simulator::Schedule (delay + pause,
|
||||
&RandomDirectionMobilityModel::ResetDirectionAndSpeed, this);
|
||||
}
|
||||
void
|
||||
RandomDirectionMobilityModel::ResetDirectionAndSpeed (void)
|
||||
{
|
||||
Update ();
|
||||
double direction = UniformVariable::GetSingleValue (0, PI);
|
||||
switch (m_side)
|
||||
|
||||
switch (m_helper.GetSide (m_parameters->m_bounds))
|
||||
{
|
||||
case RandomDirectionMobilityModel::RIGHT:
|
||||
case MobilityModelHelper::RIGHT:
|
||||
direction += PI / 2;
|
||||
break;
|
||||
case RandomDirectionMobilityModel::LEFT:
|
||||
case MobilityModelHelper::LEFT:
|
||||
direction += - PI / 2;
|
||||
break;
|
||||
case RandomDirectionMobilityModel::TOP:
|
||||
case MobilityModelHelper::TOP:
|
||||
direction += PI;
|
||||
break;
|
||||
case RandomDirectionMobilityModel::BOTTOM:
|
||||
case MobilityModelHelper::BOTTOM:
|
||||
direction += 0.0;
|
||||
break;
|
||||
case MobilityModelHelper::NONE:
|
||||
NS_ASSERT (false);
|
||||
break;
|
||||
}
|
||||
SetDirectionAndSpeed (direction);
|
||||
NotifyCourseChange ();
|
||||
}
|
||||
void
|
||||
RandomDirectionMobilityModel::Update (void) const
|
||||
{
|
||||
Time end = std::min (Simulator::Now (), m_pauseStart);
|
||||
if (m_prevTime >= end)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Time deltaTime = end - m_prevTime;
|
||||
m_prevTime = Simulator::Now ();
|
||||
double deltaS = deltaTime.GetSeconds ();
|
||||
NS_ASSERT (CheckMobilityModel ());
|
||||
m_x += m_dx * deltaS;
|
||||
m_y += m_dy * deltaS;
|
||||
// round to closest boundaries.
|
||||
m_x = std::min (m_x, m_parameters->m_xMax);
|
||||
m_x = std::max (m_x, m_parameters->m_xMin);
|
||||
m_y = std::min (m_y, m_parameters->m_yMax);
|
||||
m_y = std::max (m_y, m_parameters->m_yMin);
|
||||
NS_ASSERT (CheckMobilityModel ());
|
||||
}
|
||||
Position
|
||||
RandomDirectionMobilityModel::DoGet (void) const
|
||||
{
|
||||
Update ();
|
||||
return Position (m_x, m_y, 0.0);
|
||||
Position2D position = m_helper.GetCurrentPosition (m_parameters->m_bounds);
|
||||
return Position (position.x, position.y, 0.0);
|
||||
}
|
||||
void
|
||||
RandomDirectionMobilityModel::DoSet (const Position &position)
|
||||
{
|
||||
bool changed = false;
|
||||
if (m_x != position.x || m_y != position.y)
|
||||
{
|
||||
changed = true;
|
||||
}
|
||||
m_x = position.x;
|
||||
m_y = position.y;
|
||||
m_prevTime = Simulator::Now ();
|
||||
m_pauseStart = Simulator::Now ();
|
||||
Position2D pos (position.x, position.y);
|
||||
m_helper.InitializePosition (pos);
|
||||
Simulator::Remove (m_event);
|
||||
InitializeDirectionAndSpeed ();
|
||||
NotifyCourseChange ();
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "ns3/event-id.h"
|
||||
#include "ns3/component-manager.h"
|
||||
#include "mobility-model.h"
|
||||
#include "mobility-model-helper.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
@@ -47,10 +48,7 @@ class RandomDirectionParameters : public Object
|
||||
void SetBounds (double xMin, double xMax, double yMin, double yMax);
|
||||
private:
|
||||
friend class RandomDirectionMobilityModel;
|
||||
double m_xMin;
|
||||
double m_xMax;
|
||||
double m_yMin;
|
||||
double m_yMax;
|
||||
struct AreaBounds m_bounds;
|
||||
RandomVariable *m_speedVariable;
|
||||
RandomVariable *m_pauseVariable;
|
||||
std::string m_speedVariableValue;
|
||||
@@ -88,14 +86,8 @@ class RandomDirectionMobilityModel : public MobilityModel
|
||||
|
||||
static const double PI;
|
||||
Ptr<RandomDirectionParameters> m_parameters;
|
||||
mutable double m_x;
|
||||
mutable double m_y;
|
||||
double m_dx;
|
||||
double m_dy;
|
||||
mutable Time m_prevTime;
|
||||
Time m_pauseStart;
|
||||
EventId m_event;
|
||||
enum Side m_side;
|
||||
MobilityModelHelper m_helper;
|
||||
};
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2006,2007 INRIA
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation;
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
||||
*/
|
||||
#include "random-walk-mobility-model.h"
|
||||
#include "ns3/default-value.h"
|
||||
#include "ns3/time-default-value.h"
|
||||
#include "ns3/simulator.h"
|
||||
#include "ns3/debug.h"
|
||||
#include <cmath>
|
||||
|
||||
NS_DEBUG_COMPONENT_DEFINE ("RandomWalk");
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
const InterfaceId RandomWalkMobilityModel::iid =
|
||||
MakeInterfaceId ("RandomWalkMobilityModel", MobilityModel::iid);
|
||||
const ClassId RandomWalkMobilityModel::cid =
|
||||
MakeClassId<RandomWalkMobilityModel, double, double> ("RandomWalkMobilityModel", RandomWalkMobilityModel::iid);
|
||||
|
||||
|
||||
static IntegerDefaultValue<double> g_minSpeed ("RandomWalkMinSpeed",
|
||||
"Minimum speed used during a random walk",
|
||||
0.1);
|
||||
static IntegerDefaultValue<double> g_maxSpeed ("RandomWalkMaxSpeed",
|
||||
"Maximum speed used during a random walk",
|
||||
0.5);
|
||||
static EnumDefaultValue<RandomWalkMobilityModelParameters::Mode> g_mode
|
||||
("RandomWalkMode",
|
||||
"The mode indicates the condition used to "
|
||||
"change the current speed and direction",
|
||||
RandomWalkMobilityModelParameters::MODE_DISTANCE, "Distance",
|
||||
RandomWalkMobilityModelParameters::MODE_TIME, "Time",
|
||||
0, 0);
|
||||
static IntegerDefaultValue<double> g_modeDistance ("RandomWalkModeDistance",
|
||||
"Distance to walk before changing direction and speed.",
|
||||
10);
|
||||
static TimeDefaultValue g_modeTime ("RandomWalkModeTime",
|
||||
"Time to walk before changing direction and speed.",
|
||||
Seconds (1));
|
||||
|
||||
RandomWalkMobilityModelParameters::RandomWalkMobilityModelParameters ()
|
||||
: m_minSpeed (g_minSpeed.GetValue ()),
|
||||
m_maxSpeed (g_maxSpeed.GetValue ()),
|
||||
m_mode (g_mode.GetValue ()),
|
||||
m_modeDistance (g_modeDistance.GetValue ()),
|
||||
m_modeTime (g_modeTime.GetValue ())
|
||||
{}
|
||||
bool
|
||||
RandomWalkMobilityModelParameters::IsDefault (void) const
|
||||
{
|
||||
if (m_minSpeed != g_minSpeed.GetValue () ||
|
||||
m_maxSpeed != g_maxSpeed.GetValue () ||
|
||||
m_mode != g_mode.GetValue () ||
|
||||
m_modeDistance != g_modeDistance.GetValue () ||
|
||||
m_modeTime != g_modeTime.GetValue ())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkMobilityModelParameters::SetSpeedBounds (double minSpeed, double maxSpeed)
|
||||
{
|
||||
m_minSpeed = minSpeed;
|
||||
m_maxSpeed = maxSpeed;
|
||||
}
|
||||
|
||||
|
||||
UniformVariable RandomWalkMobilityModel::m_randomDirection (0.0, 2*3.141592);
|
||||
|
||||
Ptr<RandomWalkMobilityModelParameters>
|
||||
RandomWalkMobilityModel::GetDefaultParameters (void)
|
||||
{
|
||||
static Ptr<RandomWalkMobilityModelParameters> parameters = Create<RandomWalkMobilityModelParameters> ();
|
||||
if (!parameters->IsDefault ())
|
||||
{
|
||||
parameters = Create<RandomWalkMobilityModelParameters> ();
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
RandomWalkMobilityModel::RandomWalkMobilityModel ()
|
||||
: m_x (0.0),
|
||||
m_y (0.0),
|
||||
m_dx (0.0),
|
||||
m_dy (0.0),
|
||||
m_prevTime (Simulator::Now ()),
|
||||
m_parameters (RandomWalkMobilityModel::GetDefaultParameters ())
|
||||
{
|
||||
SetInterfaceId (RandomWalkMobilityModel::iid);
|
||||
Reset ();
|
||||
}
|
||||
|
||||
RandomWalkMobilityModel::RandomWalkMobilityModel (double x, double y)
|
||||
: m_x (x),
|
||||
m_y (y),
|
||||
m_dx (0.0),
|
||||
m_dy (0.0),
|
||||
m_prevTime (Simulator::Now ()),
|
||||
m_parameters (RandomWalkMobilityModel::GetDefaultParameters ())
|
||||
{
|
||||
SetInterfaceId (RandomWalkMobilityModel::iid);
|
||||
Reset ();
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkMobilityModel::Reset (void)
|
||||
{
|
||||
Update ();
|
||||
double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed,
|
||||
m_parameters->m_maxSpeed);
|
||||
NS_DEBUG ("min="<< m_parameters->m_minSpeed << ", max=" << m_parameters->m_maxSpeed <<
|
||||
", speed=" << speed);
|
||||
double direction = m_randomDirection.GetValue ();
|
||||
double dx = std::cos (direction) * speed;
|
||||
double dy = std::sin (direction) * speed;
|
||||
m_dx = dx;
|
||||
m_dy = dy;
|
||||
Time delay;
|
||||
if (m_parameters->m_mode == RandomWalkMobilityModelParameters::MODE_TIME)
|
||||
{
|
||||
delay = m_parameters->m_modeTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
double distance = m_parameters->m_modeDistance;
|
||||
delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy));
|
||||
}
|
||||
NotifyCourseChange ();
|
||||
NS_DEBUG ("change speed at " << Simulator::Now () << " in " << delay);
|
||||
Simulator::Schedule (delay, &RandomWalkMobilityModel::Reset, this);
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkMobilityModel::Update (void) const
|
||||
{
|
||||
Time deltaTime = Simulator::Now () - m_prevTime;
|
||||
m_prevTime = Simulator::Now ();
|
||||
double deltaS = deltaTime.GetSeconds ();
|
||||
m_x += m_dx * deltaS;
|
||||
m_y += m_dy * deltaS;
|
||||
}
|
||||
|
||||
void
|
||||
RandomWalkMobilityModel::DoDispose (void)
|
||||
{
|
||||
m_parameters = 0;
|
||||
// chain up
|
||||
MobilityModel::DoDispose ();
|
||||
}
|
||||
void
|
||||
RandomWalkMobilityModel::DoGet (double &x, double &y, double &z) const
|
||||
{
|
||||
Update ();
|
||||
x = m_x;
|
||||
y = m_y;
|
||||
z = 0;
|
||||
}
|
||||
void
|
||||
RandomWalkMobilityModel::DoSet (double x, double y, double z)
|
||||
{
|
||||
bool changed = false;
|
||||
if (m_x != x || m_y != y)
|
||||
{
|
||||
changed = true;
|
||||
}
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_prevTime = Simulator::Now ();
|
||||
if (changed)
|
||||
{
|
||||
NotifyCourseChange ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
@@ -1,131 +0,0 @@
|
||||
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2006,2007 INRIA
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation;
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
||||
*/
|
||||
#ifndef RANDOM_WALK_MOBILITY_MODEL_H
|
||||
#define RANDOM_WALK_MOBILITY_MODEL_H
|
||||
|
||||
#include "ns3/object.h"
|
||||
#include "ns3/mobility-model.h"
|
||||
#include "ns3/nstime.h"
|
||||
#include "ns3/random-variable.h"
|
||||
#include "ns3/component-manager.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
/**
|
||||
* \brief parameters to control a random walk model
|
||||
*/
|
||||
class RandomWalkMobilityModelParameters : public Object
|
||||
{
|
||||
public:
|
||||
enum Mode {
|
||||
MODE_DISTANCE,
|
||||
MODE_TIME
|
||||
};
|
||||
/**
|
||||
* Instantiate a set of RandomWalk parameters initialized
|
||||
* with the Bind default values.
|
||||
*/
|
||||
RandomWalkMobilityModelParameters ();
|
||||
/**
|
||||
* \param minSpeed the minimum speed
|
||||
* \param maxSpeed the maximum speed
|
||||
*
|
||||
* The speed of any node is chosen such that minSpeed <= speed <= maxSpeed
|
||||
*/
|
||||
void SetSpeedBounds (double minSpeed, double maxSpeed);
|
||||
/**
|
||||
* \param distance the distance before a direction change
|
||||
*
|
||||
* Unit is meters
|
||||
*/
|
||||
void SetModeDistance (double distance);
|
||||
/**
|
||||
* \param time the delay before a direction change.
|
||||
*/
|
||||
void SetModeTime (Time time);
|
||||
private:
|
||||
bool IsDefault (void) const;
|
||||
friend class RandomWalkMobilityModel;
|
||||
double m_minSpeed;
|
||||
double m_maxSpeed;
|
||||
enum Mode m_mode;
|
||||
double m_modeDistance;
|
||||
Time m_modeTime;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief an unbounded 2D random walk position model
|
||||
*
|
||||
* Each instance moves with a speed and direction choosen at random
|
||||
* in the intervals [minspeed,maxspeed] and [0,2pi] until
|
||||
* either a fixed distance has been walked or until a fixed amount
|
||||
* of time.
|
||||
*
|
||||
* The parameters of the model can be specified either with the ns3::Bind
|
||||
* function and the variables "RandomWalkMinSpeed", "RandomWalkMaxSpeed",
|
||||
* "RandomWalkMode", "RandomWalkModeDistance", and, "RandomWalkModeTime" or
|
||||
* with an instance of the RandomWalkMobilityModelParameters class which
|
||||
* must be fed to the RandomWalkMobilityModel constructors.
|
||||
*/
|
||||
class RandomWalkMobilityModel : public MobilityModel
|
||||
{
|
||||
public:
|
||||
static const InterfaceId iid;
|
||||
static const ClassId cid;
|
||||
/**
|
||||
* Create a new position object located at position (0,0,0)
|
||||
*/
|
||||
RandomWalkMobilityModel ();
|
||||
/**
|
||||
* Create a new position object located at position (x,y,0)
|
||||
*/
|
||||
RandomWalkMobilityModel (double x, double y);
|
||||
/**
|
||||
* Create a new position object located at position (0,0,0)
|
||||
*/
|
||||
RandomWalkMobilityModel (Ptr<RandomWalkMobilityModelParameters> parameters);
|
||||
/**
|
||||
* Create a new position object located at position (x,y,0)
|
||||
*/
|
||||
RandomWalkMobilityModel (Ptr<RandomWalkMobilityModelParameters> parameters,
|
||||
double x, double y);
|
||||
private:
|
||||
virtual void DoDispose (void);
|
||||
virtual void DoGet (double &x, double &y, double &z) const;
|
||||
virtual void DoSet (double x, double y, double z);
|
||||
|
||||
void Reset (void);
|
||||
void Update (void) const;
|
||||
static Ptr<RandomWalkMobilityModelParameters> GetDefaultParameters (void);
|
||||
static UniformVariable m_randomDirection;
|
||||
|
||||
mutable double m_x;
|
||||
mutable double m_y;
|
||||
double m_dx;
|
||||
double m_dy;
|
||||
mutable Time m_prevTime;
|
||||
Ptr<RandomWalkMobilityModelParameters> m_parameters;
|
||||
};
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
#endif /* RANDOM_WALK_MOBILITY_MODEL_H */
|
||||
Reference in New Issue
Block a user