A Discrete-Event Network Simulator
API
waypoint-mobility-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Phillip Sitbon
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Phillip Sitbon <phillip@sitbon.net>
18  */
20 
21 #include "ns3/abort.h"
22 #include "ns3/boolean.h"
23 #include "ns3/config.h"
24 #include "ns3/log.h"
25 #include "ns3/simulator.h"
26 #include "ns3/test.h"
27 #include "ns3/uinteger.h"
28 
29 #include <limits>
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("WaypointMobilityModel");
35 
36 NS_OBJECT_ENSURE_REGISTERED(WaypointMobilityModel);
37 
38 TypeId
40 {
41  static TypeId tid =
42  TypeId("ns3::WaypointMobilityModel")
44  .SetGroupName("Mobility")
45  .AddConstructor<WaypointMobilityModel>()
46  .AddAttribute("NextWaypoint",
47  "The next waypoint used to determine position.",
49  WaypointValue(),
50  MakeWaypointAccessor(&WaypointMobilityModel::GetNextWaypoint),
51  MakeWaypointChecker())
52  .AddAttribute("WaypointsLeft",
53  "The number of waypoints remaining.",
55  UintegerValue(0),
57  MakeUintegerChecker<uint32_t>())
58  .AddAttribute("LazyNotify",
59  "Only call NotifyCourseChange when position is calculated.",
60  BooleanValue(false),
63  .AddAttribute("InitialPositionIsWaypoint",
64  "Calling SetPosition with no waypoints creates a waypoint.",
65  BooleanValue(false),
68  return tid;
69 }
70 
72  : m_first(true),
73  m_lazyNotify(false),
74  m_initialPositionIsWaypoint(false)
75 {
76 }
77 
79 {
80 }
81 
82 void
84 {
86 }
87 
88 void
90 {
91  if (m_first)
92  {
93  m_first = false;
94  m_current = m_next = waypoint;
95  }
96  else
97  {
98  NS_ABORT_MSG_IF(!m_waypoints.empty() && (m_waypoints.back().time >= waypoint.time),
99  "Waypoints must be added in ascending time order");
100  m_waypoints.push_back(waypoint);
101  }
102 
103  if (!m_lazyNotify)
104  {
106  }
107 }
108 
109 Waypoint
111 {
112  Update();
113  return m_next;
114 }
115 
116 uint32_t
118 {
119  Update();
120  return m_waypoints.size();
121 }
122 
123 void
125 {
126  const Time now = Simulator::Now();
127  bool newWaypoint = false;
128 
129  if (now < m_current.time)
130  {
131  return;
132  }
133 
134  while (now >= m_next.time)
135  {
136  if (m_waypoints.empty())
137  {
138  if (m_current.time <= m_next.time)
139  {
140  /*
141  Set m_next.time = -1 to make sure this doesn't happen more than once.
142  The comparison here still needs to be '<=' in the case of mobility with one
143  waypoint.
144  */
145  m_next.time = Seconds(-1.0);
147  m_current.time = now;
148  m_velocity = Vector(0, 0, 0);
150  }
151  else
152  {
153  m_current.time = now;
154  }
155 
156  return;
157  }
158 
159  m_current = m_next;
160  m_next = m_waypoints.front();
161  m_waypoints.pop_front();
162  newWaypoint = true;
163 
164  const double t_span = (m_next.time - m_current.time).GetSeconds();
165  NS_ASSERT(t_span > 0);
166  m_velocity.x = (m_next.position.x - m_current.position.x) / t_span;
167  m_velocity.y = (m_next.position.y - m_current.position.y) / t_span;
168  m_velocity.z = (m_next.position.z - m_current.position.z) / t_span;
169  }
170 
171  if (now > m_current.time) // Won't ever be less, but may be equal
172  {
173  const double t_diff = (now - m_current.time).GetSeconds();
174  m_current.position.x += m_velocity.x * t_diff;
175  m_current.position.y += m_velocity.y * t_diff;
176  m_current.position.z += m_velocity.z * t_diff;
177  m_current.time = now;
178  }
179 
180  if (newWaypoint)
181  {
183  }
184 }
185 
186 Vector
188 {
189  Update();
190  return m_current.position;
191 }
192 
193 void
195 {
196  const Time now = Simulator::Now();
197 
199  {
200  AddWaypoint(Waypoint(now, position));
201  return;
202  }
203 
204  Update();
206  m_current.position = position;
207  m_velocity = Vector(0, 0, 0);
208 
209  if (!m_first && (now >= m_current.time))
210  {
211  // This is only a course change if the node is actually moving
213  }
214 }
215 
216 void
218 {
219  m_waypoints.clear();
220  m_current.time = Time(std::numeric_limits<uint64_t>::infinity());
222  m_first = true;
223 }
224 
225 Vector
227 {
228  return m_velocity;
229 }
230 
231 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
Keep track of the current position and velocity of an object.
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:64
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Hold an unsigned integer type.
Definition: uinteger.h:45
a (time, location) pair.
Definition: waypoint.h:36
Time time
The waypoint time.
Definition: waypoint.h:53
Vector position
The position of the waypoint.
Definition: waypoint.h:57
Waypoint-based mobility model.
Vector m_velocity
The current velocity vector.
void DoDispose() override
The dispose method.
Waypoint GetNextWaypoint() const
Get the waypoint that this object is traveling towards.
bool m_initialPositionIsWaypoint
If true, calling SetPosition with no waypoints creates a waypoint.
virtual void Update() const
Update the underlying state corresponding to the stored waypoints.
Vector DoGetVelocity() const override
Returns the current velocity of a node.
WaypointMobilityModel()
Create a path with no waypoints at location (0,0,0).
uint32_t WaypointsLeft() const
Get the number of waypoints left for this object, excluding the next one.
Waypoint m_current
The ns3::Waypoint currently being used.
bool m_first
This variable is set to true if there are no waypoints in the std::deque.
std::deque< Waypoint > m_waypoints
The double ended queue containing the ns3::Waypoint objects.
bool m_lazyNotify
If true, course change updates are only notified when position is calculated.
void EndMobility()
Clear any existing waypoints and set the current waypoint time to infinity.
void DoSetPosition(const Vector &position) override
Sets a new position for the node.
Vector DoGetPosition() const override
Get current position.
Waypoint m_next
The next ns3::Waypoint in the deque.
static TypeId GetTypeId()
Register this type with the TypeId system.
void AddWaypoint(const Waypoint &waypoint)
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46