A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
outdoor-random-walk-example.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3
* Copyright (c) 2019, University of Padova, Dep. of Information Engineering, SIGNET lab
4
*
5
* SPDX-License-Identifier: GPL-2.0-only
6
*
7
* Author: Nicola Baldo <nbaldo@cttc.es> for the code adapted from the lena-dual-stripe.cc example
8
* Author: Michele Polese <michele.polese@gmail.com> for this version
9
*/
10
11
#include "ns3/buildings-module.h"
12
#include "ns3/core-module.h"
13
#include "ns3/mobility-module.h"
14
#include "ns3/network-module.h"
15
16
using namespace
ns3
;
17
18
NS_LOG_COMPONENT_DEFINE
(
"OutdoorRandomWalkExample"
);
19
20
/**
21
* Print the buildings list in a format that can be used by Gnuplot to draw them.
22
*
23
* @param filename The output filename.
24
*/
25
void
26
PrintGnuplottableBuildingListToFile
(std::string
filename
)
27
{
28
std::ofstream
outFile
;
29
outFile
.open(
filename
.c_str(), std::ios_base::out | std::ios_base::trunc);
30
if
(!
outFile
.is_open())
31
{
32
NS_LOG_ERROR
(
"Can't open file "
<<
filename
);
33
return
;
34
}
35
uint32_t
index = 0;
36
for
(
auto
it
=
BuildingList::Begin
();
it
!=
BuildingList::End
(); ++
it
)
37
{
38
++index;
39
Box
box
= (*it)->GetBoundaries();
40
outFile
<<
"set object "
<< index <<
" rect from "
<<
box
.xMin <<
","
<<
box
.yMin <<
" to "
41
<<
box
.xMax <<
","
<<
box
.yMax << std::endl;
42
}
43
}
44
45
/**
46
* This is an example on how to use the RandomWalk2dOutdoorMobilityModel class.
47
* The script outdoor-random-walk-example.sh can be used to visualize the
48
* positions visited by the random walk.
49
*/
50
int
51
main(
int
argc
,
char
*
argv
[])
52
{
53
LogComponentEnable
(
"RandomWalk2dOutdoor"
,
LOG_LEVEL_LOGIC
);
54
CommandLine
cmd
(
__FILE__
);
55
cmd
.Parse(
argc
,
argv
);
56
57
// create a grid of buildings
58
double
buildingSizeX
= 100;
// m
59
double
buildingSizeY
= 50;
// m
60
double
streetWidth
= 25;
// m
61
double
buildingHeight
= 10;
// m
62
uint32_t
numBuildingsX
= 10;
63
uint32_t
numBuildingsY
= 10;
64
double
maxAxisX
= (
buildingSizeX
+
streetWidth
) *
numBuildingsX
;
65
double
maxAxisY
= (
buildingSizeY
+
streetWidth
) *
numBuildingsY
;
66
67
std::vector<Ptr<Building>>
buildingVector
;
68
for
(
uint32_t
buildingIdX
= 0;
buildingIdX
<
numBuildingsX
; ++
buildingIdX
)
69
{
70
for
(
uint32_t
buildingIdY
= 0;
buildingIdY
<
numBuildingsY
; ++
buildingIdY
)
71
{
72
Ptr<Building>
building
;
73
building
=
CreateObject<Building>
();
74
75
building
->SetBoundaries(
Box
(
buildingIdX
* (
buildingSizeX
+
streetWidth
),
76
buildingIdX
* (
buildingSizeX
+
streetWidth
) +
buildingSizeX
,
77
buildingIdY
* (
buildingSizeY
+
streetWidth
),
78
buildingIdY
* (
buildingSizeY
+
streetWidth
) +
buildingSizeY
,
79
0.0,
80
buildingHeight
));
81
building
->SetNRoomsX(1);
82
building
->SetNRoomsY(1);
83
building
->SetNFloors(1);
84
buildingVector
.push_back(
building
);
85
}
86
}
87
88
// print the list of buildings to file
89
PrintGnuplottableBuildingListToFile
(
"buildings.txt"
);
90
91
// create one node
92
NodeContainer
nodes
;
93
nodes
.
Create
(1);
94
95
// set the RandomWalk2dOutdoorMobilityModel mobility model
96
MobilityHelper
mobility
;
97
mobility
.SetMobilityModel(
98
"ns3::RandomWalk2dOutdoorMobilityModel"
,
99
"Bounds"
,
100
RectangleValue
(
Rectangle
(-
streetWidth
,
maxAxisX
, -
streetWidth
,
maxAxisY
)));
101
// create an OutdoorPositionAllocator and set its boundaries to match those of the mobility
102
// model
103
Ptr<OutdoorPositionAllocator>
position =
CreateObject<OutdoorPositionAllocator>
();
104
Ptr<UniformRandomVariable>
xPos
=
CreateObject<UniformRandomVariable>
();
105
xPos
->SetAttribute(
"Min"
,
DoubleValue
(-
streetWidth
));
106
xPos
->SetAttribute(
"Max"
,
DoubleValue
(
maxAxisX
));
107
Ptr<UniformRandomVariable>
yPos
=
CreateObject<UniformRandomVariable>
();
108
yPos
->SetAttribute(
"Min"
,
DoubleValue
(-
streetWidth
));
109
yPos
->SetAttribute(
"Max"
,
DoubleValue
(
maxAxisY
));
110
position->SetAttribute(
"X"
,
PointerValue
(
xPos
));
111
position->SetAttribute(
"Y"
,
PointerValue
(
yPos
));
112
mobility
.SetPositionAllocator(position);
113
// install the mobility model
114
mobility
.Install(
nodes
.
Get
(0));
115
116
// enable the traces for the mobility model
117
AsciiTraceHelper
ascii
;
118
MobilityHelper::EnableAsciiAll
(
ascii
.CreateFileStream(
"mobility-trace-example.mob"
));
119
120
Simulator::Stop
(
Seconds
(1
e4
));
121
Simulator::Run
();
122
Simulator::Destroy
();
123
124
return
0;
125
}
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition
trace-helper.h:163
ns3::Box
a 3d box
Definition
box.h:24
ns3::BuildingList::End
static Iterator End()
Definition
building-list.cc:219
ns3::BuildingList::Begin
static Iterator Begin()
Definition
building-list.cc:213
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition
double.h:31
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition
mobility-helper.h:33
ns3::MobilityHelper::EnableAsciiAll
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
Definition
mobility-helper.cc:200
ns3::NodeContainer
keep track of a set of node pointers.
Definition
node-container.h:29
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition
node-container.cc:73
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition
node-container.cc:67
ns3::PointerValue
AttributeValue implementation for Pointer.
Definition
pointer.h:37
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Rectangle
a 2d rectangle
Definition
rectangle.h:24
ns3::RectangleValue
AttributeValue implementation for Rectangle.
Definition
rectangle.h:114
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::Simulator::Stop
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition
simulator.cc:175
uint32_t
NS_LOG_ERROR
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition
log.h:243
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
nodes
NodeContainer nodes
Definition
lr-wpan-bootstrap.cc:43
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LogComponentEnable
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition
log.cc:291
ns3::LOG_LEVEL_LOGIC
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition
log.h:99
second.cmd
cmd
Definition
second.py:29
third.mobility
mobility
Definition
third.py:92
PrintGnuplottableBuildingListToFile
void PrintGnuplottableBuildingListToFile(std::string filename)
Print the buildings list in a format that can be used by Gnuplot to draw them.
Definition
outdoor-random-walk-example.cc:26
src
buildings
examples
outdoor-random-walk-example.cc
Generated on Mon Dec 15 2025 15:21:49 for ns-3 by
1.9.8