Add Waypoint Mobility model to mainstream
authorPhillip Sitbon <phillip.sitbon@gmail.com>
Tue Nov 10 12:10:35 2009 +0100 (3 months ago)
changeset 54945774651f7b98
parent 5493 8ffa53e9a701
child 5495 7f6bb3ad07b4
Add Waypoint Mobility model to mainstream
src/mobility/mobility.h
src/mobility/waypoint-mobility-model.cc
src/mobility/waypoint-mobility-model.h
src/mobility/waypoint.cc
src/mobility/waypoint.h
src/mobility/wscript
     1.1 --- a/src/mobility/mobility.h	Tue Nov 10 10:31:51 2009 +0100
     1.2 +++ b/src/mobility/mobility.h	Tue Nov 10 12:10:35 2009 +0100
     1.3 @@ -28,4 +28,9 @@
     1.4   *   - ns3::RandomDirection2dMobilityModel: a 2d random direction mobility
     1.5   *     model where the bounds of the mobility are are a rectangle.
     1.6   *
     1.7 + *   - ns3::WaypointMobilityModel: A model which determines paths from sets
     1.8 + *     of ns3::Waypoint objects, similar to using events to update velocity
     1.9 + *     and direction with a ns3::ConstantVelocityMobilityModel. This model
    1.10 + *     is slightly faster for this task and uses less memory.
    1.11 + *
    1.12   */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/mobility/waypoint-mobility-model.cc	Tue Nov 10 12:10:35 2009 +0100
     2.3 @@ -0,0 +1,182 @@
     2.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     2.5 +/*
     2.6 + * Copyright (c) 2009 Phillip Sitbon
     2.7 + *
     2.8 + * This program is free software; you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License version 2 as
    2.10 + * published by the Free Software Foundation;
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program; if not, write to the Free Software
    2.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2.20 + *
    2.21 + * Author: Phillip Sitbon <phillip@sitbon.net>
    2.22 + */
    2.23 +#include <limits>
    2.24 +#include "ns3/abort.h"
    2.25 +#include "ns3/simulator.h"
    2.26 +#include "ns3/uinteger.h"
    2.27 +#include "ns3/log.h"
    2.28 +#include "waypoint-mobility-model.h"
    2.29 +
    2.30 +NS_LOG_COMPONENT_DEFINE ("WaypointMobilityModel");
    2.31 +
    2.32 +namespace ns3 {
    2.33 +
    2.34 +NS_OBJECT_ENSURE_REGISTERED (WaypointMobilityModel);
    2.35 +
    2.36 +
    2.37 +TypeId
    2.38 +WaypointMobilityModel::GetTypeId (void)
    2.39 +{
    2.40 +  static TypeId tid = TypeId ("ns3::WaypointMobilityModel")
    2.41 +    .SetParent<MobilityModel> ()
    2.42 +    .SetGroupName ("Mobility")
    2.43 +    .AddConstructor<WaypointMobilityModel> ()
    2.44 +    .AddAttribute ("NextWaypoint", "The next waypoint used to determine position.",
    2.45 +                   TypeId::ATTR_GET,
    2.46 +                   WaypointValue (),
    2.47 +                   MakeWaypointAccessor (&WaypointMobilityModel::GetNextWaypoint),
    2.48 +                   MakeWaypointChecker ())
    2.49 +    .AddAttribute ("WaypointsLeft", "The number of waypoints remaining.",
    2.50 +                   TypeId::ATTR_GET,
    2.51 +                   UintegerValue (0),
    2.52 +                   MakeUintegerAccessor (&WaypointMobilityModel::WaypointsLeft),
    2.53 +                   MakeUintegerChecker<uint32_t> ())
    2.54 +    ;
    2.55 +  return tid;
    2.56 +}
    2.57 +
    2.58 +
    2.59 +WaypointMobilityModel::WaypointMobilityModel ()
    2.60 + : m_first (true)
    2.61 +{
    2.62 +}
    2.63 +WaypointMobilityModel::~WaypointMobilityModel ()
    2.64 +{
    2.65 +}
    2.66 +void 
    2.67 +WaypointMobilityModel::DoDispose (void)
    2.68 +{
    2.69 +  MobilityModel::DoDispose ();
    2.70 +}
    2.71 +void
    2.72 +WaypointMobilityModel::AddWaypoint (const Waypoint &waypoint)
    2.73 +{
    2.74 +  if ( m_first )
    2.75 +    {
    2.76 +      m_first = false;
    2.77 +      m_current = m_next = waypoint;
    2.78 +    }
    2.79 +  else
    2.80 +    {
    2.81 +      NS_ABORT_MSG_IF ( !m_waypoints.empty () && (m_waypoints.back ().GetTime () >= waypoint.GetTime ()),
    2.82 +                        "Waypoints must be added in ascending time order");
    2.83 +      m_waypoints.push_back (waypoint);
    2.84 +    }
    2.85 +}
    2.86 +Waypoint
    2.87 +WaypointMobilityModel::GetNextWaypoint (void) const
    2.88 +{
    2.89 +  Update ();
    2.90 +  return m_next;
    2.91 +}
    2.92 +uint32_t
    2.93 +WaypointMobilityModel::WaypointsLeft (void) const
    2.94 +{
    2.95 +  Update ();
    2.96 +  return m_waypoints.size();
    2.97 +}
    2.98 +void
    2.99 +WaypointMobilityModel::Update (void) const
   2.100 +{
   2.101 +  const Time now = Simulator::Now ();
   2.102 +  bool newWaypoint = false;
   2.103 +
   2.104 +  if ( now <= m_current.GetTime () )
   2.105 +    {
   2.106 +      return;
   2.107 +    }
   2.108 +
   2.109 +  while ( now >= m_next.GetTime ()  )
   2.110 +    {
   2.111 +      if ( m_waypoints.empty () )
   2.112 +        {
   2.113 +          if ( m_current.GetTime () <= m_next.GetTime () )
   2.114 +            {
   2.115 +              m_current.SetPosition(m_next.GetPosition ());
   2.116 +              m_current.SetTime (now);
   2.117 +              m_velocity = Vector (0,0,0);
   2.118 +              NotifyCourseChange ();
   2.119 +            }
   2.120 +          else
   2.121 +            {
   2.122 +              m_current.SetTime (now);
   2.123 +            }
   2.124 +
   2.125 +          return;
   2.126 +        }
   2.127 +
   2.128 +      m_current = m_next;
   2.129 +      m_next = m_waypoints.front ();
   2.130 +      m_waypoints.pop_front ();
   2.131 +      newWaypoint = true;
   2.132 +
   2.133 +      const double t_span = (m_next.GetTime () - m_current.GetTime ()).GetSeconds ();
   2.134 +      NS_ASSERT (t_span > 0);
   2.135 +      m_velocity.x = (m_next.GetPosition ().x - m_current.GetPosition ().x) / t_span;
   2.136 +      m_velocity.y = (m_next.GetPosition ().y - m_current.GetPosition ().y) / t_span;
   2.137 +      m_velocity.z = (m_next.GetPosition ().z - m_current.GetPosition ().z) / t_span;
   2.138 +    }
   2.139 +
   2.140 +
   2.141 +  const double t_diff = (now - m_current.GetTime ()).GetSeconds();
   2.142 +  Vector pos;
   2.143 +  pos.x = m_current.GetPosition ().x + m_velocity.x * t_diff;
   2.144 +  pos.y = m_current.GetPosition ().y + m_velocity.y * t_diff;
   2.145 +  pos.z = m_current.GetPosition ().z + m_velocity.z * t_diff;
   2.146 +  m_current.SetPosition (pos);
   2.147 +  m_current.SetTime (now);
   2.148 +
   2.149 +  if ( newWaypoint )
   2.150 +    {
   2.151 +      NotifyCourseChange ();
   2.152 +    }
   2.153 +}
   2.154 +Vector
   2.155 +WaypointMobilityModel::DoGetPosition (void) const
   2.156 +{
   2.157 +  Update ();
   2.158 +  return m_current.GetPosition ();
   2.159 +}
   2.160 +void
   2.161 +WaypointMobilityModel::DoSetPosition (const Vector &position)
   2.162 +{
   2.163 +  const Time now = Simulator::Now ();
   2.164 +  Update ();
   2.165 +  m_current.SetTime (std::max (now, m_next.GetTime ()));
   2.166 +  m_current.SetPosition (position);
   2.167 +  m_velocity = Vector (0,0,0);
   2.168 +  NotifyCourseChange ();
   2.169 +}
   2.170 +void
   2.171 +WaypointMobilityModel::EndMobility (void)
   2.172 +{
   2.173 +  m_waypoints.clear ();
   2.174 +  m_current.SetTime (Seconds (std::numeric_limits<double>::infinity ()));
   2.175 +  m_next.SetTime (m_current.GetTime ());
   2.176 +  m_first = true;
   2.177 +}
   2.178 +Vector
   2.179 +WaypointMobilityModel::DoGetVelocity (void) const
   2.180 +{
   2.181 +  return m_velocity;
   2.182 +}
   2.183 +
   2.184 +} // namespace ns3
   2.185 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/mobility/waypoint-mobility-model.h	Tue Nov 10 12:10:35 2009 +0100
     3.3 @@ -0,0 +1,115 @@
     3.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     3.5 +/*
     3.6 + * Copyright (c) 2009 Phillip Sitbon
     3.7 + *
     3.8 + * This program is free software; you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License version 2 as
    3.10 + * published by the Free Software Foundation;
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program; if not, write to the Free Software
    3.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.20 + *
    3.21 + * Author: Phillip Sitbon <phillip@sitbon.net>
    3.22 + */
    3.23 +#ifndef WAYPOINT_MOBILITY_MODEL_H
    3.24 +#define WAYPOINT_MOBILITY_MODEL_H
    3.25 +
    3.26 +#include <stdint.h>
    3.27 +#include <deque>
    3.28 +#include "mobility-model.h"
    3.29 +#include "ns3/vector.h"
    3.30 +#include "waypoint.h"
    3.31 +
    3.32 +namespace ns3 {
    3.33 +
    3.34 +/**
    3.35 + * \brief a waypoint-based mobility model
    3.36 + *
    3.37 + * Each object determines its velocity and position at a given time
    3.38 + * from a set of ns3::Waypoint objects. The position of each object
    3.39 + * is not updated unless queried, and past waypoints are discarded
    3.40 + * after the current simulation time greater than their time value.
    3.41 + * 
    3.42 + * The initial position of each object corresponds to the position of
    3.43 + * the first waypoint, and the initial velocity of each object is zero.
    3.44 + * Upon reaching the last waypoint, object positions becomes static and
    3.45 + * velocity is zero.
    3.46 + *
    3.47 + * When a node is in between waypoints (in time), it moves with a constant
    3.48 + * velocity between the position at the previous waypoint and the position
    3.49 + * at the current waypoint. To make a node hold a certain position for a
    3.50 + * time interval, two waypoints with the same position (but different times)
    3.51 + * should be inserted sequentially.
    3.52 + *
    3.53 + * Waypoints can be added at any time, and setting the current position
    3.54 + * of an object will set its velocity to zero until the next waypoint time
    3.55 + * (at which time the object jumps to the next waypoint), unless there are
    3.56 + * no more waypoints in which case it will not change without user
    3.57 + * intervention.
    3.58 + *
    3.59 + */
    3.60 +class WaypointMobilityModel : public MobilityModel
    3.61 +{
    3.62 + public:
    3.63 +  static TypeId GetTypeId (void);
    3.64 +
    3.65 +  /**
    3.66 +   * Create a path with no waypoints at location (0,0,0).
    3.67 +   */
    3.68 +  WaypointMobilityModel ();
    3.69 +  virtual ~WaypointMobilityModel ();
    3.70 +
    3.71 +  /**
    3.72 +   * \param waypoint waypoint to append to the object path.
    3.73 +   *
    3.74 +   * Add a waypoint to the path of the object. The time must
    3.75 +   * be greater than the previous waypoint added, otherwise
    3.76 +   * a fatal error occurs. The first waypoint is set as the
    3.77 +   * current position with a velocity of zero.
    3.78 +   * 
    3.79 +   */
    3.80 +  void AddWaypoint (const Waypoint &waypoint);
    3.81 +
    3.82 +  /**
    3.83 +   * Get the waypoint that this object is traveling towards.
    3.84 +   */
    3.85 +  Waypoint GetNextWaypoint (void) const;
    3.86 +
    3.87 +  /**
    3.88 +   * Get the number of waypoints left for this object, excluding
    3.89 +   * the next one.
    3.90 +   */
    3.91 +  uint32_t WaypointsLeft (void) const;
    3.92 +
    3.93 +  /**
    3.94 +   * Clear any existing waypoints and set the current waypoint
    3.95 +   * time to infinity. Calling this is only an optimization and
    3.96 +   * not required. After calling this function, adding waypoints
    3.97 +   * behaves as it would for a new object.
    3.98 +   */
    3.99 +  void EndMobility (void);
   3.100 +
   3.101 + private:
   3.102 +  void Update (void) const;
   3.103 +  virtual void DoDispose (void);
   3.104 +  virtual Vector DoGetPosition (void) const;
   3.105 +  virtual void DoSetPosition (const Vector &position);
   3.106 +  virtual Vector DoGetVelocity (void) const;
   3.107 +
   3.108 +  bool m_first;
   3.109 +  mutable std::deque<Waypoint> m_waypoints;
   3.110 +  mutable Waypoint m_current;
   3.111 +  mutable Waypoint m_next;
   3.112 +  mutable Vector m_velocity;
   3.113 +};
   3.114 +
   3.115 +} // namespace ns3
   3.116 +
   3.117 +#endif /* WAYPOINT_MOBILITY_MODEL_H */
   3.118 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/mobility/waypoint.cc	Tue Nov 10 12:10:35 2009 +0100
     4.3 @@ -0,0 +1,74 @@
     4.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     4.5 +/*
     4.6 + * Copyright (c) 2009 Phillip Sitbon
     4.7 + *
     4.8 + * This program is free software; you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License version 2 as
    4.10 + * published by the Free Software Foundation;
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details.
    4.16 + *
    4.17 + * You should have received a copy of the GNU General Public License
    4.18 + * along with this program; if not, write to the Free Software
    4.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    4.20 + *
    4.21 + * Author: Phillip Sitbon <phillip@sitbon.net>
    4.22 + */
    4.23 +#include "waypoint.h"
    4.24 +
    4.25 +namespace ns3 {
    4.26 +
    4.27 +ATTRIBUTE_HELPER_CPP (Waypoint);
    4.28 +
    4.29 +Waypoint::Waypoint (const Time &waypointTime, const Vector &waypointPosition)
    4.30 +  : m_time (waypointTime),
    4.31 +  m_position (waypointPosition)
    4.32 +{}
    4.33 +Waypoint::Waypoint ()
    4.34 +  : m_time (0.0),
    4.35 +  m_position (0,0,0)
    4.36 +{}
    4.37 +
    4.38 +Time Waypoint::GetTime () const
    4.39 +{
    4.40 +  return m_time;
    4.41 +}
    4.42 +
    4.43 +void Waypoint::SetTime (Time time)
    4.44 +{
    4.45 +  m_time = time;
    4.46 +}
    4.47 +
    4.48 +Vector Waypoint::GetPosition () const
    4.49 +{
    4.50 +  return m_position;
    4.51 +}
    4.52 +
    4.53 +void Waypoint::SetPosition (Vector pos)
    4.54 +{
    4.55 +  m_position = pos;
    4.56 +}
    4.57 +
    4.58 +std::ostream &operator << (std::ostream &os, const Waypoint &waypoint)
    4.59 +{
    4.60 +  os << waypoint.GetTime ().GetSeconds () << "$" << waypoint.GetPosition ();
    4.61 +  return os;
    4.62 +}
    4.63 +std::istream &operator >> (std::istream &is, Waypoint &waypoint)
    4.64 +{
    4.65 +  char separator;
    4.66 +  Time time = waypoint.GetTime ();
    4.67 +  Vector pos = waypoint.GetPosition ();
    4.68 +  is >> time >> separator >> pos;
    4.69 +  if (separator != '$')
    4.70 +    {
    4.71 +      is.setstate (std::ios_base::failbit);
    4.72 +    }
    4.73 +  return is;
    4.74 +}
    4.75 +
    4.76 +} // namespace ns3
    4.77 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/mobility/waypoint.h	Tue Nov 10 12:10:35 2009 +0100
     5.3 @@ -0,0 +1,74 @@
     5.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     5.5 +/*
     5.6 + * Copyright (c) 2009 Phillip Sitbon
     5.7 + *
     5.8 + * This program is free software; you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License version 2 as
    5.10 + * published by the Free Software Foundation;
    5.11 + *
    5.12 + * This program is distributed in the hope that it will be useful,
    5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.15 + * GNU General Public License for more details.
    5.16 + *
    5.17 + * You should have received a copy of the GNU General Public License
    5.18 + * along with this program; if not, write to the Free Software
    5.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    5.20 + *
    5.21 + * Author: Phillip Sitbon <phillip@sitbon.net>
    5.22 + */
    5.23 +#ifndef WAYPOINT_H
    5.24 +#define WAYPOINT_H
    5.25 +
    5.26 +#include "ns3/attribute.h"
    5.27 +#include "ns3/attribute-helper.h"
    5.28 +#include "ns3/nstime.h"
    5.29 +#include "ns3/vector.h"
    5.30 +
    5.31 +namespace ns3 {
    5.32 +
    5.33 +/**
    5.34 + * \brief a (time, location) pair.
    5.35 + *
    5.36 + */
    5.37 +class Waypoint
    5.38 +{
    5.39 +public:
    5.40 +  /**
    5.41 +   * \param _time time of waypoint.
    5.42 +   * \param _position position of waypoint corresponding to the given time.
    5.43 +   *
    5.44 +   * Create a waypoint.
    5.45 +   */
    5.46 +  Waypoint (const Time &waypointTime, const Vector &waypointPosition);
    5.47 +
    5.48 +  /**
    5.49 +   * Create a waypoint at time 0 and position (0,0,0).
    5.50 +   */
    5.51 +  Waypoint ();
    5.52 +  Time GetTime () const;
    5.53 +  void SetTime (Time time);
    5.54 +  Vector GetPosition () const;
    5.55 +  void SetPosition (Vector vec);
    5.56 +private:
    5.57 +  /* The waypoint time */
    5.58 +  Time m_time;
    5.59 +  /* The position of the waypoint */
    5.60 +  Vector m_position;
    5.61 +};
    5.62 +
    5.63 +/**
    5.64 + * \class ns3::WaypointValue
    5.65 + * \brief hold objects of type ns3::Waypoint
    5.66 + */
    5.67 +ATTRIBUTE_HELPER_HEADER ( Waypoint);
    5.68 +
    5.69 +std::ostream &
    5.70 +operator << (std::ostream &os, const Waypoint &waypoint);
    5.71 +std::istream &
    5.72 +operator >> (std::istream &is, Waypoint &waypoint);
    5.73 +
    5.74 +} // namespace ns3
    5.75 +
    5.76 +#endif /* WAYPOINT_H */
    5.77 +
     6.1 --- a/src/mobility/wscript	Tue Nov 10 10:31:51 2009 +0100
     6.2 +++ b/src/mobility/wscript	Tue Nov 10 12:10:35 2009 +0100
     6.3 @@ -14,6 +14,8 @@
     6.4          'random-walk-2d-mobility-model.cc',
     6.5          'random-direction-2d-mobility-model.cc',
     6.6          'constant-acceleration-mobility-model.cc',
     6.7 +        'waypoint.cc',
     6.8 +        'waypoint-mobility-model.cc',
     6.9          ]
    6.10  
    6.11      headers = bld.new_task_gen('ns3header')
    6.12 @@ -30,4 +32,6 @@
    6.13          'random-walk-2d-mobility-model.h',
    6.14          'random-direction-2d-mobility-model.h',
    6.15          'constant-acceleration-mobility-model.h',
    6.16 +        'waypoint.h',
    6.17 +        'waypoint-mobility-model.h',
    6.18          ]