-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStart.cpp
424 lines (261 loc) · 9.1 KB
/
Start.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <thread>
#include <iostream> //cout
#include <queue>
#include <utility> // std::pair, std::make_pair
#include <math.h> // pow, sqrt
#include <stdlib.h> /* srand, rand */
#include <atomic> //access
#include <mutex>
#include <dds/DCPS/Service_Participant.h> //neccessary to start_opendds OpenDDSThreat without error
#include "OpenDDS.h" //to start_opendds OpenDDSThread
#include "MriTypeSupportImpl.h" //Mri data formats
#include "Sleep.h" // Sleep()
#include "MriTimeSync.h"
#include "QueueTs.h"
//#include "main.h"
#include "Start.h"
double const fullAngle = 2 * PI;
extern bool finish_application;
extern bool threadOpenDDS_initialized;
extern std::map<long, Mri::VehData> vehs_map;
std::thread threadVehsMap;
std::thread threadV2Xreading;
std::thread threadPublishSubjectCarLocation;
std::thread threadPublishBSM;
std::thread threadOpenDDS;
std::thread threadTimestamp;
int vehdataCount;
std::string log_rd;
extern std::map<long, UnityVehicle> unityVehsMap;
extern std::map<long, std::set<UnityVehicle>> unityVehsMapSets;
extern std::atomic<int> access_unityVehMapSets;
extern std::mutex mutexMap;
extern std::atomic<float> dnpw_closestVehicleMessage_distance;
extern std::atomic<long> dnpw_closestVehicleMessage_timestamp;
extern std::atomic<float> eebl_closestVehicleMessage_distance;
extern std::atomic<long> eebl_closestVehicleMessage_timestamp;
extern std::atomic<float> iw_closestVehicle_time;
extern std::atomic<long> iw_closestVehicle_timestamp;
extern Mri::VehData subjectCar;
extern float subjectCarBrakeForce; // 0 min, 1 max
extern QueueTs<Mri::VehData> vehdata_queue_in;
extern std::atomic<long> veh_id_to_remove;
extern long VEH_ID;
UnityVehicle unityVehArray[200];
void getVehsArray(int* Num_Vehicles, UnityVehicle** unityVehicleData) {
std::lock_guard<std::mutex> guard(mutexMap);
int indx = 0;
*Num_Vehicles = 0;
log_rd = "";
try
{
if (!unityVehsMapSets.empty())
{
long interpolate_timestamp = GetTimestamp() - 15; //interpolate location of vehicle 15 x 10 ms = 150 ms ago
long elapsedMicrosecondsFromLastTimeSTamp = GetElapsedMicroseconds();
//std::map<long, std::set<UnityVehicle>> vehs_map_copy2;
//vehs_map_copy2 = unityVehsMapSets;
std::set<UnityVehicle> setUnityVehs;
long key_mapUnityVehs;
// during each getVehsArray we create new Map, and at the end we replace content of unityVehsMapSets
std::map<long, std::set<UnityVehicle>> unityVehsMapSets2;
for (auto& x : unityVehsMapSets) {
setUnityVehs = x.second;
key_mapUnityVehs = x.first;
// log_rd = "START, go to interpolate";
UnityVehicle uVeh = interpolateVehPosition(&setUnityVehs, interpolate_timestamp, elapsedMicrosecondsFromLastTimeSTamp);
//log_rd += ", after interpolate, indx=";
//log_rd += std::to_string(indx);
// vehicle_id!=-1 means we couldn't get interpolated location of vehicle
if (uVeh.vehicle_id > -1)
{
unityVehArray[indx] = uVeh;
indx++;
// update a new map
//x.second = setUnityVehs;
//update value in map
unityVehsMapSets[key_mapUnityVehs] = setUnityVehs;
//unityVehsMapSets2.emplace(key_mapUnityVehs, setUnityVehs);
// log_rd += ", after update map";
}
else
{
//unityVehsMapSets2.emplace(x.first, x.second);
}
//std::cout << x.second.timestamp << " vehId:" << x.second.vehicle_id << std::endl;
}
//unityVehsMapSets.swap(unityVehsMapSets2);
*unityVehicleData = unityVehArray;
*Num_Vehicles = indx;
std::cout << "---------------------------num vehs=" << indx << std::endl;
}
else
{
std::cout << "..VehsMap empty ..." << std::endl;
}
// access_unityVehMapSets = ACCESS_none;
log_rd += ", END ";
}
catch (const std::exception&)
{
std::cout << "ERROR !" << std::endl;
//access_unityVehMapSets = ACCESS_none;
//log_rd += ", Error at the end";
}
}
UnityVehicle interpolateVehPosition(std::set<UnityVehicle> * _set, long x_timestamp, long x_elapsed_microseconds) {
UnityVehicle unityVehicle;
UnityVehicle uPrev;
UnityVehicle uNext;
//if we didn't interpolate, return id=-1
unityVehicle.vehicle_id = -1;
int setSize = _set->size();
if (setSize >= 4)
{
if (setSize>100)
{
std::cout << "Wow";
}
//std::cout << ' ' << it.;
try
{
for (std::set<UnityVehicle>::iterator it = _set->begin(); it != _set->end(); ++it)
{
if (x_timestamp < it->timestamp)
{
uNext = *it;
//log_rd += ", before lerp";
//check if it is no beginning of the set
if (it != _set->begin())
{
it--;
uPrev = *it;
unityVehicle = lerpRD(&uPrev, &uNext, x_timestamp, x_elapsed_microseconds);
//check if it is no beginning of the set
//{
if (it != _set->begin()) {
//std::cout << "............erase%%%%";
it--;
_set->erase(_set->begin(), it);
//std::cout << "%%set......" << std::endl << std::endl;
//log_rd += ", after erase";
}
//}
}
break;
}
}
}
catch (const std::exception&)
{
std::cout << "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEERRRRRRRRRRRRRRRRRRRRRoR" << std::endl;
}
}
return unityVehicle;
}
UnityVehicle lerpRD(UnityVehicle * u_prev, UnityVehicle * u_next, long x_timestamp, long elapsedMicrosecond) {
UnityVehicle _vehicle = *u_prev;
try
{
long timestamp_prev_us = u_prev->timestamp * 1000; //in microseconds
long timestamp_next_us = u_next->timestamp * 1000;
long x_timestamp_us = (x_timestamp * 1000) + elapsedMicrosecond;
//long timestamp_prev = u_prev->timestamp;
//long timestamp_next = u_next->timestamp;
float factor = (float)(x_timestamp_us - timestamp_prev_us) / (timestamp_next_us - timestamp_prev_us);
_vehicle.position_x = u_prev->position_x + ((u_next->position_x - u_prev->position_x) * factor);
_vehicle.position_y = u_prev->position_y + ((u_next->position_y - u_prev->position_y) * factor);
_vehicle.position_z = u_prev->position_z + ((u_next->position_z - u_prev->position_z) * factor);
}
catch (const std::exception&)
{
std::cout << "ERROR" << std::endl;
}
return _vehicle;
}
void updateBrakeStatus(float brake) {
subjectCarBrakeForce = brake;
}
void updateSubjectCarLocation( float pos_x, float pos_y, float pos_z, float heading, float pitch, float roll, float speed) {
// normalize radian angle
//fullAngle - const = 2 * PI
double heading_rad = fmod((heading + (PI / 2) + fullAngle), fullAngle);
double pitch_rad = fmod((pitch + (PI / 2) + fullAngle), fullAngle);
double roll_rad = fmod((roll + (PI / 2) + fullAngle), fullAngle);
subjectCar.vehicle_id = VEH_ID;
subjectCar.position_x = (double)pos_x;
subjectCar.position_y = (double)pos_y;
subjectCar.position_z = (double)pos_z;
subjectCar.orient_heading = (double)heading_rad;
subjectCar.orient_pitch = (double)pitch_rad;
subjectCar.orient_roll = (double)roll_rad;
subjectCar.speed = (double)speed;
}
bool start_opendds() {
std::cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& START &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&";
finish_application = false;
threadOpenDDS_initialized = false;
//char *argv2[] = { "-DCPSConfigFile","C:\\rtps\\rtps.ini" };
//char *argv2[] = { "-DCPSConfigFile","C:\\iCAVE2\\Bin\\OpenDDS_Repo\\rtps.ini" };
char *argv2[] = { "-DCPSConfigFile","C:\\iCAVE2\\Bin\\rtps.ini" };
int argc = 2;
long old_veh_timestamp = 0;
//unityVehArray
unityVehsMap.clear();
//clear vehdata_queue_in
std::queue <Mri::VehData>vehdata_queue_empty;
vehdata_queue_in.swap(&vehdata_queue_empty);
threadTimestamp = std::thread (TimestampThread);
threadOpenDDS = std::thread(OpenDDSThread, argc, argv2);
while (!threadOpenDDS_initialized)
{
//wait until threadOpenDDS is initialized;
}
//check if initialization of OpenDDSThread was successfull
if (!finish_application)
{
threadVehsMap = std::thread(unityVehsMapThread);
threadPublishSubjectCarLocation = std::thread(publishSubjectCarLocationThread);
threadV2Xreading = std::thread(v2xMapThread);
threadPublishBSM = std::thread(publishBSMThread);
std::cout << "Thread unityVehsMap started " << std::endl;
return true;
}
else
{
threadTimestamp.detach();
threadOpenDDS.detach();
return false;
}
}
void stop_opendds() {
std::cout << "--------------------------------- Try to STOP ------------------------------------";
finish_application = true;
threadV2Xreading.detach();
threadTimestamp.detach();
threadVehsMap.detach();
threadPublishSubjectCarLocation.detach();
threadPublishBSM.detach();
SendUnregisterAppMessage();
Sleep(1000);
threadOpenDDS.detach();
std::cout << "--------------------------------- STOPPED ------------------------------------";
}
float GetDnpwDistance()
{
float _distance = dnpw_closestVehicleMessage_distance;
//after reading data -> reset data
/*dnpw_closestVehicleMessage_distance = 99999;
dnpw_closestVehicleMessage_timestamp = 0;*/
return _distance;
}
float GetEeblDistance()
{
float _eebl_distance = eebl_closestVehicleMessage_distance;
return _eebl_distance;
}
// check the Intersection Warning time to collision, it time=-1 -> no warning
float GetIwTime() {
float _iw_time = iw_closestVehicle_time;
return _iw_time;
}