A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mobility-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "mobility-helper.h"
9
10#include "ns3/config.h"
11#include "ns3/hierarchical-mobility-model.h"
12#include "ns3/log.h"
13#include "ns3/mobility-model.h"
14#include "ns3/names.h"
15#include "ns3/pointer.h"
16#include "ns3/position-allocator.h"
17#include "ns3/simulator.h"
18#include "ns3/string.h"
19
20#include <iostream>
21
22namespace ns3
23{
24
25NS_LOG_COMPONENT_DEFINE("MobilityHelper");
26
28{
30 "X",
31 StringValue("ns3::ConstantRandomVariable[Constant=0.0]"),
32 "Y",
33 StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
34 m_mobility.SetTypeId("ns3::ConstantPositionMobilityModel");
35}
36
40
41void
46
47void
49{
50 Ptr<MobilityModel> mobility = reference->GetObject<MobilityModel>();
51 m_mobilityStack.push_back(mobility);
52}
53
54void
56{
57 Ptr<MobilityModel> mobility = Names::Find<MobilityModel>(referenceName);
58 m_mobilityStack.push_back(mobility);
59}
60
61void
66
67std::string
72
73void
75{
76 Ptr<Object> object = node;
77 Ptr<MobilityModel> model = object->GetObject<MobilityModel>();
78 if (!model)
79 {
81 if (!model)
82 {
83 NS_FATAL_ERROR("The requested mobility model is not a mobility model: \""
84 << m_mobility.GetTypeId().GetName() << "\"");
85 }
86 if (m_mobilityStack.empty())
87 {
88 NS_LOG_DEBUG("node=" << object << ", mob=" << model);
89 object->AggregateObject(model);
90 }
91 else
92 {
93 // we need to setup a hierarchical mobility model
94 Ptr<MobilityModel> parent = m_mobilityStack.back();
95 Ptr<MobilityModel> hierarchical =
97 PointerValue(model),
98 "Parent",
99 PointerValue(parent));
100 object->AggregateObject(hierarchical);
101 NS_LOG_DEBUG("node=" << object << ", mob=" << hierarchical);
102 }
103 }
104 Vector position = m_position->GetNext();
105 model->SetPosition(position);
106}
107
108void
109MobilityHelper::Install(std::string nodeName) const
110{
111 Ptr<Node> node = Names::Find<Node>(nodeName);
112 Install(node);
113}
114
115void
117{
118 for (auto i = c.Begin(); i != c.End(); ++i)
119 {
120 Install(*i);
121 }
122}
123
124void
129
130/**
131 * Utility function that rounds |1e-4| < input value < |1e-3| up to +/- 1e-3
132 * and value <= |1e-4| to zero
133 * @param v value to round
134 * @return rounded value
135 */
136static double
137DoRound(double v)
138{
139 if (v <= 1e-4 && v >= -1e-4)
140 {
141 return 0.0;
142 }
143 else if (v <= 1e-3 && v >= 0)
144 {
145 return 1e-3;
146 }
147 else if (v >= -1e-3 && v <= 0)
148 {
149 return -1e-3;
150 }
151 else
152 {
153 return v;
154 }
155}
156
157void
159{
160 std::ostream* os = stream->GetStream();
161 Vector pos = mobility->GetPosition();
162 Vector vel = mobility->GetVelocity();
163 *os << "now=" << Simulator::Now() << " node=" << mobility->GetObject<Node>()->GetId();
164 pos.x = DoRound(pos.x);
165 pos.y = DoRound(pos.y);
166 pos.z = DoRound(pos.z);
167 vel.x = DoRound(vel.x);
168 vel.y = DoRound(vel.y);
169 vel.z = DoRound(vel.z);
170 std::streamsize saved_precision = os->precision();
171 std::ios::fmtflags saved_flags = os->flags();
172 os->precision(3);
173 os->setf(std::ios::fixed, std::ios::floatfield);
174 *os << " pos=" << pos.x << ":" << pos.y << ":" << pos.z << " vel=" << vel.x << ":" << vel.y
175 << ":" << vel.z << std::endl;
176 os->flags(saved_flags);
177 os->precision(saved_precision);
178}
179
180void
182{
183 std::ostringstream oss;
184 oss << "/NodeList/" << nodeid << "/$ns3::MobilityModel/CourseChange";
186 oss.str(),
188}
189
190void
192{
193 for (auto i = n.Begin(); i != n.End(); ++i)
194 {
195 EnableAscii(stream, (*i)->GetId());
196 }
197}
198
199void
204
205int64_t
207{
208 int64_t currentStream = stream;
209 Ptr<Node> node;
210 Ptr<MobilityModel> mobility;
211 for (auto i = c.Begin(); i != c.End(); ++i)
212 {
213 node = (*i);
214 mobility = node->GetObject<MobilityModel>();
215 if (mobility)
216 {
217 currentStream += mobility->AssignStreams(currentStream);
218 }
219 }
220 return (currentStream - stream);
221}
222
223double
225{
227 double distSq = 0.0;
228
229 Ptr<MobilityModel> rxPosition = n1->GetObject<MobilityModel>();
230 NS_ASSERT(rxPosition);
231
232 Ptr<MobilityModel> txPosition = n2->GetObject<MobilityModel>();
233 NS_ASSERT(txPosition);
234
235 double dist = rxPosition->GetDistanceFrom(txPosition);
236 distSq = dist * dist;
237
238 return distSq;
239}
240
241} // namespace ns3
void PopReferenceMobilityModel()
Remove the top item from the top of the stack of "reference mobility models".
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the mobility models on t...
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
static double GetDistanceSquaredBetween(Ptr< Node > n1, Ptr< Node > n2)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
ObjectFactory m_mobility
Object factory to create mobility objects.
static void CourseChanged(Ptr< OutputStreamWrapper > stream, Ptr< const MobilityModel > mobility)
Output course change events from mobility model to output stream.
std::string GetMobilityModelType() const
~MobilityHelper()
Destroy a Mobility Helper.
Ptr< PositionAllocator > m_position
Position allocator for use in hierarchical mobility model.
void PushReferenceMobilityModel(Ptr< Object > reference)
void InstallAll() const
Perform the work of MobilityHelper::Install on all nodes which exist in the simulation.
MobilityHelper()
Construct a Mobility Helper which is used to make life easier when working with mobility models.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
static void EnableAscii(Ptr< OutputStreamWrapper > stream, uint32_t nodeid)
std::vector< Ptr< MobilityModel > > m_mobilityStack
Internal stack of mobility models.
Keep track of the current position and velocity of an object.
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition names.h:443
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
static NodeContainer GetGlobal()
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
A network Node.
Definition node.h:46
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
TypeId GetTypeId() const
Get the TypeId which will be created by this ObjectFactory.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Hold variables of type string.
Definition string.h:45
std::string GetName() const
Get the name.
Definition type-id.cc:1061
#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
bool ConnectWithoutContextFailSafe(std::string path, const CallbackBase &cb)
Definition config.cc:953
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static double DoRound(double v)
Utility function that rounds |1e-4| < input value < |1e-3| up to +/- 1e-3 and value <= |1e-4| to zero...