A Discrete-Event Network Simulator
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 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Nicola Baldo <nbaldo@cttc.es> for the code adapted from the lena-dual-stripe.cc example
19 * Author: Michele Polese <michele.polese@gmail.com> for this version
20 */
21
22#include "ns3/buildings-module.h"
23#include "ns3/core-module.h"
24#include "ns3/mobility-module.h"
25#include "ns3/network-module.h"
26
27using namespace ns3;
28
29NS_LOG_COMPONENT_DEFINE("OutdoorRandomWalkExample");
30
31/**
32 * Print the buildings list in a format that can be used by Gnuplot to draw them.
33 *
34 * \param filename The output filename.
35 */
36void
38{
39 std::ofstream outFile;
40 outFile.open(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
41 if (!outFile.is_open())
42 {
43 NS_LOG_ERROR("Can't open file " << filename);
44 return;
45 }
46 uint32_t index = 0;
47 for (auto it = BuildingList::Begin(); it != BuildingList::End(); ++it)
48 {
49 ++index;
50 Box box = (*it)->GetBoundaries();
51 outFile << "set object " << index << " rect from " << box.xMin << "," << box.yMin << " to "
52 << box.xMax << "," << box.yMax << std::endl;
53 }
54}
55
56/**
57 * This is an example on how to use the RandomWalk2dOutdoorMobilityModel class.
58 * The script outdoor-random-walk-example.sh can be used to visualize the
59 * positions visited by the random walk.
60 */
61int
62main(int argc, char* argv[])
63{
64 LogComponentEnable("RandomWalk2dOutdoor", LOG_LEVEL_LOGIC);
65 CommandLine cmd(__FILE__);
66 cmd.Parse(argc, argv);
67
68 // create a grid of buildings
69 double buildingSizeX = 100; // m
70 double buildingSizeY = 50; // m
71 double streetWidth = 25; // m
72 double buildingHeight = 10; // m
73 uint32_t numBuildingsX = 10;
74 uint32_t numBuildingsY = 10;
75 double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
76 double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
77
78 std::vector<Ptr<Building>> buildingVector;
79 for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
80 {
81 for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
82 {
83 Ptr<Building> building;
84 building = CreateObject<Building>();
85
86 building->SetBoundaries(Box(buildingIdX * (buildingSizeX + streetWidth),
87 buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
88 buildingIdY * (buildingSizeY + streetWidth),
89 buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
90 0.0,
91 buildingHeight));
92 building->SetNRoomsX(1);
93 building->SetNRoomsY(1);
94 building->SetNFloors(1);
95 buildingVector.push_back(building);
96 }
97 }
98
99 // print the list of buildings to file
101
102 // create one node
104 nodes.Create(1);
105
106 // set the RandomWalk2dOutdoorMobilityModel mobility model
108 mobility.SetMobilityModel(
109 "ns3::RandomWalk2dOutdoorMobilityModel",
110 "Bounds",
111 RectangleValue(Rectangle(-streetWidth, maxAxisX, -streetWidth, maxAxisY)));
112 // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility
113 // model
114 Ptr<OutdoorPositionAllocator> position = CreateObject<OutdoorPositionAllocator>();
115 Ptr<UniformRandomVariable> xPos = CreateObject<UniformRandomVariable>();
116 xPos->SetAttribute("Min", DoubleValue(-streetWidth));
117 xPos->SetAttribute("Max", DoubleValue(maxAxisX));
118 Ptr<UniformRandomVariable> yPos = CreateObject<UniformRandomVariable>();
119 yPos->SetAttribute("Min", DoubleValue(-streetWidth));
120 yPos->SetAttribute("Max", DoubleValue(maxAxisY));
121 position->SetAttribute("X", PointerValue(xPos));
122 position->SetAttribute("Y", PointerValue(yPos));
123 mobility.SetPositionAllocator(position);
124 // install the mobility model
125 mobility.Install(nodes.Get(0));
126
127 // enable the traces for the mobility model
128 AsciiTraceHelper ascii;
129 MobilityHelper::EnableAsciiAll(ascii.CreateFileStream("mobility-trace-example.mob"));
130
134
135 return 0;
136}
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
a 3d box
Definition: box.h:35
double yMax
The y coordinate of the top bound of the box.
Definition: box.h:116
double xMin
The x coordinate of the left bound of the box.
Definition: box.h:110
double yMin
The y coordinate of the bottom bound of the box.
Definition: box.h:114
double xMax
The x coordinate of the right bound of the box.
Definition: box.h:112
static Iterator End()
static Iterator Begin()
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Helper class used to assign positions and mobility models to nodes.
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
AttributeValue implementation for Pointer.
Definition: pointer.h:48
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a 2d rectangle
Definition: rectangle.h:35
AttributeValue implementation for Rectangle.
Definition: rectangle.h:125
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:110
ns cmd
Definition: second.py:40
ns mobility
Definition: third.py:105
void PrintGnuplottableBuildingListToFile(std::string filename)
Print the buildings list in a format that can be used by Gnuplot to draw them.