tutorial2.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2010 SRI International
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "SpaSolver.h"
  18. #include <open_karto/Mapper.h>
  19. /**
  20. * Sample code to demonstrate karto map creation
  21. * Create a laser range finder device and three "dummy" range scans.
  22. * Add the device and range scans to a karto Mapper.
  23. */
  24. karto::Dataset* CreateMap(karto::Mapper* pMapper)
  25. {
  26. karto::Dataset* pDataset = new karto::Dataset();
  27. /////////////////////////////////////
  28. // Create a laser range finder device - use SmartPointer to let karto subsystem manage memory.
  29. karto::Name name("laser0");
  30. karto::LaserRangeFinder* pLaserRangeFinder = karto::LaserRangeFinder::CreateLaserRangeFinder(karto::LaserRangeFinder_Custom, name);
  31. pLaserRangeFinder->SetOffsetPose(karto::Pose2(1.0, 0.0, 0.0));
  32. pLaserRangeFinder->SetAngularResolution(karto::math::DegreesToRadians(0.5));
  33. pLaserRangeFinder->SetRangeThreshold(12.0);
  34. pDataset->Add(pLaserRangeFinder);
  35. /////////////////////////////////////
  36. // Create three localized range scans, all using the same range readings, but with different poses.
  37. karto::LocalizedRangeScan* pLocalizedRangeScan = NULL;
  38. // 1. localized range scan
  39. // Create a vector of range readings. Simple example where all the measurements are the same value.
  40. std::vector<kt_double> readings;
  41. for (int i=0; i<361; i++)
  42. {
  43. readings.push_back(3.0);
  44. }
  45. // create localized range scan
  46. pLocalizedRangeScan = new karto::LocalizedRangeScan(name, readings);
  47. pLocalizedRangeScan->SetOdometricPose(karto::Pose2(0.0, 0.0, 0.0));
  48. pLocalizedRangeScan->SetCorrectedPose(karto::Pose2(0.0, 0.0, 0.0));
  49. // Add the localized range scan to the mapper
  50. pMapper->Process(pLocalizedRangeScan);
  51. std::cout << "Pose: " << pLocalizedRangeScan->GetOdometricPose() << " Corrected Pose: " << pLocalizedRangeScan->GetCorrectedPose() << std::endl;
  52. // Add the localized range scan to the dataset
  53. pDataset->Add(pLocalizedRangeScan);
  54. // 2. localized range scan
  55. // create localized range scan
  56. pLocalizedRangeScan = new karto::LocalizedRangeScan(name, readings);
  57. pLocalizedRangeScan->SetOdometricPose(karto::Pose2(1.0, 0.0, 1.57));
  58. pLocalizedRangeScan->SetCorrectedPose(karto::Pose2(1.0, 0.0, 1.57));
  59. // Add the localized range scan to the mapper
  60. pMapper->Process(pLocalizedRangeScan);
  61. std::cout << "Pose: " << pLocalizedRangeScan->GetOdometricPose() << " Corrected Pose: " << pLocalizedRangeScan->GetCorrectedPose() << std::endl;
  62. // Add the localized range scan to the dataset
  63. pDataset->Add(pLocalizedRangeScan);
  64. // 3. localized range scan
  65. // create localized range scan
  66. pLocalizedRangeScan = new karto::LocalizedRangeScan(name, readings);
  67. pLocalizedRangeScan->SetOdometricPose(karto::Pose2(1.0, -1.0, 2.35619449));
  68. pLocalizedRangeScan->SetCorrectedPose(karto::Pose2(1.0, -1.0, 2.35619449));
  69. // Add the localized range scan to the mapper
  70. pMapper->Process(pLocalizedRangeScan);
  71. std::cout << "Pose: " << pLocalizedRangeScan->GetOdometricPose() << " Corrected Pose: " << pLocalizedRangeScan->GetCorrectedPose() << std::endl;
  72. // Add the localized range scan to the dataset
  73. pDataset->Add(pLocalizedRangeScan);
  74. return pDataset;
  75. }
  76. /**
  77. * Sample code to demonstrate basic occupancy grid creation and print occupancy grid.
  78. */
  79. karto::OccupancyGrid* CreateOccupancyGrid(karto::Mapper* pMapper, kt_double resolution)
  80. {
  81. std::cout << "Generating map..." << std::endl;
  82. // Create a map (occupancy grid) - time it
  83. karto::OccupancyGrid* pOccupancyGrid = karto::OccupancyGrid::CreateFromScans(pMapper->GetAllProcessedScans(), resolution);
  84. return pOccupancyGrid;
  85. }
  86. /**
  87. * Sample code to print a basic occupancy grid
  88. */
  89. void PrintOccupancyGrid(karto::OccupancyGrid* pOccupancyGrid)
  90. {
  91. if (pOccupancyGrid != NULL)
  92. {
  93. // Output ASCII representation of map
  94. kt_int32s width = pOccupancyGrid->GetWidth();
  95. kt_int32s height = pOccupancyGrid->GetHeight();
  96. karto::Vector2<kt_double> offset = pOccupancyGrid->GetCoordinateConverter()->GetOffset();
  97. std::cout << "width = " << width << ", height = " << height << ", scale = " << pOccupancyGrid->GetCoordinateConverter()->GetScale() << ", offset: " << offset.GetX() << ", " << offset.GetY() << std::endl;
  98. for (kt_int32s y=height-1; y>=0; y--)
  99. {
  100. for (kt_int32s x=0; x<width; x++)
  101. {
  102. // Getting the value at position x,y
  103. kt_int8u value = pOccupancyGrid->GetValue(karto::Vector2<kt_int32s>(x, y));
  104. switch (value)
  105. {
  106. case karto::GridStates_Unknown:
  107. std::cout << "*";
  108. break;
  109. case karto::GridStates_Occupied:
  110. std::cout << "X";
  111. break;
  112. case karto::GridStates_Free:
  113. std::cout << " ";
  114. break;
  115. default:
  116. std::cout << "?";
  117. }
  118. }
  119. std::cout << std::endl;
  120. }
  121. std::cout << std::endl;
  122. }
  123. }
  124. int main(int /*argc*/, char **/*argv*/)
  125. {
  126. // use try/catch to catch karto exceptions that might be thrown by the karto subsystem.
  127. /////////////////////////////////////
  128. // Get karto default mapper
  129. karto::Mapper* pMapper = new karto::Mapper();
  130. if (pMapper != NULL)
  131. {
  132. // set solver
  133. SpaSolver* pSolver = new SpaSolver();
  134. pMapper->SetScanSolver(pSolver);
  135. karto::OccupancyGrid* pOccupancyGrid = NULL;
  136. /////////////////////////////////////
  137. // sample code that creates a map from sample device and sample localized range scans
  138. // clear mapper
  139. pMapper->Reset();
  140. // create map from created dataset
  141. karto::Dataset* pDataset = CreateMap(pMapper);
  142. // create occupancy grid at 0.1 resolution and print grid
  143. pOccupancyGrid = CreateOccupancyGrid(pMapper, 0.1);
  144. PrintOccupancyGrid(pOccupancyGrid);
  145. delete pOccupancyGrid;
  146. // delete solver
  147. delete pSolver;
  148. // delete mapper
  149. delete pMapper;
  150. // delete dataset and all allocated devices and device states
  151. delete pDataset;
  152. }
  153. return 0;
  154. }