-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanet-highway-test.cc
187 lines (160 loc) · 6.28 KB
/
vanet-highway-test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2005-2009 Old Dominion University [ARBABI]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Hadi Arbabi <[email protected]>
*/
/*
This the starting point of the simulation and experiments.
The main function will parse the input and parameter settings.
Creates a highway and set the highway parameters. then bind the events (callbacks)
to the created controller and designed handlers. Sets the highway start and end time,
and eventually runs the simulation which is basically running a highway with a controller.
You can add your functions to controller to create various scenarios.
*/
#include <fstream>
#include <iostream>
#include <iomanip>
#include "Model.h"
#include "ns3/core-module.h"
#include "ns3/common-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/mobility-module.h"
#include "ns3/contrib-module.h"
#include "ns3/wifi-module.h"
#include "ns3/random-variable.h"
#include "math.h"
#include "Highway.h"
#include "VehicleGenerator.h"
#include "tinyxml.h"
#include "HighwayProjectXml.h"
#include "HighwayXml.h"
#include "WifiConfigurationXml.h"
#include "HighwayProject.h"
NS_LOG_COMPONENT_DEFINE("HADI");
using namespace ns3;
using namespace std;
ifstream dataFile;
static void addCustomVehicle(Ptr<Highway> highway) {
int id = -1;
int lane = 1;
double speed = 40.0;
Ptr<Model> model = CreateObject<Model>();
model->SetDesiredVelocity(speed);
model->SetDeltaV(4.0);
model->SetAcceleration(0.5);
model->SetDeceleration(3.0);
model->SetMinimumGap(2.0);
model->SetTimeHeadway(0.1);
model->SetSqrtAccelerationDeceleration(sqrt(model->GetAcceleration() * model->GetDeceleration()));
Ptr<LaneChange> laneChange = CreateObject<LaneChange>();
laneChange->SetPolitenessFactor(0.2);
laneChange->SetDbThreshold(0.3);
laneChange->SetGapMin(2.0);
laneChange->SetMaxSafeBreakingDeceleration(12.0);
laneChange->SetBiasRight(0.2);
Ptr<Vehicle> temp=CreateObject<Vehicle>();
temp->SetVehicleId(id);
temp->IsEquipped=false;
temp->SetModel(model);
temp->SetLaneChange(laneChange);
temp->SetLength(4);
temp->SetWidth(2);
temp->SetVelocity(speed);
temp->SetAcceleration(0.0);
temp->SetLane(lane);
temp->SetDirection(0.0);
temp->SetPosition(highway->GetLaneStart(lane));
highway->AddVehicleToBeginning(temp);
}
static int msgCounter = 0;
static bool controlVehicle(Ptr<Highway> highway, Ptr<Vehicle> veh, double dt) {
if ((veh->GetVehicleId() == 1) && (msgCounter == 499)) {
stringstream msg;
msg << veh->GetVehicleId()
<< " x=" << veh->GetPosition().x
<< " y=" << veh->GetPosition().y
<< " v=" << veh->GetVelocity()
<< " d=" << veh->GetDirection()
<< " l=" << veh->GetLane();
Ptr<Packet> packet = Create<Packet>((uint8_t*) msg.str().c_str(), msg.str().length());
veh->SendTo(veh->GetBroadcastAddress(), packet);
}
msgCounter = (msgCounter + 1)%500;
return false;
}
int main(int argc, char *argv[]) {
string projectXmlFile = "";
string vehicleTraceFile = "vehicleTrace.csv";
string networkTraceFile = "networkTrace.csv";
bool enableVehicleReceive = false;
bool enableDeviceTrace = false;
bool enablePhyRxOkTrace = false;
bool enablePhyRxErrorTrace = false;
bool enablePhyTxTrace = false;
bool enablePhyStateTrace = false;
dataFile.open("/home/bdupont/vehicleTestInfo2.txt");
CommandLine cmd;
cmd.AddValue("project", "highway xml description", projectXmlFile);
cmd.AddValue("vehtracefile", "trace file for vehicle locations", vehicleTraceFile);
cmd.AddValue("nettracefile", "trace file for network messages", networkTraceFile);
cmd.AddValue("enablevehiclereceive", "enable tracing vehicle receive", enableVehicleReceive);
cmd.AddValue("enabledevicetrace", "enable device trace", enableDeviceTrace);
cmd.AddValue("enablephyrxoktrace", "enable phy rx ok trace", enablePhyRxOkTrace);
cmd.AddValue("enablephyrxerrortrace", "enable phy rx error trace", enablePhyRxErrorTrace);
cmd.AddValue("enablephytxtrace", "enable phy tx trace", enablePhyTxTrace);
cmd.AddValue("enablephystatetrace", "enable phy state trace", enablePhyStateTrace);
cmd.Parse(argc, argv);
TiXmlDocument doc(projectXmlFile.c_str());
doc.LoadFile();
TiXmlHandle hDoc(&doc);
TiXmlElement* root = hDoc.FirstChildElement().Element();
TiXmlHandle hroot = TiXmlHandle(root);
HighwayProjectXml xml;
xml.LoadFromXml(hroot);
ns3::PacketMetadata::Enable();
Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue("2200"));
Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("2200"));
HighwayProject project(xml);
project.SetVehTraceFile(vehicleTraceFile);
project.SetNetTraceFile(networkTraceFile);
if(enableVehicleReceive) {
project.EnableVehicleReceive();
}
if(enableDeviceTrace) {
project.EnableDeviceTrace();
}
if(enablePhyRxOkTrace) {
project.EnablePhyRxOkTrace();
}
if(enablePhyRxErrorTrace) {
project.EnablePhyRxErrorTrace();
}
if(enablePhyTxTrace) {
project.EnablePhyTxTrace();
}
if(enablePhyStateTrace) {
project.EnablePhyStateTrace();
}
Ptr<Highway> highway = project.getHighways()[0];
Simulator::Schedule(Seconds(10), &addCustomVehicle, highway);
project.SetVehicleControlCallback(MakeCallback(&controlVehicle));
project.Start();
Simulator::Run();
Simulator::Destroy();
return 0;
}