A Discrete-Event Network Simulator
API
outdoor-random-walk-example.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4  * Copyright (c) 2019, University of Padova, Dep. of Information Engineering, SIGNET lab
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Nicola Baldo <nbaldo@cttc.es> for the code adapted from the lena-dual-stripe.cc example
20  * Author: Michele Polese <michele.polese@gmail.com> for this version
21  */
22 
23 #include "ns3/buildings-module.h"
24 #include "ns3/mobility-module.h"
25 #include "ns3/core-module.h"
26 #include "ns3/network-module.h"
27 
28 using namespace ns3;
29 
30 NS_LOG_COMPONENT_DEFINE ("OutdoorRandomWalkExample");
31 
32 void
34 {
35  std::ofstream outFile;
36  outFile.open (filename.c_str (), std::ios_base::out | std::ios_base::trunc);
37  if (!outFile.is_open ())
38  {
39  NS_LOG_ERROR ("Can't open file " << filename);
40  return;
41  }
42  uint32_t index = 0;
44  {
45  ++index;
46  Box box = (*it)->GetBoundaries ();
47  outFile << "set object " << index
48  << " rect from " << box.xMin << "," << box.yMin
49  << " to " << box.xMax << "," << box.yMax
50  << std::endl;
51  }
52 }
53 
54 
60 int
61 main (int argc, char *argv[])
62 {
63  LogComponentEnable ("RandomWalk2dOutdoor", LOG_LEVEL_LOGIC);
64  CommandLine cmd (__FILE__);
65  cmd.Parse (argc, argv);
66 
67  // create a grid of buildings
68  double buildingSizeX = 100; // m
69  double buildingSizeY = 50; // m
70  double streetWidth = 25; // m
71  double buildingHeight = 10; // m
72  uint32_t numBuildingsX = 10;
73  uint32_t numBuildingsY = 10;
74  double maxAxisX = (buildingSizeX + streetWidth) * numBuildingsX;
75  double maxAxisY = (buildingSizeY + streetWidth) * numBuildingsY;
76 
77  std::vector<Ptr<Building> > buildingVector;
78  for (uint32_t buildingIdX = 0; buildingIdX < numBuildingsX; ++buildingIdX)
79  {
80  for (uint32_t buildingIdY = 0; buildingIdY < numBuildingsY; ++buildingIdY)
81  {
82  Ptr < Building > building;
83  building = CreateObject<Building> ();
84 
85  building->SetBoundaries (Box (buildingIdX * (buildingSizeX + streetWidth),
86  buildingIdX * (buildingSizeX + streetWidth) + buildingSizeX,
87  buildingIdY * (buildingSizeY + streetWidth),
88  buildingIdY * (buildingSizeY + streetWidth) + buildingSizeY,
89  0.0, buildingHeight));
90  building->SetNRoomsX (1);
91  building->SetNRoomsY (1);
92  building->SetNFloors (1);
93  buildingVector.push_back (building);
94  }
95  }
96 
97  // print the list of buildings to file
98  PrintGnuplottableBuildingListToFile ("buildings.txt");
99 
100  // create one node
102  nodes.Create (1);
103 
104  // set the RandomWalk2dOutdoorMobilityModel mobility model
106  mobility.SetMobilityModel ("ns3::RandomWalk2dOutdoorMobilityModel",
107  "Bounds", RectangleValue (
108  Rectangle (-streetWidth, maxAxisX, -streetWidth, maxAxisY)));
109  // create an OutdoorPositionAllocator and set its boundaries to match those of the mobility model
110  Ptr<OutdoorPositionAllocator> position = CreateObject<OutdoorPositionAllocator> ();
111  Ptr<UniformRandomVariable> xPos = CreateObject<UniformRandomVariable>();
112  xPos->SetAttribute ("Min", DoubleValue (-streetWidth));
113  xPos->SetAttribute ("Max", DoubleValue (maxAxisX));
114  Ptr<UniformRandomVariable> yPos = CreateObject<UniformRandomVariable>();
115  yPos->SetAttribute ("Min", DoubleValue (-streetWidth));
116  yPos->SetAttribute ("Max", DoubleValue (maxAxisY));
117  position->SetAttribute ("X", PointerValue (xPos));
118  position->SetAttribute ("Y", PointerValue (yPos));
119  mobility.SetPositionAllocator (position);
120  // install the mobility model
121  mobility.Install (nodes.Get (0));
122 
123  // enable the traces for the mobility model
124  AsciiTraceHelper ascii;
125  MobilityHelper::EnableAsciiAll (ascii.CreateFileStream ("mobility-trace-example.mob"));
126 
127  Simulator::Stop (Seconds (1e4));
128  Simulator::Run ();
130 }
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
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
std::vector< Ptr< Building > >::const_iterator Iterator
Const Iterator.
Definition: building-list.h:40
static Iterator End(void)
static Iterator Begin(void)
Parse command-line arguments.
Definition: command-line.h:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
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 SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
a 2d rectangle
Definition: rectangle.h:35
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:113
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
cmd
Definition: second.py:35
mobility
Definition: third.py:108
void PrintGnuplottableBuildingListToFile(std::string filename)