-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
141 lines (83 loc) · 2.88 KB
/
main.cpp
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
#include <thread>
#include <chrono> // std::chrono::seconds to test this_thread sleep
#include <iostream> //cout
#include <stdlib.h> /* srand, rand */
#include <dds/DCPS/Service_Participant.h> //neccessary to start OpenDDSThreat without error
#include "OpenDDS.h" //to start OpenDDSThread
#include "MriTypeSupportImpl.h" //Mri data formats
#include "Sleep.h" // Sleep()
#include "MriTimeSync.h" //Sleep()
#include "V2xApps.h"
#include "main.h"
extern bool finish_application;
extern std::map<long, Mri::VehData> vehs_map;
extern long veh_id_to_remove;
//char *argv2[] = { "-DCPSConfigFile","rtps.ini" };
//-DCPSConfigFile rtps.ini
int main(int argc, char* argv[]) {
//startOpenDDSThread(argc, argv);
std::thread threadOpenDDS(OpenDDSThread, argc, argv);
std::thread threadVehsMap(vehsMapThread);
//rd
//std::thread threadV2Xreading(v2xMapThread);
//// start thread OpenDDS
//std::thread threadOpenDDS(OpenDDSThread, argc, argv);
long old_veh_timestamp = 0;
std::map<long, Mri::VehData> vehs_map_copy;
/* initialize random seed: */
srand(time(NULL));
int wait_time_random;
int map_size;
int wait_time_factor; // more vehs in the map => factor become smaller, time between messages shorter
std::string message_text;
while (!finish_application)
{
//wait, wait....
//Sleep(100);
map_size = vehs_map.size();
if (map_size>0)
{
vehs_map_copy = vehs_map;
if (map_size>=400)
{
map_size = 400;
}
wait_time_factor = 400 / map_size;
//std::cout << "------------------------------------------------"<< std::endl;
for (auto& x : vehs_map_copy) {
//send V2X message in random intervals of time
//20181219 rd
//don't send BSM from human controlled vehs (Unity) - skip vehs id 1 to 10
if (x.first >10)
{
message_text = createBSMcoreData(x.second);
sendV2X(x.second.vehicle_id, GetTimestamp(), message_text);
wait_time_random = rand() % wait_time_factor;
Sleep(wait_time_random);
}
//std::cout << "wait time " << wait_time_random << std::endl;
//std::cout << "timestamp=" << x.second.timestamp << " veh_id=" << x.second.vehicle_id << " x=" << x.second.position_x << " y=" << x.second.position_y << std::endl;
}
old_veh_timestamp = GetTimestamp() - 50; //to find veh data not updated for 50 x 10ms = 500 ms
for (auto& x : vehs_map_copy) {
if (x.second.timestamp < old_veh_timestamp)
{
if (x.second.vehicle_id!=0)
{
//select this veh_id to removing
veh_id_to_remove = x.second.vehicle_id;
break;
}
}
}
std::cout << ".";
//std::cout << std::endl << std::endl;
}
}
std::cout << std::endl << "--- CLOSING APPLICATION ---" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
//threadV2Xreading.detach(); rd
threadVehsMap.detach();
threadOpenDDS.detach();
return 0;
}