A Discrete-Event Network Simulator
API
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 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("WaypointMobilityModel");
33 
34 NS_OBJECT_ENSURE_REGISTERED (WaypointMobilityModel);
35 
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::WaypointMobilityModel")
42  .SetGroupName ("Mobility")
43  .AddConstructor<WaypointMobilityModel> ()
44  .AddAttribute ("NextWaypoint", "The next waypoint used to determine position.",
46  WaypointValue (),
49  .AddAttribute ("WaypointsLeft", "The number of waypoints remaining.",
51  UintegerValue (0),
53  MakeUintegerChecker<uint32_t> ())
54  .AddAttribute ("LazyNotify", "Only call NotifyCourseChange when position is calculated.",
55  BooleanValue (false),
58  .AddAttribute ("InitialPositionIsWaypoint", "Calling SetPosition with no waypoints creates a waypoint.",
59  BooleanValue (false),
62  ;
63  return tid;
64 }
65 
66 
68  : m_first (true),
69  m_lazyNotify (false),
70  m_initialPositionIsWaypoint (false)
71 {
72 }
74 {
75 }
76 void
78 {
80 }
81 void
83 {
84  if ( m_first )
85  {
86  m_first = false;
87  m_current = m_next = waypoint;
88  }
89  else
90  {
91  NS_ABORT_MSG_IF ( !m_waypoints.empty () && (m_waypoints.back ().time >= waypoint.time),
92  "Waypoints must be added in ascending time order");
93  m_waypoints.push_back (waypoint);
94  }
95 
96  if ( !m_lazyNotify )
97  {
99  }
100 }
101 Waypoint
103 {
104  Update ();
105  return m_next;
106 }
107 uint32_t
109 {
110  Update ();
111  return m_waypoints.size ();
112 }
113 void
115 {
116  const Time now = Simulator::Now ();
117  bool newWaypoint = false;
118 
119  if ( now < m_current.time )
120  {
121  return;
122  }
123 
124  while ( now >= m_next.time )
125  {
126  if ( m_waypoints.empty () )
127  {
128  if ( m_current.time <= m_next.time )
129  {
130  /*
131  Set m_next.time = -1 to make sure this doesn't happen more than once.
132  The comparison here still needs to be '<=' in the case of mobility with one waypoint.
133  */
134  m_next.time = Seconds (-1.0);
136  m_current.time = now;
137  m_velocity = Vector (0,0,0);
139  }
140  else
141  {
142  m_current.time = now;
143  }
144 
145  return;
146  }
147 
148  m_current = m_next;
149  m_next = m_waypoints.front ();
150  m_waypoints.pop_front ();
151  newWaypoint = true;
152 
153  const double t_span = (m_next.time - m_current.time).GetSeconds ();
154  NS_ASSERT (t_span > 0);
155  m_velocity.x = (m_next.position.x - m_current.position.x) / t_span;
156  m_velocity.y = (m_next.position.y - m_current.position.y) / t_span;
157  m_velocity.z = (m_next.position.z - m_current.position.z) / t_span;
158  }
159 
160  if ( now > m_current.time ) // Won't ever be less, but may be equal
161  {
162  const double t_diff = (now - m_current.time).GetSeconds ();
163  m_current.position.x += m_velocity.x * t_diff;
164  m_current.position.y += m_velocity.y * t_diff;
165  m_current.position.z += m_velocity.z * t_diff;
166  m_current.time = now;
167  }
168 
169  if ( newWaypoint )
170  {
172  }
173 }
174 Vector
176 {
177  Update ();
178  return m_current.position;
179 }
180 void
181 WaypointMobilityModel::DoSetPosition (const Vector &position)
182 {
183  const Time now = Simulator::Now ();
184 
186  {
187  AddWaypoint (Waypoint (now, position));
188  return;
189  }
190 
191  Update ();
192  m_current.time = std::max (now, m_next.time);
193  m_current.position = position;
194  m_velocity = Vector (0,0,0);
195 
196  if ( !m_first && (now >= m_current.time) )
197  {
198  // This is only a course change if the node is actually moving
200  }
201 }
202 void
204 {
205  m_waypoints.clear ();
206  m_current.time = Time(std::numeric_limits<uint64_t>::infinity());
208  m_first = true;
209 }
210 Vector
212 {
213  return m_velocity;
214 }
215 
216 } // namespace ns3
217 
virtual void DoDispose(void)
The dispose method.
virtual Vector DoGetVelocity(void) const
Returns the current velocity of a node.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Time time
The waypoint time.
Definition: waypoint.h:53
AttributeValue implementation for Boolean.
Definition: boolean.h:36
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
bool m_lazyNotify
If true, course change updates are only notified when position is calculated.
void AddWaypoint(const Waypoint &waypoint)
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:84
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Vector m_velocity
The current velocity vector.
std::deque< Waypoint > m_waypoints
The double ended queue containing the ns3::Waypoint objects.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:740
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
uint32_t WaypointsLeft(void) const
Get the number of waypoints left for this object, excluding the next one.
The attribute can be read.
Definition: type-id.h:63
Keep track of the current position and velocity of an object.
bool m_first
This variable is set to true if there are no waypoints in the std::deque.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
#define max(a, b)
Definition: 80211b.c:43
Vector position
The position of the waypoint.
Definition: waypoint.h:57
virtual void DoSetPosition(const Vector &position)
Sets a new position for the node
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< const AttributeChecker > MakeWaypointChecker(void)
Definition: waypoint.cc:24
virtual void Update(void) const
Update the underlying state corresponding to the stored waypoints.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Waypoint m_next
The next ns3::Waypoint in the deque.
virtual Vector DoGetPosition(void) const
Get current position.
Waypoint GetNextWaypoint(void) const
Get the waypoint that this object is traveling towards.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
Ptr< const AttributeAccessor > MakeWaypointAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: waypoint.h:60
void EndMobility(void)
Clear any existing waypoints and set the current waypoint time to infinity.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
Waypoint m_current
The ns3::Waypoint currently being used.
Waypoint-based mobility model.
WaypointMobilityModel()
Create a path with no waypoints at location (0,0,0).
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
a (time, location) pair.
Definition: waypoint.h:35
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
bool m_initialPositionIsWaypoint
If true, calling SetPosition with no waypoints creates a waypoint.
AttributeValue implementation for Waypoint.
Definition: waypoint.h:60