-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.cpp
2275 lines (1933 loc) · 82.7 KB
/
map.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
#include <map.h>
#include <random>
namespace dummy {
Map::Map()
: map_grid_(vector<string>({"layer"})),
nav_grid_(vector<string>({"layer"})),
planning_grid_(vector<string>({"layer"})),
rfid_grid_(vector<string>({"layer"})) {}
Map::Map(float plan_resolution, nav_msgs::OccupancyGrid occupancyGrid)
: map_grid_(vector<string>({"layer"})),
nav_grid_(vector<string>({"layer"})),
planning_grid_(vector<string>({"layer"})),
rfid_grid_(vector<string>({"layer"})) {
Map::createMap(occupancyGrid);
plotMyGrid("/tmp/initial_map.pgm", &map_grid_);
Map::createGrid(map_grid_.getResolution());
plotMyGrid("/tmp/initial_nav.pgm", &nav_grid_);
Map::createNewMap();
Map::createPathPlanningGrid(plan_resolution);
plotMyGrid("/tmp/initial_planning.pgm", &planning_grid_);
// once both are created, we can obtain the ratio
gridToPathGridScale =
planning_grid_.getResolution() / nav_grid_.getResolution();
ROS_ASSERT_MSG(gridToPathGridScale >= 1, "[Map.cpp@map] Planning resolution "
"lower than navigation resolution = "
"%3.3f",
gridToPathGridScale);
Map::createRFIDGrid(map_grid_.getResolution());
ROS_DEBUG("[Map.cpp@map] map created from OccupancyGrid");
}
Map::Map(std::ifstream &infile, float resolution)
: map_grid_(vector<string>({"layer"})),
nav_grid_(vector<string>({"layer"})),
planning_grid_(vector<string>({"layer"})),
rfid_grid_(vector<string>({"layer"})) {
Map::createMap(infile);
Map::createGrid(resolution);
Map::createNewMap();
ROS_DEBUG("[Map.cpp@map] map created from ifstream");
}
Map::Map(float plan_resolution, float map_resolution, int width, int height,
vector<int> data, geometry_msgs::Pose origin)
: map_grid_(vector<string>({"layer"})),
nav_grid_(vector<string>({"layer"})),
planning_grid_(vector<string>({"layer"})),
rfid_grid_(vector<string>({"layer"})) {
Map::createMap(width, height, map_resolution, data, origin);
plotMyGrid("/tmp/initial_map.pgm", &map_grid_);
Map::createGrid(map_resolution);
plotMyGrid("/tmp/initial_nav.pgm", &nav_grid_);
Map::createNewMap();
Map::createPathPlanningGrid(plan_resolution);
plotMyGrid("/tmp/initial_planning.pgm", &planning_grid_);
// once both are created, we can obtain the ratio
gridToPathGridScale =
planning_grid_.getResolution() / nav_grid_.getResolution();
ROS_ASSERT_MSG(gridToPathGridScale >= 1, "[Map.cpp@map] Planning resolution "
"lower than navigation resolution = "
"%3.3f",
gridToPathGridScale);
Map::createRFIDGrid(map_resolution);
ROS_DEBUG("[Map.cpp@map] map created from data vector");
}
void Map::createMap(nav_msgs::OccupancyGrid occupancyGrid) {
GridMapRosConverter::fromOccupancyGrid(occupancyGrid, "layer", map_grid_);
float maxValue = map_grid_.get("layer").maxCoeffOfFinites();
float minValue = map_grid_.get("layer").minCoeffOfFinites();
// maxValue = 20;
ROS_DEBUG("[Map.cpp@createMap] encoding map grid between values: %3.3f, %3.3f",maxValue, minValue);
encodeGrid(&map_grid_, maxValue, minValue);
printGridData("map", &map_grid_);
}
void Map::createMap(std::ifstream &infile) {
// load an image from cv
ROS_DEBUG("[Map.cpp@createMap] Loading image into cv mat.");
cv::Mat imageCV = MreadImage(infile);
geometry_msgs::Pose origin;
// orig will be placed at bottom left position
// TODO : CHECK THIS!!!
origin.position.x = 0;
origin.position.y = 0;
// harcoded values!!!!
std::string map_frame_id = "map";
double map_resolution = 0.1;
ROS_WARN("[Map.cpp@createMap] MAP RESOLUTION MUST BE 0.1 m/cell !!!.");
createMap(imageCV, origin, map_frame_id, map_resolution);
}
void Map::createMap(cv::Mat imageCV, geometry_msgs::Pose origin,
std::string map_frame_id, double map_resolution) {
// map data WAS stored in map internal var, now is map_grid_
// this method is simiarl to RFIDGridmap::createGrid
// 2D position of the grid map in the grid map frame [m].
double orig_x;
double orig_y;
// cell value ranges
double minValue;
double maxValue;
// grayscale opencv mat where 0 are free and 1 are obstacles.
imageCV = binarizeImage(imageCV);
// grid size in pixels
Map::numRows = imageCV.rows;
Map::numCols = imageCV.cols;
// map origin in rangeInMeters
orig_x = origin.position.x + imageCV.rows * map_resolution / 2;
orig_y = origin.position.y + imageCV.cols * map_resolution / 2;
ROS_DEBUG("[Map.cpp@createMap] Image size [%lu, %lu]", numRows, numCols);
cv::minMaxLoc(imageCV, &minValue, &maxValue);
ROS_DEBUG("[Map.cpp@createMap] Min, max values [%3.3f %3.3f]", minValue,
maxValue);
ROS_DEBUG("[Map.cpp@createMap] Channels [%d]", imageCV.channels());
ROS_DEBUG("[Map.cpp@createMap] Encoding [%s]",
type2str(imageCV.type()).c_str());
ROS_DEBUG("[Map.cpp@createMap] Map resolution [%3.3f]", map_resolution);
ROS_DEBUG("[Map.cpp@createMap] Map resolution [%3.3f]", map_resolution);
// create empty grid map
ROS_DEBUG("[Map.cpp@createMap] Creating empty grid");
grid_map::GridMap tempMap(vector<string>({"layer"}));
// map lenth MUST be given in meters!
tempMap.setGeometry(
Length(numRows * map_resolution, numCols * map_resolution),
map_resolution, Position(orig_x, orig_y));
tempMap.setFrameId(map_frame_id);
tempMap.clearAll();
// Convert cv image to grid map.
ROS_DEBUG("[Map.cpp@createMap] Storing cv mat into emtpy grid");
string format = ("mono8");
sensor_msgs::ImagePtr imageROS =
cv_bridge::CvImage(std_msgs::Header(), format, imageCV).toImageMsg();
GridMapRosConverter::addLayerFromImage(*imageROS, "layer", tempMap);
ROS_DEBUG("[Map.cpp@createMap] encoding map grid between values: %3.3f, %3.3f",1.0,0.0);
encodeGrid(&tempMap, 1, 0);
map_grid_ = tempMap;
printGridData("map", &map_grid_);
}
void Map::encodeGrid(grid_map::GridMap *gm, int obstValue, int freeValue) {
long n_obsts = 0;
long n_free = 0;
long n_others = 0;
long total = 0;
for (grid_map::GridMapIterator iterator(*gm); !iterator.isPastEnd();
++iterator) {
// if (gm->at("layer", *iterator) == obstValue) {
// gm->at("layer", *iterator) = Map::CellValue::OBST;
// n_obsts++;
// } else if (gm->at("layer", *iterator) == freeValue) {
// gm->at("layer", *iterator) = Map::CellValue::FREE;
// n_free++;
// } else //free by default
// {
// gm->at("layer", *iterator) = Map::CellValue::FREE;
// n_others++;
// }
if (gm->at("layer", *iterator) >= obstValue) {
gm->at("layer", *iterator) = Map::CellValue::OBST;
n_obsts++;
// } else if (gm->at("layer", *iterator) == freeValue) {
// gm->at("layer", *iterator) = Map::CellValue::FREE;
// n_free++;
} else // free by default
{
gm->at("layer", *iterator) = Map::CellValue::FREE;
n_others++;
}
}
total = n_obsts + n_free + n_others;
ROS_DEBUG("[Map.cpp@encodeGrid] Encoded grid with %3.3f %% of free cells and "
"%3.3f %% of occupied cells",
100.0 * n_free / (1.0 * total), 100.0 * n_obsts / (1.0 * total));
if (n_others)
ROS_WARN("[Map.cpp@encodeGrid] Found %3.3f %% undefined cells, CASTED to "
"free space",
100.0 * n_others / (1.0 * total));
}
void Map::createMap(int width, int height, double resolution, vector<int> data,
geometry_msgs::Pose origin) {
// load an image from vector
ROS_DEBUG("[Map.cpp@createMap] Loading vector into cv mat.");
cv::Mat imageCV = MreadImage(width, height, data);
// TODO : CHECK THIS!!!
// harcoded values!!!!
std::string map_frame_id = "map";
createMap(imageCV, origin, map_frame_id, resolution);
}
cv::Mat Map::MreadImage(int width, int height, vector<int> data) {
cv::Mat mImg(width, height, CV_8UC1);
uchar pv[data.size()];
for (unsigned int i = 0; i < data.size(); i++) {
pv[i] = (uchar)data.at(i);
}
memcpy(mImg.data, &pv, data.size() * sizeof(uchar));
return mImg;
}
// solution proposed https://codeday.me/es/qa/20190427/576956.html
cv::Mat Map::MreadImage(std::ifstream &input) {
input.seekg(0, std::ios::end);
size_t fileSize = input.tellg();
input.seekg(0, std::ios::beg);
if (fileSize == 0) {
return cv::Mat();
ROS_FATAL("[Map.cpp@MreadImage] Image input stream size is 0.");
}
std::vector<unsigned char> data(fileSize);
input.read(reinterpret_cast<char *>(&data[0]),
sizeof(unsigned char) * fileSize);
if (!input) {
return cv::Mat();
ROS_FATAL("[Map.cpp@MreadImage] Cant cast input data");
}
cv::Mat mImg = cv::imdecode(cv::Mat(data), CV_LOAD_IMAGE_UNCHANGED);
return mImg;
}
cv::Mat Map::binarizeImage(cv::Mat mImg) {
// how many different values does it have?
ROS_DEBUG("[Map.cpp@binarize] Input grid values:");
std::vector<uchar> diffValues;
for (int y = 0; y < mImg.rows; ++y) {
const uchar *row_ptr = mImg.ptr<uchar>(y);
for (int x = 0; x < mImg.cols; ++x) {
uchar value = row_ptr[x];
if (std::find(diffValues.begin(), diffValues.end(), value) ==
diffValues.end()) {
diffValues.push_back(value);
ROS_DEBUG("[Map.cpp@binarize] \t 0x%x", value);
}
}
}
std::sort(diffValues.begin(), diffValues.end());
if (diffValues.size() > 2) {
// Apply thresholding to binarize!
// cv::adaptiveThreshold(mImg, mImg, 255,
// cv::ADAPTIVE_THRESH_GAUSSIAN_C,cv::THRESH_BINARY,3,5);
cv::threshold(mImg, mImg, diffValues[diffValues.size() - 2], 255,
cv::THRESH_BINARY);
diffValues.clear();
// how many different values does it have?
for (int y = 0; y < mImg.rows; ++y) {
const uchar *row_ptr = mImg.ptr<uchar>(y);
for (int x = 0; x < mImg.cols; ++x) {
uchar value = row_ptr[x];
if (std::find(diffValues.begin(), diffValues.end(), value) ==
diffValues.end())
diffValues.push_back(value);
}
}
if (diffValues.size() != 2) {
ROS_ERROR("[Map.cpp@createMap] After binarizing, we still have [%lu] "
"different values: ",
diffValues.size());
for (int i = 0; i < diffValues.size(); i++) {
ROS_ERROR("[Map.cpp@createMap] \t\t [0x%x]==[%d] ", diffValues[i],
diffValues[i]);
}
} else {
ROS_DEBUG(
"[Map.cpp@createMap] Input image only has [%lu] different values: ",
diffValues.size());
}
} else {
ROS_ERROR(
"[Map.cpp@createMap] Input image only has [%lu] different values: ",
diffValues.size());
}
return mImg;
}
void Map::createGrid(float resolution) {
// change gridmap resolution from map_resolution to gridResolution
ROS_DEBUG("[Map.cpp@createGrid] creating grid with resolution %3.3f",
resolution);
GridMapCvProcessing::changeResolution(map_grid_, nav_grid_, resolution);
float maxValue = nav_grid_.get("layer").maxCoeffOfFinites();
float minValue = nav_grid_.get("layer").minCoeffOfFinites();
// cout << "maxValue(obstacle) = " << maxValue << ", minValue(free) = "
// << minValue << endl;
ROS_DEBUG("[Map.cpp@createGrid] encoding nav grid between values: %3.3f, %3.3f",maxValue, minValue);
encodeGrid(&nav_grid_, maxValue, minValue);
Map::numGridRows = nav_grid_.getSize()(0);
Map::numGridCols = nav_grid_.getSize()(1);
printGridData("nav", &nav_grid_);
// createSecondNavigationGrid(map_grid_); // TODO: Enable it only when fixed
}
void Map::createPathPlanningGrid(float resolution) {
// change gridmap resolution from map_resolution to gridResolution
ROS_DEBUG("[Map.cpp@createPathPlanningGrid] creating planning grid with resolution %3.3f",
resolution);
GridMapCvProcessing::changeResolution(map_grid_, planning_grid_, resolution);
float maxValue = planning_grid_.get("layer").maxCoeffOfFinites();
float minValue = planning_grid_.get("layer").minCoeffOfFinites();
encodeGrid(&planning_grid_, maxValue, minValue);
Map::numPathPlanningGridRows = planning_grid_.getSize()(0);
Map::numPathPlanningGridCols = planning_grid_.getSize()(1);
printGridData("plan", &planning_grid_);
}
void Map::createRFIDGrid(float resolution) {
// change gridmap resolution from map_resolution to gridResolution
ROS_DEBUG("[Map.cpp@createGrid] creating RFID grid with resolution %3.3f",
resolution);
GridMapCvProcessing::changeResolution(map_grid_, rfid_grid_, resolution);
rfid_grid_["layer"].setConstant(0.0);
printGridData("rfid", &rfid_grid_);
}
void Map::updatePathPlanningGrid(int cellX_pp, int cellY_pp,
int rangeInCells_pp, double power) {
int minX_pp = cellX_pp - rangeInCells_pp;
int maxX_pp = cellX_pp + rangeInCells_pp;
if (minX_pp < 0)
minX_pp = 0;
if (maxX_pp > numPathPlanningGridRows - 1)
maxX_pp = numPathPlanningGridRows - 1;
int minY_pp = cellY_pp - rangeInCells_pp;
int maxY_pp = cellY_pp + rangeInCells_pp;
if (minY_pp < 0)
minY_pp = 0;
if (maxY_pp > numPathPlanningGridCols - 1)
maxY_pp = numPathPlanningGridCols - 1;
std::cout << endl;
std::cout << "[Map.cpp@updatePathPlanningGrid] rangeInCells_pp = "
<< rangeInCells_pp << endl;
std::cout << "[Map.cpp@updatePathPlanningGrid] [numPathPlanningGridRows, "
"numPathPlanningGridCols] = ["
<< numPathPlanningGridRows << "," << numPathPlanningGridCols << "]"
<< endl;
std::cout << "[Map.cpp@updatePathPlanningGrid] [cellX_pp, cellY_pp] = ["
<< cellX_pp << "," << cellY_pp << "]" << endl;
std::cout << "[Map.cpp@updatePathPlanningGrid] [minX_pp, maxX_pp] = ["
<< minX_pp << "," << maxX_pp << "]" << endl;
std::cout << "[Map.cpp@updatePathPlanningGrid] [minY_pp, maxY_pp] = ["
<< minY_pp << "," << maxY_pp << "]" << endl;
grid_map::Index planningStartIndex(minX_pp, minX_pp);
grid_map::Index planningBufferSize(2 * rangeInCells_pp, 2 * rangeInCells_pp);
Position position_pp, upper_left_pp;
// this is the number of navigation cells inside a planning cell
grid_map::Index navBufferSize(gridToPathGridScale, gridToPathGridScale);
grid_map::Index navStartIndex, rfid_index;
int countScanned = 0;
int setToOne = 0;
double k =
(this->planning_grid_.getResolution() / 2) - nav_grid_.getResolution();
int counter_planning_grid_scanned = 0;
// iterate over the submap in the planning grid (lower res)
for (grid_map::SubmapIterator planning_iterator(
this->planning_grid_, planningStartIndex, planningBufferSize);
!planning_iterator.isPastEnd(); ++planning_iterator) {
// get the centre of the current planning cell
this->planning_grid_.getPosition(*planning_iterator, position_pp);
// cout << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] Current
// position(meters) in PLANNING grid = [ " << position_pp.x() << ","
// << position_pp.y() << "]" << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] Current
// position(cells) in PLANNING grid = [ " << *planning_iterator <<
// endl;
countScanned = 0;
setToOne = 0;
// obtain the position of the upper-left navigation cell INSIDE current
// planning cell
upper_left_pp.x() = position_pp.x() - k;
upper_left_pp.y() = position_pp.y() - k;
// and corresponding index in nav_grid_
nav_grid_.getIndex(upper_left_pp, navStartIndex);
// std::cout << "[Map.cpp@updatePathPlanningGrid] Upper_left index of
// NAVIGATION submap = [ " << navStartIndex << endl;
int counter = 0;
for (grid_map::SubmapIterator nav_iterator(nav_grid_, navStartIndex,
navBufferSize);
!nav_iterator.isPastEnd(); ++nav_iterator) {
// std::cout << "[Map.cpp@updatePathPlanningGrid] Inside : " <<
// getGridValue((*nav_iterator)(0),(*nav_iterator)(1)) << " at "
// << (*nav_iterator)(0) << ", " << (*nav_iterator)(0) << endl;
counter++;
if (isGridValueObst(*nav_iterator)) {
setToOne = 1;
// std::cout << "[Map.cpp@updatePathPlanningGrid] 1" << endl;
}
if (isGridValueVist(*nav_iterator)) {
countScanned++;
}
}
// std::cout << "[Map.cpp@updatePathPlanningGrid] countScanned: " <<
// countScanned << endl;
if (countScanned >= 0.9 * gridToPathGridScale * gridToPathGridScale) {
setPathPlanningGridValue(Map::CellValue::VIST, (*planning_iterator)(0),
(*planning_iterator)(1));
grid_map::Index ind;
rfid_grid_.getIndex(position_pp, ind);
setRFIDGridValue(power, ind(0), ind(1));
counter_planning_grid_scanned++;
// std::cout << "[Map.cpp@updatePathPlanningGrid] CountScanned"
// << endl;
}
// if (setToOne == 1) {
// setPathPlanningGridValue(Map::CellValue::OBST,
// (*planning_iterator)(0),(*planning_iterator)(1));
//// std::cout << "[Map.cpp@updatePathPlanningGrid] SetToOne" <<
///endl;
// }
}
std::cout << "[Map.cpp@updatePathPlanningGrid] PlanningGrid scanned cells: "
<< counter_planning_grid_scanned << endl;
}
void Map::printSubmapBoundaries(grid_map::Index startIndex,
grid_map::Index bufferSize,
const grid_map::GridMap *gm) const {
int rowInc;
int colInc;
grid_map::Index indexInc;
grid_map::Index topLeftI;
grid_map::Index topRightI;
grid_map::Index bottomRightI;
grid_map::Index bottomLeftI;
indexInc = bufferSize - grid_map::Index(1, 1);
topLeftI = startIndex;
topRightI = startIndex + grid_map::Index(0, indexInc(1));
bottomRightI = startIndex + indexInc;
bottomLeftI = startIndex + grid_map::Index(indexInc(0), 0);
grid_map::Position topLeftP, topRightP, bottomRightP, bottomLeftP;
gm->getPosition(topLeftI, topLeftP);
gm->getPosition(topRightI, topRightP);
gm->getPosition(bottomRightI, bottomRightP);
gm->getPosition(bottomLeftI, bottomLeftP);
ROS_DEBUG("Submap boundaries position: [%3.3f, %3.3f], [%3.3f, %3.3f], "
"[%3.3f, %3.3f], [%3.3f, %3.3f]",
topLeftP.x(), topLeftP.y(), topRightP.x(), topRightP.y(),
bottomRightP.x(), bottomRightP.y(), bottomLeftP.x(),
bottomLeftP.y());
}
void Map::updatePathPlanningGrid(float posX, float posY, float rangeInMeters) {
int rangeInCells_pp;
int numVistNavCellsPerPathCell;
int numObstNavCellsPerPathCell;
double k;
int numVistPathCell;
// long upper_left_pp_X, upper_left_pp_X;
grid_map::Index planningStartIndex;
grid_map::Index planningBufferSize;
Position upper_left_pp;
Position position_pp;
Position robot_pp(posX, posY);
grid_map::Index robot_index;
planning_grid_.getIndex(robot_pp, robot_index);
grid_map::Index navStartIndex;
grid_map::Index navBufferSize;
// get the boundaries of the planning submap
rangeInCells_pp = (int)std::ceil(rangeInMeters / planning_grid_.getResolution());
planningBufferSize = grid_map::Index(2 * rangeInCells_pp, 2 * rangeInCells_pp);
planningStartIndex(0) = robot_index(0) - rangeInCells_pp;
planningStartIndex(1) = robot_index(1) - rangeInCells_pp;
// std::cout << "\n[Map.cpp@updatePathPlanningGrid] upper_left_pp = [" <<
// upper_left_pp.x() << "," << upper_left_pp.y() << "]" << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] robotIndex = "
// << (robot_index)(0) << "," << (robot_index)(1) << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] planningStartIndex = "
// << (planningStartIndex)(0) << "," << (planningStartIndex)(1) << endl;
// planningStartIndex = Position(posX - rangeInMeters, posY -
// rangeInMeters);
// this is the number of navigation cells inside a planning cell
navBufferSize = grid_map::Index(gridToPathGridScale, gridToPathGridScale);
// distance from the center of a planning grid cell to the center
// of the furthest navigation cell. IN METERS
k = (planning_grid_.getResolution() / 2) - nav_grid_.getResolution();
numVistPathCell = 0;
printSubmapBoundaries(planningStartIndex, planningBufferSize, &planning_grid_);
// std::cout << "[Map.cpp@updatePathPlanningGrid] rangeInCells_pp = " << rangeInCells_pp << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] robot position = " << posX << "," << posY << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] k = " << k << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] navGridResolution = "
// << nav_grid_.getResolution() << ", pathPlanningGridResolution =" << planning_grid_.getResolution()<< endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] navBufferSize: " << navBufferSize << endl;
// iterate over the submap in the planning grid (lower res)
// cout << "PlanningGrid cell selected: " << endl;
for (grid_map::SubmapIterator planning_iterator(planning_grid_, planningStartIndex, planningBufferSize);
!planning_iterator.isPastEnd(); ++planning_iterator) {
// get the centre of the current planning cell
planning_grid_.getPosition(*planning_iterator, position_pp);
// std::cout << "\n[Map.cpp@updatePathPlanningGrid] planning_iterator= [" <<
// (*planning_iterator)(0) << "," << (*planning_iterator)(1) << "]" << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] position_pp = " <<
// position_pp.x() << "," << position_pp.y() << endl;
numVistNavCellsPerPathCell = 0;
numObstNavCellsPerPathCell = 0;
// obtain the position of the upper-left navigation cell INSIDE current
// planning cell
upper_left_pp.x() = position_pp.x() - k;
upper_left_pp.y() = position_pp.y() - k;
// std::cout << "[Map.cpp@updatePathPlanningGrid] upper_left_pp = "
// << upper_left_pp.x() << "," << upper_left_pp.y() << endl;
// and corresponding index in nav_grid_
nav_grid_.getIndex(upper_left_pp, navStartIndex);
for (grid_map::SubmapIterator nav_iterator(nav_grid_, navStartIndex,navBufferSize); !nav_iterator.isPastEnd(); ++nav_iterator) {
// std::cout << "[Map.cpp@updatePathPlanningGrid] Visited : " << isGridValueVist(*nav_iterator) << " at " <<
// (*nav_iterator)(0) << ", " << (*nav_iterator)(1) << endl;
if (isGridValueVist(*nav_iterator)) {
numVistNavCellsPerPathCell++;
}
if (isGridValueObst(*nav_iterator)) {
numObstNavCellsPerPathCell++;
}
}
// if (numObstNavCellsPerPathCell <= 0.6 * gridToPathGridScale * gridToPathGridScale){
if (numVistNavCellsPerPathCell >= 0.9 * gridToPathGridScale * gridToPathGridScale && !containsNavObstacles(position_pp)) {
setPathPlanningGridValue(Map::CellValue::VIST, (*planning_iterator)(0) +1,
(*planning_iterator)(1) +1) ; // TODO: the +1 has been added because the cells where not correctly marked, it needs further investigation
numVistPathCell++;
// cout << "(" << (*planning_iterator)(0) <<"," << (*planning_iterator)(1) + 1 <<")" << endl;
}
// }
}
// ROS_DEBUG("[Map.cpp@updatePathPlanningGrid] PlanningGrid scanned cells: %d", numVistPathCell);
// cout << "[Map.cpp@updatePathPlanningGrid] PlanningGrid scanned cells: " << numVistPathCell << endl;
}
bool Map::containsNavObstacles(Position position_pp)
{
Position upper_left_pp;
grid_map::Index navStartIndex;
grid_map::Index navBufferSize;
int numObstNavCellsPerPathCell = 0;
bool result = false;
// this is the number of navigation cells inside a planning cell
navBufferSize = grid_map::Index( gridToPathGridScale, gridToPathGridScale);
// distance from the center of a planning grid cell to the center
// of the furthest navigation cell. IN METERS
double k = (planning_grid_.getResolution() / 2) - nav_grid_.getResolution();
// cout << "k: " << k << endl;
upper_left_pp.x() = position_pp.x() - k;// - planning_grid_.getResolution();
upper_left_pp.y() = position_pp.y() - k;// - planning_grid_.getResolution();
// and corresponding index in nav_grid_
nav_grid_.getIndex(upper_left_pp, navStartIndex);
for (grid_map::SubmapIterator nav_iterator(nav_grid_, navStartIndex, navBufferSize); !nav_iterator.isPastEnd(); ++nav_iterator) {
if (isGridValueObst(*nav_iterator)) {
numObstNavCellsPerPathCell++;
}
}
// cout << "numObstNavCellsPerPathCell: " << numObstNavCellsPerPathCell << endl;
if (numObstNavCellsPerPathCell >= 0.01 * gridToPathGridScale * gridToPathGridScale) {
result = true;
}
// cout << "(" << position_pp.x() << "," << position_pp.y() << ") contains Obstacle: " << result << endl;
return result;
}
bool Map::checkWallsPathPlanningGrid(float posX, float posY, float rangeInMeters) {
int rangeInCells_pp = 3;
int numVistNavCellsPerPathCell;
double k;
int numVistPathCell;
bool result = false;
// long upper_left_pp_X, upper_left_pp_X;
grid_map::Index planningStartIndex;
grid_map::Index planningBufferSize;
Position upper_left_pp;
Position position_pp;
Position robot_pp(posX, posY);
grid_map::Index robot_index;
planning_grid_.getIndex(robot_pp, robot_index);
grid_map::Index navStartIndex;
grid_map::Index navBufferSize;
// get the boundaries of the planning submap
planningBufferSize =
grid_map::Index(2 * rangeInCells_pp, 2 * rangeInCells_pp);
planningStartIndex(0) = robot_index(0) - rangeInCells_pp;
planningStartIndex(1) = robot_index(1) - rangeInCells_pp;
// this is the number of navigation cells inside a planning cell
navBufferSize = grid_map::Index(gridToPathGridScale, gridToPathGridScale);
// distance from the center of a planning grid cell to the center
// of the furthest navigation cell. IN METERS
k = (planning_grid_.getResolution() / 2) - nav_grid_.getResolution();
numVistPathCell = 0;
printSubmapBoundaries(planningStartIndex, planningBufferSize,
&planning_grid_);
// std::cout << "[Map.cpp@updatePathPlanningGrid] rangeInCells_pp = " <<
// rangeInCells_pp << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] robot position = " <<
// posX << "," << posY << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] k = " << k << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] navGridResolution = "
// << nav_grid_.getResolution() <<
// ", pathPlanningGridResolution =" <<
// planning_grid_.getResolution()<< endl;
// iterate over the submap in the planning grid (lower res)
for (grid_map::SubmapIterator planning_iterator(
planning_grid_, planningStartIndex, planningBufferSize);
!planning_iterator.isPastEnd(); ++planning_iterator) {
// get the centre of the current planning cell
planning_grid_.getPosition(*planning_iterator, position_pp);
// std::cout << "\n[Map.cpp@updatePathPlanningGrid] planning_iterator
// = [" << (*planning_iterator)(0) << "," << (*planning_iterator)(1)
// << "]" << endl;
// std::cout << "[Map.cpp@updatePathPlanningGrid] position_pp = " <<
// position_pp.x() << "," << position_pp.y() << endl;
int numObstNavCellsPerPathCell = 0;
// obtain the position of the upper-left navigation cell INSIDE current
// planning cell
upper_left_pp.x() = position_pp.x() - k;
upper_left_pp.y() = position_pp.y() - k;
// std::cout << "[Map.cpp@updatePathPlanningGrid] upper_left_pp = "
// << upper_left_pp.x() << "," << upper_left_pp.y() << endl;
// and corresponding index in nav_grid_
nav_grid_.getIndex(upper_left_pp, navStartIndex);
for (grid_map::SubmapIterator nav_iterator(nav_grid_, navStartIndex,
navBufferSize);
!nav_iterator.isPastEnd(); ++nav_iterator) {
// std::cout << "[Map.cpp@updatePathPlanningGrid] Visited : " <<
// isGridValueVist(*nav_iterator) << " at " <<
// (*nav_iterator)(0) << ", " << (*nav_iterator)(1) << endl;
if (isGridValueObst(*nav_iterator)) {
numObstNavCellsPerPathCell++;
}
}
if (numObstNavCellsPerPathCell >=
0.1 * gridToPathGridScale * gridToPathGridScale) {
setPathPlanningGridValue(Map::CellValue::OBST, (*planning_iterator)(0),
(*planning_iterator)(1));
result = true;
}
}
return result;
}
float Map::getGridToPathGridScale() const {
return planning_grid_.getResolution() / nav_grid_.getResolution();
}
// Getter shortcuts ........................................................
bool Map::getPathPlanningPosition(double &x, double &y, long i, long j) {
return getPosition(x, y, i, j, &planning_grid_);
}
bool Map::getPathPlanningIndex(double x, double y, long &i, long &j) {
return getIndex(x, y, i, j, &planning_grid_);
}
Map::CellValue Map::getPathPlanningGridValue(long i, long j) const {
return toCellValue(getValue(i, j, &planning_grid_), "planning");
}
Map::CellValue
Map::getPathPlanningGridValue(geometry_msgs::PoseStamped ps) const {
return toCellValue(getValue(ps, &planning_grid_), "planning");
}
Map::CellValue Map::getPathPlanningGridValue(long i) const {
return toCellValue(getValue(i, &planning_grid_), "planning");
}
int Map::getPathPlanningNumCols() const {
return getGridNumCols(&planning_grid_);
}
int Map::getPathPlanningNumRows() const {
return getGridNumRows(&planning_grid_);
}
bool Map::getGridPosition(double &x, double &y, long i, long j) {
return getPosition(x, y, i, j, &nav_grid_);
}
bool Map::getGridIndex(double x, double y, long &i, long &j) {
return getIndex(x, y, i, j, &nav_grid_);
}
Map::CellValue Map::getGridValue(long i, long j) const {
return toCellValue(getValue(i, j, &nav_grid_), "nav");
}
Map::CellValue Map::getGridValue(geometry_msgs::PoseStamped ps) const {
return toCellValue(getValue(ps, &nav_grid_), "nav");
}
Map::CellValue Map::getGridValue(long i) const {
return toCellValue(getValue(i, &nav_grid_), "nav");
}
long Map::getNumGridCols() const { return getGridNumCols(&nav_grid_); }
long Map::getNumGridRows() const { return getGridNumRows(&nav_grid_); }
bool Map::getMapPosition(double &x, double &y, long i, long j) {
return getPosition(x, y, i, j, &map_grid_);
}
bool Map::getMapIndex(double x, double y, long &i, long &j) {
return getIndex(x, y, i, j, &map_grid_);
}
Map::CellValue Map::getMapValue(long i, long j) {
return toCellValue(getValue(i, j, &map_grid_), "map");
}
Map::CellValue Map::getMapValue(geometry_msgs::PoseStamped ps) const {
return toCellValue(getValue(ps, &map_grid_), "map");
}
Map::CellValue Map::getMapValue(long i) const {
return toCellValue(getValue(i, &map_grid_), "map");
}
long Map::getNumCols() { return getGridNumCols(&map_grid_); }
long Map::getNumRows() { return getGridNumRows(&map_grid_); }
bool Map::getRFIDPosition(double &x, double &y, long i, long j) {
return getPosition(x, y, i, j, &rfid_grid_);
}
bool Map::getRFIDIndex(double x, double y, long &i, long &j) {
return getIndex(x, y, i, j, &rfid_grid_);
}
float Map::getRFIDGridValue(long i, long j) const {
return getValue(i, j, &rfid_grid_);
}
float Map::getRFIDGridValue(geometry_msgs::PoseStamped ps) const {
return getValue(ps, &rfid_grid_);
}
float Map::getRFIDGridValue(long i) const { return getValue(i, &rfid_grid_); }
long Map::getRFIDGridNumCols() { return getGridNumCols(&rfid_grid_); }
long Map::getRFIDGridNumRows() { return getGridNumRows(&map_grid_); }
// Generic (internal) getters ..............................................
bool Map::getIndex(double x, double y, long &i, long &j,
grid_map::GridMap *gm) {
grid_map::Index resIndex;
Position inPose(x, y);
bool success;
if (gm->isInside(inPose)) {
gm->getIndex(inPose, resIndex);
i = resIndex(0);
j = resIndex(1);
success = true;
} else {
printErrorReason(inPose, gm);
i = -1;
j = -1;
success = false;
}
return success;
}
bool Map::getPosition(double &x, double &y, long i, long j,
grid_map::GridMap *gm) {
grid_map::Index inIndex(i, j);
Position inPose;
bool success;
if (gm->getPosition(inIndex, inPose)) {
x = inPose.x();
y = inPose.y();
success = true;
} else {
ROS_ERROR("[1]Requested [%lu, %lu] index is outside grid boundaries: [0,0] "
"- [%d, %d]",
i, j, gm->getSize()(0) - 1, gm->getSize()(1) - 1);
x = std::numeric_limits<double>::max();
y = std::numeric_limits<double>::max();
success = false;
}
return success;
}
float Map::getValue(long i, long j, const grid_map::GridMap *gm) const {
float val;
grid_map::Index ind(i, j);
Position position;
if (gm->getPosition(ind, position)) {
val = gm->at("layer", ind);
} else {
ROS_ERROR("[2]Requested [%lu, %lu] index is outside grid boundaries: [0,0] "
"- [%d, %d]",
i, j, gm->getSize()(0) - 1, gm->getSize()(1) - 1);
val = -1;
}
return val;
}
float Map::getValue(geometry_msgs::PoseStamped ps,
const grid_map::GridMap *gm) const {
float val;
if (ps.header.frame_id != gm->getFrameId()) {
ROS_ERROR("Pose given in different frame id: grid==[%s], point ==[%s]",
ps.header.frame_id.c_str(), gm->getFrameId().c_str());
val = -1;
} else {
grid_map::Position point(ps.pose.position.x, ps.pose.position.y);
if (gm->isInside(point)) {
val = gm->atPosition("layer", point);
} else {
printErrorReason(point, gm);
val = -1;
}
}
return val;
}
float Map::getValue(long i, const grid_map::GridMap *gm) const {
long rows = getGridNumRows(gm);
std::ldiv_t result = std::div(i, rows);
rows = result.quot;
long cols = result.rem;
return getValue(rows, cols, gm);
}
int Map::getGridNumCols(const grid_map::GridMap *gm) const {
return gm->getSize()(1);
}
int Map::getGridNumRows(const grid_map::GridMap *gm) const {
return gm->getSize()(0);
}
// E.O. Generic (internal) getters .........................................
// Generic (internal) setters ..............................................
void Map::setValue(float value, long i, long j, grid_map::GridMap *gm) {
grid_map::Index ind(i, j);
Position position;
if (gm->getPosition(ind, position)) {
// cout << "Set value: " << value <<" at index: [" << i << "," << j
// <<"]" << endl;
(gm->at("layer", ind)) = value;
} else {
ROS_ERROR("[3]Requested [%lu, %lu] index is outside grid boundaries: [0,0] "
"- [%d, %d]",
i, j, gm->getSize()(0) - 1, gm->getSize()(1) - 1);
}
}
void Map::setValue(float value, geometry_msgs::PoseStamped ps,
grid_map::GridMap *gm) {
if (ps.header.frame_id != gm->getFrameId()) {
ROS_ERROR("Pose given in different frame id: grid==[%s], point ==[%s]",
ps.header.frame_id.c_str(), gm->getFrameId().c_str());
} else {
grid_map::Position point(ps.pose.position.x, ps.pose.position.y);
if (gm->isInside(point)) {
gm->atPosition("layer", point) = value;
} else {
printErrorReason(point, gm);
}
}
}
void Map::setValue(float value, long i, grid_map::GridMap *gm) {
long rows = getGridNumRows(gm);
std::ldiv_t result = std::div(i, rows);
rows = result.quot;
long cols = result.rem;
setValue((value), rows, cols, gm);
}
// E.O. Generic (internal) setters .........................................
void Map::setPathPlanningGridValue(Map::CellValue value, int i, int j) {
setValue(toFloat(value), i, j, &planning_grid_);
}
void Map::setPathPlanningGridValue(Map::CellValue value,
geometry_msgs::PoseStamped ps) {
setValue(toFloat(value), ps, &planning_grid_);
}
void Map::setPathPlanningGridValue(Map::CellValue value, long i) {
setValue(toFloat(value), i, &planning_grid_);
}
void Map::setGridValue(Map::CellValue value, long i, long j) {
setValue(toFloat(value), i, j, &nav_grid_);
}
void Map::setGridValue(Map::CellValue value, geometry_msgs::PoseStamped ps) {
setValue(toFloat(value), ps, &nav_grid_);
}
void Map::setGridValue(Map::CellValue value, long i) {
setValue(toFloat(value), i, &nav_grid_);
}