A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
waypoint-mobility-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 Phillip Sitbon
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Phillip Sitbon <phillip@sitbon.net>
7 */
9
10#include "ns3/abort.h"
11#include "ns3/boolean.h"
12#include "ns3/config.h"
13#include "ns3/log.h"
14#include "ns3/simulator.h"
15#include "ns3/test.h"
16#include "ns3/uinteger.h"
17
18#include <limits>
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("WaypointMobilityModel");
24
25NS_OBJECT_ENSURE_REGISTERED(WaypointMobilityModel);
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::WaypointMobilityModel")
33 .SetGroupName("Mobility")
34 .AddConstructor<WaypointMobilityModel>()
35 .AddAttribute("NextWaypoint",
36 "The next waypoint used to determine position.",
41 .AddAttribute("WaypointsLeft",
42 "The number of waypoints remaining.",
47 .AddAttribute("LazyNotify",
48 "Only call NotifyCourseChange when position is calculated.",
49 BooleanValue(false),
52 .AddAttribute("InitialPositionIsWaypoint",
53 "Calling SetPosition with no waypoints creates a waypoint.",
54 BooleanValue(false),
57 return tid;
58}
59
61 : m_first(true),
62 m_lazyNotify(false),
63 m_initialPositionIsWaypoint(false)
64{
65}
66
71
72void
77
78void
80{
81 if (m_first)
82 {
83 m_first = false;
84 m_current = m_next = waypoint;
85 }
86 else
87 {
88 NS_ABORT_MSG_IF(!m_waypoints.empty() && (m_waypoints.back().time >= waypoint.time),
89 "Waypoints must be added in ascending time order");
90 m_waypoints.push_back(waypoint);
91 }
92
93 if (!m_lazyNotify)
94 {
97 this);
98 }
99}
100
103{
104 Update();
105 return m_next;
106}
107
110{
111 Update();
112 return m_waypoints.size();
113}
114
115void
117{
118 const Time now = Simulator::Now();
119 bool newWaypoint = false;
120
121 if (now < m_current.time)
122 {
123 return;
124 }
125
126 while (now >= m_next.time)
127 {
128 if (m_waypoints.empty())
129 {
130 if (m_current.time <= m_next.time)
131 {
132 /*
133 Set m_next.time = -1 to make sure this doesn't happen more than once.
134 The comparison here still needs to be '<=' in the case of mobility with one
135 waypoint.
136 */
137 m_next.time = Seconds(-1.0);
139 m_current.time = now;
140 m_velocity = Vector(0, 0, 0);
142 }
143 else
144 {
145 m_current.time = now;
146 }
147
148 return;
149 }
150
152 m_next = m_waypoints.front();
153 m_waypoints.pop_front();
154 newWaypoint = true;
155
156 const double t_span = (m_next.time - m_current.time).GetSeconds();
157 NS_ASSERT(t_span > 0);
158 m_velocity.x = (m_next.position.x - m_current.position.x) / t_span;
159 m_velocity.y = (m_next.position.y - m_current.position.y) / t_span;
160 m_velocity.z = (m_next.position.z - m_current.position.z) / t_span;
161 }
162
163 if (now > m_current.time) // Won't ever be less, but may be equal
164 {
165 const double t_diff = (now - m_current.time).GetSeconds();
166 m_current.position.x += m_velocity.x * t_diff;
167 m_current.position.y += m_velocity.y * t_diff;
168 m_current.position.z += m_velocity.z * t_diff;
169 m_current.time = now;
170 }
171
172 if (newWaypoint)
173 {
175 }
176}
177
178Vector
184
185void
187{
188 const Time now = Simulator::Now();
189
191 {
192 AddWaypoint(Waypoint(now, position));
193 return;
194 }
195
196 Update();
197 m_current.time = std::max(now, m_next.time);
198 m_current.position = position;
199 m_velocity = Vector(0, 0, 0);
200
201 if (!m_first && (now >= m_current.time))
202 {
203 // This is only a course change if the node is actually moving
205 }
206}
207
208void
210{
211 m_waypoints.clear();
212 m_current.time = Time(std::numeric_limits<uint64_t>::infinity());
214 m_first = true;
215}
216
217Vector
222
223} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
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:433
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
@ ATTR_GET
The attribute can be read.
Definition type-id.h:53
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
a (time, location) pair.
Definition waypoint.h:25
Time time
The waypoint time.
Definition waypoint.h:42
Vector position
The position of the waypoint.
Definition waypoint.h:46
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:55
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeAccessor > MakeWaypointAccessor(T1 a1)
Definition waypoint.h:49
Ptr< const AttributeChecker > MakeWaypointChecker()
Definition waypoint.cc:13
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70