-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclustering_precision_tracking_monitors.cpp
2640 lines (1998 loc) · 82.7 KB
/
clustering_precision_tracking_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
/***
Carlos Gómez Huélamo December 2019
3D tracking, precision tracking and monitors
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>
// OpenCV includes
#include <opencv2/video/tracking.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/core/core.hpp"
#include <cv_bridge/cv_bridge.h>
// 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 <rviz_visual_tools/rviz_visual_tools.h>
// PCL includes
#include <pcl/filters/passthrough.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/kdtree/kdtree.h>
#include <pcl/search/kdtree.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl_ros/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/features/normal_3d.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/impl/point_types.hpp>
// Precision tracking includes
#include <precision_tracking/track_manager_color.h>
#include <precision_tracking/tracker.h>
#include <precision_tracking/high_res_timer.h>
#include <precision_tracking/sensor_specs.h>
// YOLO includes
#include "yolov3_centernet_ros/yolo_list.h"
#include "yolov3_centernet_ros/yolo_obstacle.h"
// CARLA includes
#include "carla_msgs/CarlaObjectLocation.h"
#include "carla_msgs/CarlaObjectLocationList.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"
// Precision tracking includes
#include <precision_tracking/track_manager_color.h>
#include <precision_tracking/tracker.h>
#include <precision_tracking/high_res_timer.h>
#include <precision_tracking/sensor_specs.h>
// End Includes //
// Defines //
#define VER_OBJETOS_AGRUPADOS 0
#define VER_CLUSTERES 0
#define VER_TRAYECTORIA 0
#define KALMAN 0
#define VIEWER_3D 0
#define LaneletFilter 0
#define DEBUG 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
#define TIME_PRECISION_TRACKING 1.5
// 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 Kalman_Points_History
{
// Store up to 10 measurements on each Kalman filter
// Position of the obstacle
double x[10];
double y[10];
double z[10];
// Dimensions of the obstacle
double w[10];
double h[10];
double d[10];
// Prediction of the obstacle
double predicted_x[10];
double predicted_y[10];
double predicted_z[10];
double time[10];
string type;
}kalman_points_history;
typedef struct Points_Kalman
{
double x;
double y;
double z;
double w;
double h;
double d;
string type;
}points_kalman;
typedef struct Points
{
int x;
int z;
}points[100];
struct Object
{
float centroid_x; // Local centroid (with respect to the "base_link" frame)
float centroid_y;
float centroid_z;
float global_centroid_x; // Global centroid (with respect to the "map" frame)
float global_centroid_y;
float global_centroid_z;
double r;
double g;
double b;
float x_min;
float y_min;
float z_min;
float x_max;
float y_max;
float z_max;
float w;
float h;
float d;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
string type;
int id;
int pedestrian_state; // If the object is a pedestrian, store its state
}object;
struct Merged_Object
{
int cluster[10];
float centroid_x; // Local centroid (with respect to the "base_link" frame)
float centroid_y;
float centroid_z;
float global_centroid_x; // Global centroid (with respect to the "map" frame)
float global_centroid_y;
float global_centroid_z;
float w;
float h;
float d;
double r;
double g;
double b;
float x_min;
float y_min;
float z_min;
float x_max;
float y_max;
float z_max;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
string type;
int id;
};
struct Cluster
{
int cluster[10];
int repetitions;
float centroid_x[10];
float centroid_y[10];
float centroid_z[10];
double r;
double g;
double b;
float x_min;
float y_min;
float z_min;
float x_max;
float y_max;
float z_max;
float w;
float h;
float d;
int id;
}cluster;
struct Precision_Trackers
{
int id;
double time;
Eigen::Vector3f centroid; // x, y, z position
Eigen::Vector3f previous_centroid;
Eigen::Vector3f previous_velocity;
Eigen::Vector3f estimated_velocity; // x, y, z speed
Eigen::Vector3f size; // Height, Width, Depth
string type;
int pedestrian_state;
}precision_trackers;
// End Structures //
// ROS communication //
// ROS Publishers
ros::Publisher pub_LiDAR_Pointcloud_Coloured_XYZ_Filtered;
ros::Publisher pub_LiDAR_Pointcloud_Coloured_XYZ_Angle_Filtered;
ros::Publisher pub_LiDAR_Obstacles;
ros::Publisher pub_LiDAR_Obstacles_Velocity_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)
// End ROS communication //
// Global variables //
// Transform variables
tf::StampedTransform transformBaseLinkBaseCamera, transformOdomBaseLink, transformBaseLinkOdom, transformMaptoVelodyne, transformVelodynetoMap;
tf::Transform tfBaseLinkBaseCamera, tfOdomBaseLink;
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;
// Visualization variables
//boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer")); // 3D viewer
// Namespaces //
using namespace std;
namespace rvt = rviz_visual_tools;
namespace rviz_visual_tools
{
class RvizVisualToolsDemo
{
private:
rvt::RvizVisualToolsPtr visual_tools_;
string name_;
public:
// Constructor
RvizVisualToolsDemo() : name_("rviz_tracking")
{
visual_tools_.reset(new rvt::RvizVisualTools("/map", "/rviz_visual_tools"));
visual_tools_->loadMarkerPub(); // create publisher before waiting
// Clear messages
visual_tools_->deleteAllMarkers();
visual_tools_->enableBatchPublishing();
}
void publishLabelHelper(const Eigen::Isometry3d& pose, const string& label)
{
Eigen::Isometry3d pose_copy = pose;
pose_copy.translation().x() -= 0.2;
visual_tools_->publishText(pose_copy, label, rvt::WHITE, rvt::LARGE, false);
}
void Show_WireFrame(geometry_msgs::Point32 location, const string label)
{
Eigen::Isometry3d pose = Eigen::Isometry3d::Identity();
pose.translation() = Eigen::Vector3d(location.x, location.y, location.z);
double depth = PEDESTRIAN_LENGTH, width = PEDESTRIAN_WIDTH, height = PEDESTRIAN_HEIGHT;
visual_tools_->publishWireframeCuboid(pose, depth, width, height, rvt::RED);
publishLabelHelper(pose, label);
//visual_tools_->trigger();
}
};
}
// 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
cv::Mat imgProjection;
vector<std_msgs::ColorRGBA> colours;
vector<Object> only_laser_objects, merged_objects, output_objects;
//vector<Tracking_Points> tracking_points, tracking_points_prev, tracking_points_aux, tracking_points_lidar, tracking_points_prev_lidar, tracking_points_aux_lidar;
vector<Precision_Trackers> pTrackers;
vector<cv::KalmanFilter> kfs;
vector<int> kfsTime;
precision_tracking::Params params;
Eigen::Vector3f estimated_velocity;
vector<precision_tracking::Tracker> trackers;
int indexpTrackers = 0;
int flag_tracking_points = 0;
int number_of_clusters = 0; // Number of clusters after the first filter
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 Global_To_Local_Coordinates(geometry_msgs::PointStamped );
geometry_msgs::Point32 Local_To_Global_Coordinates(geometry_msgs::PointStamped );
float get_Centroids_Distance(pcl::PointXYZ , pcl::PointXYZ );
void Inside_Polygon(Area_Point *, int , Area_Point, bool &);
void Obstacle_in_Lanelet(pcl::PointCloud<pcl::PointXYZRGB>::Ptr , geometry_msgs::PointStamped , geometry_msgs::Point32 , geometry_msgs::PointStamped , geometry_msgs::PointStamped , geometry_msgs::PointStamped , geometry_msgs::PointStamped , ros::Time , sec_msgs::Lanelet );
// Kalman functions
cv::KalmanFilter initKalman(float, float, float, float, float, float, float, float, float, float, float, float, float );
void updateKalman(cv::KalmanFilter &, float , float , float , float , float , float , bool );
Points_Kalman getKalmanPrediction(cv::KalmanFilter );
// Point Cloud filters
pcl::PointCloud<pcl::PointXYZRGB> xyz_filter(pcl::PointCloud<pcl::PointXYZRGB>::Ptr );
pcl::PointCloud<pcl::PointXYZRGB> angle_filter(pcl::PointCloud<pcl::PointXYZRGB>::Ptr );
void cluster_filter (pcl::PointCloud<pcl::PointXYZRGB>::Ptr , float , int , int , vector<Object> *, int *);
void segmentation_filter (pcl::PointCloud<pcl::PointXYZRGB>::Ptr , float , float , float , float , float , float , float , int , int , vector<Object> *, int *, string);
vector<Merged_Object> merging_z(vector<Object> );
// Cluster functions
vector<Merged_Object> merging_z(vector<Object> );
// Calbacks
void route_cb(const sec_msgs::Route::ConstPtr& );
void waiting_cb(const std_msgs::Empty );
void regelement_cb(const sec_msgs::RegElem::ConstPtr& , const sec_msgs::Route::ConstPtr& );
//void sensor_fusion_and_monitors_cb(ESPECIFICAR);
void clustering_precision_tracking_monitors_cb(const sensor_msgs::PointCloud2::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_LiDAR_Pointcloud_Coloured_XYZ_Filtered = nh.advertise<sensor_msgs::PointCloud2>("/velodyne_points_coloured_xyz_filtered", 1);
pub_LiDAR_Pointcloud_Coloured_XYZ_Angle_Filtered = nh.advertise<sensor_msgs::PointCloud2>("/velodyne_points_coloured_xyz_angle_filtered", 1);
pub_LiDAR_Obstacles = nh.advertise<sec_msgs::ObstacleArray>("/obstacles", 1, true);
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_LiDAR_Obstacles_Velocity_Marker = nh.advertise<visualization_msgs::Marker>("/Obstacle_marker_vel", 1, true); // Only LiDAR
/*tracked_objects_vel_marker_pub = nh.advertise<visualization_msgs::Marker>("/tracked_objects_marker_vel", 1, true); // Only vision
tracked_objects_marker_pub = nh.advertise<visualization_msgs::Marker>("/tracked_objects_marker", 1, true); // Only vision
tracked_merged_objects_vel_marker_pub = nh.advertise<visualization_msgs::Marker>("/tracked_merged_objects_marker_vel", 1, true); // Sensor fusion LiDAR and Camera
tracked_merged_objects_marker_pub = nh.advertise<visualization_msgs::Marker>("/tracked_merged_objects_marker", 1, true); // Sensor fusion LiDAR and Camera*/
// 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> odom_sub_; // Odometry
message_filters::Subscriber<yolov3_centernet_ros::yolo_list> vision_sub_; // Detection and Tracking with camera (CenterNet + Deep Sort + YOLO)
message_filters::Subscriber<carla_msgs::CarlaObjectLocationList> carla_sub_;
regelem_sub_.subscribe(nh, "/currentRegElem", 1);
regelemLanelet_sub_.subscribe(nh, "/monitorizedLanelets", 1);
cloud_sub_.subscribe(nh, "/velodyne_coloured", 1); // Colored point cloud (based on semantic segmentation)
velodyne_cloud_sub_.subscribe(nh, "/velodyne_points", 1);
odom_sub_.subscribe(nh, "/carla/ego_vehicle/odometry", 1); // Note that in CARLA, each dynamic object has its own odometry topic. /odom has all objects odometries
waiting_sub = nh.subscribe<std_msgs::Empty>("/waitingAtStop", 1, &waiting_cb);
route_sub = nh.subscribe<sec_msgs::Route>("/route", 1, &route_cb);
//vision_sub_.subscribe(nh, "/yolov3_tracking_list", 1);
//carla_sub_.subscribe(nh, "/carla/hero/location_list", 1);
//carla_sub_ = nh.subscribe<carla_msgs::CarlaObjectLocation>("/carla/hero/location", 1, &Procesar_carla_cb);
//yolo_sub_ = nh.subscribe<nav_msgs::Path>("/yolov3_tracking_list", 1, &Procesar_yolo_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) (Approximate time)
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, yolov3_centernet_ros::yolo_list> MySyncPolicy2;
message_filters::Synchronizer<MySyncPolicy2> sync2_(MySyncPolicy2(200), velodyne_cloud_sub_, vision_sub_);
sync2_.registerCallback(boost::bind(&sensor_fusion_and_monitors_cb, _1, _2));*/
// Callback 2: Synchonize LiDAR point cloud and ego-vehicle odometry. Perform tracking and monitors (Approximate time)
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, nav_msgs::Odometry> MySyncPolicy2;
message_filters::Synchronizer<MySyncPolicy2> sync2_(MySyncPolicy2(100), cloud_sub_, odom_sub_);
sync2_.registerCallback(boost::bind(&clustering_precision_tracking_monitors_cb, _1, _2));
// Load map
string map_frame = "";
//string map_path = ros::package::getPath("sec_map_manager") + "/maps/uah_lanelets_v42.osm"; // Load this path if /map_path argument does not exit in the network
string map_path = ros::package::getPath("sec_map_manager") + "/maps/Town03_CARLA.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
// Transform global coordinates ("map" frame) to local coordinates ("base_link" frame)
geometry_msgs::Point32 Global_To_Local_Coordinates(geometry_msgs::PointStamped point_global)
{
// 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_local;
geometry_msgs::Point32 point32_local;
aux.setX(point_global.point.x);
aux.setY(point_global.point.y);
aux.setZ(point_global.point.z);
aux2 = transformBaseLinkOdom * aux;
point_local.point.x = aux2.getX();
point_local.point.y = aux2.getY();
point_local.point.z = aux2.getZ();
point32_local.x = point_local.point.x;
point32_local.y = point_local.point.y;
point32_local.z = point_local.point.z;
return(point32_local);
}
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 = transformOdomBaseLink * 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);
}
// Obtain the distance between two 3D points
float get_Centroids_Distance(pcl::PointXYZ p1, pcl::PointXYZ p2)
{
// Parameters:
// p1, p2: Two 3D points
// Returns the Euclidean distance between both 3D points
float root;
root = float(sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2)+pow(p1.z-p2.z,2)));
return root;
}
// 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;
}
}
void Obstacle_in_Lanelet(pcl::PointCloud<pcl::PointXYZ>::Ptr ObstaclesInLanelet_Ptr, geometry_msgs::PointStamped point_local, geometry_msgs::Point32 point32_global, geometry_msgs::PointStamped v1, geometry_msgs::PointStamped v2, geometry_msgs::PointStamped v3, geometry_msgs::PointStamped v4, ros::Time stamp, sec_msgs::Lanelet lanelet)
{
current_obstacle.header.stamp = stamp;
current_obstacle.header.frame_id = "/base_link";
//cout<<"Point local x: "<<point_local.point.x;
//cout<<"Point local y: "<<point_local.point.y;
current_obstacle.pose.position.x = point_local.point.x;
current_obstacle.pose.position.y = point_local.point.y;
current_obstacle.pose.position.z = point_local.point.z;
current_obstacle.laneletID = lanelet.id;
geometry_msgs::Point32 pointaux32;
pcl::PointXYZ pointaux;
// Store the vertices of the obstacle
current_obstacle.shape.points.clear();
pointaux32.x = v1.point.x;
pointaux32.y = v1.point.y;
pointaux32.z = v1.point.z;
current_obstacle.shape.points.push_back(pointaux32);
pointaux32.x = v2.point.x;
pointaux32.y = v2.point.y;
pointaux32.z = v2.point.z;
current_obstacle.shape.points.push_back(pointaux32);
pointaux32.x = v3.point.x;
pointaux32.y = v3.point.y;
pointaux32.z = v3.point.z;
current_obstacle.shape.points.push_back(pointaux32);
pointaux32.x = v4.point.x;
pointaux32.y = v4.point.y;
pointaux32.z = v4.point.z;
current_obstacle.shape.points.push_back(pointaux32);
Obstacles.obstacles.push_back(current_obstacle);
// Obstacle in lanelet
pointaux.x=point32_global.x;
pointaux.y=point32_global.y;
pointaux.z=point32_global.z;
ObstaclesInLanelet_Ptr->points.push_back(pointaux);
}
// Kalman functions
// Initialize a Kalman filter and put the initial values in the matrices used by the algorithm
cv::KalmanFilter initKalman(float x, float y, float z, float w, float h, float d, float sigmaR1, float sigmaR2, float sigmaR3, float sigmaQ1, float sigmaQ2, float sigmaQ3, float sigmaP)
{
// Parameters:
// x, y, z: Centroid of the object
// w, h, d: Dimensions of the object
// sigmaR1, sigmaR2, sigmaR3: Error Noise Covariance
// sigmaQ1, sigmaQ2, sigmaQ3: Process Noise Covariance
// sigmaP: Error Posterior Matrix
// Creates Kalman Filter with 6 measures
cv::KalmanFilter kf(12,6,0); // (int dynamParams, int measureParams, int controlParams = 0)
// dynamParams: Dimensionality of the state
// measureParams: Dimensionality of the measurement
// controlParams: Dimensionality of the control vector
// type: Type of the created matrices that should be CV_32F (by default) or CV_64F
// Transition matrix
kf.transitionMatrix = (cv::Mat_<float>(12,12) << 1,0,0,0,0,0,1,0,0,0,0,0,
0,1,0,0,0,0,0,1,0,0,0,0,
0,0,1,0,0,0,0,0,1,0,0,0,
0,0,0,1,0,0,0,0,0,1,0,0,
0,0,0,0,1,0,0,0,0,0,1,0,
0,0,0,0,0,1,0,0,0,0,0,1,
0,0,0,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,0,0,1);
// Measurement matrix
kf.measurementMatrix = (cv::Mat_<float>(6,12) << 1,0,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,0,0);
// Process noise covariance
kf.processNoiseCov = (cv::Mat_<float>(12,12) << sigmaQ1,0,0,0,0,0,0,0,0,0,0,0,
0,sigmaQ1,0,0,0,0,0,0,0,0,0,0,
0,0,sigmaQ2,0,0,0,0,0,0,0,0,0,
0,0,0,sigmaQ2,0,0,0,0,0,0,0,0,
0,0,0,0,sigmaQ3,0,0,0,0,0,0,0,
0,0,0,0,0,sigmaQ3,0,0,0,0,0,0,
0,0,0,0,0,0,sigmaQ1,0,0,0,0,0,
0,0,0,0,0,0,0,sigmaQ1,0,0,0,0,
0,0,0,0,0,0,0,0,sigmaQ2,0,0,0,
0,0,0,0,0,0,0,0,0,sigmaQ2,0,0,
0,0,0,0,0,0,0,0,0,0,sigmaQ3,0,
0,0,0,0,0,0,0,0,0,0,0,sigmaQ3);
// Measurement noise covariance
kf.measurementNoiseCov = (cv::Mat_<float>(6,6) << sigmaR1,0,0,0,0,0,
0,sigmaR1,0,0,0,0,
0,0,sigmaR2,0,0,0,
0,0,0,sigmaR2,0,0,
0,0,0,0,sigmaR3,0,
0,0,0,0,0,sigmaR3);
// Error posterior matrix
kf.errorCovPost = (cv::Mat_<float>(12,12) << sigmaP,0,0,0,0,0,0,0,0,0,0,0,
0,sigmaP,0,0,0,0,0,0,0,0,0,0,
0,0,sigmaP,0,0,0,0,0,0,0,0,0,
0,0,0,sigmaP,0,0,0,0,0,0,0,0,
0,0,0,0,sigmaP,0,0,0,0,0,0,0,
0,0,0,0,0,sigmaP,0,0,0,0,0,0,
0,0,0,0,0,0,sigmaP,0,0,0,0,0,
0,0,0,0,0,0,0,sigmaP,0,0,0,0,
0,0,0,0,0,0,0,0,sigmaP,0,0,0,
0,0,0,0,0,0,0,0,0,sigmaP,0,0,
0,0,0,0,0,0,0,0,0,0,sigmaP,0,
0,0,0,0,0,0,0,0,0,0,0,sigmaP);
// Initialize filter with given data
// State post: Corrected state (x(k)): x(k) = x'(k)+K(k)*(z(k)-H*x'(k))
kf.statePost.at<float>(0) = x;
kf.statePost.at<float>(1) = y;
kf.statePost.at<float>(2) = z;
kf.statePost.at<float>(3) = w;
kf.statePost.at<float>(4) = h;
kf.statePost.at<float>(5) = d;
kf.statePost.at<float>(6) = 0;
kf.statePost.at<float>(7) = 0;
kf.statePost.at<float>(8) = 0;
kf.statePost.at<float>(9) = 0;
kf.statePost.at<float>(10) = 0;
kf.statePost.at<float>(11) = 0;
// State Pre: Predicted state (x'(k)): x(k) = A*x(k-1)+B*u(k)
kf.statePre.at<float>(0) = x;
kf.statePre.at<float>(1) = y;
kf.statePre.at<float>(2) = z;
kf.statePre.at<float>(3) = w;
kf.statePre.at<float>(4) = h;
kf.statePre.at<float>(5) = d;
return kf;
}
// Update Kalman filter
void updateKalman(cv::KalmanFilter &kf, float x, float y, float z, float w, float h, float d, bool useMeasurement)
{
// Parameters:
// kf: Kalman filter calculated in initKalman
// x, y, z: Center of the obstacle
// w, h, d: Dimensions of the object
// useMeasurement: If true, use measurements. If false, use previous prediction
cv::Mat prediction; // cv::Mat n-dimensional dense array class
cv::Mat measurement(6,1,CV_32FC1);
// Kalman prediction
prediction = kf.predict(); // predict Public member function computes a predicted state