diff -r d64b1561b1c2 src/mobility/waypoint-list-mobility-model.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mobility/waypoint-list-mobility-model.cc Mon Mar 03 11:26:39 2008 +0000 @@ -0,0 +1,179 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * 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 +#include "ns3/simulator.h" +#include "ns3/random-variable.h" +#include "ns3/random-variable-default-value.h" +#include "ns3/type-id-default-value.h" +#include "waypoint-list-mobility-model.h" +#include "random-position.h" + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (WaypointListMobilityModel); + +static RandomVariableDefaultValue +g_speed ("WaypointListSpeed", + "A random variable used to pick the speed of a random waypoint model.", + "Uniform:10:20"); + + +WaypointListMobilityModelParameters::WaypointListMobilityModelParameters () + : m_speed (g_speed.Get ()) +{ +} +WaypointListMobilityModelParameters::WaypointListMobilityModelParameters (const RandomVariable &speed, + const std::vector &waypoints) + : m_speed (speed), + m_waypoints (waypoints) +{ +} +void +WaypointListMobilityModelParameters::SetWaypoints (const std::vector &waypoints) +{ + m_waypoints = waypoints; +} +void +WaypointListMobilityModelParameters::SetSpeed (const RandomVariable &speed) +{ + m_speed = speed; +} + +Ptr +WaypointListMobilityModelParameters::GetCurrent (void) +{ + static Ptr parameters = 0; + if (parameters == 0 || + g_speed.IsDirty ()) + { + parameters = CreateObject (); + } + return parameters; +} + +TypeId +WaypointListMobilityModel::GetTypeId (void) +{ + static TypeId tid = TypeId ("WaypointListMobilityModel") + .SetParent () + .AddConstructor () + .AddConstructor > (); + return tid; +} + +WaypointListMobilityModel::WaypointListMobilityModel () + : m_parameters (WaypointListMobilityModelParameters::GetCurrent ()) +{ + Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this); +} + +WaypointListMobilityModel::WaypointListMobilityModel (Ptr parameters) + : m_parameters (parameters) +{ + Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this); + NotifyCourseChange (); +} + +void +WaypointListMobilityModel::BeginWalk (void) +{ + if (m_nextWaypoint == m_parameters->m_waypoints.end ()) + { + // list of waypoints is empty; nothing to do + return; + } + Vector destination = *m_nextWaypoint; + NextWaypoint (); + Vector current = GetPosition (); + m_walkStartPoint = current; + m_walkStartTime = Simulator::Now (); + double speed = m_parameters->m_speed.GetValue (); + double dx = (destination.x - current.x); + double dy = (destination.y - current.y); + double dz = (destination.z - current.z); + double sq = dx*dx + dy*dy + dz*dz; + if (sq == 0) + { + m_velocity = Vector (0, 0, 0); + } + else + { + double k = speed / std::sqrt (sq); + m_velocity = Vector (k*dx, k*dy, k*dz); + } + Time travelDelay = Seconds (CalculateDistance (destination, current) / speed); + m_walkEndEvent = Simulator::Schedule + (travelDelay, &WaypointListMobilityModel::BeginWalk, this); + NotifyCourseChange (); +} + + +/// prepare next waypoint +void +WaypointListMobilityModel::NextWaypoint (void) +{ + m_nextWaypoint++; + if (m_nextWaypoint == m_parameters->m_waypoints.end ()) + { + // cycle back to the first waypoint + m_nextWaypoint = m_parameters->m_waypoints.begin (); + } +} + +void +WaypointListMobilityModel::Start (void) +{ + if (m_nextWaypoint == m_parameters->m_waypoints.end ()) + { + NS_FATAL_ERROR("list of waypoints is empty; nothing to do"); + return; + } + m_nextWaypoint = m_parameters->m_waypoints.begin (); + m_velocity = Vector (0, 0, 0); + m_walkStartTime = Simulator::Now (); + m_walkStartPoint = *m_nextWaypoint; + BeginWalk (); +} + +Vector +WaypointListMobilityModel::DoGetPosition (void) const +{ + double dt = (Simulator::Now () - m_walkStartTime).GetSeconds (); + return Vector (m_walkStartPoint.x + dt*m_velocity.x, + m_walkStartPoint.y + dt*m_velocity.y, + m_walkStartPoint.z + dt*m_velocity.z); +} +void +WaypointListMobilityModel::DoSetPosition (const Vector &position) +{ + NS_FATAL_ERROR ("Cannot change the position WaypointListMobilityModel models, only the waypoints"); +} +Vector +WaypointListMobilityModel::DoGetVelocity (void) const +{ + return m_velocity; +} + +WaypointListMobilityModel::~WaypointListMobilityModel () +{ + m_walkEndEvent.Cancel (); +} + +} // namespace ns3 diff -r d64b1561b1c2 src/mobility/waypoint-list-mobility-model.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mobility/waypoint-list-mobility-model.h Mon Mar 03 11:26:39 2008 +0000 @@ -0,0 +1,112 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 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 WAYPOINT_LIST_MOBILITY_MODEL_H +#define WAYPOINT_LIST_MOBILITY_MODEL_H + +#include "static-speed-helper.h" +#include "mobility-model.h" +#include "random-position.h" +#include "ns3/ptr.h" +#include "ns3/random-variable.h" + +namespace ns3 { + +/** + * \brief the parameters which control the behavior of a random waypoint + * mobility model. + */ +class WaypointListMobilityModelParameters : public Object +{ +public: + /** + * Default parameters from \valueref{WaypointListPause}, + * and, \valueref{WaypointListPosition}. + */ + WaypointListMobilityModelParameters (); + /** + * \param randomPosition a random position model to choose the position of waypoints. + * \param speed a random variable to choose the speed + * \param pause a random variable to choose the pause delay + */ + WaypointListMobilityModelParameters (const RandomVariable &speed, + const std::vector &waypoints); + /** + * \param waypoints a list of waypoints + */ + void SetWaypoints (const std::vector &waypoints); + /** + * \param speed a random variable to choose the speed + */ + void SetSpeed (const RandomVariable &speed); +private: + friend class WaypointListMobilityModel; + static Ptr GetCurrent (void); + RandomVariable m_speed; + std::vector m_waypoints; +}; + +/** + * \brief a waypoint list mobility model + * + * Each object chooses a random speed, and a random pause time: it + * then pauses for the specified pause time, and starts moving towards + * the specified destination with the specified speed. Once the + * destination is reached it picks the next waypoint and the process + * starts again. + * + * The implementation of this model is not 2d-specific. i.e. if you provide + * a 3d random waypoint position model to this mobility model, the model + * will still work. + */ +class WaypointListMobilityModel : public MobilityModel +{ +public: + static TypeId GetTypeId (void); + /** + * Default parameters from \valueref{WaypointListPause}, + * and, \valueref{WaypointListPosition}. + */ + WaypointListMobilityModel (); + /** + * \param parameters the parameters which control the behavior of this model. + */ + WaypointListMobilityModel (Ptr parameters); + + ~WaypointListMobilityModel (); +private: + void Start (void); + virtual Vector DoGetPosition (void) const; + virtual void DoSetPosition (const Vector &position); + virtual Vector DoGetVelocity (void) const; + void NextWaypoint (void); + void BeginWalk (void); + + Ptr m_parameters; + std::vector::const_iterator m_nextWaypoint; + EventId m_walkEndEvent; + Time m_walkStartTime; + Vector m_walkStartPoint; + Vector m_velocity; +}; + +} // namespace ns3 + +#endif /* WAYPOINT_LIST_MOBILITY_MODEL_H */ diff -r d64b1561b1c2 src/mobility/wscript --- a/src/mobility/wscript Tue Feb 26 01:39:59 2008 +0100 +++ b/src/mobility/wscript Mon Mar 03 11:26:39 2008 +0000 @@ -19,6 +19,7 @@ def build(bld): 'random-walk-2d-mobility-model.cc', 'random-direction-2d-mobility-model.cc', 'ns2-mobility-file-topology.cc', + 'waypoint-list-mobility-model.cc', ] headers = bld.create_obj('ns3header') @@ -39,4 +40,5 @@ def build(bld): 'random-walk-2d-mobility-model.h', 'random-direction-2d-mobility-model.h', 'ns2-mobility-file-topology.h', + 'waypoint-list-mobility-model.h', ] diff -r d64b1561b1c2 utils/mobility-visualizer-model.cc --- a/utils/mobility-visualizer-model.cc Tue Feb 26 01:39:59 2008 +0100 +++ b/utils/mobility-visualizer-model.cc Mon Mar 03 11:26:39 2008 +0000 @@ -15,6 +15,8 @@ #include "ns3/node-list.h" #include "ns3/rectangle-default-value.h" #include "ns3/type-id-default-value.h" +#include "ns3/waypoint-list-mobility-model.h" +#include "ns3/log.h" #include "mobility-visualizer.h" @@ -76,6 +78,7 @@ int model_init (int argc, char *argv[], DefaultValue::Bind ("RandomWalk2dBounds", "0:400:0:300"); DefaultValue::Bind ("RandomDirection2dArea", "0:400:0:300"); DefaultValue::Bind ("RandomWaypointSpeed", "Uniform:10:30"); + DefaultValue::Bind ("WaypointListSpeed", "Uniform:100:150"); // DefaultValue::Bind ("RandomDiscPositionX", "100"); // DefaultValue::Bind ("RandomDiscPositionY", "50"); @@ -98,6 +101,18 @@ int model_init (int argc, char *argv[], } topology.Layout (NodeList::Begin (), NodeList::End ()); + g_numNodes++; + Ptr node = CreateObject (); + std::vector waypoints; + for (double theta = 0; theta < 2*M_PI; theta += 2*M_PI/20) + { + double x = cos(theta)*100; + double y = sin(theta)*100; + waypoints.push_back (Vector (200+x, 150+y, 0)); + } + Ptr params = Create (); + params->SetWaypoints (waypoints); + node->AggregateObject (CreateObject (params)); Simulator::Schedule (g_sampleInterval, Sample);