Karto.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <math.h>
  18. #include <assert.h>
  19. #include <float.h>
  20. #include <limits.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include "open_karto/Karto.h"
  25. namespace karto
  26. {
  27. ////////////////////////////////////////////////////////////////////////////////////////
  28. ////////////////////////////////////////////////////////////////////////////////////////
  29. ////////////////////////////////////////////////////////////////////////////////////////
  30. SensorManager* SensorManager::GetInstance()
  31. {
  32. static Singleton<SensorManager> sInstance;
  33. return sInstance.Get();
  34. }
  35. ////////////////////////////////////////////////////////////////////////////////////////
  36. ////////////////////////////////////////////////////////////////////////////////////////
  37. ////////////////////////////////////////////////////////////////////////////////////////
  38. Object::Object()
  39. : m_pParameterManager(new ParameterManager())
  40. {
  41. }
  42. Object::Object(const Name& rName)
  43. : m_Name(rName)
  44. , m_pParameterManager(new ParameterManager())
  45. {
  46. }
  47. Object::~Object()
  48. {
  49. delete m_pParameterManager;
  50. m_pParameterManager = NULL;
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////////////
  53. ////////////////////////////////////////////////////////////////////////////////////////
  54. ////////////////////////////////////////////////////////////////////////////////////////
  55. Module::Module(const std::string& rName)
  56. : Object(rName)
  57. {
  58. }
  59. Module::~Module()
  60. {
  61. }
  62. ////////////////////////////////////////////////////////////////////////////////////////
  63. ////////////////////////////////////////////////////////////////////////////////////////
  64. ////////////////////////////////////////////////////////////////////////////////////////
  65. Sensor::Sensor(const Name& rName)
  66. : Object(rName)
  67. {
  68. m_pOffsetPose = new Parameter<Pose2>("OffsetPose", Pose2(), GetParameterManager());
  69. }
  70. Sensor::~Sensor()
  71. {
  72. }
  73. ////////////////////////////////////////////////////////////////////////////////////////
  74. ////////////////////////////////////////////////////////////////////////////////////////
  75. ////////////////////////////////////////////////////////////////////////////////////////
  76. SensorData::SensorData(const Name& rSensorName)
  77. : Object()
  78. , m_StateId(-1)
  79. , m_UniqueId(-1)
  80. , m_SensorName(rSensorName)
  81. , m_Time(0.0)
  82. {
  83. }
  84. SensorData::~SensorData()
  85. {
  86. forEach(CustomDataVector, &m_CustomData)
  87. {
  88. delete *iter;
  89. }
  90. m_CustomData.clear();
  91. }
  92. ////////////////////////////////////////////////////////////////////////////////////////
  93. ////////////////////////////////////////////////////////////////////////////////////////
  94. ////////////////////////////////////////////////////////////////////////////////////////
  95. void CellUpdater::operator() (kt_int32u index)
  96. {
  97. kt_int8u* pDataPtr = m_pOccupancyGrid->GetDataPointer();
  98. kt_int32u* pCellPassCntPtr = m_pOccupancyGrid->m_pCellPassCnt->GetDataPointer();
  99. kt_int32u* pCellHitCntPtr = m_pOccupancyGrid->m_pCellHitsCnt->GetDataPointer();
  100. m_pOccupancyGrid->UpdateCell(&pDataPtr[index], pCellPassCntPtr[index], pCellHitCntPtr[index]);
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////////////
  103. ////////////////////////////////////////////////////////////////////////////////////////
  104. ////////////////////////////////////////////////////////////////////////////////////////
  105. std::ostream& operator << (std::ostream& rStream, Exception& rException)
  106. {
  107. rStream << "Error detect: " << std::endl;
  108. rStream << " ==> error code: " << rException.GetErrorCode() << std::endl;
  109. rStream << " ==> error message: " << rException.GetErrorMessage() << std::endl;
  110. return rStream;
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////////////
  113. ////////////////////////////////////////////////////////////////////////////////////////
  114. ////////////////////////////////////////////////////////////////////////////////////////
  115. const PointVectorDouble LaserRangeFinder::GetPointReadings(LocalizedRangeScan* pLocalizedRangeScan,
  116. CoordinateConverter* pCoordinateConverter,
  117. kt_bool ignoreThresholdPoints,
  118. kt_bool flipY) const
  119. {
  120. PointVectorDouble pointReadings;
  121. Pose2 scanPose = pLocalizedRangeScan->GetSensorPose();
  122. // compute point readings
  123. kt_int32u beamNum = 0;
  124. kt_double* pRangeReadings = pLocalizedRangeScan->GetRangeReadings();
  125. for (kt_int32u i = 0; i < m_NumberOfRangeReadings; i++, beamNum++)
  126. {
  127. kt_double rangeReading = pRangeReadings[i];
  128. if (ignoreThresholdPoints)
  129. {
  130. if (!math::InRange(rangeReading, GetMinimumRange(), GetRangeThreshold()))
  131. {
  132. continue;
  133. }
  134. }
  135. else
  136. {
  137. rangeReading = math::Clip(rangeReading, GetMinimumRange(), GetRangeThreshold());
  138. }
  139. kt_double angle = scanPose.GetHeading() + GetMinimumAngle() + beamNum * GetAngularResolution();
  140. Vector2<kt_double> point;
  141. point.SetX(scanPose.GetX() + (rangeReading * cos(angle)));
  142. point.SetY(scanPose.GetY() + (rangeReading * sin(angle)));
  143. if (pCoordinateConverter != NULL)
  144. {
  145. Vector2<kt_int32s> gridPoint = pCoordinateConverter->WorldToGrid(point, flipY);
  146. point.SetX(gridPoint.GetX());
  147. point.SetY(gridPoint.GetY());
  148. }
  149. pointReadings.push_back(point);
  150. }
  151. return pointReadings;
  152. }
  153. kt_bool LaserRangeFinder::Validate(SensorData* pSensorData)
  154. {
  155. LaserRangeScan* pLaserRangeScan = dynamic_cast<LaserRangeScan*>(pSensorData);
  156. // verify number of range readings in LaserRangeScan matches the number of expected range readings
  157. // if (pLaserRangeScan->GetNumberOfRangeReadings() != GetNumberOfRangeReadings())
  158. // {
  159. // std::cout << "LaserRangeScan contains " << pLaserRangeScan->GetNumberOfRangeReadings()
  160. // << " range readings, expected " << GetNumberOfRangeReadings() << std::endl;
  161. // return false;
  162. // }
  163. return true;
  164. }
  165. ////////////////////////////////////////////////////////////////////////////////////////
  166. ////////////////////////////////////////////////////////////////////////////////////////
  167. ////////////////////////////////////////////////////////////////////////////////////////
  168. void ParameterManager::Clear()
  169. {
  170. forEach(karto::ParameterVector, &m_Parameters)
  171. {
  172. delete *iter;
  173. }
  174. m_Parameters.clear();
  175. m_ParameterLookup.clear();
  176. }
  177. void ParameterManager::Add(AbstractParameter* pParameter)
  178. {
  179. if (pParameter != NULL && pParameter->GetName() != "")
  180. {
  181. if (m_ParameterLookup.find(pParameter->GetName()) == m_ParameterLookup.end())
  182. {
  183. m_Parameters.push_back(pParameter);
  184. m_ParameterLookup[pParameter->GetName()] = pParameter;
  185. }
  186. else
  187. {
  188. m_ParameterLookup[pParameter->GetName()]->SetValueFromString(pParameter->GetValueAsString());
  189. assert(false);
  190. }
  191. }
  192. }
  193. ////////////////////////////////////////////////////////////////////////////////////////
  194. ////////////////////////////////////////////////////////////////////////////////////////
  195. ////////////////////////////////////////////////////////////////////////////////////////
  196. /*
  197. std::string LaserRangeFinder::LaserRangeFinderTypeNames[6] =
  198. {
  199. "Custom",
  200. "Sick_LMS100",
  201. "Sick_LMS200",
  202. "Sick_LMS291",
  203. "Hokuyo_UTM_30LX",
  204. "Hokuyo_URG_04LX"
  205. };
  206. */
  207. } // namespace karto