diff --git a/SConstruct b/SConstruct index aaf27f5cb..95f578c36 100644 --- a/SConstruct +++ b/SConstruct @@ -129,6 +129,7 @@ simu.add_sources([ 'scheduler-map.cc', 'event-impl.cc', 'simulator.cc', + 'time-default-value.cc', ]) simu.add_headers([ 'scheduler-heap.h', @@ -144,6 +145,7 @@ simu.add_inst_headers([ 'scheduler.h', 'scheduler-factory.h', 'simulation-singleton.h', + 'time-default-value.h', ]) high_precision_as_double = ARGUMENTS.get('high-precision-as-double', 'n') if high_precision_as_double == 'y': @@ -247,6 +249,7 @@ node.add_sources ([ 'notify-static-position.cc', 'static-speed-position.cc', 'grid-topology.cc', + 'random-walk-position.cc', ]) node.add_inst_headers ([ 'node.h', @@ -269,6 +272,7 @@ node.add_inst_headers ([ 'notify-static-position.h', 'static-speed-position.h', 'grid-topology.h', + 'random-walk-position.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc new file mode 100644 index 000000000..e545bc4e7 --- /dev/null +++ b/src/node/random-walk-position.cc @@ -0,0 +1,153 @@ +/* -*- 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 + */ +#include "random-walk-position.h" +#include "ns3/default-value.h" +#include "ns3/time-default-value.h" +#include "ns3/simulator.h" +#include + +namespace ns3 { + +static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", + "Minimum speed used during a random walk", + 0); +static IntegerDefaultValue g_maxSpeed ("RandomWalkMaxSpeed", + "Maximum speed used during a random walk", + 0); +static EnumDefaultValue g_mode + ("RandomWalkMode", + "The mode indicates the condition used to " + "change the current speed and direction", + RandomWalkPositionParameters::MODE_DISTANCE, "Distance", + RandomWalkPositionParameters::MODE_TIME, "Time", + 0, 0); +static IntegerDefaultValue 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)); + +RandomWalkPositionParameters::RandomWalkPositionParameters () + : m_minSpeed (g_minSpeed.GetValue ()), + m_maxSpeed (g_maxSpeed.GetValue ()), + m_mode (g_mode.GetValue ()), + m_modeDistance (g_modeDistance.GetValue ()), + m_modeTime (g_modeDistance.GetValue ()) +{} +bool +RandomWalkPositionParameters::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 +RandomWalkPositionParameters::SetSpeedBounds (double minSpeed, double maxSpeed) +{ + m_minSpeed = minSpeed; + m_maxSpeed = maxSpeed; +} + + +UniformVariable RandomWalkPosition::m_randomDirection (0.0, 2*3.141592); + +Ptr +RandomWalkPosition::GetDefaultParameters (void) +{ + static Ptr parameters = Create (); + if (!parameters->IsDefault ()) + { + parameters = Create (); + } + return parameters; +} + +RandomWalkPosition::RandomWalkPosition () + : m_x (0.0), + m_y (0.0), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_parameters (RandomWalkPosition::GetDefaultParameters ()) +{ + Reset (); +} + +void +RandomWalkPosition::Reset (void) +{ + Update (); + double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, + m_parameters->m_maxSpeed); + double direction = m_randomDirection.GetValue (); + double dx = std::cos (direction); + double dy = std::sin (direction); + m_dx = dx * speed; + m_dy = dy * speed; + Time delay; + if (m_parameters->m_mode == RandomWalkPositionParameters::MODE_TIME) + { + delay = m_parameters->m_modeTime; + } + else + { + double distance = g_modeDistance.GetValue (); + delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); + } + Simulator::Schedule (delay, &RandomWalkPosition::Reset, this); +} + +void +RandomWalkPosition::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 +RandomWalkPosition::DoGet (double &x, double &y, double &z) const +{ + Update (); + x = m_x; + y = m_y; + z = 0; +} +void +RandomWalkPosition::DoSet (double x, double y, double z) +{ + m_x = x; + m_y = y; + m_prevTime = Simulator::Now (); +} + + +} // namespace ns3 diff --git a/src/node/random-walk-position.h b/src/node/random-walk-position.h new file mode 100644 index 000000000..c4b814b79 --- /dev/null +++ b/src/node/random-walk-position.h @@ -0,0 +1,94 @@ +/* -*- 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 + */ +#ifndef RANDOM_WALK_POSITION_H +#define RANDOM_WALK_POSITION_H + +#include "ns3/object.h" +#include "ns3/position.h" +#include "ns3/nstime.h" +#include "ns3/random-variable.h" + +namespace ns3 { + +class RandomWalkPositionParameters : public Object +{ + public: + enum Mode { + MODE_DISTANCE, + MODE_TIME + }; + RandomWalkPositionParameters (); + bool IsDefault (void) const; + void SetSpeedBounds (double minSpeed, double maxSpeed); + private: + friend class RandomWalkPosition; + double m_xMin; + double m_xMax; + double m_yMin; + double m_yMax; + double m_minSpeed; + double m_maxSpeed; + enum Mode m_mode; + double m_modeDistance; + Time m_modeTime; +}; + +/** + * \brief a 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 period + * of time. + */ +class RandomWalkPosition : public Position +{ + public: + /** + * Create a new position object located at a random + * position within the default random walk area. + */ + RandomWalkPosition (); + RandomWalkPosition (double x, double y); + RandomWalkPosition (Ptr parameters); + RandomWalkPosition (Ptr parameters, + double x, double y); + private: + 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 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 m_parameters; +}; + + +} // namespace ns3 + +#endif /* RANDOM_WALK_POSITION_H */