-
Notifications
You must be signed in to change notification settings - Fork 35
/
pose_estimator.cpp
752 lines (630 loc) · 33.5 KB
/
pose_estimator.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
#include "pose_estimator.h"
#include "../FRICP-toolkit/registeration.h"
pose_estimator::pose_estimator(){
allocateMemory();
nh.param<std::string>("relo/priorDir", priorDir, " ");
nh.param<std::string>("relo/cloudTopic", cloudTopic, "/cloud_registered");
nh.param<std::string>("relo/poseTopic", poseTopic, "/Odometry");
cloudTopic_repub = cloudTopic + "repub"; // FIXME: no use
poseTopic_repub = poseTopic + "repub"; // FIXME: no use
nh.param<float>("relo/searchDis", searchDis, 10.0);
nh.param<int>("relo/searchNum", searchNum, 3);
nh.param<float>("relo/trustDis", trustDis, 5.0);
nh.param<int>("relo/regMode", regMode, 5);
nh.param<std::vector<double>>("relo/extrinsic_T", extrinT_, std::vector<double>());
nh.param<std::vector<double>>("relo/extrinsic_R", extrinR_, std::vector<double>());
extrinT << VEC_FROM_ARRAY(extrinT_);
extrinR << MAT_FROM_ARRAY(extrinR_);
std::cout << "extrinT: " << extrinT << "\t" << "extrinR: " << extrinR << std::endl;
Eigen::Matrix<double, 3, 1> euler_ext = RotMtoEuler(extrinR);
pose_ext.x = extrinT(0);
pose_ext.y = extrinT(1);
pose_ext.z = extrinT(2);
pose_ext.roll = euler_ext(0, 0);
pose_ext.pitch = euler_ext(1, 0);
pose_ext.yaw = euler_ext(2, 0);
pose_zero.x = 0.0;
pose_zero.y = 0.0;
pose_zero.z = 0.0;
pose_zero.roll = 0.0;
pose_zero.pitch = 0.0;
pose_zero.yaw = 0.0;
subCloud = nh.subscribe<sensor_msgs::PointCloud2>(cloudTopic, 1, &pose_estimator::cloudCBK, this);
subPose = nh.subscribe<nav_msgs::Odometry>(poseTopic, 500, &pose_estimator::poseCBK, this);
pubCloud = nh.advertise<sensor_msgs::PointCloud2>("/cloud", 1);
pubPose = nh.advertise<nav_msgs::Odometry>("/pose", 1);
fout_relo.open(priorDir + "relo_pose.txt", ios::out);
subExternalPose = nh.subscribe<geometry_msgs::PoseWithCovarianceStamped>("/initialpose", 500, &pose_estimator::externalCBK, this);
pubPriorMap = nh.advertise<sensor_msgs::PointCloud2>("/prior_map", 1);
pubPriorPath = nh.advertise<sensor_msgs::PointCloud2>("/prior_path", 1);
pubReloCloud = nh.advertise<sensor_msgs::PointCloud2>("/relo_cloud", 1);
pubInitCloud = nh.advertise<sensor_msgs::PointCloud2>("/init_cloud", 1);
pubNearCloud = nh.advertise<sensor_msgs::PointCloud2>("/near_cloud", 1);
pubMeasurementEdge = nh.advertise<visualization_msgs::MarkerArray>("measurement", 1);
pubPath = nh.advertise<nav_msgs::Path>("/path_loc", 1e00000);
std::cout << ANSI_COLOR_GREEN << "rostopic is ok" << ANSI_COLOR_RESET << std::endl;
sessions.push_back(MultiSession::Session(1, "priorMap", priorDir, true));
// std::string path_6D = priorDir + "/transformations.pcd";
// std::string path_3D = priorDir + "/trajectory.pcd";
// std::string path_map = priorDir + "/globalMap.pcd";
// pcl::io::loadPCDFile(path_6D, *sessions[0].cloudKeyPoses6D);
// pcl::io::loadPCDFile(path_3D, *sessions[0].cloudKeyPoses3D);
// pcl::io::loadPCDFile(path_map, *priorMap);
*priorMap += *sessions[0].globalMap;
*priorPath += *sessions[0].cloudKeyPoses3D;
downSizeFilterPub.setLeafSize(3.0, 3.0, 3.0);
height = priorPath->points[0].z;
kdtreeGlobalMapPoses->setInputCloud(priorPath);
kdtreeGlobalMapPoses_copy->setInputCloud(priorPath);
std::cout << ANSI_COLOR_GREEN << "load prior knowledge" << ANSI_COLOR_RESET << std::endl;
reg.push_back(Registeration(regMode));
}
void pose_estimator::allocateMemory(){
priorMap.reset(new pcl::PointCloud<PointType>());
priorPath.reset(new pcl::PointCloud<PointType>());
reloCloud.reset(new pcl::PointCloud<PointType>());
initCloud.reset(new pcl::PointCloud<PointType>());
initCloud_.reset(new pcl::PointCloud<PointType>());
nearCloud.reset(new pcl::PointCloud<PointType>());
kdtreeGlobalMapPoses.reset(new pcl::KdTreeFLANN<PointType>());
kdtreeGlobalMapPoses_copy.reset(new pcl::KdTreeFLANN<PointType>());
}
void pose_estimator::cloudCBK(const sensor_msgs::PointCloud2::ConstPtr& msg){
pcl::PointCloud<PointType>::Ptr msgCloud(new pcl::PointCloud<PointType>());
pcl::fromROSMsg(*msg, *msgCloud);
if(msgCloud->points.size() == 0){
return ;
}
msgCloud->width = msgCloud->points.size();
msgCloud->height = 1;
cloudBuffer.push_back(msgCloud);
}
void pose_estimator::poseCBK(const nav_msgs::Odometry::ConstPtr& msg){
PointTypePose pose;
pose.x = msg->pose.pose.position.x;
pose.y = msg->pose.pose.position.y;
pose.z = msg->pose.pose.position.z;
Eigen::Vector4d q(msg->pose.pose.orientation.x,
msg->pose.pose.orientation.y,
msg->pose.pose.orientation.z,
msg->pose.pose.orientation.w);
quaternionNormalize(q);
Eigen::Matrix3d rot = quaternionToRotation(q);
Eigen::Matrix<double, 3, 1> euler = RotMtoEuler(rot);
pose.roll = euler(0,0);
pose.pitch = euler(1,0);
pose.yaw = euler(2,0);
poseBuffer_6D.push_back(pose);
PointType pose3d;
pose3d.x = msg->pose.pose.position.x;
pose3d.y = msg->pose.pose.position.y;
// pose3d.z = msg->pose.pose.position.z;
pose3d.z = height;
poseBuffer_3D.push_back(pose3d);
}
void pose_estimator::externalCBK(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& msg){
if(external_flg){
return ;
}
std::cout << ANSI_COLOR_RED << "please set your external pose now ... " << ANSI_COLOR_RESET << std::endl;
externalPose.x = msg->pose.pose.position.x;
externalPose.y = msg->pose.pose.position.y;
externalPose.z = 0.0;
double roll, pitch, yaw;
tf::Quaternion q;
tf::quaternionMsgToTF(msg->pose.pose.orientation, q);
tf::Matrix3x3(q).getRPY(roll, pitch, yaw);
externalPose.roll = 0.0; // FIXME: it's better to choose zero
externalPose.pitch = 0.0;
externalPose.yaw = yaw;
std::cout << ANSI_COLOR_GREEN << "Get initial pose: " << externalPose.x << " " << externalPose.y << " " << externalPose.z
<< " " << externalPose.roll << " " << externalPose.pitch << " " << externalPose.yaw << ANSI_COLOR_RESET << std::endl;
external_flg = true;
}
void pose_estimator::run(){
ros::Rate rate(20);
while(ros::ok()){
ros::spinOnce();
if(!global_flg){
if(cout_count_ < 1)
std::cout << ANSI_COLOR_RED << "wait for global pose initialization ... " << ANSI_COLOR_RESET << std::endl;
global_flg = globalRelo();
cout_count_ = 1;
continue;
}
if(idx >= cloudBuffer.size()){
// std::cout << ANSI_COLOR_RED << "relo > subscribe ... " << ANSI_COLOR_RESET << std::endl;
publish_odometry(pubPose);
publish_path(pubPath);
publishCloud(&pubReloCloud, reloCloud, ros::Time().fromSec(ld_time), "world");
continue;
}
ld_time = ros::Time().fromSec(ld_time).toSec();
pcl::PointCloud<PointType>::Ptr relo_pt(new pcl::PointCloud<PointType>());
relo_pt->points.emplace_back(poseBuffer_3D[idx]);
relo_pt = transformPointCloud(relo_pt, &initPose);
std::cout << ANSI_COLOR_GREEN << "get current relo point " << idx << ANSI_COLOR_RESET << std::endl;
if(easyToRelo(relo_pt->points[0])){
// if(0){
std::cout << ANSI_COLOR_GREEN << "relo mode for frame: " << idx << ANSI_COLOR_RESET << std::endl;
pcl::PointCloud<PointType>::Ptr curCloud(new pcl::PointCloud<PointType>());
*curCloud += *transformPointCloud(cloudBuffer[idx], &initPose);
std::cout << "current cloud size: " << curCloud->points.size() << std::endl;
nearCloud->clear();
for(auto& it : idxVec){
std::cout << ANSI_COLOR_GREEN << "get near frame: " << it << ANSI_COLOR_RESET << std::endl;
pcl::PointCloud<PointType>::Ptr nearCloud_tmp(new pcl::PointCloud<PointType>());
*nearCloud_tmp += *transformPointCloud(sessions[0].cloudKeyFrames[it].all_cloud, &pose_ext);
*nearCloud += *transformPointCloud(nearCloud_tmp, &sessions[0].cloudKeyPoses6D->points[it]);
}
Eigen::MatrixXd transform = reg[0].run(curCloud, nearCloud);
Eigen::Matrix3d rot = transform.block(0, 0, 3, 3);
Eigen::MatrixXd linear = transform.block(0, 3, 3, 1);
Eigen::Matrix<double, 3, 1> euler = RotMtoEuler(rot);
PointTypePose pose_icp;
pose_icp.x = linear(0, 0);
pose_icp.y = linear(1, 0);
pose_icp.z = linear(2, 0);
pose_icp.roll = euler(0, 0);
pose_icp.pitch = euler(1, 0);
pose_icp.yaw = euler(2, 0);
// floor(pose_icp.x, 0.5);
// floor(pose_icp.y, 0.5);
// floor(pose_icp.z, 0.1);
// floor(pose_icp.roll, 0.1);
// floor(pose_icp.pitch, 0.1);
// floor(pose_icp.yaw, 0.2);
reloCloud->clear();
*reloCloud += *transformPointCloud(curCloud, &pose_icp);
publishCloud(&pubReloCloud, reloCloud, ros::Time().fromSec(ld_time), "world");
Eigen::Affine3f trans_buffer = pcl::getTransformation(poseBuffer_6D[idx].x, poseBuffer_6D[idx].y, poseBuffer_6D[idx].z, poseBuffer_6D[idx].roll, poseBuffer_6D[idx].pitch, poseBuffer_6D[idx].yaw);
Eigen::Affine3f trans_init = pcl::getTransformation(initPose.x, initPose.y, initPose.z, initPose.roll, initPose.pitch, initPose.yaw);
Eigen::Affine3f trans_res = pcl::getTransformation(pose_icp.x, pose_icp.y, pose_icp.z, pose_icp.roll, pose_icp.pitch, pose_icp.yaw);
Eigen::Affine3f trans_aft = trans_res * trans_init * trans_buffer;
float aft[6];
pcl::getTranslationAndEulerAngles(trans_aft, aft[0], aft[1], aft[2],
aft[3], aft[4], aft[5]);
PointTypePose pose_aft;
pose_aft.x = aft[0];
pose_aft.y = aft[1];
pose_aft.z = aft[2];
pose_aft.roll = aft[3];
pose_aft.pitch = aft[4];
pose_aft.yaw = aft[5];
Eigen::Matrix3d R = Exp((double)pose_aft.roll, (double)pose_aft.pitch, (double)pose_aft.yaw);
Eigen::Vector3d t((double)pose_aft.x, (double)pose_aft.y, (double)pose_aft.z);
fout_relo << std::fixed << R(0, 0) << " " << R(0, 1) << " " << R(0, 2) << " " << t[0] << " "
<< R(1, 0) << " " << R(1, 1) << " " << R(1, 2) << " " << t[1] << " "
<< R(2, 0) << " " << R(2, 1) << " " << R(2, 2) << " " << t[2] << std::endl;
;
reloPoseBuffer.push_back(pose_aft);
Eigen::Matrix<double, 3, 3> ang_rot = Exp((double)pose_aft.roll, (double)pose_aft.pitch, (double)pose_aft.yaw);
Eigen::Vector4d q = rotationToQuaternion(ang_rot);
quaternionNormalize(q);
odomAftMapped.pose.pose.position.x = pose_aft.x;
odomAftMapped.pose.pose.position.y = pose_aft.y;
odomAftMapped.pose.pose.position.z = pose_aft.z;
odomAftMapped.pose.pose.orientation.x = q(0);
odomAftMapped.pose.pose.orientation.y = q(1);
odomAftMapped.pose.pose.orientation.z = q(2);
odomAftMapped.pose.pose.orientation.w = q(3);
publish_odometry(pubPose);
msg_body_pose.pose.position.x = pose_aft.x;
msg_body_pose.pose.position.y = pose_aft.y;
msg_body_pose.pose.position.z = pose_aft.z;
msg_body_pose.pose.orientation.x = q(0);
msg_body_pose.pose.orientation.y = q(1);
msg_body_pose.pose.orientation.z = q(2);
msg_body_pose.pose.orientation.w = q(3);
publish_path(pubPath);
height = pose_aft.z;
idx ++;
}
else{
std::cout << ANSI_COLOR_RED << "lio mode for frame: " << idx << ANSI_COLOR_RESET << std::endl;
pcl::PointCloud<PointType>::Ptr curCloud(new pcl::PointCloud<PointType>());
*curCloud += *transformPointCloud(cloudBuffer[idx], &initPose);
std::cout << "current cloud size: " << curCloud->points.size() << std::endl;
reloCloud->clear();
*reloCloud += *curCloud;
publishCloud(&pubReloCloud, reloCloud, ros::Time().fromSec(ld_time), "world");
// *sessions[0].globalMap += *curCloud;
downSizeFilterPub.setInputCloud(curCloud);
downSizeFilterPub.filter(*curCloud);
*priorMap += *curCloud;
pcl::PointCloud<PointType>::Ptr invCloud(new pcl::PointCloud<PointType>());
*invCloud += *getBodyCloud(cloudBuffer[idx], poseBuffer_6D[idx], pose_zero);
invCloud = getBodyCloud(invCloud, pose_ext, pose_zero);
KeyFrame newFrame;
newFrame.all_cloud = invCloud;
sessions[0].cloudKeyFrames.push_back(newFrame);
std::cout << "newFrame.all_cloud size: " << newFrame.all_cloud->points.size() << std::endl;
sessions[0].scManager.makeAndSaveScancontextAndKeys(*invCloud);
std::cout << "add current sc" << std::endl;
Eigen::Affine3f trans_buffer = pcl::getTransformation(poseBuffer_6D[idx].x, poseBuffer_6D[idx].y, poseBuffer_6D[idx].z, poseBuffer_6D[idx].roll, poseBuffer_6D[idx].pitch, poseBuffer_6D[idx].yaw);
Eigen::Affine3f trans_init = pcl::getTransformation(initPose.x, initPose.y, initPose.z, initPose.roll, initPose.pitch, initPose.yaw);
Eigen::Affine3f trans_aft = trans_init * trans_buffer;
float aft[6];
pcl::getTranslationAndEulerAngles(trans_aft, aft[0], aft[1], aft[2],
aft[3], aft[4], aft[5]);
PointTypePose pose_aft;
pose_aft.x = aft[0];
pose_aft.y = aft[1];
// pose_aft.z = aft[2];
pose_aft.z = height;
pose_aft.roll = aft[3];
pose_aft.pitch = aft[4];
pose_aft.yaw = aft[5];
Eigen::Matrix3d R = Exp((double)pose_aft.roll, (double)pose_aft.pitch, (double)pose_aft.yaw);
Eigen::Vector3d t((double)pose_aft.x, (double)pose_aft.y, (double)pose_aft.z);
fout_relo << std::fixed << R(0, 0) << " " << R(0, 1) << " " << R(0, 2) << " " << t[0] << " "
<< R(1, 0) << " " << R(1, 1) << " " << R(1, 2) << " " << t[1] << " "
<< R(2, 0) << " " << R(2, 1) << " " << R(2, 2) << " " << t[2] << std::endl;
reloPoseBuffer.push_back(pose_aft);
std::cout << "reloPoseBuffer size: " << reloPoseBuffer.size() << std::endl;
sessions[0].cloudKeyPoses6D->points.push_back(pose_aft);
sessions[0].cloudKeyPoses6D->width = sessions[0].cloudKeyPoses6D->points.size();
sessions[0].cloudKeyPoses6D->height = 1;
std::cout << "cloudKeyPoses6D size: " << sessions[0].cloudKeyPoses6D->points.size() << std::endl;
PointType pose3d;
pose3d.x = pose_aft.x;
pose3d.y = pose_aft.y;
// pose3d.z = pose_aft.z;
pose3d.z = height;
sessions[0].cloudKeyPoses3D->points.push_back(pose3d);
sessions[0].cloudKeyPoses3D->width = sessions[0].cloudKeyPoses3D->points.size();
sessions[0].cloudKeyPoses3D->height = 1;
priorPath->points.push_back(pose3d);
priorPath->width = priorPath->points.size();
priorPath->height = 1;
kdtreeGlobalMapPoses_copy->setInputCloud(priorPath);
std::cout << "priorPath size: " << priorPath->points.size() << std::endl;
Eigen::Matrix<double, 3, 3> ang_rot = Exp((double)pose_aft.roll, (double)pose_aft.pitch, (double)pose_aft.yaw);
Eigen::Vector4d q = rotationToQuaternion(ang_rot);
quaternionNormalize(q);
odomAftMapped.pose.pose.position.x = pose_aft.x;
odomAftMapped.pose.pose.position.y = pose_aft.y;
odomAftMapped.pose.pose.position.z = pose_aft.z;
odomAftMapped.pose.pose.orientation.x = q(0);
odomAftMapped.pose.pose.orientation.y = q(1);
odomAftMapped.pose.pose.orientation.z = q(2);
odomAftMapped.pose.pose.orientation.w = q(3);
publish_odometry(pubPose);
msg_body_pose.pose.position.x = pose_aft.x;
msg_body_pose.pose.position.y = pose_aft.y;
msg_body_pose.pose.position.z = pose_aft.z;
msg_body_pose.pose.orientation.x = q(0);
msg_body_pose.pose.orientation.y = q(1);
msg_body_pose.pose.orientation.z = q(2);
msg_body_pose.pose.orientation.w = q(3);
publish_path(pubPath);
idx ++;
}
rate.sleep();
}
}
void pose_estimator::publishThread(){
bool status = ros::ok();
ros::Rate rate(10);
while(status){
ros::spinOnce();
publishCloud(&pubPriorMap, priorMap, ros::Time().fromSec(ld_time), "world");
publishCloud(&pubPriorPath, priorPath, ros::Time().fromSec(ld_time), "world");
publishCloud(&pubInitCloud, initCloud, ros::Time().fromSec(ld_time), "world");
publishCloud(&pubNearCloud, nearCloud, ros::Time().fromSec(ld_time), "world");
rate.sleep();
}
}
bool pose_estimator::easyToRelo(const PointType& pose3d){
idxVec.clear();
disVec.clear();
kdtreeGlobalMapPoses->nearestKSearch(pose3d, 1, idxVec, disVec);
if(disVec[0] > searchDis){
// detectResult = sessions[0].scManager.detectLoopClosureID();
// sc_new = detectResult.first;
// if(detectResult.first != -1 && sc_new != sc_old){
// std::cout << ANSI_COLOR_GREEN_BG << "lio -> relo " << ANSI_COLOR_RESET << std::endl;
// idxVec.clear();
// idxVec.emplace_back(detectResult.first);
// sc_old = detectResult.first;
// return true;
// }
// else{
// return false;
// }
idxVec_copy.clear();
disVec_copy.clear();
kdtreeGlobalMapPoses_copy->radiusSearchT(pose3d, searchDis, idxVec_copy, disVec_copy);
bool status;
for(auto& it : idxVec_copy){
if(priorPath->points.size() - it >= 100){ // FIXME: default 100
std::cout << ANSI_COLOR_GREEN_BG << "lio -> relo with " << priorPath->points.size() << " to " << it << ANSI_COLOR_RESET << std::endl;
idxVec.clear();
idxVec.emplace_back(it);
status = true;
break;
}
else{
status= false;
}
}
if(status){
return true;
}
else{
return false;
}
}
else{
return true;
}
// pcl::PointCloud<PointType>::Ptr cloud_search(new pcl::PointCloud<PointType>());
// pcl::PointCloud<PointType>::Ptr cloud_tmp(new pcl::PointCloud<PointType>());
// *cloud_tmp += *transformPointCloud(sessions[0].cloudKeyFrames[idxVec[0]].all_cloud, &pose_ext);
// *cloud_search += *transformPointCloud(cloud_tmp, &sessions[0].cloudKeyPoses6D->points[idxVec[0]]);
// pcl::PointXYZINormal point_min, point_max;
// pcl::getMinMax3D(*cloud_search, point_min, point_max);
// float min_x = point_min.x;
// float min_y = point_min.y;
// float min_z = point_min.z;
// float max_x = point_max.x;
// float max_y = point_max.y;
// float max_z = point_max.z;
// if(pose3d.x > (min_x + 10.0) && pose3d.x < (max_x - 10.0) && pose3d.y > (min_y + 10.0) && pose3d.y < (max_y + 10.0)){
// return true;
// }
// else{
// return false;
// }
// kdtreeGlobalMapPoses->radiusSearch(pose3d, searchDis, idxVec, disVec);
// if(idxVec.size() >= searchNum){
// // std::cout << ANSI_COLOR_GREEN << "relo mode start for frame " << idx << ANSI_COLOR_RESET << std::endl;
// return true;
// }
// else{
// // std::cout << ANSI_COLOR_GREEN << "lio mode start for frame " << idx << ANSI_COLOR_RESET << std::endl;
// return false;
// }
}
bool pose_estimator::globalRelo(){
if(cloudBuffer.size() < 1 || poseBuffer_6D.size() < 1){
if(buffer_flg){
std::cout << ANSI_COLOR_RED << "wait for cloud and pose from fast-lio2 ... " << ANSI_COLOR_RESET << std::endl;
buffer_flg = false;
}
return false;
}
if(!sc_flg){
// TODO: The pointcloud saved in fast-lio2 is calculated without extrinsic, but the pose ignored the extrinsic
initCloud_->clear();
*initCloud_ += *cloudBuffer[0];
std::cout << "init cloud size: " << initCloud_->points.size() << std::endl;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/cur_imu.pcd", *initCloud_);
std::cout << ANSI_COLOR_GREEN << "global relo by sc ... " << ANSI_COLOR_RESET << std::endl;
pcl::PointCloud<PointType>::Ptr cloud_lid(new pcl::PointCloud<PointType>());
*cloud_lid += *getBodyCloud(initCloud_, pose_ext, pose_zero);
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/cur_lid.pcd", *cloud_lid);
// sessions[0].scManager.makeAndSaveScancontextAndKeys(*cloud_lid);
// detectResult = sessions[0].scManager.detectLoopClosureID();
// sessions[0].scManager.polarcontexts_.pop_back();
// sessions[0].scManager.polarcontext_invkeys_.pop_back();
// sessions[0].scManager.polarcontext_vkeys_.pop_back();
// sessions[0].scManager.polarcontext_invkeys_mat_.pop_back();
Eigen::MatrixXd initSC = sessions[0].scManager.makeScancontext(*cloud_lid);
Eigen::MatrixXd ringkey = sessions[0].scManager.makeRingkeyFromScancontext(initSC);
Eigen::MatrixXd sectorkey = sessions[0].scManager.makeSectorkeyFromScancontext(initSC);
std::vector<float> polarcontext_invkey_vec = ScanContext::eig2stdvec(ringkey);
detectResult = sessions[0].scManager.detectLoopClosureIDBetweenSession(polarcontext_invkey_vec, initSC);
std::cout << ANSI_COLOR_RED << "init relocalization by current SC id: " << 0 << " in prior map's SC id: " << detectResult.first
<< " yaw offset: " << detectResult.second << ANSI_COLOR_RESET << std::endl;
if(detectResult.first != -1){
PointTypePose pose_com;
pose_com.x = 0.0;
pose_com.y = 0.0;
pose_com.z = 0.0;
pose_com.roll = 0.0;
pose_com.pitch = 0.0;
pose_com.yaw = -detectResult.second;
initCloud->clear();
*initCloud += *getAddCloud(cloud_lid, pose_com, pose_ext);
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/cur_sc.pcd", *initCloud);
nearCloud->clear();
*nearCloud += *sessions[0].cloudKeyFrames[detectResult.first].all_cloud;
pcl::PointCloud<PointType>::Ptr near_ext(new pcl::PointCloud<PointType>());
*near_ext += *transformPointCloud(nearCloud, &pose_ext);
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/near_sc.pcd", *near_ext);
nearCloud->clear();
*nearCloud += *transformPointCloud(near_ext, &sessions[0].cloudKeyPoses6D->points[detectResult.first]);
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/near_world.pcd", *nearCloud);
}
else{
initCloud->clear();
*initCloud += *initCloud_;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/cur_sc.pcd", *initCloud);
}
sc_flg = true;
return false;
}
if(!external_flg){
if(cout_count <= 0){
std::cout << ANSI_COLOR_RED << "wait for external pose ... " << ANSI_COLOR_RESET << std::endl;
}
cout_count = 1;
return false;
}
std::cout << ANSI_COLOR_GREEN << "global relocalization processing ... " << ANSI_COLOR_RESET << std::endl;
bool trust;
PointTypePose poseSC = sessions[0].cloudKeyPoses6D->points[detectResult.first];
if(detectResult.first < 0){
trust = false;
std::cout << ANSI_COLOR_RED << "can not relo by SC ... " << ANSI_COLOR_RESET << std::endl;
}
else{
float x_diff = externalPose.x - poseSC.x;
float y_diff = externalPose.y - poseSC.y;
float dis = std::sqrt(x_diff * x_diff + y_diff * y_diff);
std::cout << ANSI_COLOR_GREEN << "distance between sc pose and external pose: " << dis << ANSI_COLOR_RESET << std::endl;
trust = (dis <= trustDis) ? true : false; // select SC-pose or extermal-pose
}
if(trust){
// if(0){
std::cout << ANSI_COLOR_GREEN << "init relo by SC-pose ... " << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_GREEN << "use prior frame " << detectResult.first << " to relo init cloud ..." << ANSI_COLOR_RESET << std::endl;
nearCloud->clear();
PointType tmp;
tmp.x = poseSC.x;
tmp.y = poseSC.y;
tmp.z = poseSC.z;
idxVec.clear();
disVec.clear();
kdtreeGlobalMapPoses->nearestKSearch(tmp, searchNum, idxVec, disVec);
for(int i = 0; i < idxVec.size(); i++){
pcl::PointCloud<PointType>::Ptr nearCloud_tmp(new pcl::PointCloud<PointType>());
*nearCloud_tmp += *transformPointCloud(sessions[0].cloudKeyFrames[idxVec[i]].all_cloud, &pose_ext);
*nearCloud += *transformPointCloud(nearCloud_tmp, &sessions[0].cloudKeyPoses6D->points[idxVec[i]]);
}
std::cout << "near cloud size: " << nearCloud->points.size() << std::endl;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/near.pcd", *nearCloud);
PointTypePose poseOffset;
poseOffset.x = poseSC.x;
poseOffset.y = poseSC.y;
poseOffset.z = poseSC.z;
poseOffset.roll = 0.0;
poseOffset.pitch = 0.0;
poseOffset.yaw = 0.0;
initCloud = transformPointCloud(initCloud, &poseOffset);
std::cout << "init cloud size: " << initCloud->points.size() << std::endl;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/init_offset.pcd", *initCloud);
std::cout << ANSI_COLOR_GREEN << "get precise pose by FR-ICP ... " << ANSI_COLOR_RESET << std::endl;
Eigen::MatrixXd transform = reg[0].run(initCloud, nearCloud);
Eigen::Matrix3d rot = transform.block(0, 0, 3, 3);
Eigen::MatrixXd linear = transform.block(0, 3, 3, 1);
Eigen::Matrix<double, 3, 1> euler = RotMtoEuler(rot);
PointTypePose poseReg;
poseReg.x = linear(0, 0);
poseReg.y = linear(1, 0);
poseReg.z = linear(2, 0);
poseReg.roll = euler(0, 0);
poseReg.pitch = euler(1, 0);
poseReg.yaw = euler(2, 0);
initCloud = transformPointCloud(initCloud, &poseReg);
initCloud->width = initCloud->points.size();
initCloud->height = 1;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/init_result.pcd", *initCloud);
Eigen::Affine3f trans_com = pcl::getTransformation(0.0, 0.0, 0.0, 0.0, 0.0, -detectResult.second);
Eigen::Affine3f trans_offset = pcl::getTransformation(poseOffset.x, poseOffset.y, poseOffset.z, poseOffset.roll, poseOffset.pitch, poseOffset.yaw);
Eigen::Affine3f trans_reg = pcl::getTransformation(poseReg.x, poseReg.y, poseReg.z, poseReg.roll, poseReg.pitch, poseReg.yaw);
Eigen::Affine3f trans_init = trans_com * trans_offset * trans_reg;
float pose_init[6];
pcl::getTranslationAndEulerAngles(trans_init, pose_init[0], pose_init[1], pose_init[2],
pose_init[3], pose_init[4], pose_init[5]);
initPose.x = pose_init[0];
initPose.y = pose_init[1];
initPose.z = pose_init[2];
initPose.roll = pose_init[3];
initPose.pitch = pose_init[4];
initPose.yaw = pose_init[5];
global_flg = true;
std::cout << ANSI_COLOR_GREEN << "Get optimized pose: " << initPose.x << " " << initPose.y << " " << initPose.z
<< " " << initPose.roll << " " << initPose.pitch << " " << initPose.yaw << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_RED << "init relocalization has been finished ... " << ANSI_COLOR_RESET << std::endl;
return true;
}
else{
std::cout << ANSI_COLOR_GREEN << "init relo by external-pose ... " << ANSI_COLOR_RESET << std::endl;
PointType tmp;
tmp.x = externalPose.x;
tmp.y = externalPose.y;
tmp.z = externalPose.z;
std::cout << ANSI_COLOR_GREEN << "use prior frame " << idxVec[0] << " to relo init cloud ..." << ANSI_COLOR_RESET << std::endl;
PointTypePose pose_offset;
pose_offset.x = externalPose.x;
pose_offset.y = externalPose.y;
pose_offset.z = externalPose.z;
pose_offset.roll = 0.0;
pose_offset.pitch = 0.0;
pose_offset.yaw = externalPose.yaw;
initCloud = transformPointCloud(initCloud, &pose_offset);
std::cout << "init cloud size: " << initCloud->points.size() << std::endl;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/init_offset.pcd", *initCloud);
PointType tmp2;
tmp2.x = externalPose.x;
tmp2.y = externalPose.y;
tmp2.z = externalPose.z;
idxVec.clear();
disVec.clear();
kdtreeGlobalMapPoses->nearestKSearch(tmp2, searchNum, idxVec, disVec);
PointTypePose pose_new = sessions[0].cloudKeyPoses6D->points[idxVec[0]];
nearCloud->clear();
for(int i = 0; i < idxVec.size(); i++){
pcl::PointCloud<PointType>::Ptr nearCloud_tmp(new pcl::PointCloud<PointType>());
*nearCloud_tmp += *transformPointCloud(sessions[0].cloudKeyFrames[idxVec[i]].all_cloud, &pose_ext);
*nearCloud += *transformPointCloud(nearCloud_tmp, &sessions[0].cloudKeyPoses6D->points[idxVec[i]]);
// *nearCloud += *getBodyCloud(sessions[0].cloudKeyFrames[idxVec[i]].all_cloud, pose_ext, sessions[0].cloudKeyPoses6D->points[idxVec[i]]);
}
std::cout << "near cloud size: " << nearCloud->points.size() << std::endl;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/near.pcd", *nearCloud);
std::cout << ANSI_COLOR_GREEN << "get precise pose by FR-ICP ... " << ANSI_COLOR_RESET << std::endl;
Eigen::MatrixXd transform = reg[0].run(initCloud, nearCloud);
Eigen::Matrix3d rot = transform.block(0, 0, 3, 3);
Eigen::MatrixXd linear = transform.block(0, 3, 3, 1);
// std::cout << "linear: " << linear << std::endl;
Eigen::Matrix<double, 3, 1> euler = RotMtoEuler(rot);
PointTypePose poseReg;
poseReg.x = linear(0, 0);
poseReg.y = linear(1, 0);
poseReg.z = linear(2, 0);
poseReg.roll = euler(0, 0);
poseReg.pitch = euler(1, 0);
poseReg.yaw = euler(2, 0);
// std::cout << poseReg.x << " " << poseReg.y << " " << poseReg.z << std::endl;
// std::cout << poseReg.roll << " " << poseReg.pitch << " " << poseReg.yaw << std::endl;
initCloud = getAddCloud(initCloud, poseReg, pose_zero);
initCloud->width = initCloud->points.size();
initCloud->height = 1;
pcl::io::savePCDFile("/home/yixin-f/fast-lio2/src/data_loc/init_result.pcd", *initCloud);
Eigen::Affine3f trans_offset = pcl::getTransformation(pose_offset.x, pose_offset.y, pose_offset.z, pose_offset.roll, pose_offset.pitch, pose_offset.yaw);
Eigen::Affine3f trans_reg = pcl::getTransformation(poseReg.x, poseReg.y, poseReg.z, poseReg.roll, poseReg.pitch, poseReg.yaw);
Eigen::Affine3f trans_init = trans_offset * trans_reg ;
float pose_init[6];
pcl::getTranslationAndEulerAngles(trans_init, pose_init[0], pose_init[1], pose_init[2],
pose_init[3], pose_init[4], pose_init[5]);
initPose.x = pose_init[0];
initPose.y = pose_init[1];
initPose.z = pose_init[2];
initPose.roll = pose_init[3];
initPose.pitch = pose_init[4];
initPose.yaw = pose_init[5];
global_flg = true;
std::cout << ANSI_COLOR_GREEN << "Get optimized pose: " << initPose.x << " " << initPose.y << " " << initPose.z
<< " " << initPose.roll << " " << initPose.pitch << " " << initPose.yaw << ANSI_COLOR_RESET << std::endl;
std::cout << ANSI_COLOR_RED << "init relocalization has been finished ... " << ANSI_COLOR_RESET << std::endl;
return true;
}
}
void pose_estimator::publish_odometry(const ros::Publisher &pubOdomAftMapped){
pubOdomAftMapped.publish(odomAftMapped);
odomAftMapped.header.frame_id = "world";
odomAftMapped.child_frame_id = "body";
odomAftMapped.header.stamp = ros::Time().fromSec(ld_time);
// static tf::TransformBroadcaster br1;
// tf::Transform transform;
// tf::Quaternion q;
// transform.setOrigin(tf::Vector3(odomAftMapped.pose.pose.position.x,
// odomAftMapped.pose.pose.position.y,
// odomAftMapped.pose.pose.position.z));
// q.setW(odomAftMapped.pose.pose.orientation.w);
// q.setX(odomAftMapped.pose.pose.orientation.x);
// q.setY(odomAftMapped.pose.pose.orientation.y);
// q.setZ(odomAftMapped.pose.pose.orientation.z);
// transform.setRotation(q);
// br1.sendTransform(tf::StampedTransform(transform, odomAftMapped.header.stamp, "world", "body"));
}
void pose_estimator::publish_path(const ros::Publisher& pubPath){
msg_body_pose.header.frame_id = "world";
msg_body_pose.header.stamp = ros::Time().fromSec(ld_time);
path.header.frame_id = "world";
path.header.stamp = ros::Time().fromSec(ld_time);
path.poses.push_back(msg_body_pose);
pubPath.publish(path);
}