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 
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::WaypointMobilityModel::DoGetVelocity
virtual Vector DoGetVelocity(void) const
Returns the current velocity of a node.
Definition: waypoint-mobility-model.cc:211
waypoint-mobility-model.h
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#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
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::WaypointMobilityModel::m_waypoints
std::deque< Waypoint > m_waypoints
The double ended queue containing the ns3::Waypoint objects.
Definition: waypoint-mobility-model.h:179
ns3::WaypointMobilityModel::m_velocity
Vector m_velocity
The current velocity vector.
Definition: waypoint-mobility-model.h:191
ns3::MobilityModel::NotifyCourseChange
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
Definition: mobility-model.cc:95
ns3::WaypointMobilityModel::AddWaypoint
void AddWaypoint(const Waypoint &waypoint)
Definition: waypoint-mobility-model.cc:82
ns3::TypeId::ATTR_GET
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:64
ns3::MakeWaypointChecker
Ptr< const AttributeChecker > MakeWaypointChecker(void)
Definition: waypoint.cc:24
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::MakeBooleanAccessor
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:85
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::WaypointMobilityModel::DoGetPosition
virtual Vector DoGetPosition(void) const
Get current position.
Definition: waypoint-mobility-model.cc:175
ns3::WaypointMobilityModel::Update
virtual void Update(void) const
Update the underlying state corresponding to the stored waypoints.
Definition: waypoint-mobility-model.cc:114
max
#define max(a, b)
Definition: 80211b.c:43
ns3::WaypointMobilityModel
Waypoint-based mobility model.
Definition: waypoint-mobility-model.h:87
ns3::WaypointMobilityModel::~WaypointMobilityModel
virtual ~WaypointMobilityModel()
Definition: waypoint-mobility-model.cc:73
ns3::WaypointMobilityModel::EndMobility
void EndMobility(void)
Clear any existing waypoints and set the current waypoint time to infinity.
Definition: waypoint-mobility-model.cc:203
ns3::MakeBooleanChecker
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
ns3::WaypointMobilityModel::GetNextWaypoint
Waypoint GetNextWaypoint(void) const
Get the waypoint that this object is traveling towards.
Definition: waypoint-mobility-model.cc:102
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
NS_ABORT_MSG_IF
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
ns3::WaypointMobilityModel::DoDispose
virtual void DoDispose(void)
The dispose method.
Definition: waypoint-mobility-model.cc:77
ns3::WaypointValue
AttributeValue implementation for Waypoint.
Definition: waypoint.h:60
ns3::WaypointMobilityModel::WaypointMobilityModel
WaypointMobilityModel()
Create a path with no waypoints at location (0,0,0).
Definition: waypoint-mobility-model.cc:67
ns3::Waypoint::time
Time time
The waypoint time.
Definition: waypoint.h:53
ns3::WaypointMobilityModel::m_first
bool m_first
This variable is set to true if there are no waypoints in the std::deque.
Definition: waypoint-mobility-model.h:166
ns3::Waypoint
a (time, location) pair.
Definition: waypoint.h:36
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::WaypointMobilityModel::DoSetPosition
virtual void DoSetPosition(const Vector &position)
Sets a new position for the node
Definition: waypoint-mobility-model.cc:181
ns3::MakeWaypointAccessor
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
ns3::WaypointMobilityModel::WaypointsLeft
uint32_t WaypointsLeft(void) const
Get the number of waypoints left for this object, excluding the next one.
Definition: waypoint-mobility-model.cc:108
ns3::WaypointMobilityModel::GetTypeId
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Definition: waypoint-mobility-model.cc:38
ns3::WaypointMobilityModel::m_lazyNotify
bool m_lazyNotify
If true, course change updates are only notified when position is calculated.
Definition: waypoint-mobility-model.h:171
ns3::Waypoint::position
Vector position
The position of the waypoint.
Definition: waypoint.h:57
ns3::MobilityModel
Keep track of the current position and velocity of an object.
Definition: mobility-model.h:40
ns3::WaypointMobilityModel::m_next
Waypoint m_next
The next ns3::Waypoint in the deque.
Definition: waypoint-mobility-model.h:187
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::TracedValueCallback::Time
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:813
ns3::WaypointMobilityModel::m_current
Waypoint m_current
The ns3::Waypoint currently being used.
Definition: waypoint-mobility-model.h:183
ns3::MakeUintegerAccessor
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
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::WaypointMobilityModel::m_initialPositionIsWaypoint
bool m_initialPositionIsWaypoint
If true, calling SetPosition with no waypoints creates a waypoint.
Definition: waypoint-mobility-model.h:175