A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
waypoint-mobility-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 Phillip Sitbon
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Phillip Sitbon <phillip@sitbon.net>
19  */
20 #include <limits>
21 #include "ns3/abort.h"
22 #include "ns3/simulator.h"
23 #include "ns3/uinteger.h"
24 #include "ns3/log.h"
25 #include "ns3/boolean.h"
27 #include "ns3/config.h"
28 #include "ns3/test.h"
29 
30 NS_LOG_COMPONENT_DEFINE ("WaypointMobilityModel");
31 
32 namespace ns3 {
33 
34 NS_OBJECT_ENSURE_REGISTERED (WaypointMobilityModel)
35  ;
36 
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::WaypointMobilityModel")
43  .SetGroupName ("Mobility")
44  .AddConstructor<WaypointMobilityModel> ()
45  .AddAttribute ("NextWaypoint", "The next waypoint used to determine position.",
47  WaypointValue (),
48  MakeWaypointAccessor (&WaypointMobilityModel::GetNextWaypoint),
49  MakeWaypointChecker ())
50  .AddAttribute ("WaypointsLeft", "The number of waypoints remaining.",
52  UintegerValue (0),
53  MakeUintegerAccessor (&WaypointMobilityModel::WaypointsLeft),
54  MakeUintegerChecker<uint32_t> ())
55  .AddAttribute ("LazyNotify", "Only call NotifyCourseChange when position is calculated.",
56  BooleanValue (false),
57  MakeBooleanAccessor (&WaypointMobilityModel::m_lazyNotify),
58  MakeBooleanChecker ())
59  .AddAttribute ("InitialPositionIsWaypoint", "Calling SetPosition with no waypoints creates a waypoint.",
60  BooleanValue (false),
62  MakeBooleanChecker ())
63  ;
64  return tid;
65 }
66 
67 
69  : m_first (true),
70  m_lazyNotify (false),
71  m_initialPositionIsWaypoint (false)
72 {
73 }
75 {
76 }
77 void
79 {
81 }
82 void
84 {
85  if ( m_first )
86  {
87  m_first = false;
88  m_current = m_next = waypoint;
89  }
90  else
91  {
92  NS_ABORT_MSG_IF ( !m_waypoints.empty () && (m_waypoints.back ().time >= waypoint.time),
93  "Waypoints must be added in ascending time order");
94  m_waypoints.push_back (waypoint);
95  }
96 
97  if ( !m_lazyNotify )
98  {
100  }
101 }
102 Waypoint
104 {
105  Update ();
106  return m_next;
107 }
108 uint32_t
110 {
111  Update ();
112  return m_waypoints.size ();
113 }
114 void
116 {
117  const Time now = Simulator::Now ();
118  bool newWaypoint = false;
119 
120  if ( now < m_current.time )
121  {
122  return;
123  }
124 
125  while ( now >= m_next.time )
126  {
127  if ( m_waypoints.empty () )
128  {
129  if ( m_current.time <= m_next.time )
130  {
131  /*
132  Set m_next.time = -1 to make sure this doesn't happen more than once.
133  The comparison here still needs to be '<=' in the case of mobility with one waypoint.
134  */
135  m_next.time = Seconds (-1.0);
137  m_current.time = now;
138  m_velocity = Vector (0,0,0);
140  }
141  else
142  {
143  m_current.time = now;
144  }
145 
146  return;
147  }
148 
149  m_current = m_next;
150  m_next = m_waypoints.front ();
151  m_waypoints.pop_front ();
152  newWaypoint = true;
153 
154  const double t_span = (m_next.time - m_current.time).GetSeconds ();
155  NS_ASSERT (t_span > 0);
156  m_velocity.x = (m_next.position.x - m_current.position.x) / t_span;
157  m_velocity.y = (m_next.position.y - m_current.position.y) / t_span;
158  m_velocity.z = (m_next.position.z - m_current.position.z) / t_span;
159  }
160 
161  if ( now > m_current.time ) // Won't ever be less, but may be equal
162  {
163  const double t_diff = (now - m_current.time).GetSeconds ();
164  m_current.position.x += m_velocity.x * t_diff;
165  m_current.position.y += m_velocity.y * t_diff;
166  m_current.position.z += m_velocity.z * t_diff;
167  m_current.time = now;
168  }
169 
170  if ( newWaypoint )
171  {
173  }
174 }
175 Vector
177 {
178  Update ();
179  return m_current.position;
180 }
181 void
183 {
184  const Time now = Simulator::Now ();
185 
187  {
188  AddWaypoint (Waypoint (now, position));
189  return;
190  }
191 
192  Update ();
193  m_current.time = std::max (now, m_next.time);
194  m_current.position = position;
195  m_velocity = Vector (0,0,0);
196 
197  if ( !m_first && (now >= m_current.time) )
198  {
199  // This is only a course change if the node is actually moving
201  }
202 }
203 void
205 {
206  m_waypoints.clear ();
207  m_current.time = Time(std::numeric_limits<uint64_t>::infinity());
209  m_first = true;
210 }
211 Vector
213 {
214  return m_velocity;
215 }
216 
217 } // namespace ns3
218 
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
virtual Vector DoGetVelocity(void) const
double x
x coordinate of vector
Definition: vector.h:49
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Hold a bool native type.
Definition: boolean.h:38
void AddWaypoint(const Waypoint &waypoint)
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual Vector DoGetPosition(void) const
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
std::deque< Waypoint > m_waypoints
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
a 3d vector
Definition: vector.h:31
The attribute can be read.
Definition: type-id.h:56
Keep track of the current position and velocity of an object.
Vector position
Definition: waypoint.h:52
virtual void DoSetPosition(const Vector &position)
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
Hold an unsigned integer type.
Definition: uinteger.h:46
Vector3D Vector
Definition: vector.h:118
NS_LOG_COMPONENT_DEFINE("WaypointMobilityModel")
double y
y coordinate of vector
Definition: vector.h:53
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void EndMobility(void)
Clear any existing waypoints and set the current waypoint time to infinity.
Waypoint-based mobility model.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition: abort.h:98
WaypointMobilityModel()
Create a path with no waypoints at location (0,0,0).
Waypoint GetNextWaypoint(void) const
Get the waypoint that this object is traveling towards.
a unique identifier for an interface.
Definition: type-id.h:49
a (time, location) pair.
Definition: waypoint.h:34
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
uint32_t WaypointsLeft(void) const
Get the number of waypoints left for this object, excluding the next one.
hold objects of type ns3::Waypoint
double z
z coordinate of vector
Definition: vector.h:57