-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLDMmap.cpp
More file actions
executable file
·411 lines (332 loc) · 14.1 KB
/
Copy pathLDMmap.cpp
File metadata and controls
executable file
·411 lines (332 loc) · 14.1 KB
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
#include "LDMmap.h"
#include "utils.h"
#include <cmath>
#include <iostream>
#define DEFINE_KEYS(k_up,k_low,stationID) \
uint32_t k_low = stationID & 0x0000FFFF; \
uint32_t k_up = (stationID & 0xFFFF0000) >> 16;
#define COMPOSE_KEYS(k_up,k_low) (((uint64_t) k_up) << 16) + k_low;
#define DEG_2_RAD_LDM(val) ((val)*M_PI/180.0)
namespace ldmmap
{
static inline double haversineDist(double lat_a, double lon_a, double lat_b, double lon_b) {
// 12742000 is the mean Earth radius (6371 km) * 2 * 1000 (to convert from km to m)
return 12742000.0*asin(sqrt(sin(DEG_2_RAD_LDM(lat_b-lat_a)/2)*sin(DEG_2_RAD_LDM(lat_b-lat_a)/2)+cos(DEG_2_RAD_LDM(lat_a))*cos(DEG_2_RAD_LDM(lat_b))*sin(DEG_2_RAD_LDM(lon_b-lon_a)/2)*sin(DEG_2_RAD_LDM(lon_b-lon_a)/2)));
}
LDMMap::LDMMap() {
m_card = 0;
m_central_lat = 0.0;
m_central_lon = 0.0;
m_ego_lon = 0.0;
m_ego_lat = 0.0;
}
LDMMap::~LDMMap() {
clear();
}
LDMMap::LDMMap_error_t
LDMMap::insert(vehicleData_t newVehicleData) {
LDMMap_error_t retval;
DEFINE_KEYS(key_upper,key_lower,newVehicleData.stationID);
if(m_card==UINT64_MAX) {
return LDMMAP_MAP_FULL;
}
std::shared_mutex *mapmutexptr;
std::unordered_map<uint32_t,returnedVehicleData_t> lowerMap;
if(m_ldmmap.count(key_upper) == 0) {
mapmutexptr = new std::shared_mutex();
lowerMap[key_lower].vehData = newVehicleData;
lowerMap[key_lower].phData = new PHpoints();
lowerMap[key_lower].kf = new KalmanFilter(0.05);
lowerMap[key_lower].kf->init_step(newVehicleData);
lowerMap[key_lower].vehData.lastCPMincluded = 0;
std::lock_guard<std::shared_mutex> lk(m_mainmapmut);
m_ldmmap[key_upper] = std::pair<std::shared_mutex*,std::unordered_map<uint32_t,returnedVehicleData_t>>(mapmutexptr,lowerMap);
m_card++;
retval = LDMMAP_OK;
} else {
if(m_ldmmap[key_upper].second.count(key_lower) == 0) {
m_card++;
retval = LDMMAP_OK;
} else {
retval = LDMMAP_UPDATED;
}
std::lock_guard<std::shared_mutex> lk(*m_ldmmap[key_upper].first);
m_ldmmap[key_upper].second[key_lower].vehData = newVehicleData;
// INSERT operation -> create a new PHpoints object
if(retval == LDMMAP_OK) {
m_ldmmap[key_upper].second[key_lower].phData = new PHpoints();
m_ldmmap[key_upper].second[key_lower].kf = new KalmanFilter(0.04);
m_ldmmap[key_upper].second[key_lower].kf->init_step(newVehicleData);
m_ldmmap[key_upper].second[key_lower].vehData.lastCPMincluded = 0;
}
}
// std::cout << "Updating vehicle: " << newVehicleData.stationID << std::endl;
m_ldmmap[key_upper].second[key_lower].phData->insert(newVehicleData);
if(newVehicleData.detected)
{
//std::cout << "[LDM] KF step for object " << newVehicleData.stationID << std::endl;
auto ego_data=m_gpsc_ptr->getCAMMandatoryData();
if (ego_data.latitude!= Latitude_unavailable && ego_data.longitude!= Longitude_unavailable) {
m_ldmmap[key_upper].second[key_lower].kf->set_ego_position((double) ego_data.longitude / DOT_ONE_MICRO
,(double) ego_data.latitude / DOT_ONE_MICRO);
}
m_ldmmap[key_upper].second[key_lower].kf->update(((double) newVehicleData.xDistance)/CENTI, ((double) newVehicleData.yDistance)/CENTI, m_ego_lon);
m_ldmmap[key_upper].second[key_lower].vehData.lat = m_ldmmap[key_upper].second[key_lower].kf->get_state().lat;
m_ldmmap[key_upper].second[key_lower].vehData.lon = m_ldmmap[key_upper].second[key_lower].kf->get_state().lon;
}
return retval;
}
LDMMap::LDMMap_error_t
LDMMap::remove(uint64_t stationID) {
DEFINE_KEYS(key_upper,key_lower,stationID);
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
if(m_ldmmap.count(key_upper) == 0) {
return LDMMAP_ITEM_NOT_FOUND;
} else {
if(m_ldmmap[key_upper].second.count(key_lower) == 0) {
return LDMMAP_ITEM_NOT_FOUND;
} else {
std::lock_guard<std::shared_mutex> lk(*m_ldmmap[key_upper].first);
m_ldmmap[key_upper].second.erase(stationID);
m_card--;
}
}
return LDMMAP_OK;
}
LDMMap::LDMMap_error_t
LDMMap::lookup(uint64_t stationID,returnedVehicleData_t &retVehicleData) {
DEFINE_KEYS(key_upper,key_lower,stationID);
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
if(m_ldmmap.count(key_upper) == 0) {
return LDMMAP_ITEM_NOT_FOUND;
} else {
if(m_ldmmap[key_upper].second.count(key_lower) == 0) {
return LDMMAP_ITEM_NOT_FOUND;
} else {
std::shared_lock<std::shared_mutex> lk(*m_ldmmap[key_upper].first);
retVehicleData = m_ldmmap[key_upper].second[key_lower];
}
}
return LDMMAP_OK;
}
LDMMap::LDMMap_error_t
LDMMap::rangeSelect(double range_m, double lat, double lon, std::vector<returnedVehicleData_t> &selectedVehicles) {
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto const& [key, val] : m_ldmmap) {
// Iterate over the single lower maps
val.first->lock_shared();
for(auto const& [keyl, vall] : val.second) {
if(haversineDist(lat,lon,vall.vehData.lat,vall.vehData.lon)<=range_m) {
selectedVehicles.push_back(vall);
}
}
val.first->unlock_shared();
}
return LDMMAP_OK;
}
LDMMap::LDMMap_error_t
LDMMap::rangeSelect(double range_m, uint64_t stationID, std::vector<returnedVehicleData_t> &selectedVehicles) {
returnedVehicleData_t retData;
// Get the latitude and longitude of the speficied vehicle
if(lookup(stationID,retData)!=LDMMAP_OK) {
return LDMMAP_ITEM_NOT_FOUND;
}
// Perform a rangeSelect() centered on that latitude and longitude values
return rangeSelect(range_m,retData.vehData.lat,retData.vehData.lon,selectedVehicles);
}
void
LDMMap::deleteOlderThan(double time_milliseconds) {
uint64_t now = get_timestamp_us();
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto& [key, val] : m_ldmmap) {
// Iterate over the single lower maps
val.first->lock();
for (auto mit=val.second.cbegin();mit!=val.second.cend();) {
//double time_diff = ((double)(now-mit->second.vehData.timestamp_us))/1000.0;
//std::cout << "[LDM] Delete older than: Time difference for object "<< mit->second.vehData.stationID << " : " << time_diff << std::endl;
if(((double)(now-mit->second.vehData.timestamp_us))/1000.0 > time_milliseconds) {
mit = val.second.erase(mit);
m_card--;
} else {
++mit;
}
}
val.first->unlock();
}
}
void
LDMMap::deleteOlderThanAndExecute(double time_milliseconds,void (*oper_fcn)(uint64_t,void *),void *additional_args) {
uint64_t now = get_timestamp_us();
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto& [key, val] : m_ldmmap) {
// Iterate over the single lower maps
val.first->lock();
for (auto mit=val.second.cbegin();mit!=val.second.cend();) {
if(((double)(now-mit->second.vehData.timestamp_us))/1000.0 > time_milliseconds) {
// With respect to deleteOlderThan(), this function will also call oper_fcn() for each deleted entry
oper_fcn(mit->second.vehData.stationID,additional_args);
mit = val.second.erase(mit);
m_card--;
} else {
++mit;
}
}
val.first->unlock();
}
}
void
LDMMap::clear() {
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto& [key, val] : m_ldmmap) {
// Iterate over the single lower maps and clear them
// Delete also the shared mutex objects
for(auto const& [keyl, vall] : val.second) {
vall.phData->clear();
delete vall.phData;
}
val.second.clear();
delete val.first;
}
// Clear the upper map
m_ldmmap.clear();
// Set the cardinality of the map to 0 again
m_card = 0;
}
void
LDMMap::printAllContents(std::string label) {
std::cout << "[" << label << "] Vehicle IDs: ";
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto const& [key, val] : m_ldmmap) {
// Iterate over the single lower maps
val.first->lock_shared();
for(auto const& [keyl, vall] : val.second) {
std::cout << vall.vehData.stationID << ", ";
}
val.first->unlock_shared();
}
std::cout << std::endl;
}
void
LDMMap::executeOnAllContents(void (*oper_fcn)(vehicleData_t,void *),void *additional_args) {
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto const& [key, val] : m_ldmmap) {
// Iterate over the single lower maps
val.first->lock_shared();
for(auto const& [keyl, vall] : val.second) {
// Execute the callback for every entry
oper_fcn(vall.vehData,additional_args);
}
val.first->unlock_shared();
}
}
LDMMap::LDMMap_error_t
LDMMap::updateCPMincluded(vehicleData_t newVehicleData) {
DEFINE_KEYS(key_upper,key_lower,newVehicleData.stationID);
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
if(m_ldmmap.count(key_upper) == 0) {
return LDMMAP_ITEM_NOT_FOUND;
} else {
if(m_ldmmap[key_upper].second.count(key_lower) == 0) {
return LDMMAP_ITEM_NOT_FOUND;
} else {
std::shared_lock<std::shared_mutex> lk(*m_ldmmap[key_upper].first);
m_ldmmap[key_upper].second[key_lower].phData->insertCPMincluded(newVehicleData);
m_ldmmap[key_upper].second[key_lower].vehData.lastCPMincluded = newVehicleData.timestamp_us;
}
}
return LDMMAP_OK;
}
LDMMap::LDMMap_error_t
LDMMap::getAllPOs(std::vector<returnedVehicleData_t> &selectedVehicles) {
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto const& [key, val] : m_ldmmap) {
// Iterate over the single lower maps
val.first->lock_shared();
for(auto const& [keyl, vall] : val.second) {
if(vall.vehData.detected){
selectedVehicles.push_back(vall);
}
}
val.first->unlock_shared();
}
return LDMMAP_OK;
}
LDMMap::LDMMap_error_t
LDMMap::getAllIDs(std::set<uint64_t> &selectedIDs) {
std::shared_lock<std::shared_mutex> lk(m_mainmapmut);
for(auto const& [key, val] : m_ldmmap) {
// Iterate over the single lower maps
val.first->lock_shared();
for(auto const& [keyl, vall] : val.second) {
selectedIDs.insert(vall.vehData.stationID);
}
val.first->unlock_shared();
}
return LDMMAP_OK;
}
// Update ego position for a vehicle or pedestrian - this function inserts the ego road user in the LDM
// Two station types are supported for the time being: StationType_passengerCar and StationType_pedestrian
// Other station types will make the function not add anything to the LDM and return LDMMAP_INVALID_STATIONTYPE
LDMMap::LDMMap_error_t LDMMap::updateEgoPosition(StationType_t stationType) {
if (m_gpsc_ptr == nullptr) {
return LDMMAP_NO_VDP;
}
if(stationType==StationType_passengerCar) {
auto ego_data=m_gpsc_ptr->getCAMMandatoryData();
ldmmap::vehicleData_t vehdata;
ldmmap::LDMMap::LDMMap_error_t db_retval;
if (ego_data.latitude!= Latitude_unavailable && ego_data.longitude!= Longitude_unavailable) {
vehdata.detected = false;
vehdata.lat = (double) ego_data.latitude / DOT_ONE_MICRO;
vehdata.lon = (double) ego_data.longitude / DOT_ONE_MICRO;
m_ego_lon = vehdata.lon;
m_ego_lat = vehdata.lat;
vehdata.elevation = (double) ego_data.altitude.getValue() / CENTI;
vehdata.timestamp_us = get_timestamp_us();
vehdata.stationType = StationType_LDM_passengerCar;
if (ego_data.heading.getValue() == HeadingValue_unavailable) {
vehdata.heading = LDM_HEADING_UNAVAILABLE;
} else {
vehdata.heading = (double) ego_data.heading.getValue() / DECI;
}
vehdata.speed_ms = (double) ego_data.speed.getValue() / CENTI;
vehdata.stationID = m_station_id;
vehdata.vehicleWidth = ldmmap::OptionalDataItem<long>((long) ego_data.VehicleWidth / DECI);
vehdata.vehicleLength = ldmmap::OptionalDataItem<long>((long) ego_data.VehicleLength.getValue() / DECI);
db_retval = this->insert(vehdata);
if ((db_retval != LDMMAP_OK) && (db_retval != LDMMAP_UPDATED)) {
return db_retval;
}
//std::cout << "[LDM] Ego Position: [" << vehdata.lat << ", " << vehdata.lon << "], heading: " << vehdata.heading << ", speed: " << vehdata.speed_ms << " m/s" << std::endl;
}
} else if(stationType==StationType_pedestrian) {
auto ego_data=m_gpsc_ptr->getVAMMandatoryData();
ldmmap::vehicleData_t vehdata;
ldmmap::LDMMap::LDMMap_error_t db_retval;
if (ego_data.latitude!= Latitude_unavailable && ego_data.longitude!= Longitude_unavailable) {
vehdata.detected = false;
vehdata.lat = (double) ego_data.latitude / DOT_ONE_MICRO;
vehdata.lon = (double) ego_data.longitude / DOT_ONE_MICRO;
m_ego_lon = vehdata.lon;
m_ego_lat = vehdata.lat;
vehdata.elevation = (double) ego_data.altitude.getValue() / CENTI;
vehdata.timestamp_us = get_timestamp_us();
vehdata.stationType = StationType_LDM_pedestrian;
if (ego_data.heading.getValue() == HeadingValue_unavailable) {
vehdata.heading = LDM_HEADING_UNAVAILABLE;
} else {
vehdata.heading = (double) ego_data.heading.getValue() / DECI;
}
vehdata.speed_ms = (double) ego_data.speed.getValue() / CENTI;
vehdata.stationID = m_station_id;
db_retval = this->insert(vehdata);
if ((db_retval != LDMMAP_OK) && (db_retval != LDMMAP_UPDATED)) {
return db_retval;
}
}
} else {
return LDMMAP_INVALID_STATIONTYPE;
}
return LDMMAP_OK;
}
}