10#include "ns3/double.h"
13#include "ns3/pointer.h"
14#include "ns3/simulator.h"
15#include "ns3/string.h"
30 TypeId(
"ns3::RandomWalk2dMobilityModel")
32 .SetGroupName(
"Mobility")
34 .AddAttribute(
"Bounds",
35 "Bounds of the area to cruise.",
40 "Change current direction and speed after moving for this delay.",
44 .AddAttribute(
"Distance",
45 "Change current direction and speed after moving for this distance.",
50 "The mode indicates the condition used to "
51 "change the current speed and direction",
59 .AddAttribute(
"Direction",
60 "A random variable used to pick the direction (radians).",
61 StringValue(
"ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
64 .AddAttribute(
"Speed",
65 "A random variable used to pick the speed (m/s).",
66 StringValue(
"ns3::UniformRandomVariable[Min=2.0|Max=4.0]"),
95 Vector velocity(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
101 velocity.x = -1 * std::abs(velocity.x);
104 velocity.x = std::abs(velocity.x);
107 velocity.y = -1 * std::abs(velocity.y);
110 velocity.y = std::abs(velocity.y);
113 velocity.x = -1 * std::abs(velocity.x);
114 velocity.y = -1 * std::abs(velocity.y);
117 velocity.x = std::abs(velocity.x);
118 velocity.y = -1 * std::abs(velocity.y);
121 velocity.x = -1 * std::abs(velocity.x);
122 velocity.y = std::abs(velocity.y);
125 velocity.x = std::abs(velocity.x);
126 velocity.y = std::abs(velocity.y);
130 NS_LOG_INFO(
"Setting new velocity to " << velocity);
154 Vector nextPosition = position;
155 nextPosition.x += velocity.x * delayLeft.
GetSeconds();
156 nextPosition.y += velocity.y * delayLeft.
GetSeconds();
168 double delaySeconds = std::numeric_limits<double>::max();
172 std::min(delaySeconds, std::abs((nextPosition.x - position.x) / velocity.x));
174 else if (velocity.y != 0)
177 std::min(delaySeconds, std::abs((nextPosition.y - position.y) / velocity.y));
181 NS_ABORT_MSG(
"RandomWalk2dMobilityModel::DoWalk: unable to calculate the rebound time "
182 "(the node is stationary).");
206 velocity.x = -velocity.x;
210 velocity.y = -velocity.y;
216 velocity.x = -velocity.x;
217 velocity.y = -velocity.y;
222 NS_LOG_INFO(
"Rebounding with new velocity " << velocity);
Vector GetCurrentPosition() const
Get current position vector.
Vector GetVelocity() const
Get velocity; if paused, will return a zero vector.
void Update() const
Update position, if not paused, from last position and time of last update.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
void Unpause()
Resume mobility from current position at current velocity.
void SetPosition(const Vector &position)
Set position vector.
void SetVelocity(const Vector &vel)
Set new velocity vector.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Hold variables of type enum.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Keep track of the current position and velocity of an object.
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoInitialize()
Initialize() implementation.
virtual void DoDispose()
Destructor implementation.
virtual double GetValue()=0
Get the next random value drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
2D random walk mobility model.
Ptr< RandomVariableStream > m_speed
rv for picking speed
static TypeId GetTypeId()
Register this type with the TypeId system.
void DoInitialize() override
Initialize() implementation.
double m_modeDistance
Change direction and speed after this distance.
~RandomWalk2dMobilityModel() override
void DrawRandomVelocityAndDistance()
Draw a new random velocity and distance to travel, and call DoWalk()
void DoWalk(Time timeLeft)
Walk according to position and velocity, until distance is reached, time is reached,...
void Rebound(Time timeLeft)
Performs the rebound of the node if it reaches a boundary.
Vector DoGetPosition() const override
ConstantVelocityHelper m_helper
helper for this object
Vector DoGetVelocity() const override
Time m_modeTime
Change current direction and speed after this delay.
Rectangle m_bounds
Bounds of the area to cruise.
void DoSetPosition(const Vector &position) override
void DoDispose() override
Destructor implementation.
Ptr< RandomVariableStream > m_direction
rv for picking direction
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
Mode m_mode
whether in time or distance mode
EventId m_event
stored event ID
Side GetClosestSideOrCorner(const Vector &position) const
bool IsInside(const Vector &position) const
bool IsOnTheBorder(const Vector &position) const
Vector CalculateIntersection(const Vector ¤t, const Vector &speed) const
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Hold variables of type string.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Seconds(double value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Ptr< const AttributeChecker > MakeDoubleChecker()
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Ptr< const AttributeChecker > MakeRectangleChecker()
Ptr< const AttributeAccessor > MakeRectangleAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.