-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenDDST.cpp
897 lines (621 loc) · 21 KB
/
OpenDDST.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
#include <dds/DCPS/Marked_Default_Qos.h>
#include <dds/DCPS/Service_Participant.h>
#include <string>
#include <thread>
#include <iostream> //cout
#include <ctime> //time format
#include <conio.h> //key press
#include <map> //map - list of vehicles
#include <atomic> //access
#include <mutex>
#include "MriTypeSupportImpl.h"
#include "DataWriter_Aux2Strings.h"
#include "DataReader_Aux2Strings.h"
#include "DataReader_V2XMessage.h"
#include "DataWriter_V2XMessage.h"
#include "DataReader_VehData.h"
#include "DataWriter_VehData.h"
#include "ParticipantClass.h"
#include "SubscriberClass.h"
#include "PublisherClass.h"
#include "QueueTs.h"
#include "MriTimeSync.h"
#include "OpenDDS.h"
#include "Start.h"
#include "V2xApps.h"
using std::cerr;
using std::cout;
using std::endl;
using std::string;
Mri::Aux2StringsDataWriter_var writer_global_aux2strings;
Mri::V2XMessageDataWriter_var writer_global_v2xmessage;
Mri::VehDataDataWriter_var writer_global_vehdata;
std::atomic<long> veh_id_to_remove;
QueueTs<Mri::VehData> vehdata_queue_in;
Mri::VehData subjectCar;
float subjectCarBrakeForce; // store value of pressed brake in the subjectCar , 0 no press, 1 max press
std::map<long, Mri::VehData> vehs_map;
std::map<long, UnityVehicle> unityVehsMap;
std::map<long, std::set<UnityVehicle>> unityVehsMapSets;
//std::atomic<int> access_unityVehMapSets;
std::mutex mutexMap;
//std::queue<Mri::V2XMessage> v2x_queue_in;
//QueueTs<Mri::V2XMessage> v2x_queue_in;
QueueTs<Mri::V2XMessage> v2x_queue;
std::map<long, Mri::V2XMessage> v2xs_map;
bool finish_application;
bool threadOpenDDS_initialized;
long timestamp_previous; //used by generateV2xUniqueTimestamp
long sample_counter_per_timestamp; //used by generateV2xUniqueTimestamp
std::atomic<float> dnpw_closestVehicleMessage_distance;
std::atomic<long> dnpw_closestVehicleMessage_timestamp;
//Electronic Emergency Brake Light (EEBL)
std::atomic<float> eebl_closestVehicleMessage_distance;
std::atomic<long> eebl_closestVehicleMessage_timestamp;
//Intersection Warning
std::atomic<float> iw_closestVehicle_time;
std::atomic<long> iw_closestVehicle_timestamp;
std::string log_rd_VehsMapThread;
std::string log_rd_Opendds;
extern long VEH_ID;
UnityVehicle convertUnityVeh(Mri::VehData veh) {
//no copy strings
UnityVehicle uVeh;
uVeh.color = veh.color;
uVeh.lane_index = veh.lane_index;
uVeh.leading_vehicle_id = veh.leading_vehicle_id;
uVeh.link_coordinate = veh.link_coordinate;
uVeh.link_id = veh.link_id;
uVeh.orient_heading = veh.orient_heading;
uVeh.orient_pitch = veh.orient_pitch;
uVeh.orient_roll = veh.orient_roll;
uVeh.position_x = veh.position_x;
uVeh.position_y = veh.position_y;
uVeh.position_z = veh.position_z;
uVeh.speed = veh.speed;
uVeh.timestamp = veh.timestamp;
uVeh.trailing_vehicle_id = veh.trailing_vehicle_id;
uVeh.turning_indicator = veh.turning_indicator;
uVeh.vehicle_id = veh.vehicle_id;
uVeh.vehicle_type = veh.vehicle_type;
return uVeh;
}
void publishBSMThread() {
Mri::VehData _veh;
float _brakeForce;
std::string _bsm;
long _timestamp;
std::chrono::time_point<std::chrono::system_clock> t = std::chrono::system_clock::now(); //sleep below
while (!finish_application) {
_veh = getSubjectCarPosition();
_timestamp = GetTimestamp();
_brakeForce = subjectCarBrakeForce;
_bsm = createBSMcoreData(_veh, _brakeForce);
//publishVehDataMessage(_veh);
sendV2X(_veh.vehicle_id, _timestamp, _bsm);
t += std::chrono::milliseconds(100); //each loop 100 ms
std::this_thread::sleep_until(t);
}
cout << endl << "-------------------" << endl << "- publishBSMThread Stopped " << endl << "-------------------" << endl << endl;
}
void publishSubjectCarLocationThread() {
Mri::VehData _veh;
std::chrono::time_point<std::chrono::system_clock> t = std::chrono::system_clock::now(); //sleep below
while (!finish_application) {
_veh = getSubjectCarPosition();
_veh.timestamp = GetTimestamp();
publishVehDataMessage(_veh);
t += std::chrono::milliseconds(50); //each loop 50 ms
std::this_thread::sleep_until(t);
}
cout << endl << "-------------------" << endl << "- publishSubjectCarThread Stopped " << endl << "-------------------" << endl << endl;
}
Mri::VehData getSubjectCarPosition() {
return subjectCar;
}
void unityVehsMapThread() {
Mri::VehData _veh;
try
{
std::map<long, std::set<UnityVehicle>>::iterator it;
veh_id_to_remove = -1; // initial value
while (!finish_application)
{
//wait for something at the queue
vehdata_queue_in.pop(_veh);
{
addVehToMap(_veh);
if (veh_id_to_remove != -1)
{
if (removeVehFromMap()==0)
{
//success
cout << "############ Removed from vehsMap vehicle_id =" << veh_id_to_remove << " ############" << endl << endl;
}
}
}
}
}
catch (const std::exception&)
{
cout << "ERROR" << endl;
}
}
int removeVehFromMap() {
std::lock_guard<std::mutex> guard(mutexMap);
std::map<long, std::set<UnityVehicle>>::iterator it;
it = unityVehsMapSets.find(veh_id_to_remove);
veh_id_to_remove = -1; //reset this global variable
if (it != unityVehsMapSets.end())
{
unityVehsMapSets.erase(it);
return 0;
}
else
{
// can't find vehicle
return -1;
}
}
void addVehToMap(Mri::VehData _veh)
{
if (_veh.vehicle_id == VEH_ID)
{
return;
}
std::lock_guard<std::mutex> guard(mutexMap);
UnityVehicle _uVeh;
std::map<long, std::set<UnityVehicle>>::iterator it;
it = unityVehsMapSets.find(_veh.vehicle_id);
//convert from veh to Unity Veh
_uVeh = convertUnityVeh(_veh);
//convertUnityVeh(&_veh, &_uVeh);
if (it != unityVehsMapSets.end()) {
//there is a car with id = _veh.vehicle_id
//add to set
it->second.emplace(_uVeh);
//cout << "Timestamp=" << GetTimestamp() << " veh timestamp=" << _veh.timestamp << endl;
}
else
{
//no car with id = _veh.vehicle_id
//add new set to the map
std::set<UnityVehicle> _set;
_set.emplace(_uVeh);
unityVehsMapSets.emplace(_veh.vehicle_id, _set);
cout << "++++ ++++ NEW VEH = " << _veh.vehicle_id << " ++++ ++++" << endl << endl;
}
}
//..........................................................................................................
void OpenDDSThread(int argc, char* argv[]){
// start_opendds thread
//char key = ' ';
DDS::DomainId_t DomainID{ 246 };
const char * topic_traffic_vehs_name = "Mri_TrafficVeh";
const char * topic_subject_car_name = "Mri_SubjectCar";
//initial value
timestamp_previous = 0;
//access_unityVehMapSets = ACCESS_none;
//________________________________________________________
//initialization
subjectCar.vehicle_id = 1;
subjectCar.position_x = 0;
subjectCar.position_y = 0;
subjectCar.position_z = 0;
subjectCar.orient_heading = 0;
subjectCar.orient_pitch = 0;
subjectCar.orient_roll = 0;
subjectCar.speed = 0;
subjectCar.timestamp = 0;
std::cout << "*******^^^^^^^^* Starting openddsThread *********" << std::endl;
try
{
//it helps a lot !!!!!
ACE::init();
/*It was a following error :
ACE_INET_Addr::ACE_INET_Addr : localhost : WSA Startup not initialized
notification pipe open failed : WSA Startup not initialized
ACE_INET_Addr::ACE_INET_Addr : localhost : WSA Startup not initialized
notification pipe open failed : WSA Startup not initialized
ACE_Select_Reactor_T::open failed inside ACE_Select_Reactor_T::CTOR : WSA Startup
not initialized*/
// Initialize DomainParticipantFactory
DDS::DomainParticipantFactory_var dpf =
TheParticipantFactoryWithArgs(argc, argv);
// Create DomainParticipant
DDS::DomainParticipant_var participant =
dpf->create_participant(DomainID,
PARTICIPANT_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(participant.in())) {
cerr << "Participant: Failed to create participant..." << endl;
}
else {
cout << "Participant: participant created successfully" << endl;
}
// Create Subscriber
DDS::Subscriber_var subscriber = participant->create_subscriber(
SUBSCRIBER_QOS_DEFAULT,
DDS::SubscriberListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(subscriber.in())) {
cerr << "create_subscriber failed." << endl;
}
else {
cout << "SubscriberClass: subscriber created correctly" << endl;
}
// Create Publisher
DDS::Publisher_var publisher = participant->create_publisher(
PUBLISHER_QOS_DEFAULT,
DDS::PublisherListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK
);
if (CORBA::is_nil(publisher.in())) {
cerr << "create_publisher failed." << endl;
}
else {
cout << "PublisherClass: publisher created correctly" << endl;
}
// run timestamp synchronization and get VEH_ID
if (!TimeSynchronizationGetVEH_ID(participant, subscriber, publisher))
{
//there was a problem with time synchronization
cout << "#### THERE WAS A PROBLEM WITH TIMESTAMP SYNCHRONIZATION. PROGRAM WILL CLOSE... ####" << endl;
threadOpenDDS_initialized = true;
finish_application = true;
}
else
{
//Timestamps synchronized !
cout << "Timestamp synchronized. t=" << GetTimestamp() << endl << endl;
// topics: Mri_TrafficVeh Mri_SubjectCar
//WARNING: both readers uses the same "on_data_available" code in DataReaderListenerImpl_VehData
//we can do this, because formats of the messages are the same
cout << "Create reader_traffic..." << endl;
DataReader_VehData reader_traffic_vehs(participant, subscriber, "Mri_TrafficVeh");
DataReader_VehData reader_subject_vehs(participant, subscriber, "Mri_SubjectCar");
//subjec Car writer
cout << "Create writer_subjectCar..." << endl;
DataWriter_VehData writer_vehdata_message(participant, publisher, "Mri_SubjectCar");
writer_global_vehdata = writer_vehdata_message.msg_writer;
//DataReader_VehData reader_subject_car(participant, subscriber, "Mri_SubjectCar");
//
// for reading dnpw v2x messages
dnpw_closestVehicleMessage_distance = 99999; //initial value. 99999 means a vehicle is out of range
dnpw_closestVehicleMessage_timestamp = 0;
//emergency brake v2x
eebl_closestVehicleMessage_distance = 99999;
eebl_closestVehicleMessage_timestamp = 0;
//create reader to receive V2X message Mri_V2XfromNS3 (Mri_V2XtoNS3)
DataReader_V2XMessage reader_v2xmessage(participant, subscriber, "Mri_V2XfromNS3");
cout << "Create writer_v2xmessage..." << endl;
DataWriter_V2XMessage writer_v2xmessage(participant, publisher, "Mri_V2XtoNS3");
writer_global_v2xmessage = writer_v2xmessage.msg_writer;
// to use register veh_id unregister
DataReader_Aux2Strings reader(participant, subscriber, "Mri_Control");
DataWriter_Aux2Strings writer_aux_message(participant, publisher, "Mri_Control");
writer_global_aux2strings = writer_aux_message.msg_writer;
threadOpenDDS_initialized = true;
//----------------------------------------------------------------------------------------------------------
//cout << "It's time for loop..." << endl;
//while (key != 'q')
while (!finish_application)
{
//getInput(&key);
Sleep(100);
//log_rd_Opendds = "After sleep, ";
//keep dnpw_closestVehicleMessage_distance always up-to-date
long oldDistanceTimestamp = GetTimestamp() - 50; //500 ms
if (dnpw_closestVehicleMessage_distance != 99999)
{
if (dnpw_closestVehicleMessage_timestamp < oldDistanceTimestamp)
{
//old value, let's reset it
dnpw_closestVehicleMessage_distance = 99999;
dnpw_closestVehicleMessage_timestamp = 0;
}
}
oldDistanceTimestamp = oldDistanceTimestamp - 100;
if (eebl_closestVehicleMessage_distance != 99999)
{
if (eebl_closestVehicleMessage_timestamp < oldDistanceTimestamp)
{
//old value, let's reset it
eebl_closestVehicleMessage_distance = 99999;
eebl_closestVehicleMessage_timestamp = 0;
}
}
//if (iw_closestVehicle_time!=99999)
//{
// if (iw_closestVehicle_timestamp < oldDistanceTimestamp)
// {
// //old value, let's reset it
// iw_closestVehicle_time = 99999;
// iw_closestVehicle_timestamp = 0;
// }
//}
garbageCollectionMap();
}
}
participant->delete_contained_entities();
dpf->delete_participant(participant);
//TheServiceParticipant->shutdown();
finish_application = true;
cout << endl << "$$$$$$$$$$$$$$$$$$$$$$" << endl << "$ End of OpenDDS thread" << endl << "$$$$$$$$$$$$$$$$$$$$$$" << endl << endl;
}
catch (const CORBA::Exception& e) {
e._tao_print_exception("Exception caught in OpenDDSThread:");
}
}
void v2xMapThread() {
// thread where app receives v2x messages from NS-3
Mri::V2XMessage _v2x;
while (!finish_application)
{
Mri::VehData subjectCar1;
//Mri::VehData _veh;
BSMCoreData _vehBSM;
float distance = -1;
float timeIntersectionCollisionWarning = -1; // Intersection Warning V2X
//wait for something at the queue
v2x_queue.pop(_v2x);
{
_vehBSM = readVehDatafromString((string)_v2x.message);
subjectCar1 = getSubjectCarPosition();
if (subjectCar1.vehicle_id != _vehBSM.vehicle_id)
{
//checks distance between our subject car(human controled) and other car _veh ---------------------------------------------
distance = doNotPassWarning(subjectCar1.position_x, subjectCar1.position_y, subjectCar1.orient_heading, _vehBSM.position_x, _vehBSM.position_y, _vehBSM.orient_heading);
if (distance > 0 && distance < 160)
{
// dnpw_closestVehicleMessage_distance - variable with distance to the nearest vehicle on the opposite lane
if (distance < dnpw_closestVehicleMessage_distance)
{
//this vehicle is closer, we have to update
dnpw_closestVehicleMessage_distance = distance;
dnpw_closestVehicleMessage_timestamp = GetTimestamp();
//cout << "Warning distance=" << dnpw_closestVehicleMessage_distance << " at=" << dnpw_closestVehicleMessage_timestamp << endl;
}
}
if (_vehBSM.brake_boost == BRAKE_BOOST_ON)
{
//emergencyBrakeWarning (double h_x, double h_y, double h_h, double t_x, double t_y, double t_h)
distance = emergencyBrakeWarning(subjectCar1.position_x, subjectCar1.position_y, subjectCar1.orient_heading, _vehBSM.position_x, _vehBSM.position_y, _vehBSM.orient_heading);
if (distance > 0 && distance < 160)
{
if (distance < eebl_closestVehicleMessage_distance)
{
//this vehicle is closer, we have to update
eebl_closestVehicleMessage_distance = distance;
eebl_closestVehicleMessage_timestamp = GetTimestamp();
//cout << "Warning distance=" << dnpw_closestVehicleMessage_distance << " at=" << dnpw_closestVehicleMessage_timestamp << endl;
}
}
}
//--------------------------------------------------------------------------------------------------------------------------
//timeIntersectionCollisionWarning = intersectionWarning(subjectCar1.position_x, subjectCar1.position_y, subjectCar1.orient_heading, subjectCar1.speed,
// _vehBSM.position_x, _vehBSM.position_y, _vehBSM.orient_heading, _vehBSM.speed);
//if (timeIntersectionCollisionWarning > 0)
//{
// if (timeIntersectionCollisionWarning < iw_closestVehicle_time)
// {
// //we display the closest vehicle warning, we need to update iw_closestVehicle_time
// iw_closestVehicle_time = timeIntersectionCollisionWarning;
// iw_closestVehicle_timestamp = GetTimestamp();
// }
//}
//}
}
//TEST ONLY
//8/2/2019
/*eebl_closestVehicleMessage_distance = 92;
eebl_closestVehicleMessage_timestamp = GetTimestamp();*/
}
}
}
void garbageCollectionMap()
{
std::lock_guard<std::mutex> guard(mutexMap);
// if info about veh wasn't updated for 500 ms it means this vehicle dissapeared, so we have to delete info about this vehicle
long old_veh_timestamp = GetTimestamp() - 50; //to find veh data not updated for 50 x 10ms = 500 ms
std::map<long, std::set<UnityVehicle>> unityVehsMapSetsCopy2;
try
{
/* while (access_unityVehMapSets != ACCESS_none) {
std::this_thread::yield();
}*/
unityVehsMapSetsCopy2 = unityVehsMapSets;
if (!unityVehsMapSetsCopy2.empty()) {
for (auto x : unityVehsMapSetsCopy2)
{
std::set<UnityVehicle>::iterator it;
it = x.second.end();
--it;
if (it->timestamp < old_veh_timestamp)
{
veh_id_to_remove = x.first;
std::cout << "&&&&&&&&&&&&& REMOVE id=" << veh_id_to_remove << std::endl << std::endl;
break;
}
}
}
}
catch (const std::exception&)
{
cout << "ERROR main loop " << endl;
}
}
void publishVehDataMessage(Mri::VehData car) {
int success = writer_global_vehdata->write(car, DDS::HANDLE_NIL);
if (success == DDS::RETCODE_OK)
{
//std::cout << "Send SubjectCar position x=" << car.position_x << ", y=" << car.position_y << ", z=" << car.position_z << ", timestamp=" << car.timestamp << std::endl;
}
else
{
throw std::string("ERROR: DataWriter VehData::sendMessage write");
//ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) ERROR: Publisher::sendMessage write returned %d.\n"), success));
}
}
//-----------------------------------------------------------
// NOT used !!
//-----------------------------------------------------------
void ProcessDoNotPassWarningMessage(Mri::Aux2Strings dnpwAux)
{
float aux_distance = atof(dnpwAux.str1);
if (aux_distance < dnpw_closestVehicleMessage_distance)
{
//this vehicle is closer, we have to update
dnpw_closestVehicleMessage_distance = aux_distance;
dnpw_closestVehicleMessage_timestamp = GetTimestamp();
cout << "Warning distance=" << dnpw_closestVehicleMessage_distance << " at=" << dnpw_closestVehicleMessage_timestamp << endl;
}
}
//
//long generateV2xUniqueTimestamp(long v2x_timestamp) {
//
// long timestamp_now = GetTimestamp();
// if (timestamp_now > timestamp_previous)
// {
// sample_counter_per_timestamp = 0;
// timestamp_previous = timestamp_now;
// }
// else
// {
// sample_counter_per_timestamp++;
// }
// return ((v2x_timestamp * 1000) + sample_counter_per_timestamp);
//}
//void addV2xToMap(long extended_timestamp, Mri::V2XMessage v2x) {
//
// std::map<long, Mri::V2XMessage>::iterator it_v2x;
//
// it_v2x = v2xs_map.find(extended_timestamp);
//
// if (it_v2x != v2xs_map.end())
// {
// std::cerr << "Error - duplicated elements in v2xs_map." << std::endl;
// }
// else
// {
// v2xs_map.emplace(extended_timestamp, v2x);
// }
//}
//void transmitV2X(Mri::V2XMessage v2x, long destination_veh_id) {
//
// const double V2X_RANGE = 300.0; //150 meters
//
// try
// {
// if (v2x.sender_id != destination_veh_id)
// {
// Mri::VehData veh_sender = vehs_map[v2x.sender_id];
// Mri::VehData veh_receiver = vehs_map[destination_veh_id];
//
//
//
// double distance = sqrt(pow((veh_receiver.position_x - veh_sender.position_x), 2.0) + pow((veh_receiver.position_y - veh_sender.position_y), 2.0));
//
// if (distance < V2X_RANGE)
// {
// //transmit
// long delay = rand() % 50 + 15; // delay 15 - 64 ms
// //update data in v2x message
//
// v2x.recipient_id = destination_veh_id;
// v2x.recipient_timestamp = v2x.sender_timestamp + (delay / 10);
//
// // resend v2x
//
//
// publishV2xMessage(v2x);
//
// }
// else
// {
// v2x.recipient_id = destination_veh_id;
// std::cout << endl << "-- NO TRANSMITION from veh_id:" << v2x.sender_id << " to veh_id:" << v2x.recipient_id << " timestamps: "
// << v2x.sender_timestamp << " distance=" << distance << std::endl;
// }
// }
//
//
//
// }
// catch (const std::exception&)
// {
// //do nothing
// //transmit nothing
// }
//
//}
//void vehsMapThread() {
//
//
// Mri::VehData _veh;
//
// //std::map<long, Mri::VehData> vehs_map;
// std::map<long, Mri::VehData>::iterator it;
//
// veh_id_to_remove = -1; // initial value
//
//
// std::cout << "*************** vehsMapThread " << std::endl;
//
// while (!finish_application)
// {
// //wait for something at the queue
// vehdata_queue_in.pop(_veh);
// {
// it = vehs_map.find(_veh.vehicle_id);
// if (it != vehs_map.end()) {
// //there is a car with id = _veh.vehicle_id
//
//
// //check if new data is older than in vehs_map
// if (_veh.timestamp > it->second.timestamp)
// {
// //update data in vehs_map
// vehs_map[it->first] = _veh;
// }
// else
// {
// cout << "OpenDDS data is older than data in vehs_list. Veh_id=" << _veh.vehicle_id
// << " timestamp_vehs_list=" << it->second.timestamp
// << " timestamp_OpenDDS=" << _veh.timestamp << endl;
// }
// }
// else
// {
// //no car with id = _veh.vehicle_id
// vehs_map.emplace(_veh.vehicle_id, _veh);
//
// }
//
// //check if we have to remove a car
// if (veh_id_to_remove!=-1)
// {
// it = vehs_map.find(veh_id_to_remove);
// if (it != vehs_map.end())
// {
// vehs_map.erase(it);
// cout << endl << "####################################################################################" << endl;
// cout << "############ Removed from vehsMap vehicle_id =" << veh_id_to_remove << " ############" << endl<<endl;
// veh_id_to_remove = -1; //reset this variable
// }
// }
//
// }
// }
//}
//
//bool getInput(char *c)
//{
// if (_kbhit())
// {
// *c = _getch();
// return true; // Key Was Hit
// }
// return false; // No keys were pressed
//}