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 * 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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "mobility-helper.h"
20
21#include "ns3/config.h"
22#include "ns3/hierarchical-mobility-model.h"
23#include "ns3/log.h"
24#include "ns3/mobility-model.h"
25#include "ns3/names.h"
26#include "ns3/pointer.h"
27#include "ns3/position-allocator.h"
28#include "ns3/simulator.h"
29#include "ns3/string.h"
30
31#include <iostream>
32
33namespace ns3
34{
35
36NS_LOG_COMPONENT_DEFINE("MobilityHelper");
37
39{
40 m_position = CreateObjectWithAttributes<RandomRectanglePositionAllocator>(
41 "X",
42 StringValue("ns3::ConstantRandomVariable[Constant=0.0]"),
43 "Y",
44 StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
45 m_mobility.SetTypeId("ns3::ConstantPositionMobilityModel");
46}
47
49{
50}
51
52void
54{
55 m_position = allocator;
56}
57
58void
60{
61 Ptr<MobilityModel> mobility = reference->GetObject<MobilityModel>();
62 m_mobilityStack.push_back(mobility);
63}
64
65void
67{
68 Ptr<MobilityModel> mobility = Names::Find<MobilityModel>(referenceName);
69 m_mobilityStack.push_back(mobility);
70}
71
72void
74{
75 m_mobilityStack.pop_back();
76}
77
78std::string
80{
81 return m_mobility.GetTypeId().GetName();
82}
83
84void
86{
87 Ptr<Object> object = node;
88 Ptr<MobilityModel> model = object->GetObject<MobilityModel>();
89 if (!model)
90 {
92 if (!model)
93 {
94 NS_FATAL_ERROR("The requested mobility model is not a mobility model: \""
95 << m_mobility.GetTypeId().GetName() << "\"");
96 }
97 if (m_mobilityStack.empty())
98 {
99 NS_LOG_DEBUG("node=" << object << ", mob=" << model);
100 object->AggregateObject(model);
101 }
102 else
103 {
104 // we need to setup a hierarchical mobility model
105 Ptr<MobilityModel> parent = m_mobilityStack.back();
106 Ptr<MobilityModel> hierarchical =
107 CreateObjectWithAttributes<HierarchicalMobilityModel>("Child",
108 PointerValue(model),
109 "Parent",
110 PointerValue(parent));
111 object->AggregateObject(hierarchical);
112 NS_LOG_DEBUG("node=" << object << ", mob=" << hierarchical);
113 }
114 }
115 Vector position = m_position->GetNext();
116 model->SetPosition(position);
117}
118
119void
120MobilityHelper::Install(std::string nodeName) const
121{
122 Ptr<Node> node = Names::Find<Node>(nodeName);
123 Install(node);
124}
125
126void
128{
129 for (auto i = c.Begin(); i != c.End(); ++i)
130 {
131 Install(*i);
132 }
133}
134
135void
137{
139}
140
141/**
142 * Utility function that rounds |1e-4| < input value < |1e-3| up to +/- 1e-3
143 * and value <= |1e-4| to zero
144 * \param v value to round
145 * \return rounded value
146 */
147static double
148DoRound(double v)
149{
150 if (v <= 1e-4 && v >= -1e-4)
151 {
152 return 0.0;
153 }
154 else if (v <= 1e-3 && v >= 0)
155 {
156 return 1e-3;
157 }
158 else if (v >= -1e-3 && v <= 0)
159 {
160 return -1e-3;
161 }
162 else
163 {
164 return v;
165 }
166}
167
168void
170{
171 std::ostream* os = stream->GetStream();
172 Vector pos = mobility->GetPosition();
173 Vector vel = mobility->GetVelocity();
174 *os << "now=" << Simulator::Now() << " node=" << mobility->GetObject<Node>()->GetId();
175 pos.x = DoRound(pos.x);
176 pos.y = DoRound(pos.y);
177 pos.z = DoRound(pos.z);
178 vel.x = DoRound(vel.x);
179 vel.y = DoRound(vel.y);
180 vel.z = DoRound(vel.z);
181 std::streamsize saved_precision = os->precision();
182 std::ios::fmtflags saved_flags = os->flags();
183 os->precision(3);
184 os->setf(std::ios::fixed, std::ios::floatfield);
185 *os << " pos=" << pos.x << ":" << pos.y << ":" << pos.z << " vel=" << vel.x << ":" << vel.y
186 << ":" << vel.z << std::endl;
187 os->flags(saved_flags);
188 os->precision(saved_precision);
189}
190
191void
193{
194 std::ostringstream oss;
195 oss << "/NodeList/" << nodeid << "/$ns3::MobilityModel/CourseChange";
197 oss.str(),
199}
200
201void
203{
204 for (auto i = n.Begin(); i != n.End(); ++i)
205 {
206 EnableAscii(stream, (*i)->GetId());
207 }
208}
209
210void
212{
214}
215
216int64_t
218{
219 int64_t currentStream = stream;
220 Ptr<Node> node;
221 Ptr<MobilityModel> mobility;
222 for (auto i = c.Begin(); i != c.End(); ++i)
223 {
224 node = (*i);
225 mobility = node->GetObject<MobilityModel>();
226 if (mobility)
227 {
228 currentStream += mobility->AssignStreams(currentStream);
229 }
230 }
231 return (currentStream - stream);
232}
233
234double
236{
238 double distSq = 0.0;
239
240 Ptr<MobilityModel> rxPosition = n1->GetObject<MobilityModel>();
241 NS_ASSERT(rxPosition);
242
243 Ptr<MobilityModel> txPosition = n2->GetObject<MobilityModel>();
244 NS_ASSERT(txPosition);
245
246 double dist = rxPosition->GetDistanceFrom(txPosition);
247 distSq = dist * dist;
248
249 return distSq;
250}
251
252} // 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.
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:57
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:522
AttributeValue implementation for Pointer.
Definition: pointer.h:48
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Hold variables of type string.
Definition: string.h:56
std::string GetName() const
Get the name.
Definition: type-id.cc:992
#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
bool ConnectWithoutContextFailSafe(std::string path, const CallbackBase &cb)
Definition: config.cc:964
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:767
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...