-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsensor_fusion_and_monitors.cpp
1640 lines (1235 loc) · 49.6 KB
/
sensor_fusion_and_monitors.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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
Created on Thu Dec 8 16:45:21 2019
@author: Carlos Gomez-Huelamo
Code to process the fusion between LiDAR clusters and BEV (Bird's Eye View)
Object Tracking using the Global Nearest Neighbour (GNN) approach and evaluate the objects according
to some specified behaviours (ACC, Give Way, STOP, etc.)
Inputs: BEV Object Tracking, LiDAR pointclud and Monitorized Lanes
Outputs: Evaluated behaviours
SmartElderlyCar (SEC) - Tech4AgeCar (T4AC) project
Simulation
***/
// Includes //
// General purpose includes
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
#include <string.h>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <vector>
// ROS includes
#include <ros/ros.h>
#include <geodesy/utm.h>
#include <geodesy/wgs84.h>
#include <geographic_msgs/GeoPoint.h>
#include <geometry_msgs/PoseWithCovarianceStamped.h>
#include <geometry_msgs/PointStamped.h>
#include <geometry_msgs/Point.h>
#include <geometry_msgs/Point32.h>
#include <geometry_msgs/Vector3.h>
#include <geometry_msgs/Quaternion.h>
#include <geometry_msgs/Pose.h>
#include <geometry_msgs/PoseStamped.h>
#include <geometry_msgs/Transform.h>
#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/exact_time.h>
#include <message_filters/sync_policies/approximate_time.h>
#include "nav_msgs/OccupancyGrid.h"
#include "nav_msgs/Path.h"
#include <sensor_msgs/point_cloud_conversion.h>
#include <sensor_msgs/PointCloud2.h>
#include <std_msgs/ColorRGBA.h>
#include <std_msgs/Int64.h>
#include <std_msgs/Float64.h>
#include <std_msgs/Time.h>
#include "tf/tf.h"
#include "tf/transform_listener.h"
#include <tf/transform_broadcaster.h>
#include <visualization_msgs/Marker.h>
#include <visualization_msgs/MarkerArray.h>
// BEV Tracking includes
#include "t4ac_msgs/BEV_detections_list.h"
#include "t4ac_msgs/BEV_trackers_list.h"
// SEC (SmartElderlyCar) includes
#include <sec_msgs/Route.h>
#include <sec_msgs/Lanelet.h>
#include <sec_msgs/RegElem.h>
#include <sec_msgs/Distance.h>
#include <sec_msgs/CarControl.h>
#include <sec_msgs/ObstacleArray.h>
#include "map_manager_base.hpp"
// End Includes //
// Defines //
#define lanelet_filter 0
#define PI 3.1415926
#define THMIN 10.0
#define THMAX 70.0
#define SENSOR_HEIGHT 1.73 // LiDAR
#define CAR_LENGTH 3.7
#define CAR_WIDTH 1.7
#define CAR_HEIGHT 1.5
#define PEDESTRIAN_LENGTH 0.6
#define PEDESTRIAN_WIDTH 0.6
#define PEDESTRIAN_HEIGHT 1.85
#define MIN_MONITOR(x,y) (x < y ? x : y) // Returns x if x less than y. Otherwise, returns y
#define MAX_MONITOR(x,y) (x > y ? x : y) // Returns x if x greater than y. Otherwise, returns y
#define INSIDE 0
#define OUTSIDE 1
// End Defines //
// Structures //
typedef struct
{
double x; // x UTM with respect to the map origin
double y; // y UTM with respect to the map origin
}Area_Point;
typedef struct
{
float centroid_x; // Local centroid (with respect to the "base_link" frame)
float centroid_y;
float global_centroid_x; // Global centroid (with respect to the "map" frame)
float global_centroid_y;
float l;
float w;
float h;
double orientation;
string type;
double time;
}Object;
typedef struct
{
float centroid_x; // Local centroid (with respect to the "base_link" frame)
float centroid_y;
float global_centroid_x; // Global centroid (with respect to the "map" frame)
float global_centroid_y;
float l;
float w;
float h;
double orientation;
string type;
int object_id;
double time;
int pedestrian_state;
int stop_state;
int give_way_state;
}Tracked_Object;
// End Structures //
// ROS communication //
// ROS Publishers
ros::Publisher pub_Tracked_Obstacles_Marker;
ros::Publisher pub_LiDAR_Obstacles_Marker;
ros::Publisher pub_VOT_Obstacles_Marker;
ros::Publisher pub_Detected_Pedestrian;
ros::Publisher pub_Safe_Merge;
ros::Publisher pub_Front_Car;
ros::Publisher pub_Front_Car_Distance;
ros::Publisher pub_Safe_Lane_Change;
ros::Publisher pub_Distance_Overtake;
// ROS Subscribers
ros::Subscriber monitorizedlanes_sub; // Monitorized lanelets
ros::Subscriber route_sub; // Route
ros::Subscriber waiting_sub; // Empty message waiting (STOP behaviour)
ros::Subscriber test_1_sub;
ros::Subscriber test_2_sub;
ros::Subscriber test_3_sub;
// End ROS communication //
// Global variables //
// Transform variables
tf::StampedTransform TF_map2base_link;
tf::TransformListener *listener;
// SEC variables
sec_msgs::Route pedestrian_crossing_lanelets;
sec_msgs::Route merging_lanelets; // Merging role lanelets
sec_msgs::Route route_lanelets; // Monitorized lanelets that are in the planified route
sec_msgs::Route left_lanelets; // Left lanelets of the planified route
sec_msgs::Route right_lanelets; // Right lanelets of the planified route
sec_msgs::Route route_left_lanelets;
sec_msgs::Route route_right_lanelets;
sec_msgs::Route all_lefts;
sec_msgs::Route all_rights;
sec_msgs::Route route; // Received route
sec_msgs::Route monitorized_lanelets; // Important lanelet for each use case (STOP, give way, etc.)
sec_msgs::ObstacleArray Obstacles;
sec_msgs::Obstacle current_obstacle;
sec_msgs::RegElem current_regulatory_element;
// Monitors
bool merging_monitor;
bool pedestrian_crossing_monitor;
int stop = 0; // 0 = Inactive, 1 Active (Car cannot cross the STOP), 2 Merging monitor (Car can cross the stop if merging monitor allows)
std_msgs::Bool lane_change;
int id_lanelet_pedestrian_crossing = 0;
int id_lanelet_merging = 0;
int global_pedestrian_crossing_occupied;
int merging_occupied;
double persistence_time=2.0; // Maximum difference in time to not delete an object
// Visualization variables
// Namespaces //
using namespace std;
// End Namespaces //
// Geographic variables
geodesy::UTMPoint odomUTMmsg;
nav_msgs::Odometry previous_odom;
geodesy::UTMPoint utm_origin;
double lat_origin, lon_origin;
shared_ptr<LaneletMap> loadedMap;
nav_msgs::OccupancyGrid localMap;
geographic_msgs::GeoPoint geo_origin;
// General purpose variables
vector<std_msgs::ColorRGBA> colours;
vector<Tracked_Object> tracked_objects;
Area_Point polygon_area[] = {0,0,
0,0,
0,0,
0,0};
int Number_of_sides = 4; // Of the area you have defined. Here is a rectangle, so area[] has four rows
// End Global variables
// Declarations of functions //
// General use functions
geometry_msgs::Point32 Local_To_Global_Coordinates(geometry_msgs::PointStamped );
void Inside_Polygon(Area_Point *, int , Area_Point, bool &);
// Calbacks
void route_cb(const sec_msgs::Route::ConstPtr& );
void waiting_cb(const std_msgs::Empty );
void test_1_cb(const t4ac_msgs::BEV_trackers_list::ConstPtr& );
void test_2_cb(const t4ac_msgs::BEV_detections_list::ConstPtr& );
void test_3_cb(const nav_msgs::Odometry::ConstPtr& );
void regelement_cb(const sec_msgs::RegElem::ConstPtr& , const sec_msgs::Route::ConstPtr& );
void sensor_fusion_and_monitors_cb(const t4ac_msgs::BEV_detections_list::ConstPtr& , const t4ac_msgs::BEV_trackers_list::ConstPtr& , const nav_msgs::Odometry::ConstPtr& );
// End Declarations of functions //
// Main //
int main (int argc, char ** argv)
{
// Initialize ROS
ros::init(argc, argv, "sensor_fusion_and_monitors_node");
ros::NodeHandle nh;
// Map origin latitude and longitude by parameters
nh.param<double>("/lat_origin",lat_origin,40.5126566);
nh.param<double>("/lon_origin",lon_origin,-3.34460735);
// Initialize origin of the map
geographic_msgs::GeoPoint geo_origin;
geo_origin.latitude = lat_origin;
geo_origin.longitude = lon_origin;
geo_origin.altitude = 0;
utm_origin = geodesy::UTMPoint(geo_origin); // Convert to geodesy::UTMPoint
// Transform listener
listener = new tf::TransformListener(ros::Duration(5.0));
// Publishers //
pub_Detected_Pedestrian = nh.advertise<std_msgs::Bool>("/pedestrian",1);
pub_Safe_Merge = nh.advertise<std_msgs::Bool>("/safeMerge",1);
pub_Front_Car = nh.advertise<sec_msgs::Obstacle>("/frontCarCurrentLane", 1, true);
pub_Front_Car_Distance = nh.advertise<std_msgs::Float64>("/frontCarCurrentLane_distance", 1, true);
pub_Safe_Lane_Change = nh.advertise<std_msgs::Bool>("/safeLaneChange",1);
pub_Distance_Overtake = nh.advertise<sec_msgs::Distance>("/distOvertake", 1);
// Tracking publishers
pub_Tracked_Obstacles_Marker = nh.advertise<visualization_msgs::MarkerArray>("/t4ac/perception/detection/Tracked_Obstacles", 1, true); // Merged (Tracked obstacles)
pub_LiDAR_Obstacles_Marker = nh.advertise<visualization_msgs::MarkerArray>("/t4ac/perception/detection/LiDAR_Obstacles", 1, true); // Only LiDAR
pub_VOT_Obstacles_Marker = nh.advertise<visualization_msgs::MarkerArray>("/t4ac/perception/detection/VOT_BEV_Obstacles", 1, true); // Only vision
// End Publishers //
// Subscribers //
message_filters::Subscriber<sec_msgs::RegElem> regelem_sub_; // Regulatory elements of current monitorized lanelets
message_filters::Subscriber<sec_msgs::Route> regelemLanelet_sub_; // Monitorized lanelets
message_filters::Subscriber<sec_msgs::Distance> regelemDist_sub_; // Distance to regulatory elements
message_filters::Subscriber<sensor_msgs::PointCloud2> cloud_sub_; // Coloured LiDAR point cloud
message_filters::Subscriber<sensor_msgs::PointCloud2> velodyne_cloud_sub_; // LiDAR point cloud
message_filters::Subscriber<nav_msgs::Odometry> ego_vehicle_pose_sub_; // Odometry
message_filters::Subscriber<t4ac_msgs::BEV_trackers_list> projected_vot_sub_; // Detection and Tracking with camera (CenterNet + Deep Sort)
message_filters::Subscriber<t4ac_msgs::BEV_detections_list> lidar_detections_sub_; // LiDAR detections
regelem_sub_.subscribe(nh, "/currentRegElem", 1);
regelemLanelet_sub_.subscribe(nh, "/monitorizedLanelets", 1);
velodyne_cloud_sub_.subscribe(nh, "/velodyne_points", 1);
ego_vehicle_pose_sub_.subscribe(nh, "/localization/pose", 1);
lidar_detections_sub_.subscribe(nh, "/t4ac/perception/detection/bev_detections", 1);
projected_vot_sub_.subscribe(nh, "/t4ac/perception/tracked_obstacles", 1);
/*test_1_sub = nh.subscribe<t4ac_msgs::BEV_trackers_list>("/t4ac/perception/tracked_obstacles", 1, &test_1_cb);
test_2_sub = nh.subscribe<t4ac_msgs::BEV_detections_list>("/t4ac/perception/detection/bev_detections", 1, &test_2_cb);
test_3_sub = nh.subscribe<nav_msgs::Odometry>("/localization/pose", 1, &test_3_cb);*/
waiting_sub = nh.subscribe<std_msgs::Empty>("/waitingAtStop", 1, &waiting_cb);
route_sub = nh.subscribe<sec_msgs::Route>("/route", 1, &route_cb);
// End Subscribers //
// Callbacks //
// Callback 1: Synchonize monitorized lanelets and current regulatory element (Exact time)
typedef message_filters::sync_policies::ExactTime<sec_msgs::RegElem, sec_msgs::Route> MySyncPolicy;
message_filters::Synchronizer<MySyncPolicy> sync_(MySyncPolicy(10), regelem_sub_, regelemLanelet_sub_);
sync_.registerCallback(boost::bind(®element_cb, _1, _2));
// Callback 2: Synchronize LiDAR point cloud and camera information (including detection and tracking). Evaluate monitors (Approximate time)
typedef message_filters::sync_policies::ApproximateTime<t4ac_msgs::BEV_detections_list, t4ac_msgs::BEV_trackers_list, nav_msgs::Odometry> MySyncPolicy2;
message_filters::Synchronizer<MySyncPolicy2> sync2_(MySyncPolicy2(100), lidar_detections_sub_, projected_vot_sub_, ego_vehicle_pose_sub_);
sync2_.registerCallback(boost::bind(&sensor_fusion_and_monitors_cb, _1, _2, _3));
// Load map
string map_frame = "";
string map_path = ros::package::getPath("sec_map_manager") + "/maps/uah_lanelets_v42.osm";
nh.param<string>("/map_path", map_path, map_path);
loadedMap = make_shared<LaneletMap>(map_path);
// Initialize colours
std_msgs::ColorRGBA colour;
// 0
colour.a=1.0;
colour.r=1.0;
colour.g=0.0;
colour.b=0.0;
colours.push_back(colour);
// 1
colour.a=1.0;
colour.r=0.0;
colour.g=1.0;
colour.b=0.0;
colours.push_back(colour);
// 2
colour.a=1.0;
colour.r=0.0;
colour.g=0.0;
colour.b=1.0;
colours.push_back(colour);
// 3
colour.a=1.0;
colour.r=1.0;
colour.g=1.0;
colour.b=0.0;
colours.push_back(colour);
// 4
colour.a=1.0;
colour.r=1.0;
colour.g=0.0;
colour.b=1.0;
colours.push_back(colour);
// 5
colour.a=1.0;
colour.r=0.0;
colour.g=1.0;
colour.b=1.0;
colours.push_back(colour);
// 6
colour.a=1.0;
colour.r=0.5;
colour.g=0.0;
colour.b=0.0;
colours.push_back(colour);
// 7
colour.a=1.0;
colour.r=0.0;
colour.g=0.5;
colour.b=0.0;
colours.push_back(colour);
// 8
colour.a=1.0;
colour.r=0.0;
colour.g=0.0;
colour.b=0.5;
colours.push_back(colour);
// 9
colour.a=1.0;
colour.r=0.5;
colour.g=0.5;
colour.b=0.0;
colours.push_back(colour);
// 3D viewer configuration
/*if (VIEWER_3D)
{
viewer->setBackgroundColor (0.0, 0.0, 0.0);
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
viewer->setCameraPosition (-10, 0, 5, 0.3, 0, 0.95);
}*/
// ROS Spin
ros::spin ();
}
// End Main //
// Definitions of functions //
// General use functions
geometry_msgs::Point32 Local_To_Global_Coordinates(geometry_msgs::PointStamped point_local)
{
// Parameters:
// point_global: geometry_msgs::PointStamped point in global coordinate (with respect to the "map" frame)
// Returns this point in local coordinates (with respect to the "base_link" frame)
tf::Vector3 aux, aux2;
geometry_msgs::PointStamped point_global;
geometry_msgs::Point32 point32_global;
aux.setX(point_local.point.x);
aux.setY(point_local.point.y);
aux.setZ(point_local.point.z);
aux2 = TF_map2base_link * aux;
point_global.point.x = aux2.getX();
point_global.point.y = aux2.getY();
point_global.point.z = aux2.getZ();
point32_global.x = point_global.point.x;
point32_global.y = point_global.point.y;
point32_global.z = point_global.point.z;
return(point32_global);
}
// Check if a point is inside a certain area (https://jsbsan.blogspot.com/2011/01/saber-si-un-punto-esta-dentro-o-fuera.html)
void Inside_Polygon(Area_Point *polygon, int N, Area_Point p, bool &detection)
{
// Parameters:
// polygon: Pointer that points to the beggining of the array (Area_point type) that contains the defined area vertices
// N: Number of vertices of the area
// p: Detected point to be evaluated if is inside this area or not
// detection: Boolean variable
// Returns 1 if p is inside the defined area, or 0 in the contrary case
int counter = 0;
double xinters;
Area_Point p1, p2;
p1 = polygon[0]; // = *(polygon+0). Note that polygon is a pointer that points to the beggining of that array.
for (int i=1; i<=N; i++)
{
p2 = polygon[i%N];
if ((p.y > MIN_MONITOR(p1.y,p2.y)) && (p.y <= MAX_MONITOR(p1.y,p2.y)) && (p.x <= MAX_MONITOR(p1.x,p2.x)) && (p1.y != p2.y))
{
xinters = p1.x + (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y);
if ((p1.x == p2.x) || (p.x <= xinters))
{
counter++;
}
}
p1 = p2; // p1 is updated
}
if (counter % 2 == 0) // even number of intersections -> The point is outside
{
detection = false;
}
else
{
detection = true;
}
}
// Callbacks //
// Callback to store the full path
void route_cb(const sec_msgs::Route::ConstPtr& route_msg)
{
route = *route_msg;
}
// Callback to allow the car continue (If stop signal is received (Ego-vehicle is stopped in a STOP regulatory element))
void waiting_cb(const std_msgs::Empty msg)
{
stop = 2;
}
void test_1_cb(const t4ac_msgs::BEV_trackers_list::ConstPtr& msg)
{
double time = msg->header.stamp.toSec();
cout << "VOT projected: " << time << endl;
}
void test_2_cb(const t4ac_msgs::BEV_detections_list::ConstPtr& msg)
{
double time = msg->header.stamp.toSec();
cout << "LiDAR detections: " << time << endl;
}
void test_3_cb(const nav_msgs::Odometry::ConstPtr& msg)
{
double time = msg->header.stamp.toSec();
cout << "Pose: " << time << endl;
}
// Callback to extract information of the regeleme and monitorized_lanelets_msg topics
void regelement_cb(const sec_msgs::RegElem::ConstPtr& regelem, const sec_msgs::Route::ConstPtr& monitorized_lanelets_msg)
{
// Parameters:
// regelem: Pointer to the information of the /currentRegElem topic
// monitorized_lanelets_msg: Pointer to the information of the /monitorizedLanelets topic
// Global variables
monitorized_lanelets = *monitorized_lanelets_msg;
current_regulatory_element = *regelem;
// Initialize use cases lanelets
pedestrian_crossing_lanelets.route.clear();
merging_lanelets.route.clear();
route_lanelets.route.clear();
// TODO: DELETE THESE VARIABLES?
left_lanelets.route.clear();
right_lanelets.route.clear();
route_left_lanelets.route.clear();
route_right_lanelets.route.clear();
all_lefts.route.clear();
all_rights.route.clear();
// Initialize pedestrian crossing and merging monitors
merging_monitor = false;
pedestrian_crossing_monitor = false;
// Evaluate the current regulatory elements and monitorized lanelets
// Pedestrian crossing
if (!strcmp(regelem->type.c_str(),"pedestrian_crossing"))
{
if (regelem->distance<30) // It the pedestrian crossing is within 30 m
{
// Activate pedestrian crossing monitor
pedestrian_crossing_monitor = true;
if (regelem->laneletID != id_lanelet_pedestrian_crossing)
{
global_pedestrian_crossing_occupied = 0; // We are in a new pedestrian crossing, so the presence of pedestrians must be reevaluated
}
// Store current regulatory element ID, which corresponds to a pedestrian crossing
id_lanelet_pedestrian_crossing = regelem->laneletID;
// Store pedestrian crossing lanelets
for (int i=0; i<monitorized_lanelets.route.size(); i++)
{
//cout<<"\nType: "<<monitorized_lanelets.route[i].type.c_str()<<endl;
/*if (!strcmp(monitorized_lanelets.route[i].type.c_str()," pedestrian_crossing"))
{
pedestrian_crossing_lanelets.route.push_back(monitorized_lanelets.route[i]);
}*/
string type = monitorized_lanelets.route[i].type.c_str();
istringstream iss(type);
do
{
string subs;
iss >> subs;
//cout<<"\nSubs pedestrian: "<<subs.c_str()<<endl;
if (!strcmp(subs.c_str(),"pedestrian_crossing"))
{
pedestrian_crossing_lanelets.route.push_back(monitorized_lanelets.route[i]);
}
}while(iss);
}
}
else // Pedestrian crossing far away, turn off pedestrian crossing monitor
{
pedestrian_crossing_monitor = false;
}
// Obtain data from regelem to build the pedestrian crossing area
polygon_area[0].x = regelem->A1.latitude;
polygon_area[0].y = regelem->A1.longitude;
polygon_area[1].x = regelem->A2.latitude;
polygon_area[1].y = regelem->A2.longitude;
polygon_area[2].x = regelem->A3.latitude;
polygon_area[2].y = regelem->A3.longitude;
polygon_area[3].x = regelem->A4.latitude;
polygon_area[3].y = regelem->A4.longitude;
}
else // There is not a pedestrian crossing, turn off pedestrian crossing monitor
{
pedestrian_crossing_monitor = false;
}
// Give way or STOP
if (!strcmp(regelem->type.c_str(),"give way") || !strcmp(regelem->type.c_str(),"give_way") || !strcmp(regelem->type.c_str(),"stop"))
{
if (regelem->distance<30) // It the give way/stop is within 30 m
{
// Activate merging monitor
merging_monitor = true;
int id_lanelet_merging = regelem->laneletID;
// Store merging lanelets related to regulatory element and activate merging monitor
//cout<<"\n..............."<<endl;
for (int i=0; i<monitorized_lanelets.route.size(); i++)
{
//cout<<"Type: "<<monitorized_lanelets.route[i].type.c_str()<<"-> ID: "<<monitorized_lanelets.route[i].id<<endl;
string type = monitorized_lanelets.route[i].type.c_str();
istringstream iss(type);
do
{
string subs;
iss >> subs;
//cout<<"\nSubs merging: "<<subs.c_str()<<endl;
if (!strcmp(subs.c_str(),"id"))
{
string subs;
iss >> subs;
if(!strcmp(subs.c_str(), to_string(id_lanelet_merging).c_str()))
{
merging_lanelets.route.push_back(monitorized_lanelets.route[i]);
}
}
}while(iss);
}
//cout<<"..............."<<endl;
}
else // Give way or STOP far away, turn off pedestrian monitor
{
merging_monitor = false;
}
// If particularly STOP
if (!strcmp(regelem->type.c_str(),"stop"))
{
// if stop monitor is 0 (inactive) or 1 (active), stop variable is 1 (vehicle cannot continue)
if (stop != 2)
{
stop = 1;
}
}
else // If current regulatory element is not a STOP, turn off the variable
{
stop = 0;
}
}
else // There is neither a give way not a STOP, turn off merging monitor
{
merging_monitor = false;
}
// Create a variable with the monitorized lanelets that are in the route
// Route:Lanelets in which the ego-vehicle is supposed to be driven (Coloured in blue in RVIZ)
for (int i=0; i<monitorized_lanelets.route.size(); i++)
{
string type = monitorized_lanelets.route[i].type.c_str();
istringstream iss(type);
do
{
string subs;
iss >> subs;
if (!strcmp(subs.c_str(),"route"))
{
route_lanelets.route.push_back(monitorized_lanelets.route[i]);
}
}while(iss);
}
// TODO: IMPROVE HOW TO DETERMINE EXACTLY THE CURRENT LEFT AND RIGHT LANELETS
// If the current monitorized lanelets are based on a right-defined route
for (int i=0; i<monitorized_lanelets.route.size(); i++)
{
string type = monitorized_lanelets.route[i].type.c_str();
istringstream iss(type);
do
{
string subs;
iss >> subs;
if (!strcmp(subs.c_str(),"left"))
{
left_lanelets.route.push_back(monitorized_lanelets.route[i]);
}
// If there exist left lanelets, it means that the user has planified the route along the right lanelet of the road. So, the current right lanelet is
// represented by "route" type in Monitorized Lanelets
if(!strcmp(subs.c_str(),"route") || !strcmp(subs.c_str(),"merging split route") || !strcmp(subs.c_str(),"split merging route"))
{
route_right_lanelets.route.push_back(monitorized_lanelets.route[i]);
}
}while(iss);
}
// If the current monitorized lanelets are based on a left-defined route
if (left_lanelets.route.size() == 0)
{
for (int i=0; i<monitorized_lanelets.route.size(); i++)
{
string type = monitorized_lanelets.route[i].type.c_str();
istringstream iss(type);
do
{
string subs;
iss >> subs;
if(!strcmp(subs.c_str(),"route") || !strcmp(subs.c_str(),"merging split route") || !strcmp(subs.c_str(),"split merging route"))
{
route_left_lanelets.route.push_back(monitorized_lanelets.route[i]);
}
if (i>0 && !strcmp(subs.c_str(),"lanelet"))
{
right_lanelets.route.push_back(monitorized_lanelets.route[i]);
}
}while(iss);
}
}
if (left_lanelets.route.size()>0)
{
for (int i=0;i<left_lanelets.route.size();i++){
all_lefts.route.push_back(left_lanelets.route[i]);}
for (int i=0;i<route_right_lanelets.route.size();i++){
all_rights.route.push_back(route_right_lanelets.route[i]);}
}
else
{
for (int i=0;i<route_left_lanelets.route.size();i++){
all_lefts.route.push_back(route_left_lanelets.route[i]);}
for (int i=0;i<right_lanelets.route.size();i++){
all_rights.route.push_back(right_lanelets.route[i]);}
}
}
void sensor_fusion_and_monitors_cb(const t4ac_msgs::BEV_detections_list::ConstPtr& lidar_detections_msg, const t4ac_msgs::BEV_trackers_list::ConstPtr& bev_trackers_list_msg, const nav_msgs::Odometry::ConstPtr& ego_vehicle_pose_msg)
{
cout<<"------------------------------------------------"<<endl;
// ROS_INFO("Time: [%lf]", (double)ros::Time::now().toSec());
// lidar_detections_msg contains the LiDAR detections
// bev_trackers_list_msg contains the visual tracked obstacles projected onto the Bird's Eye View space
// ego_vehicle_pose_msg contains the ego vehicle position
// Note that if --clock is not published (if we are trying to run a rosbag), the system will not create the transforms
// Get odom and velocity information
// Obtain the movement of the ego-vehicle in X and Y (Global coordinates) and Orientation (Yaw)
double displacement_x_global = ego_vehicle_pose_msg->pose.pose.position.x - previous_odom.pose.pose.position.x;
double displacement_y_global = ego_vehicle_pose_msg->pose.pose.position.y - previous_odom.pose.pose.position.y;
double yaw = tf::getYaw(ego_vehicle_pose_msg->pose.pose.orientation);
// Obtain displacement of the ego-vehicle and Velocities in Local coordinates
double displacement_x_local = displacement_x_global*cos(yaw) + displacement_y_global*sin(yaw);
double displacement_y_local = displacement_x_global*(-sin(yaw)) + displacement_y_global*cos(yaw);
double time = ego_vehicle_pose_msg->header.stamp.toSec() - previous_odom.header.stamp.toSec();
double vel_x_with_yaw = displacement_x_local/time;
double vel_y_with_yaw = displacement_y_local/time;
double abs_vel = sqrt(pow(vel_x_with_yaw,2)+pow(vel_y_with_yaw,2));
double vel_x = displacement_x_global/time;
double vel_y = displacement_y_global/time;
// Store odom in different formats: TODO: Required?
geodesy::UTMPoint odomUTMmsg;
odomUTMmsg.band = utm_origin.band;
odomUTMmsg.zone = utm_origin.zone;
odomUTMmsg.altitude = 0;
odomUTMmsg.easting = ego_vehicle_pose_msg->pose.pose.position.x + utm_origin.easting;
odomUTMmsg.northing = ego_vehicle_pose_msg->pose.pose.position.y + utm_origin.northing;
geographic_msgs::GeoPoint latLonOdom;
latLonOdom = geodesy::toMsg(odomUTMmsg);
// Store previous odometry
previous_odom = *ego_vehicle_pose_msg;
vector<Object> laser_objects;
int number_vehicles = 0;
int number_pedestrians = 0;
for (int i = 0; i < lidar_detections_msg->bev_detections_list.size(); i++)
{
Object object;
object.centroid_x = -lidar_detections_msg->bev_detections_list[i].y;
object.centroid_y = -lidar_detections_msg->bev_detections_list[i].x;
geometry_msgs::PointStamped centroid;
geometry_msgs::Point32 global_centroid;
centroid.point.x = object.centroid_x;
centroid.point.y = object.centroid_y;
centroid.point.z = 0;
global_centroid = Local_To_Global_Coordinates(centroid);
object.global_centroid_x = global_centroid.x;
object.global_centroid_y = global_centroid.y;
object.l = lidar_detections_msg->bev_detections_list[i].l;
object.w = lidar_detections_msg->bev_detections_list[i].w;
object.h = 1.7; // Height of the obstacle. TODO: Take from the 3D object detector
object.orientation = lidar_detections_msg->bev_detections_list[i].o;
string type = lidar_detections_msg->bev_detections_list[i].type;
if (!strcmp(type.c_str(),"1"))
{
object.type = "car";
number_vehicles++;
}
else if (!strcmp(type.c_str(),"2"))
{
object.type = "pedestrian";
number_pedestrians++;
}
object.time = lidar_detections_msg->header.stamp.toSec();
laser_objects.push_back(object);
}
visualization_msgs::MarkerArray obstacles_array;
int id = 0;
for (int i = 0; i < laser_objects.size(); i++)
{
visualization_msgs::Marker obstacle_points;
obstacle_points.header.frame_id = "/base_link"; // map == global coordinates. Base_link == local coordinates
obstacle_points.header.stamp = lidar_detections_msg->header.stamp;
obstacle_points.ns = "map_manager_visualization";
obstacle_points.action = visualization_msgs::Marker::ADD;
obstacle_points.type = visualization_msgs::Marker::CUBE;
obstacle_points.id = id;
id++;
obstacle_points.points.clear();
obstacle_points.color = colours[2]; // Only red;
obstacle_points.scale.x = 1;
obstacle_points.scale.y = 1;
obstacle_points.scale.z = 1;
obstacle_points.lifetime = ros::Duration(0.40);
/*obstacle_points.pose.position.x = laser_objects[i].global_centroid_x;
obstacle_points.pose.position.y = laser_objects[i].global_centroid_y;
obstacle_points.pose.position.z = laser_objects[i].global_centroid_z;*/
obstacle_points.pose.position.x = laser_objects[i].centroid_x;
obstacle_points.pose.position.y = laser_objects[i].centroid_y;
obstacle_points.pose.position.z = 0;
obstacles_array.markers.push_back(obstacle_points);
}
pub_LiDAR_Obstacles_Marker.publish(obstacles_array);
// BEV Projected VOT (Visual Object Tracking)
float diff_lidar_vot = 0;
int object_id = 0;
// Publish
obstacles_array.markers.clear();
for (int i = 0; i < bev_trackers_list_msg->bev_trackers_list.size(); i++)
{
visualization_msgs::Marker obstacle_points;
obstacle_points.header.frame_id = "/base_link"; // map == global coordinates. Base_link == local coordinates
obstacle_points.header.stamp = bev_trackers_list_msg->header.stamp;
obstacle_points.ns = "map_manager_visualization";
obstacle_points.action = visualization_msgs::Marker::ADD;
obstacle_points.type = visualization_msgs::Marker::CUBE;
obstacle_points.id = bev_trackers_list_msg->bev_trackers_list[i].object_id;
obstacle_points.points.clear();
obstacle_points.color = colours[0]; // Only red;
obstacle_points.scale.x = 1;
obstacle_points.scale.y = 1;
obstacle_points.scale.z = 1;
obstacle_points.lifetime = ros::Duration(0.40);
/*obstacle_points.pose.position.x = laser_objects[i].global_centroid_x;
obstacle_points.pose.position.y = laser_objects[i].global_centroid_y;
obstacle_points.pose.position.z = laser_objects[i].global_centroid_z;*/