forked from hku-mars/FAST-LIVO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIVMapper.cpp
More file actions
executable file
·1388 lines (1219 loc) · 55.8 KB
/
LIVMapper.cpp
File metadata and controls
executable file
·1388 lines (1219 loc) · 55.8 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
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
/*
This file is part of FAST-LIVO2: Fast, Direct LiDAR-Inertial-Visual Odometry.
Developer: Chunran Zheng <zhengcr@connect.hku.hk>
For commercial use, please contact me at <zhengcr@connect.hku.hk> or
Prof. Fu Zhang at <fuzhang@hku.hk>.
This file is subject to the terms and conditions outlined in the 'LICENSE' file,
which is included as part of this source code package.
*/
#include "LIVMapper.h"
#include <vikit/camera_loader.h>
using namespace Sophus;
LIVMapper::LIVMapper(rclcpp::Node::SharedPtr &node, std::string node_name, const rclcpp::NodeOptions & options)
: node(std::make_shared<rclcpp::Node>(node_name, options)),
extT(0, 0, 0),
extR(M3D::Identity())
{
extrinT.assign(3, 0.0);
extrinR.assign(9, 0.0);
cameraextrinT.assign(3, 0.0);
cameraextrinR.assign(9, 0.0);
p_pre.reset(new Preprocess());
p_imu.reset(new ImuProcess());
readParameters(this->node);
VoxelMapConfig voxel_config;
loadVoxelConfig(this->node, voxel_config);
visual_sub_map.reset(new PointCloudXYZI());
feats_undistort.reset(new PointCloudXYZI());
feats_down_body.reset(new PointCloudXYZI());
feats_down_world.reset(new PointCloudXYZI());
pcl_w_wait_pub.reset(new PointCloudXYZI());
pcl_wait_pub.reset(new PointCloudXYZI());
pcl_wait_save.reset(new PointCloudXYZRGB());
pcl_wait_save_intensity.reset(new PointCloudXYZI());
voxelmap_manager.reset(new VoxelMapManager(voxel_config, voxel_map));
vio_manager.reset(new VIOManager());
root_dir = ROOT_DIR;
initializeFiles();
initializeComponents(this->node); // initialize components errors
path.header.stamp = this->node->now();
path.header.frame_id = "camera_init";
}
LIVMapper::~LIVMapper() {}
void LIVMapper::readParameters(rclcpp::Node::SharedPtr &node)
{
// declare parameters
auto try_declare = [node]<typename ParameterT>(const std::string & name,
const ParameterT & default_value)
{
if (!node->has_parameter(name))
{
return node->declare_parameter<ParameterT>(name, default_value);
}
else
{
return node->get_parameter(name).get_value<ParameterT>();
}
};
// declare parameter
try_declare.template operator()<std::string>("common.lid_topic", "/livox/lidar");
try_declare.template operator()<std::string>("common.imu_topic", "/livox/imu");
try_declare.template operator()<bool>("common.ros_driver_bug_fix", false);
try_declare.template operator()<int>("common.img_en", 1);
try_declare.template operator()<int>("common.lidar_en", 1);
try_declare.template operator()<std::string>("common.img_topic", "/left_camera/image");
try_declare.template operator()<bool>("vio.normal_en", true);
try_declare.template operator()<bool>("vio.inverse_composition_en", false);
try_declare.template operator()<int>("vio.max_iterations", 5);
try_declare.template operator()<int>("vio.img_point_cov", 100);
try_declare.template operator()<bool>("vio.raycast_en", false);
try_declare.template operator()<bool>("vio.exposure_estimate_en", true);
try_declare.template operator()<double>("vio.inv_expo_cov", 0.1);
try_declare.template operator()<int>("vio.grid_size", 5);
try_declare.template operator()<int>("vio.grid_n_height", 17);
try_declare.template operator()<int>("vio.patch_pyrimid_level", 4);
try_declare.template operator()<int>("vio.patch_size", 8);
try_declare.template operator()<int>("vio.outlier_threshold", 100);
try_declare.template operator()<double>("time_offset.exposure_time_init", 0.0);
try_declare.template operator()<double>("time_offset.img_time_offset", 0.0);
try_declare.template operator()<bool>("uav.imu_rate_odom", false);
try_declare.template operator()<bool>("uav.gravity_align_en", false);
try_declare.template operator()<std::string>("evo.seq_name", "01");
try_declare.template operator()<bool>("evo.pose_output_en", false);
try_declare.template operator()<double>("imu.gyr_cov", 1.0);
try_declare.template operator()<double>("imu.acc_cov", 1.0);
try_declare.template operator()<int>("imu.imu_int_frame", 30);
try_declare.template operator()<bool>("imu.imu_en", true);
try_declare.template operator()<bool>("imu.gravity_est_en", true);
try_declare.template operator()<bool>("imu.ba_bg_est_en", true);
try_declare.template operator()<double>("preprocess.blind", 0.01);
try_declare.template operator()<double>("preprocess.filter_size_surf", 0.5);
try_declare.template operator()<int>("preprocess.lidar_type", AVIA);
try_declare.template operator()<int>("preprocess.scan_line",6);
try_declare.template operator()<int>("preprocess.point_filter_num", 3);
try_declare.template operator()<int>("preprocess.scan_rate", 10);
try_declare.template operator()<bool>("preprocess.feature_extract_enabled", false);
try_declare.template operator()<int>("pcd_save.interval", -1);
try_declare.template operator()<bool>("pcd_save.pcd_save_en", false);
try_declare.template operator()<bool>("pcd_save.colmap_output_en", false);
try_declare.template operator()<double>("pcd_save.filter_size_pcd", 0.5);
try_declare.template operator()<vector<double>>("extrin_calib.extrinsic_T", vector<double>{});
try_declare.template operator()<vector<double>>("extrin_calib.extrinsic_R", vector<double>{});
try_declare.template operator()<vector<double>>("extrin_calib.Pcl", vector<double>{});
try_declare.template operator()<vector<double>>("extrin_calib.Rcl", vector<double>{});
try_declare.template operator()<double>("debug.plot_time", -10);
try_declare.template operator()<int>("debug.frame_cnt", 6);
try_declare.template operator()<double>("publish.blind_rgb_points", 0.01);
try_declare.template operator()<int>("publish.pub_scan_num", 1);
try_declare.template operator()<bool>("publish.pub_effect_point_en", false);
try_declare.template operator()<bool>("publish.dense_map_en", false);
// get parameter
this->node->get_parameter("common.lid_topic", lid_topic);
this->node->get_parameter("common.imu_topic", imu_topic);
this->node->get_parameter("common.ros_driver_bug_fix", ros_driver_fix_en);
this->node->get_parameter("common.img_en", img_en);
this->node->get_parameter("common.lidar_en", lidar_en);
this->node->get_parameter("common.img_topic", img_topic);
this->node->get_parameter("vio.normal_en", normal_en);
this->node->get_parameter("vio.inverse_composition_en", inverse_composition_en);
this->node->get_parameter("vio.max_iterations", max_iterations);
this->node->get_parameter("vio.img_point_cov", IMG_POINT_COV);
this->node->get_parameter("vio.raycast_en", raycast_en);
this->node->get_parameter("vio.exposure_estimate_en", exposure_estimate_en);
this->node->get_parameter("vio.inv_expo_cov", inv_expo_cov);
this->node->get_parameter("vio.grid_size", grid_size);
this->node->get_parameter("vio.grid_n_height", grid_n_height);
this->node->get_parameter("vio.patch_pyrimid_level", patch_pyrimid_level);
this->node->get_parameter("vio.patch_size", patch_size);
this->node->get_parameter("vio.outlier_threshold", outlier_threshold);
this->node->get_parameter("time_offset.exposure_time_init", exposure_time_init);
this->node->get_parameter("time_offset.img_time_offset", img_time_offset);
this->node->get_parameter("uav.imu_rate_odom", imu_prop_enable);
this->node->get_parameter("uav.gravity_align_en", gravity_align_en);
this->node->get_parameter("evo.seq_name", seq_name);
this->node->get_parameter("evo.pose_output_en", pose_output_en);
this->node->get_parameter("imu.gyr_cov", gyr_cov);
this->node->get_parameter("imu.acc_cov", acc_cov);
this->node->get_parameter("imu.imu_int_frame", imu_int_frame);
this->node->get_parameter("imu.imu_en", imu_en);
this->node->get_parameter("imu.gravity_est_en", gravity_est_en);
this->node->get_parameter("imu.ba_bg_est_en", ba_bg_est_en);
this->node->get_parameter("preprocess.blind", p_pre->blind);
this->node->get_parameter("preprocess.filter_size_surf", filter_size_surf_min);
this->node->get_parameter("preprocess.lidar_type", p_pre->lidar_type);
this->node->get_parameter("preprocess.scan_line", p_pre->N_SCANS);
this->node->get_parameter("preprocess.scan_rate", p_pre->SCAN_RATE);
this->node->get_parameter("preprocess.point_filter_num", p_pre->point_filter_num);
this->node->get_parameter("preprocess.feature_extract_enabled", p_pre->feature_enabled);
this->node->get_parameter("pcd_save.interval", pcd_save_interval);
this->node->get_parameter("pcd_save.pcd_save_en", pcd_save_en);
this->node->get_parameter("pcd_save.colmap_output_en", colmap_output_en);
this->node->get_parameter("pcd_save.filter_size_pcd", filter_size_pcd);
this->node->get_parameter("extrin_calib.extrinsic_T", extrinT);
this->node->get_parameter("extrin_calib.extrinsic_R", extrinR);
this->node->get_parameter("extrin_calib.Pcl", cameraextrinT);
this->node->get_parameter("extrin_calib.Rcl", cameraextrinR);
this->node->get_parameter("debug.plot_time", plot_time);
this->node->get_parameter("debug.frame_cnt", frame_cnt);
this->node->get_parameter("publish.blind_rgb_points", blind_rgb_points);
this->node->get_parameter("publish.pub_scan_num", pub_scan_num);
this->node->get_parameter("publish.pub_effect_point_en", pub_effect_point_en);
this->node->get_parameter("publish.dense_map_en", dense_map_en);
p_pre->blind_sqr = p_pre->blind * p_pre->blind;
}
void LIVMapper::initializeComponents(rclcpp::Node::SharedPtr &node)
{
downSizeFilterSurf.setLeafSize(filter_size_surf_min, filter_size_surf_min, filter_size_surf_min);
// extrinT.assign({0.04165, 0.02326, -0.0284});
// extrinR.assign({1, 0, 0, 0, 1, 0, 0, 0, 1});
// cameraextrinT.assign({0.0194384, 0.104689,-0.0251952});
// cameraextrinR.assign({0.00610193,-0.999863,-0.0154172,-0.00615449,0.0153796,-0.999863,0.999962,0.00619598,-0.0060598});
extT << VEC_FROM_ARRAY(extrinT);
extR << MAT_FROM_ARRAY(extrinR);
voxelmap_manager->extT_ << VEC_FROM_ARRAY(extrinT);
voxelmap_manager->extR_ << MAT_FROM_ARRAY(extrinR);
if (!vk::camera_loader::loadFromRosNs(this->node, "camera", vio_manager->cam)) throw std::runtime_error("Camera model not correctly specified.");
vio_manager->grid_size = grid_size;
vio_manager->patch_size = patch_size;
vio_manager->outlier_threshold = outlier_threshold;
vio_manager->setImuToLidarExtrinsic(extT, extR);
vio_manager->setLidarToCameraExtrinsic(cameraextrinR, cameraextrinT);
vio_manager->state = &_state;
vio_manager->state_propagat = &state_propagat;
vio_manager->max_iterations = max_iterations;
vio_manager->img_point_cov = IMG_POINT_COV;
vio_manager->normal_en = normal_en;
vio_manager->inverse_composition_en = inverse_composition_en;
vio_manager->raycast_en = raycast_en;
vio_manager->grid_n_width = grid_n_width;
vio_manager->grid_n_height = grid_n_height;
vio_manager->patch_pyrimid_level = patch_pyrimid_level;
vio_manager->exposure_estimate_en = exposure_estimate_en;
vio_manager->colmap_output_en = colmap_output_en;
vio_manager->initializeVIO();
p_imu->set_extrinsic(extT, extR);
p_imu->set_gyr_cov_scale(V3D(gyr_cov, gyr_cov, gyr_cov));
p_imu->set_acc_cov_scale(V3D(acc_cov, acc_cov, acc_cov));
p_imu->set_inv_expo_cov(inv_expo_cov);
p_imu->set_gyr_bias_cov(V3D(0.0001, 0.0001, 0.0001));
p_imu->set_acc_bias_cov(V3D(0.0001, 0.0001, 0.0001));
p_imu->set_imu_init_frame_num(imu_int_frame);
if (!imu_en) p_imu->disable_imu();
if (!gravity_est_en) p_imu->disable_gravity_est();
if (!ba_bg_est_en) p_imu->disable_bias_est();
if (!exposure_estimate_en) p_imu->disable_exposure_est();
slam_mode_ = (img_en && lidar_en) ? LIVO : imu_en ? ONLY_LIO : ONLY_LO;
}
void LIVMapper::initializeFiles()
{
if (pcd_save_en && colmap_output_en)
{
const std::string folderPath = std::string(ROOT_DIR) + "/scripts/colmap_output.sh";
std::string chmodCommand = "chmod +x " + folderPath;
int chmodRet = system(chmodCommand.c_str());
if (chmodRet != 0) {
std::cerr << "Failed to set execute permissions for the script." << std::endl;
return;
}
int executionRet = system(folderPath.c_str());
if (executionRet != 0) {
std::cerr << "Failed to execute the script." << std::endl;
return;
}
}
if(colmap_output_en) fout_points.open(std::string(ROOT_DIR) + "Log/Colmap/sparse/0/points3D.txt", std::ios::out);
if(pcd_save_interval > 0) fout_pcd_pos.open(std::string(ROOT_DIR) + "Log/PCD/scans_pos.json", std::ios::out);
fout_pre.open(DEBUG_FILE_DIR("mat_pre.txt"), std::ios::out);
fout_out.open(DEBUG_FILE_DIR("mat_out.txt"), std::ios::out);
}
void LIVMapper::initializeSubscribersAndPublishers(rclcpp::Node::SharedPtr &node, image_transport::ImageTransport &it_)
{
image_transport::ImageTransport it(this->node);
if (p_pre->lidar_type == AVIA) {
sub_pcl = this->node->create_subscription<livox_ros_driver2::msg::CustomMsg>(lid_topic, 200000, std::bind(&LIVMapper::livox_pcl_cbk, this, std::placeholders::_1));
} else {
sub_pcl = this->node->create_subscription<sensor_msgs::msg::PointCloud2>(lid_topic, 200000, std::bind(&LIVMapper::standard_pcl_cbk, this, std::placeholders::_1));
}
sub_imu = this->node->create_subscription<sensor_msgs::msg::Imu>(imu_topic, 200000, std::bind(&LIVMapper::imu_cbk, this, std::placeholders::_1));
sub_img = this->node->create_subscription<sensor_msgs::msg::Image>(img_topic, 200000, std::bind(&LIVMapper::img_cbk, this, std::placeholders::_1));
pubLaserCloudFullRes = this->node->create_publisher<sensor_msgs::msg::PointCloud2>("/cloud_registered", 100);
pubNormal = this->node->create_publisher<visualization_msgs::msg::MarkerArray>("/visualization_marker", 100);
pubSubVisualMap = this->node->create_publisher<sensor_msgs::msg::PointCloud2>("/cloud_visual_sub_map_before", 100);
pubLaserCloudEffect = this->node->create_publisher<sensor_msgs::msg::PointCloud2>("/cloud_effected", 100);
pubLaserCloudMap = this->node->create_publisher<sensor_msgs::msg::PointCloud2>("/Laser_map", 100);
pubOdomAftMapped = this->node->create_publisher<nav_msgs::msg::Odometry>("/aft_mapped_to_init", 10);
pubPath = this->node->create_publisher<nav_msgs::msg::Path>("/path", 10);
plane_pub = this->node->create_publisher<visualization_msgs::msg::Marker>("/planner_normal", 1);
voxel_pub = this->node->create_publisher<visualization_msgs::msg::MarkerArray>("/voxels", 1);
pubLaserCloudDyn = this->node->create_publisher<sensor_msgs::msg::PointCloud2>("/dyn_obj", 100);
pubLaserCloudDynRmed = this->node->create_publisher<sensor_msgs::msg::PointCloud2>("/dyn_obj_removed", 100);
pubLaserCloudDynDbg = this->node->create_publisher<sensor_msgs::msg::PointCloud2>("/dyn_obj_dbg_hist", 100);
mavros_pose_publisher = this->node->create_publisher<geometry_msgs::msg::PoseStamped>("/mavros/vision_pose/pose", 10);
pubImage = it.advertise("/rgb_img", 1);
pubImuPropOdom = this->node->create_publisher<nav_msgs::msg::Odometry>("/LIVO2/imu_propagate", 10000);
imu_prop_timer = this->node->create_wall_timer(0.004s, std::bind(&LIVMapper::imu_prop_callback, this));
voxelmap_manager->voxel_map_pub_= this->node->create_publisher<visualization_msgs::msg::MarkerArray>("/planes", 10000);
}
void LIVMapper::handleFirstFrame()
{
if (!is_first_frame)
{
_first_lidar_time = LidarMeasures.last_lio_update_time;
p_imu->first_lidar_time = _first_lidar_time; // Only for IMU data log
is_first_frame = true;
cout << "FIRST LIDAR FRAME!" << endl;
}
}
void LIVMapper::gravityAlignment()
{
if (!p_imu->imu_need_init && !gravity_align_finished)
{
std::cout << "Gravity Alignment Starts" << std::endl;
V3D ez(0, 0, -1), gz(_state.gravity);
Eigen::Quaterniond G_q_I0 = Eigen::Quaterniond::FromTwoVectors(gz, ez);
M3D G_R_I0 = G_q_I0.toRotationMatrix();
_state.pos_end = G_R_I0 * _state.pos_end;
_state.rot_end = G_R_I0 * _state.rot_end;
_state.vel_end = G_R_I0 * _state.vel_end;
_state.gravity = G_R_I0 * _state.gravity;
gravity_align_finished = true;
std::cout << "Gravity Alignment Finished" << std::endl;
}
}
void LIVMapper::processImu()
{
// double t0 = omp_get_wtime();
p_imu->Process2(LidarMeasures, _state, feats_undistort);
if (gravity_align_en) gravityAlignment();
state_propagat = _state;
voxelmap_manager->state_ = _state;
voxelmap_manager->feats_undistort_ = feats_undistort;
// double t_prop = omp_get_wtime();
// std::cout << "[ Mapping ] feats_undistort: " << feats_undistort->size() << std::endl;
// std::cout << "[ Mapping ] predict cov: " << _state.cov.diagonal().transpose() << std::endl;
// std::cout << "[ Mapping ] predict sta: " << state_propagat.pos_end.transpose() << state_propagat.vel_end.transpose() << std::endl;
}
void LIVMapper::stateEstimationAndMapping()
{
switch (LidarMeasures.lio_vio_flg)
{
case VIO:
handleVIO();
break;
case LIO:
case LO:
handleLIO();
break;
}
}
void LIVMapper::handleVIO()
{
euler_cur = RotMtoEuler(_state.rot_end);
fout_pre << std::setw(20) << LidarMeasures.last_lio_update_time - _first_lidar_time << " " << euler_cur.transpose() * 57.3 << " "
<< _state.pos_end.transpose() << " " << _state.vel_end.transpose() << " " << _state.bias_g.transpose() << " "
<< _state.bias_a.transpose() << " " << V3D(_state.inv_expo_time, 0, 0).transpose() << std::endl;
if (pcl_w_wait_pub->empty() || (pcl_w_wait_pub == nullptr))
{
std::cout << "[ VIO ] No point!!!" << std::endl;
return;
}
std::cout << "[ VIO ] Raw feature num: " << pcl_w_wait_pub->points.size() << std::endl;
if (fabs((LidarMeasures.last_lio_update_time - _first_lidar_time) - plot_time) < (frame_cnt / 2 * 0.1))
{
vio_manager->plot_flag = true;
}
else
{
vio_manager->plot_flag = false;
}
vio_manager->processFrame(LidarMeasures.measures.back().img, _pv_list, voxelmap_manager->voxel_map_, LidarMeasures.last_lio_update_time - _first_lidar_time);
if (imu_prop_enable)
{
ekf_finish_once = true;
latest_ekf_state = _state;
latest_ekf_time = LidarMeasures.last_lio_update_time;
state_update_flg = true;
}
// int size_sub_map = vio_manager->visual_sub_map_cur.size();
// visual_sub_map->reserve(size_sub_map);
// for (int i = 0; i < size_sub_map; i++)
// {
// PointType temp_map;
// temp_map.x = vio_manager->visual_sub_map_cur[i]->pos_[0];
// temp_map.y = vio_manager->visual_sub_map_cur[i]->pos_[1];
// temp_map.z = vio_manager->visual_sub_map_cur[i]->pos_[2];
// temp_map.intensity = 0.;
// visual_sub_map->push_back(temp_map);
// }
publish_frame_world(pubLaserCloudFullRes, vio_manager);
publish_img_rgb(pubImage, vio_manager);
euler_cur = RotMtoEuler(_state.rot_end);
fout_out << std::setw(20) << LidarMeasures.last_lio_update_time - _first_lidar_time << " " << euler_cur.transpose() * 57.3 << " "
<< _state.pos_end.transpose() << " " << _state.vel_end.transpose() << " " << _state.bias_g.transpose() << " "
<< _state.bias_a.transpose() << " " << V3D(_state.inv_expo_time, 0, 0).transpose() << " " << feats_undistort->points.size() << std::endl;
}
void LIVMapper::handleLIO()
{
euler_cur = RotMtoEuler(_state.rot_end);
fout_pre << setw(20) << LidarMeasures.last_lio_update_time - _first_lidar_time << " " << euler_cur.transpose() * 57.3 << " "
<< _state.pos_end.transpose() << " " << _state.vel_end.transpose() << " " << _state.bias_g.transpose() << " "
<< _state.bias_a.transpose() << " " << V3D(_state.inv_expo_time, 0, 0).transpose() << endl;
if (feats_undistort->empty() || (feats_undistort == nullptr))
{
std::cout << "[ LIO ]: No point!!!" << std::endl;
return;
}
double t0 = omp_get_wtime();
downSizeFilterSurf.setInputCloud(feats_undistort);
downSizeFilterSurf.filter(*feats_down_body);
double t_down = omp_get_wtime();
feats_down_size = feats_down_body->points.size();
voxelmap_manager->feats_down_body_ = feats_down_body;
transformLidar(_state.rot_end, _state.pos_end, feats_down_body, feats_down_world);
voxelmap_manager->feats_down_world_ = feats_down_world;
voxelmap_manager->feats_down_size_ = feats_down_size;
if (!lidar_map_inited)
{
lidar_map_inited = true;
voxelmap_manager->BuildVoxelMap();
}
double t1 = omp_get_wtime();
voxelmap_manager->StateEstimation(state_propagat);
_state = voxelmap_manager->state_;
_pv_list = voxelmap_manager->pv_list_;
double t2 = omp_get_wtime();
if (imu_prop_enable)
{
ekf_finish_once = true;
latest_ekf_state = _state;
latest_ekf_time = LidarMeasures.last_lio_update_time;
state_update_flg = true;
}
if (pose_output_en)
{
static bool pos_opend = false;
static int ocount = 0;
std::ofstream outFile, evoFile;
if (!pos_opend)
{
evoFile.open(std::string(ROOT_DIR) + "Log/result/" + seq_name + ".txt", std::ios::out);
pos_opend = true;
if (!evoFile.is_open()) RCLCPP_ERROR(this->node->get_logger(), "open fail\n");
}
else
{
evoFile.open(std::string(ROOT_DIR) + "Log/result/" + seq_name + ".txt", std::ios::app);
if (!evoFile.is_open()) RCLCPP_ERROR(this->node->get_logger(), "open fail\n");
}
Eigen::Matrix4d outT;
Eigen::Quaterniond q(_state.rot_end);
evoFile << std::fixed;
evoFile << LidarMeasures.last_lio_update_time << " " << _state.pos_end[0] << " " << _state.pos_end[1] << " " << _state.pos_end[2] << " "
<< q.x() << " " << q.y() << " " << q.z() << " " << q.w() << std::endl;
}
euler_cur = RotMtoEuler(_state.rot_end);
geoQuat = tf::createQuaternionMsgFromRollPitchYaw(euler_cur(0), euler_cur(1), euler_cur(2));
publish_odometry(pubOdomAftMapped);
double t3 = omp_get_wtime();
PointCloudXYZI::Ptr world_lidar(new PointCloudXYZI());
transformLidar(_state.rot_end, _state.pos_end, feats_down_body, world_lidar);
for (size_t i = 0; i < world_lidar->points.size(); i++)
{
voxelmap_manager->pv_list_[i].point_w << world_lidar->points[i].x, world_lidar->points[i].y, world_lidar->points[i].z;
M3D point_crossmat = voxelmap_manager->cross_mat_list_[i];
M3D var = voxelmap_manager->body_cov_list_[i];
var = (_state.rot_end * extR) * var * (_state.rot_end * extR).transpose() +
(-point_crossmat) * _state.cov.block<3, 3>(0, 0) * (-point_crossmat).transpose() + _state.cov.block<3, 3>(3, 3);
voxelmap_manager->pv_list_[i].var = var;
}
voxelmap_manager->UpdateVoxelMap(voxelmap_manager->pv_list_);
std::cout << "[ LIO ] Update Voxel Map" << std::endl;
_pv_list = voxelmap_manager->pv_list_;
double t4 = omp_get_wtime();
if(voxelmap_manager->config_setting_.map_sliding_en)
{
voxelmap_manager->mapSliding();
}
PointCloudXYZI::Ptr laserCloudFullRes(dense_map_en ? feats_undistort : feats_down_body);
int size = laserCloudFullRes->points.size();
PointCloudXYZI::Ptr laserCloudWorld(new PointCloudXYZI(size, 1));
for (int i = 0; i < size; i++)
{
RGBpointBodyToWorld(&laserCloudFullRes->points[i], &laserCloudWorld->points[i]);
}
*pcl_w_wait_pub = *laserCloudWorld;
if (!img_en) publish_frame_world(pubLaserCloudFullRes, vio_manager);
if (pub_effect_point_en) publish_effect_world(pubLaserCloudEffect, voxelmap_manager->ptpl_list_);
if (voxelmap_manager->config_setting_.is_pub_plane_map_) voxelmap_manager->pubVoxelMap();
publish_path(pubPath);
publish_mavros(mavros_pose_publisher);
frame_num++;
aver_time_consu = aver_time_consu * (frame_num - 1) / frame_num + (t4 - t0) / frame_num;
// aver_time_icp = aver_time_icp * (frame_num - 1) / frame_num + (t2 - t1) / frame_num;
// aver_time_map_inre = aver_time_map_inre * (frame_num - 1) / frame_num + (t4 - t3) / frame_num;
// aver_time_solve = aver_time_solve * (frame_num - 1) / frame_num + (solve_time) / frame_num;
// aver_time_const_H_time = aver_time_const_H_time * (frame_num - 1) / frame_num + solve_const_H_time / frame_num;
// printf("[ mapping time ]: per scan: propagation %0.6f downsample: %0.6f match: %0.6f solve: %0.6f ICP: %0.6f map incre: %0.6f total: %0.6f \n"
// "[ mapping time ]: average: icp: %0.6f construct H: %0.6f, total: %0.6f \n",
// t_prop - t0, t1 - t_prop, match_time, solve_time, t3 - t1, t5 - t3, t5 - t0, aver_time_icp, aver_time_const_H_time, aver_time_consu);
// printf("\033[1;36m[ LIO mapping time ]: current scan: icp: %0.6f secs, map incre: %0.6f secs, total: %0.6f secs.\033[0m\n"
// "\033[1;36m[ LIO mapping time ]: average: icp: %0.6f secs, map incre: %0.6f secs, total: %0.6f secs.\033[0m\n",
// t2 - t1, t4 - t3, t4 - t0, aver_time_icp, aver_time_map_inre, aver_time_consu);
printf("\033[1;34m+-------------------------------------------------------------+\033[0m\n");
printf("\033[1;34m| LIO Mapping Time |\033[0m\n");
printf("\033[1;34m+-------------------------------------------------------------+\033[0m\n");
printf("\033[1;34m| %-29s | %-27s |\033[0m\n", "Algorithm Stage", "Time (secs)");
printf("\033[1;34m+-------------------------------------------------------------+\033[0m\n");
printf("\033[1;36m| %-29s | %-27f |\033[0m\n", "DownSample", t_down - t0);
printf("\033[1;36m| %-29s | %-27f |\033[0m\n", "ICP", t2 - t1);
printf("\033[1;36m| %-29s | %-27f |\033[0m\n", "updateVoxelMap", t4 - t3);
printf("\033[1;34m+-------------------------------------------------------------+\033[0m\n");
printf("\033[1;36m| %-29s | %-27f |\033[0m\n", "Current Total Time", t4 - t0);
printf("\033[1;36m| %-29s | %-27f |\033[0m\n", "Average Total Time", aver_time_consu);
printf("\033[1;34m+-------------------------------------------------------------+\033[0m\n");
euler_cur = RotMtoEuler(_state.rot_end);
fout_out << std::setw(20) << LidarMeasures.last_lio_update_time - _first_lidar_time << " " << euler_cur.transpose() * 57.3 << " "
<< _state.pos_end.transpose() << " " << _state.vel_end.transpose() << " " << _state.bias_g.transpose() << " "
<< _state.bias_a.transpose() << " " << V3D(_state.inv_expo_time, 0, 0).transpose() << " " << feats_undistort->points.size() << std::endl;
}
void LIVMapper::savePCD()
{
if (pcd_save_en && (pcl_wait_save->points.size() > 0 || pcl_wait_save_intensity->points.size() > 0) && pcd_save_interval < 0)
{
std::string raw_points_dir = std::string(ROOT_DIR) + "Log/PCD/all_raw_points.pcd";
std::string downsampled_points_dir = std::string(ROOT_DIR) + "Log/PCD/all_downsampled_points.pcd";
pcl::PCDWriter pcd_writer;
if (img_en)
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr downsampled_cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::VoxelGrid<pcl::PointXYZRGB> voxel_filter;
voxel_filter.setInputCloud(pcl_wait_save);
voxel_filter.setLeafSize(filter_size_pcd, filter_size_pcd, filter_size_pcd);
voxel_filter.filter(*downsampled_cloud);
pcd_writer.writeBinary(raw_points_dir, *pcl_wait_save); // Save the raw point cloud data
std::cout << GREEN << "Raw point cloud data saved to: " << raw_points_dir
<< " with point count: " << pcl_wait_save->points.size() << RESET << std::endl;
pcd_writer.writeBinary(downsampled_points_dir, *downsampled_cloud); // Save the downsampled point cloud data
std::cout << GREEN << "Downsampled point cloud data saved to: " << downsampled_points_dir
<< " with point count after filtering: " << downsampled_cloud->points.size() << RESET << std::endl;
if(colmap_output_en)
{
fout_points << "# 3D point list with one line of data per point\n";
fout_points << "# POINT_ID, X, Y, Z, R, G, B, ERROR\n";
for (size_t i = 0; i < downsampled_cloud->size(); ++i)
{
const auto& point = downsampled_cloud->points[i];
fout_points << i << " "
<< std::fixed << std::setprecision(6)
<< point.x << " " << point.y << " " << point.z << " "
<< static_cast<int>(point.r) << " "
<< static_cast<int>(point.g) << " "
<< static_cast<int>(point.b) << " "
<< 0 << std::endl;
}
}
}
else
{
pcd_writer.writeBinary(raw_points_dir, *pcl_wait_save_intensity);
std::cout << GREEN << "Raw point cloud data saved to: " << raw_points_dir
<< " with point count: " << pcl_wait_save_intensity->points.size() << RESET << std::endl;
}
}
}
void LIVMapper::run(rclcpp::Node::SharedPtr &node)
{
rclcpp::Rate rate(5000);
while (rclcpp::ok())
{
rclcpp::spin_some(this->node);
if (!sync_packages(LidarMeasures))
{
rate.sleep();
continue;
}
handleFirstFrame();
processImu();
// if (!p_imu->imu_time_init) continue;
stateEstimationAndMapping();
}
savePCD();
}
void LIVMapper::prop_imu_once(StatesGroup &imu_prop_state, const double dt, V3D acc_avr, V3D angvel_avr)
{
double mean_acc_norm = p_imu->IMU_mean_acc_norm;
acc_avr = acc_avr * G_m_s2 / mean_acc_norm - imu_prop_state.bias_a;
angvel_avr -= imu_prop_state.bias_g;
M3D Exp_f = Exp(angvel_avr, dt);
/* propogation of IMU attitude */
imu_prop_state.rot_end = imu_prop_state.rot_end * Exp_f;
/* Specific acceleration (global frame) of IMU */
V3D acc_imu = imu_prop_state.rot_end * acc_avr + V3D(imu_prop_state.gravity[0], imu_prop_state.gravity[1], imu_prop_state.gravity[2]);
/* propogation of IMU */
imu_prop_state.pos_end = imu_prop_state.pos_end + imu_prop_state.vel_end * dt + 0.5 * acc_imu * dt * dt;
/* velocity of IMU */
imu_prop_state.vel_end = imu_prop_state.vel_end + acc_imu * dt;
}
void LIVMapper::imu_prop_callback()
{
if (p_imu->imu_need_init || !new_imu || !ekf_finish_once) { return; }
mtx_buffer_imu_prop.lock();
new_imu = false; // 控制 propagate 频率和 IMU 频率一致
if (imu_prop_enable && !prop_imu_buffer.empty())
{
static double last_t_from_lidar_end_time = 0;
if (state_update_flg)
{
imu_propagate = latest_ekf_state;
// drop all useless imu pkg
while ((!prop_imu_buffer.empty() && stamp2Sec(prop_imu_buffer.front().header.stamp) < latest_ekf_time))
{
prop_imu_buffer.pop_front();
}
last_t_from_lidar_end_time = 0;
for (int i = 0; i < prop_imu_buffer.size(); i++)
{
double t_from_lidar_end_time = stamp2Sec(prop_imu_buffer[i].header.stamp) - latest_ekf_time;
double dt = t_from_lidar_end_time - last_t_from_lidar_end_time;
// cout << "prop dt" << dt << ", " << t_from_lidar_end_time << ", " << last_t_from_lidar_end_time << endl;
V3D acc_imu(prop_imu_buffer[i].linear_acceleration.x, prop_imu_buffer[i].linear_acceleration.y, prop_imu_buffer[i].linear_acceleration.z);
V3D omg_imu(prop_imu_buffer[i].angular_velocity.x, prop_imu_buffer[i].angular_velocity.y, prop_imu_buffer[i].angular_velocity.z);
prop_imu_once(imu_propagate, dt, acc_imu, omg_imu);
last_t_from_lidar_end_time = t_from_lidar_end_time;
}
state_update_flg = false;
}
else
{
V3D acc_imu(newest_imu.linear_acceleration.x, newest_imu.linear_acceleration.y, newest_imu.linear_acceleration.z);
V3D omg_imu(newest_imu.angular_velocity.x, newest_imu.angular_velocity.y, newest_imu.angular_velocity.z);
double t_from_lidar_end_time = stamp2Sec(newest_imu.header.stamp) - latest_ekf_time;
double dt = t_from_lidar_end_time - last_t_from_lidar_end_time;
prop_imu_once(imu_propagate, dt, acc_imu, omg_imu);
last_t_from_lidar_end_time = t_from_lidar_end_time;
}
V3D posi, vel_i;
Eigen::Quaterniond q;
posi = imu_propagate.pos_end;
vel_i = imu_propagate.vel_end;
q = Eigen::Quaterniond(imu_propagate.rot_end);
imu_prop_odom.header.frame_id = "world";
imu_prop_odom.header.stamp = newest_imu.header.stamp;
imu_prop_odom.pose.pose.position.x = posi.x();
imu_prop_odom.pose.pose.position.y = posi.y();
imu_prop_odom.pose.pose.position.z = posi.z();
imu_prop_odom.pose.pose.orientation.w = q.w();
imu_prop_odom.pose.pose.orientation.x = q.x();
imu_prop_odom.pose.pose.orientation.y = q.y();
imu_prop_odom.pose.pose.orientation.z = q.z();
imu_prop_odom.twist.twist.linear.x = vel_i.x();
imu_prop_odom.twist.twist.linear.y = vel_i.y();
imu_prop_odom.twist.twist.linear.z = vel_i.z();
pubImuPropOdom->publish(imu_prop_odom);
}
mtx_buffer_imu_prop.unlock();
}
void LIVMapper::transformLidar(const Eigen::Matrix3d rot, const Eigen::Vector3d t, const PointCloudXYZI::Ptr &input_cloud, PointCloudXYZI::Ptr &trans_cloud)
{
PointCloudXYZI().swap(*trans_cloud);
trans_cloud->reserve(input_cloud->size());
for (size_t i = 0; i < input_cloud->size(); i++)
{
pcl::PointXYZINormal p_c = input_cloud->points[i];
Eigen::Vector3d p(p_c.x, p_c.y, p_c.z);
p = (rot * (extR * p + extT) + t);
PointType pi;
pi.x = p(0);
pi.y = p(1);
pi.z = p(2);
pi.intensity = p_c.intensity;
trans_cloud->points.push_back(pi);
}
}
void LIVMapper::pointBodyToWorld(const PointType &pi, PointType &po)
{
V3D p_body(pi.x, pi.y, pi.z);
V3D p_global(_state.rot_end * (extR * p_body + extT) + _state.pos_end);
po.x = p_global(0);
po.y = p_global(1);
po.z = p_global(2);
po.intensity = pi.intensity;
}
template <typename T> void LIVMapper::pointBodyToWorld(const Matrix<T, 3, 1> &pi, Matrix<T, 3, 1> &po)
{
V3D p_body(pi[0], pi[1], pi[2]);
V3D p_global(_state.rot_end * (extR * p_body + extT) + _state.pos_end);
po[0] = p_global(0);
po[1] = p_global(1);
po[2] = p_global(2);
}
template <typename T> Matrix<T, 3, 1> LIVMapper::pointBodyToWorld(const Matrix<T, 3, 1> &pi)
{
V3D p(pi[0], pi[1], pi[2]);
p = (_state.rot_end * (extR * p + extT) + _state.pos_end);
Eigen::Matrix<T, 3, 1> po(p[0], p[1], p[2]);
return po;
}
void LIVMapper::RGBpointBodyToWorld(PointType const *const pi, PointType *const po)
{
V3D p_body(pi->x, pi->y, pi->z);
V3D p_global(_state.rot_end * (extR * p_body + extT) + _state.pos_end);
po->x = p_global(0);
po->y = p_global(1);
po->z = p_global(2);
po->intensity = pi->intensity;
}
void LIVMapper::standard_pcl_cbk(const sensor_msgs::msg::PointCloud2::ConstSharedPtr &msg)
{
if (!lidar_en) return;
mtx_buffer.lock();
// cout<<"got feature"<<endl;
if (stamp2Sec(msg->header.stamp) < last_timestamp_lidar)
{
RCLCPP_ERROR(this->node->get_logger(),"lidar loop back, clear buffer");
lid_raw_data_buffer.clear();
}
// ROS_INFO("get point cloud at time: %.6f", stamp2Sec(msg->header.stamp));
PointCloudXYZI::Ptr ptr(new PointCloudXYZI());
p_pre->process(msg, ptr);
lid_raw_data_buffer.push_back(ptr);
lid_header_time_buffer.push_back(stamp2Sec(msg->header.stamp));
last_timestamp_lidar = stamp2Sec(msg->header.stamp);
mtx_buffer.unlock();
sig_buffer.notify_all();
}
void LIVMapper::livox_pcl_cbk(const livox_ros_driver2::msg::CustomMsg::ConstSharedPtr &msg_in)
{
if (!lidar_en) return;
mtx_buffer.lock();
livox_ros_driver2::msg::CustomMsg::SharedPtr msg(new livox_ros_driver2::msg::CustomMsg(*msg_in));
// if ((abs(stamp2Sec(msg->header.stamp) - last_timestamp_lidar) > 0.2 && last_timestamp_lidar > 0) || sync_jump_flag)
// {
// ROS_WARN("lidar jumps %.3f\n", stamp2Sec(msg->header.stamp) - last_timestamp_lidar);
// sync_jump_flag = true;
// msg->header.stamp = rclcpp::Time().fromSec(last_timestamp_lidar + 0.1);
// }
if (abs(last_timestamp_imu - stamp2Sec(msg->header.stamp)) > 1.0 && !imu_buffer.empty())
{
double timediff_imu_wrt_lidar = last_timestamp_imu - stamp2Sec(msg->header.stamp);
RCLCPP_INFO(this->node->get_logger(), "\033[95mSelf sync IMU and LiDAR, HARD time lag is %.10lf \n\033[0m", timediff_imu_wrt_lidar - 0.100);
// imu_time_offset = timediff_imu_wrt_lidar;
}
double cur_head_time = stamp2Sec(msg->header.stamp);
RCLCPP_INFO(this->node->get_logger(), "Get LiDAR, its header time: %.6f", cur_head_time);
if (cur_head_time < last_timestamp_lidar)
{
RCLCPP_ERROR(this->node->get_logger(), "lidar loop back, clear buffer");
lid_raw_data_buffer.clear();
}
RCLCPP_INFO(this->node->get_logger(), "get point cloud at time: %.6f", stamp2Sec(msg->header.stamp));
PointCloudXYZI::Ptr ptr(new PointCloudXYZI());
p_pre->process(msg, ptr);
if (!ptr || ptr->empty()) {
RCLCPP_ERROR(this->node->get_logger(), "Received an empty point cloud");
mtx_buffer.unlock();
return;
}
lid_raw_data_buffer.push_back(ptr);
lid_header_time_buffer.push_back(cur_head_time);
last_timestamp_lidar = cur_head_time;
mtx_buffer.unlock();
sig_buffer.notify_all();
}
void LIVMapper::imu_cbk(const sensor_msgs::msg::Imu::ConstSharedPtr &msg_in)
{
if (!imu_en) return;
if (last_timestamp_lidar < 0.0) return;
RCLCPP_INFO(this->node->get_logger(), "get imu at time: %.6f", stamp2Sec(msg_in->header.stamp));
sensor_msgs::msg::Imu::SharedPtr msg(new sensor_msgs::msg::Imu(*msg_in));
msg->header.stamp = sec2Stamp(stamp2Sec(msg->header.stamp) - imu_time_offset);
double timestamp = stamp2Sec(msg->header.stamp);
if (fabs(last_timestamp_lidar - timestamp) > 0.5 && (!ros_driver_fix_en))
{
RCLCPP_WARN(this->node->get_logger(), "IMU and LiDAR not synced! delta time: %lf .\n", last_timestamp_lidar - timestamp);
}
if (ros_driver_fix_en) timestamp += std::round(last_timestamp_lidar - timestamp);
msg->header.stamp = sec2Stamp(timestamp);
mtx_buffer.lock();
if (last_timestamp_imu > 0.0 && timestamp < last_timestamp_imu)
{
mtx_buffer.unlock();
sig_buffer.notify_all();
RCLCPP_ERROR(this->node->get_logger(), "imu loop back, offset: %lf \n", last_timestamp_imu - timestamp);
return;
}
if (last_timestamp_imu > 0.0 && timestamp > last_timestamp_imu + 0.2)
{
RCLCPP_WARN(this->node->get_logger(), "imu time stamp Jumps %0.4lf seconds \n", timestamp - last_timestamp_imu);
mtx_buffer.unlock();
sig_buffer.notify_all();
return;
}
last_timestamp_imu = timestamp;
imu_buffer.push_back(msg);
cout<<"got imu: "<<timestamp<<" imu size "<<imu_buffer.size()<<endl;
mtx_buffer.unlock();
if (imu_prop_enable)
{
mtx_buffer_imu_prop.lock();
if (imu_prop_enable && !p_imu->imu_need_init) { prop_imu_buffer.push_back(*msg); }
newest_imu = *msg;
new_imu = true;
mtx_buffer_imu_prop.unlock();
}
sig_buffer.notify_all();
}
cv::Mat LIVMapper::getImageFromMsg(const sensor_msgs::msg::Image::ConstSharedPtr &img_msg)
{
cv::Mat img;
img = cv_bridge::toCvShare(img_msg, "bgr8")->image;
return img;
}
// static int i = 0;
void LIVMapper::img_cbk(const sensor_msgs::msg::Image::ConstSharedPtr &msg_in)
{
if (!img_en) return;
sensor_msgs::msg::Image::SharedPtr msg(new sensor_msgs::msg::Image(*msg_in));
// if ((abs(stamp2Sec(msg->header.stamp) - last_timestamp_img) > 0.2 && last_timestamp_img > 0) || sync_jump_flag)
// {
// RCLCPP_WARN(this->node->get_logger(), "img jumps %.3f\n", stamp2Sec(msg->header.stamp) - last_timestamp_img);
// sync_jump_flag = true;
// msg->header.stamp = rclcpp::Time().fromSec(last_timestamp_img + 0.1);
// }
// Hiliti2022 40Hz
// if (hilti_en)
// {
// i++;
// if (i % 4 != 0) return;
// }
// double msg_header_time = stamp2Sec(msg->header.stamp);
double msg_header_time = stamp2Sec(msg->header.stamp) + img_time_offset;
if (abs(msg_header_time - last_timestamp_img) < 0.001) return;
RCLCPP_INFO(this->node->get_logger(), "Get image, its header time: %.6f", msg_header_time);
if (last_timestamp_lidar < 0) return;
if (msg_header_time < last_timestamp_img)
{
RCLCPP_ERROR(this->node->get_logger(), "image loop back. \n");
return;
}
mtx_buffer.lock();
double img_time_correct = msg_header_time; // last_timestamp_lidar + 0.105;
if (img_time_correct - last_timestamp_img < 0.02)
{
RCLCPP_WARN(this->node->get_logger(), "Image need Jumps: %.6f", img_time_correct);
mtx_buffer.unlock();
sig_buffer.notify_all();
return;
}
cv::Mat img_cur = getImageFromMsg(msg);
img_buffer.push_back(img_cur);
img_time_buffer.push_back(img_time_correct);
// ROS_INFO("Correct Image time: %.6f", img_time_correct);
last_timestamp_img = img_time_correct;
// cv::imshow("img", img);
// cv::waitKey(1);
// cout<<"last_timestamp_img:::"<<last_timestamp_img<<endl;
mtx_buffer.unlock();
sig_buffer.notify_all();
}
bool LIVMapper::sync_packages(LidarMeasureGroup &meas)
{
if (lid_raw_data_buffer.empty() && lidar_en) return false;
if (img_buffer.empty() && img_en) return false;
if (imu_buffer.empty() && imu_en) return false;
switch (slam_mode_)
{
case ONLY_LIO:
{
if (meas.last_lio_update_time < 0.0) meas.last_lio_update_time = lid_header_time_buffer.front();
if (!lidar_pushed)
{
// If not push the lidar into measurement data buffer
meas.lidar = lid_raw_data_buffer.front(); // push the first lidar topic
if (meas.lidar->points.size() <= 1) return false;
meas.lidar_frame_beg_time = lid_header_time_buffer.front(); // generate lidar_frame_beg_time
meas.lidar_frame_end_time = meas.lidar_frame_beg_time + meas.lidar->points.back().curvature / double(1000); // calc lidar scan end time
meas.pcl_proc_cur = meas.lidar;
lidar_pushed = true; // flag
}
if (imu_en && last_timestamp_imu < meas.lidar_frame_end_time)
{ // waiting imu message needs to be
// larger than _lidar_frame_end_time,
// make sure complete propagate.
// ROS_ERROR("out sync");
return false;
}
struct MeasureGroup m; // standard method to keep imu message.
m.imu.clear();
m.lio_time = meas.lidar_frame_end_time;
mtx_buffer.lock();
while (!imu_buffer.empty())
{
if (stamp2Sec(imu_buffer.front()->header.stamp) > meas.lidar_frame_end_time) break;
m.imu.push_back(imu_buffer.front());
imu_buffer.pop_front();
}
lid_raw_data_buffer.pop_front();
lid_header_time_buffer.pop_front();
mtx_buffer.unlock();
sig_buffer.notify_all();
meas.lio_vio_flg = LIO; // process lidar topic, so timestamp should be lidar scan end.
meas.measures.push_back(m);
// ROS_INFO("ONlY HAS LiDAR and IMU, NO IMAGE!");
lidar_pushed = false; // sync one whole lidar scan.
return true;