Mapper.h 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  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. #ifndef OPEN_KARTO_MAPPER_H
  18. #define OPEN_KARTO_MAPPER_H
  19. #include <map>
  20. #include <set>
  21. #include <vector>
  22. #include <queue>
  23. #include <open_karto/Karto.h>
  24. namespace karto
  25. {
  26. ////////////////////////////////////////////////////////////////////////////////////////
  27. // Listener classes
  28. /**
  29. * Abstract class to listen to mapper general messages
  30. */
  31. class MapperListener
  32. {
  33. public:
  34. /**
  35. * Called with general message
  36. */
  37. virtual void Info(const std::string& /*rInfo*/) {};
  38. };
  39. /**
  40. * Abstract class to listen to mapper debug messages
  41. */
  42. class MapperDebugListener
  43. {
  44. public:
  45. /**
  46. * Called with debug message
  47. */
  48. virtual void Debug(const std::string& /*rInfo*/) {};
  49. };
  50. /**
  51. * Abstract class to listen to mapper loop closure messages
  52. */
  53. class MapperLoopClosureListener : public MapperListener
  54. {
  55. public:
  56. /**
  57. * Called when checking for loop closures
  58. */
  59. virtual void LoopClosureCheck(const std::string& /*rInfo*/) {};
  60. /**
  61. * Called when loop closure is starting
  62. */
  63. virtual void BeginLoopClosure(const std::string& /*rInfo*/) {};
  64. /**
  65. * Called when loop closure is over
  66. */
  67. virtual void EndLoopClosure(const std::string& /*rInfo*/) {};
  68. }; // MapperListener
  69. ////////////////////////////////////////////////////////////////////////////////////////
  70. ////////////////////////////////////////////////////////////////////////////////////////
  71. ////////////////////////////////////////////////////////////////////////////////////////
  72. /**
  73. * Class for edge labels
  74. */
  75. class EdgeLabel
  76. {
  77. public:
  78. /**
  79. * Default constructor
  80. */
  81. EdgeLabel()
  82. {
  83. }
  84. /**
  85. * Destructor
  86. */
  87. virtual ~EdgeLabel()
  88. {
  89. }
  90. }; // EdgeLabel
  91. ////////////////////////////////////////////////////////////////////////////////////////
  92. ////////////////////////////////////////////////////////////////////////////////////////
  93. ////////////////////////////////////////////////////////////////////////////////////////
  94. // A LinkInfo object contains the requisite information for the "spring"
  95. // that links two scans together--the pose difference and the uncertainty
  96. // (represented by a covariance matrix).
  97. class LinkInfo : public EdgeLabel
  98. {
  99. public:
  100. /**
  101. * Constructs a link between the given poses
  102. * @param rPose1
  103. * @param rPose2
  104. * @param rCovariance
  105. */
  106. LinkInfo(const Pose2& rPose1, const Pose2& rPose2, const Matrix3& rCovariance)
  107. {
  108. Update(rPose1, rPose2, rCovariance);
  109. }
  110. /**
  111. * Destructor
  112. */
  113. virtual ~LinkInfo()
  114. {
  115. }
  116. public:
  117. /**
  118. * Changes the link information to be the given parameters
  119. * @param rPose1
  120. * @param rPose2
  121. * @param rCovariance
  122. */
  123. void Update(const Pose2& rPose1, const Pose2& rPose2, const Matrix3& rCovariance)
  124. {
  125. m_Pose1 = rPose1;
  126. m_Pose2 = rPose2;
  127. // transform second pose into the coordinate system of the first pose
  128. Transform transform(rPose1, Pose2());
  129. m_PoseDifference = transform.TransformPose(rPose2);
  130. // transform covariance into reference of first pose
  131. Matrix3 rotationMatrix;
  132. rotationMatrix.FromAxisAngle(0, 0, 1, -rPose1.GetHeading());
  133. m_Covariance = rotationMatrix * rCovariance * rotationMatrix.Transpose();
  134. }
  135. /**
  136. * Gets the first pose
  137. * @return first pose
  138. */
  139. inline const Pose2& GetPose1()
  140. {
  141. return m_Pose1;
  142. }
  143. /**
  144. * Gets the second pose
  145. * @return second pose
  146. */
  147. inline const Pose2& GetPose2()
  148. {
  149. return m_Pose2;
  150. }
  151. /**
  152. * Gets the pose difference
  153. * @return pose difference
  154. */
  155. inline const Pose2& GetPoseDifference()
  156. {
  157. return m_PoseDifference;
  158. }
  159. /**
  160. * Gets the link covariance
  161. * @return link covariance
  162. */
  163. inline const Matrix3& GetCovariance()
  164. {
  165. return m_Covariance;
  166. }
  167. private:
  168. Pose2 m_Pose1;
  169. Pose2 m_Pose2;
  170. Pose2 m_PoseDifference;
  171. Matrix3 m_Covariance;
  172. }; // LinkInfo
  173. ////////////////////////////////////////////////////////////////////////////////////////
  174. ////////////////////////////////////////////////////////////////////////////////////////
  175. ////////////////////////////////////////////////////////////////////////////////////////
  176. template<typename T>
  177. class Edge;
  178. /**
  179. * Represents an object in a graph
  180. */
  181. template<typename T>
  182. class Vertex
  183. {
  184. friend class Edge<T>;
  185. public:
  186. /**
  187. * Constructs a vertex representing the given object
  188. * @param pObject
  189. */
  190. Vertex(T* pObject)
  191. : m_pObject(pObject)
  192. {
  193. }
  194. /**
  195. * Destructor
  196. */
  197. virtual ~Vertex()
  198. {
  199. }
  200. /**
  201. * Gets edges adjacent to this vertex
  202. * @return adjacent edges
  203. */
  204. inline const std::vector<Edge<T>*>& GetEdges() const
  205. {
  206. return m_Edges;
  207. }
  208. /**
  209. * Gets the object associated with this vertex
  210. * @return the object
  211. */
  212. inline T* GetObject() const
  213. {
  214. return m_pObject;
  215. }
  216. /**
  217. * Gets a vector of the vertices adjacent to this vertex
  218. * @return adjacent vertices
  219. */
  220. std::vector<Vertex<T>*> GetAdjacentVertices() const
  221. {
  222. std::vector<Vertex<T>*> vertices;
  223. const_forEach(typename std::vector<Edge<T>*>, &m_Edges)
  224. {
  225. Edge<T>* pEdge = *iter;
  226. // check both source and target because we have a undirected graph
  227. if (pEdge->GetSource() != this)
  228. {
  229. vertices.push_back(pEdge->GetSource());
  230. }
  231. if (pEdge->GetTarget() != this)
  232. {
  233. vertices.push_back(pEdge->GetTarget());
  234. }
  235. }
  236. return vertices;
  237. }
  238. private:
  239. /**
  240. * Adds the given edge to this vertex's edge list
  241. * @param pEdge edge to add
  242. */
  243. inline void AddEdge(Edge<T>* pEdge)
  244. {
  245. m_Edges.push_back(pEdge);
  246. }
  247. T* m_pObject;
  248. std::vector<Edge<T>*> m_Edges;
  249. }; // Vertex<T>
  250. ////////////////////////////////////////////////////////////////////////////////////////
  251. ////////////////////////////////////////////////////////////////////////////////////////
  252. ////////////////////////////////////////////////////////////////////////////////////////
  253. /**
  254. * Represents an edge in a graph
  255. */
  256. template<typename T>
  257. class Edge
  258. {
  259. public:
  260. /**
  261. * Constructs an edge from the source to target vertex
  262. * @param pSource
  263. * @param pTarget
  264. */
  265. Edge(Vertex<T>* pSource, Vertex<T>* pTarget)
  266. : m_pSource(pSource)
  267. , m_pTarget(pTarget)
  268. , m_pLabel(NULL)
  269. {
  270. m_pSource->AddEdge(this);
  271. m_pTarget->AddEdge(this);
  272. }
  273. /**
  274. * Destructor
  275. */
  276. virtual ~Edge()
  277. {
  278. m_pSource = NULL;
  279. m_pTarget = NULL;
  280. if (m_pLabel != NULL)
  281. {
  282. delete m_pLabel;
  283. m_pLabel = NULL;
  284. }
  285. }
  286. public:
  287. /**
  288. * Gets the source vertex
  289. * @return source vertex
  290. */
  291. inline Vertex<T>* GetSource() const
  292. {
  293. return m_pSource;
  294. }
  295. /**
  296. * Gets the target vertex
  297. * @return target vertex
  298. */
  299. inline Vertex<T>* GetTarget() const
  300. {
  301. return m_pTarget;
  302. }
  303. /**
  304. * Gets the link info
  305. * @return link info
  306. */
  307. inline EdgeLabel* GetLabel()
  308. {
  309. return m_pLabel;
  310. }
  311. /**
  312. * Sets the link payload
  313. * @param pLabel
  314. */
  315. inline void SetLabel(EdgeLabel* pLabel)
  316. {
  317. m_pLabel = pLabel;
  318. }
  319. private:
  320. Vertex<T>* m_pSource;
  321. Vertex<T>* m_pTarget;
  322. EdgeLabel* m_pLabel;
  323. }; // class Edge<T>
  324. ////////////////////////////////////////////////////////////////////////////////////////
  325. ////////////////////////////////////////////////////////////////////////////////////////
  326. ////////////////////////////////////////////////////////////////////////////////////////
  327. /**
  328. * Visitor class
  329. */
  330. template<typename T>
  331. class Visitor
  332. {
  333. public:
  334. /**
  335. * Applies the visitor to the vertex
  336. * @param pVertex
  337. * @return true if the visitor accepted the vertex, false otherwise
  338. */
  339. virtual kt_bool Visit(Vertex<T>* pVertex) = 0;
  340. }; // Visitor<T>
  341. ////////////////////////////////////////////////////////////////////////////////////////
  342. ////////////////////////////////////////////////////////////////////////////////////////
  343. ////////////////////////////////////////////////////////////////////////////////////////
  344. template<typename T>
  345. class Graph;
  346. /**
  347. * Graph traversal algorithm
  348. */
  349. template<typename T>
  350. class GraphTraversal
  351. {
  352. public:
  353. /**
  354. * Constructs a traverser for the given graph
  355. * @param pGraph
  356. */
  357. GraphTraversal(Graph<T>* pGraph)
  358. : m_pGraph(pGraph)
  359. {
  360. }
  361. /**
  362. * Destructor
  363. */
  364. virtual ~GraphTraversal()
  365. {
  366. }
  367. public:
  368. /**
  369. * Traverse the graph starting at the given vertex and applying the visitor to all visited nodes
  370. * @param pStartVertex
  371. * @param pVisitor
  372. */
  373. virtual std::vector<T*> Traverse(Vertex<T>* pStartVertex, Visitor<T>* pVisitor) = 0;
  374. protected:
  375. /**
  376. * Graph being traversed
  377. */
  378. Graph<T>* m_pGraph;
  379. }; // GraphTraversal<T>
  380. ////////////////////////////////////////////////////////////////////////////////////////
  381. ////////////////////////////////////////////////////////////////////////////////////////
  382. ////////////////////////////////////////////////////////////////////////////////////////
  383. /**
  384. * Graph
  385. */
  386. template<typename T>
  387. class Graph
  388. {
  389. public:
  390. /**
  391. * Maps names to vector of vertices
  392. */
  393. typedef std::map<Name, std::vector<Vertex<T>*> > VertexMap;
  394. public:
  395. /**
  396. * Default constructor
  397. */
  398. Graph()
  399. {
  400. }
  401. /**
  402. * Destructor
  403. */
  404. virtual ~Graph()
  405. {
  406. Clear();
  407. }
  408. public:
  409. /**
  410. * Adds and indexes the given vertex into the map using the given name
  411. * @param rName
  412. * @param pVertex
  413. */
  414. inline void AddVertex(const Name& rName, Vertex<T>* pVertex)
  415. {
  416. m_Vertices[rName].push_back(pVertex);
  417. }
  418. /**
  419. * Adds an edge to the graph
  420. * @param pEdge
  421. */
  422. inline void AddEdge(Edge<T>* pEdge)
  423. {
  424. m_Edges.push_back(pEdge);
  425. }
  426. /**
  427. * Deletes the graph data
  428. */
  429. void Clear()
  430. {
  431. forEachAs(typename VertexMap, &m_Vertices, indexIter)
  432. {
  433. // delete each vertex
  434. forEach(typename std::vector<Vertex<T>*>, &(indexIter->second))
  435. {
  436. delete *iter;
  437. }
  438. }
  439. m_Vertices.clear();
  440. const_forEach(typename std::vector<Edge<T>*>, &m_Edges)
  441. {
  442. delete *iter;
  443. }
  444. m_Edges.clear();
  445. }
  446. /**
  447. * Gets the edges of this graph
  448. * @return graph edges
  449. */
  450. inline const std::vector<Edge<T>*>& GetEdges() const
  451. {
  452. return m_Edges;
  453. }
  454. /**
  455. * Gets the vertices of this graph
  456. * @return graph vertices
  457. */
  458. inline const VertexMap& GetVertices() const
  459. {
  460. return m_Vertices;
  461. }
  462. protected:
  463. /**
  464. * Map of names to vector of vertices
  465. */
  466. VertexMap m_Vertices;
  467. /**
  468. * Edges of this graph
  469. */
  470. std::vector<Edge<T>*> m_Edges;
  471. }; // Graph<T>
  472. ////////////////////////////////////////////////////////////////////////////////////////
  473. ////////////////////////////////////////////////////////////////////////////////////////
  474. ////////////////////////////////////////////////////////////////////////////////////////
  475. template<typename T>
  476. class BreadthFirstTraversal : public GraphTraversal<T>
  477. {
  478. public:
  479. /**
  480. * Constructs a breadth-first traverser for the given graph
  481. */
  482. BreadthFirstTraversal(Graph<T>* pGraph)
  483. : GraphTraversal<T>(pGraph)
  484. {
  485. }
  486. /**
  487. * Destructor
  488. */
  489. virtual ~BreadthFirstTraversal()
  490. {
  491. }
  492. public:
  493. /**
  494. * Traverse the graph starting with the given vertex; applies the visitor to visited nodes
  495. * @param pStartVertex
  496. * @param pVisitor
  497. * @return visited vertices
  498. */
  499. virtual std::vector<T*> Traverse(Vertex<T>* pStartVertex, Visitor<T>* pVisitor)
  500. {
  501. std::queue<Vertex<T>*> toVisit;
  502. std::set<Vertex<T>*> seenVertices;
  503. std::vector<Vertex<T>*> validVertices;
  504. toVisit.push(pStartVertex);
  505. seenVertices.insert(pStartVertex);
  506. do
  507. {
  508. Vertex<T>* pNext = toVisit.front();
  509. toVisit.pop();
  510. if (pVisitor->Visit(pNext))
  511. {
  512. // vertex is valid, explore neighbors
  513. validVertices.push_back(pNext);
  514. std::vector<Vertex<T>*> adjacentVertices = pNext->GetAdjacentVertices();
  515. forEach(typename std::vector<Vertex<T>*>, &adjacentVertices)
  516. {
  517. Vertex<T>* pAdjacent = *iter;
  518. // adjacent vertex has not yet been seen, add to queue for processing
  519. if (seenVertices.find(pAdjacent) == seenVertices.end())
  520. {
  521. toVisit.push(pAdjacent);
  522. seenVertices.insert(pAdjacent);
  523. }
  524. }
  525. }
  526. } while (toVisit.empty() == false);
  527. std::vector<T*> objects;
  528. forEach(typename std::vector<Vertex<T>*>, &validVertices)
  529. {
  530. objects.push_back((*iter)->GetObject());
  531. }
  532. return objects;
  533. }
  534. }; // class BreadthFirstTraversal
  535. ////////////////////////////////////////////////////////////////////////////////////////
  536. ////////////////////////////////////////////////////////////////////////////////////////
  537. ////////////////////////////////////////////////////////////////////////////////////////
  538. class NearScanVisitor : public Visitor<LocalizedRangeScan>
  539. {
  540. public:
  541. NearScanVisitor(LocalizedRangeScan* pScan, kt_double maxDistance, kt_bool useScanBarycenter)
  542. : m_MaxDistanceSquared(math::Square(maxDistance))
  543. , m_UseScanBarycenter(useScanBarycenter)
  544. {
  545. m_CenterPose = pScan->GetReferencePose(m_UseScanBarycenter);
  546. }
  547. virtual kt_bool Visit(Vertex<LocalizedRangeScan>* pVertex)
  548. {
  549. LocalizedRangeScan* pScan = pVertex->GetObject();
  550. Pose2 pose = pScan->GetReferencePose(m_UseScanBarycenter);
  551. kt_double squaredDistance = pose.GetPosition().SquaredDistance(m_CenterPose.GetPosition());
  552. return (squaredDistance <= m_MaxDistanceSquared - KT_TOLERANCE);
  553. }
  554. protected:
  555. Pose2 m_CenterPose;
  556. kt_double m_MaxDistanceSquared;
  557. kt_bool m_UseScanBarycenter;
  558. }; // NearScanVisitor
  559. ////////////////////////////////////////////////////////////////////////////////////////
  560. ////////////////////////////////////////////////////////////////////////////////////////
  561. ////////////////////////////////////////////////////////////////////////////////////////
  562. class Mapper;
  563. class ScanMatcher;
  564. /**
  565. * Graph for graph SLAM algorithm
  566. */
  567. class KARTO_EXPORT MapperGraph : public Graph<LocalizedRangeScan>
  568. {
  569. public:
  570. /**
  571. * Graph for graph SLAM
  572. * @param pMapper
  573. * @param rangeThreshold
  574. */
  575. MapperGraph(Mapper* pMapper, kt_double rangeThreshold);
  576. /**
  577. * Destructor
  578. */
  579. virtual ~MapperGraph();
  580. public:
  581. /**
  582. * Adds a vertex representing the given scan to the graph
  583. * @param pScan
  584. */
  585. void AddVertex(LocalizedRangeScan* pScan);
  586. /**
  587. * Creates an edge between the source scan vertex and the target scan vertex if it
  588. * does not already exist; otherwise return the existing edge
  589. * @param pSourceScan
  590. * @param pTargetScan
  591. * @param rIsNewEdge set to true if the edge is new
  592. * @return edge between source and target scan vertices
  593. */
  594. Edge<LocalizedRangeScan>* AddEdge(LocalizedRangeScan* pSourceScan,
  595. LocalizedRangeScan* pTargetScan,
  596. kt_bool& rIsNewEdge);
  597. /**
  598. * Link scan to last scan and nearby chains of scans
  599. * @param pScan
  600. * @param rCovariance uncertainty of match
  601. */
  602. void AddEdges(LocalizedRangeScan* pScan, const Matrix3& rCovariance);
  603. /**
  604. * Tries to close a loop using the given scan with the scans from the given device
  605. * @param pScan
  606. * @param rSensorName
  607. */
  608. kt_bool TryCloseLoop(LocalizedRangeScan* pScan, const Name& rSensorName);
  609. /**
  610. * Find "nearby" (no further than given distance away) scans through graph links
  611. * @param pScan
  612. * @param maxDistance
  613. */
  614. LocalizedRangeScanVector FindNearLinkedScans(LocalizedRangeScan* pScan, kt_double maxDistance);
  615. /**
  616. * Gets the graph's scan matcher
  617. * @return scan matcher
  618. */
  619. inline ScanMatcher* GetLoopScanMatcher() const
  620. {
  621. return m_pLoopScanMatcher;
  622. }
  623. private:
  624. /**
  625. * Gets the vertex associated with the given scan
  626. * @param pScan
  627. * @return vertex of scan
  628. */
  629. inline Vertex<LocalizedRangeScan>* GetVertex(LocalizedRangeScan* pScan)
  630. {
  631. return m_Vertices[pScan->GetSensorName()][pScan->GetStateId()];
  632. }
  633. /**
  634. * Finds the closest scan in the vector to the given pose
  635. * @param rScans
  636. * @param rPose
  637. */
  638. LocalizedRangeScan* GetClosestScanToPose(const LocalizedRangeScanVector& rScans, const Pose2& rPose) const;
  639. /**
  640. * Adds an edge between the two scans and labels the edge with the given mean and covariance
  641. * @param pFromScan
  642. * @param pToScan
  643. * @param rMean
  644. * @param rCovariance
  645. */
  646. void LinkScans(LocalizedRangeScan* pFromScan,
  647. LocalizedRangeScan* pToScan,
  648. const Pose2& rMean,
  649. const Matrix3& rCovariance);
  650. /**
  651. * Find nearby chains of scans and link them to scan if response is high enough
  652. * @param pScan
  653. * @param rMeans
  654. * @param rCovariances
  655. */
  656. void LinkNearChains(LocalizedRangeScan* pScan, Pose2Vector& rMeans, std::vector<Matrix3>& rCovariances);
  657. /**
  658. * Link the chain of scans to the given scan by finding the closest scan in the chain to the given scan
  659. * @param rChain
  660. * @param pScan
  661. * @param rMean
  662. * @param rCovariance
  663. */
  664. void LinkChainToScan(const LocalizedRangeScanVector& rChain,
  665. LocalizedRangeScan* pScan,
  666. const Pose2& rMean,
  667. const Matrix3& rCovariance);
  668. /**
  669. * Find chains of scans that are close to given scan
  670. * @param pScan
  671. * @return chains of scans
  672. */
  673. std::vector<LocalizedRangeScanVector> FindNearChains(LocalizedRangeScan* pScan);
  674. /**
  675. * Compute mean of poses weighted by covariances
  676. * @param rMeans
  677. * @param rCovariances
  678. * @return weighted mean
  679. */
  680. Pose2 ComputeWeightedMean(const Pose2Vector& rMeans, const std::vector<Matrix3>& rCovariances) const;
  681. /**
  682. * Tries to find a chain of scan from the given device starting at the
  683. * given scan index that could possibly close a loop with the given scan
  684. * @param pScan
  685. * @param rSensorName
  686. * @param rStartNum
  687. * @return chain that can possibly close a loop with given scan
  688. */
  689. LocalizedRangeScanVector FindPossibleLoopClosure(LocalizedRangeScan* pScan,
  690. const Name& rSensorName,
  691. kt_int32u& rStartNum);
  692. /**
  693. * Optimizes scan poses
  694. */
  695. void CorrectPoses();
  696. private:
  697. /**
  698. * Mapper of this graph
  699. */
  700. Mapper* m_pMapper;
  701. /**
  702. * Scan matcher for loop closures
  703. */
  704. ScanMatcher* m_pLoopScanMatcher;
  705. /**
  706. * Traversal algorithm to find near linked scans
  707. */
  708. GraphTraversal<LocalizedRangeScan>* m_pTraversal;
  709. }; // MapperGraph
  710. ////////////////////////////////////////////////////////////////////////////////////////
  711. ////////////////////////////////////////////////////////////////////////////////////////
  712. ////////////////////////////////////////////////////////////////////////////////////////
  713. /**
  714. * Graph optimization algorithm
  715. */
  716. class ScanSolver
  717. {
  718. public:
  719. /**
  720. * Vector of id-pose pairs
  721. */
  722. typedef std::vector<std::pair<kt_int32s, Pose2> > IdPoseVector;
  723. /**
  724. * Default constructor
  725. */
  726. ScanSolver()
  727. {
  728. }
  729. /**
  730. * Destructor
  731. */
  732. virtual ~ScanSolver()
  733. {
  734. }
  735. public:
  736. /**
  737. * Solve!
  738. */
  739. virtual void Compute() = 0;
  740. /**
  741. * Get corrected poses after optimization
  742. * @return optimized poses
  743. */
  744. virtual const IdPoseVector& GetCorrections() const = 0;
  745. /**
  746. * Adds a node to the solver
  747. */
  748. virtual void AddNode(Vertex<LocalizedRangeScan>* /*pVertex*/)
  749. {
  750. }
  751. /**
  752. * Removes a node from the solver
  753. */
  754. virtual void RemoveNode(kt_int32s /*id*/)
  755. {
  756. }
  757. /**
  758. * Adds a constraint to the solver
  759. */
  760. virtual void AddConstraint(Edge<LocalizedRangeScan>* /*pEdge*/)
  761. {
  762. }
  763. /**
  764. * Removes a constraint from the solver
  765. */
  766. virtual void RemoveConstraint(kt_int32s /*sourceId*/, kt_int32s /*targetId*/)
  767. {
  768. }
  769. /**
  770. * Resets the solver
  771. */
  772. virtual void Clear() {};
  773. }; // ScanSolver
  774. ////////////////////////////////////////////////////////////////////////////////////////
  775. ////////////////////////////////////////////////////////////////////////////////////////
  776. ////////////////////////////////////////////////////////////////////////////////////////
  777. /**
  778. * Implementation of a correlation grid used for scan matching
  779. */
  780. class CorrelationGrid : public Grid<kt_int8u>
  781. {
  782. public:
  783. /**
  784. * Destructor
  785. */
  786. virtual ~CorrelationGrid()
  787. {
  788. delete [] m_pKernel;
  789. }
  790. public:
  791. /**
  792. * Create a correlation grid of given size and parameters
  793. * @param width
  794. * @param height
  795. * @param resolution
  796. * @param smearDeviation
  797. * @return correlation grid
  798. */
  799. static CorrelationGrid* CreateGrid(kt_int32s width,
  800. kt_int32s height,
  801. kt_double resolution,
  802. kt_double smearDeviation)
  803. {
  804. assert(resolution != 0.0);
  805. // +1 in case of roundoff
  806. kt_int32u borderSize = GetHalfKernelSize(smearDeviation, resolution) + 1;
  807. CorrelationGrid* pGrid = new CorrelationGrid(width, height, borderSize, resolution, smearDeviation);
  808. return pGrid;
  809. }
  810. /**
  811. * Gets the index into the data pointer of the given grid coordinate
  812. * @param rGrid
  813. * @param boundaryCheck
  814. * @return grid index
  815. */
  816. virtual kt_int32s GridIndex(const Vector2<kt_int32s>& rGrid, kt_bool boundaryCheck = true) const
  817. {
  818. kt_int32s x = rGrid.GetX() + m_Roi.GetX();
  819. kt_int32s y = rGrid.GetY() + m_Roi.GetY();
  820. return Grid<kt_int8u>::GridIndex(Vector2<kt_int32s>(x, y), boundaryCheck);
  821. }
  822. /**
  823. * Get the Region Of Interest (ROI)
  824. * @return region of interest
  825. */
  826. inline const Rectangle2<kt_int32s>& GetROI() const
  827. {
  828. return m_Roi;
  829. }
  830. /**
  831. * Sets the Region Of Interest (ROI)
  832. * @param roi
  833. */
  834. inline void SetROI(const Rectangle2<kt_int32s>& roi)
  835. {
  836. m_Roi = roi;
  837. }
  838. /**
  839. * Smear cell if the cell at the given point is marked as "occupied"
  840. * @param rGridPoint
  841. */
  842. inline void SmearPoint(const Vector2<kt_int32s>& rGridPoint)
  843. {
  844. assert(m_pKernel != NULL);
  845. int gridIndex = GridIndex(rGridPoint);
  846. if (GetDataPointer()[gridIndex] != GridStates_Occupied)
  847. {
  848. return;
  849. }
  850. kt_int32s halfKernel = m_KernelSize / 2;
  851. // apply kernel
  852. for (kt_int32s j = -halfKernel; j <= halfKernel; j++)
  853. {
  854. kt_int8u* pGridAdr = GetDataPointer(Vector2<kt_int32s>(rGridPoint.GetX(), rGridPoint.GetY() + j));
  855. kt_int32s kernelConstant = (halfKernel) + m_KernelSize * (j + halfKernel);
  856. // if a point is on the edge of the grid, there is no problem
  857. // with running over the edge of allowable memory, because
  858. // the grid has margins to compensate for the kernel size
  859. for (kt_int32s i = -halfKernel; i <= halfKernel; i++)
  860. {
  861. kt_int32s kernelArrayIndex = i + kernelConstant;
  862. kt_int8u kernelValue = m_pKernel[kernelArrayIndex];
  863. if (kernelValue > pGridAdr[i])
  864. {
  865. // kernel value is greater, so set it to kernel value
  866. pGridAdr[i] = kernelValue;
  867. }
  868. }
  869. }
  870. }
  871. protected:
  872. /**
  873. * Constructs a correlation grid of given size and parameters
  874. * @param width
  875. * @param height
  876. * @param borderSize
  877. * @param resolution
  878. * @param smearDeviation
  879. */
  880. CorrelationGrid(kt_int32u width, kt_int32u height, kt_int32u borderSize,
  881. kt_double resolution, kt_double smearDeviation)
  882. : Grid<kt_int8u>(width + borderSize * 2, height + borderSize * 2)
  883. , m_SmearDeviation(smearDeviation)
  884. , m_pKernel(NULL)
  885. {
  886. GetCoordinateConverter()->SetScale(1.0 / resolution);
  887. // setup region of interest
  888. m_Roi = Rectangle2<kt_int32s>(borderSize, borderSize, width, height);
  889. // calculate kernel
  890. CalculateKernel();
  891. }
  892. /**
  893. * Sets up the kernel for grid smearing.
  894. */
  895. virtual void CalculateKernel()
  896. {
  897. kt_double resolution = GetResolution();
  898. assert(resolution != 0.0);
  899. assert(m_SmearDeviation != 0.0);
  900. // min and max distance deviation for smearing;
  901. // will smear for two standard deviations, so deviation must be at least 1/2 of the resolution
  902. const kt_double MIN_SMEAR_DISTANCE_DEVIATION = 0.5 * resolution;
  903. const kt_double MAX_SMEAR_DISTANCE_DEVIATION = 10 * resolution;
  904. // check if given value too small or too big
  905. if (!math::InRange(m_SmearDeviation, MIN_SMEAR_DISTANCE_DEVIATION, MAX_SMEAR_DISTANCE_DEVIATION))
  906. {
  907. std::stringstream error;
  908. error << "Mapper Error: Smear deviation too small: Must be between "
  909. << MIN_SMEAR_DISTANCE_DEVIATION
  910. << " and "
  911. << MAX_SMEAR_DISTANCE_DEVIATION;
  912. throw std::runtime_error(error.str());
  913. }
  914. // NOTE: Currently assumes a two-dimensional kernel
  915. // +1 for center
  916. m_KernelSize = 2 * GetHalfKernelSize(m_SmearDeviation, resolution) + 1;
  917. // allocate kernel
  918. m_pKernel = new kt_int8u[m_KernelSize * m_KernelSize];
  919. if (m_pKernel == NULL)
  920. {
  921. throw std::runtime_error("Unable to allocate memory for kernel!");
  922. }
  923. // calculate kernel
  924. kt_int32s halfKernel = m_KernelSize / 2;
  925. for (kt_int32s i = -halfKernel; i <= halfKernel; i++)
  926. {
  927. for (kt_int32s j = -halfKernel; j <= halfKernel; j++)
  928. {
  929. #ifdef WIN32
  930. kt_double distanceFromMean = _hypot(i * resolution, j * resolution);
  931. #else
  932. kt_double distanceFromMean = hypot(i * resolution, j * resolution);
  933. #endif
  934. kt_double z = exp(-0.5 * pow(distanceFromMean / m_SmearDeviation, 2));
  935. kt_int32u kernelValue = static_cast<kt_int32u>(math::Round(z * GridStates_Occupied));
  936. assert(math::IsUpTo(kernelValue, static_cast<kt_int32u>(255)));
  937. int kernelArrayIndex = (i + halfKernel) + m_KernelSize * (j + halfKernel);
  938. m_pKernel[kernelArrayIndex] = static_cast<kt_int8u>(kernelValue);
  939. }
  940. }
  941. }
  942. /**
  943. * Computes the kernel half-size based on the smear distance and the grid resolution.
  944. * Computes to two standard deviations to get 95% region and to reduce aliasing.
  945. * @param smearDeviation
  946. * @param resolution
  947. * @return kernel half-size based on the parameters
  948. */
  949. static kt_int32s GetHalfKernelSize(kt_double smearDeviation, kt_double resolution)
  950. {
  951. assert(resolution != 0.0);
  952. return static_cast<kt_int32s>(math::Round(2.0 * smearDeviation / resolution));
  953. }
  954. private:
  955. /**
  956. * The point readings are smeared by this value in X and Y to create a smoother response.
  957. * Default value is 0.03 meters.
  958. */
  959. kt_double m_SmearDeviation;
  960. // Size of one side of the kernel
  961. kt_int32s m_KernelSize;
  962. // Cached kernel for smearing
  963. kt_int8u* m_pKernel;
  964. // region of interest
  965. Rectangle2<kt_int32s> m_Roi;
  966. }; // CorrelationGrid
  967. ////////////////////////////////////////////////////////////////////////////////////////
  968. ////////////////////////////////////////////////////////////////////////////////////////
  969. ////////////////////////////////////////////////////////////////////////////////////////
  970. /**
  971. * Scan matcher
  972. */
  973. class KARTO_EXPORT ScanMatcher
  974. {
  975. public:
  976. /**
  977. * Destructor
  978. */
  979. virtual ~ScanMatcher();
  980. public:
  981. /**
  982. * Create a scan matcher with the given parameters
  983. */
  984. static ScanMatcher* Create(Mapper* pMapper,
  985. kt_double searchSize,
  986. kt_double resolution,
  987. kt_double smearDeviation,
  988. kt_double rangeThreshold);
  989. /**
  990. * Match given scan against set of scans
  991. * @param pScan scan being scan-matched
  992. * @param rBaseScans set of scans whose points will mark cells in grid as being occupied
  993. * @param rMean output parameter of mean (best pose) of match
  994. * @param rCovariance output parameter of covariance of match
  995. * @param doPenalize whether to penalize matches further from the search center
  996. * @param doRefineMatch whether to do finer-grained matching if coarse match is good (default is true)
  997. * @return strength of response
  998. */
  999. kt_double MatchScan(LocalizedRangeScan* pScan,
  1000. const LocalizedRangeScanVector& rBaseScans,
  1001. Pose2& rMean, Matrix3& rCovariance,
  1002. kt_bool doPenalize = true,
  1003. kt_bool doRefineMatch = true);
  1004. /**
  1005. * Finds the best pose for the scan centering the search in the correlation grid
  1006. * at the given pose and search in the space by the vector and angular offsets
  1007. * in increments of the given resolutions
  1008. * @param pScan scan to match against correlation grid
  1009. * @param rSearchCenter the center of the search space
  1010. * @param rSearchSpaceOffset searches poses in the area offset by this vector around search center
  1011. * @param rSearchSpaceResolution how fine a granularity to search in the search space
  1012. * @param searchAngleOffset searches poses in the angles offset by this angle around search center
  1013. * @param searchAngleResolution how fine a granularity to search in the angular search space
  1014. * @param doPenalize whether to penalize matches further from the search center
  1015. * @param rMean output parameter of mean (best pose) of match
  1016. * @param rCovariance output parameter of covariance of match
  1017. * @param doingFineMatch whether to do a finer search after coarse search
  1018. * @return strength of response
  1019. */
  1020. kt_double CorrelateScan(LocalizedRangeScan* pScan,
  1021. const Pose2& rSearchCenter,
  1022. const Vector2<kt_double>& rSearchSpaceOffset,
  1023. const Vector2<kt_double>& rSearchSpaceResolution,
  1024. kt_double searchAngleOffset,
  1025. kt_double searchAngleResolution,
  1026. kt_bool doPenalize,
  1027. Pose2& rMean,
  1028. Matrix3& rCovariance,
  1029. kt_bool doingFineMatch);
  1030. /**
  1031. * Computes the positional covariance of the best pose
  1032. * @param rBestPose
  1033. * @param bestResponse
  1034. * @param rSearchCenter
  1035. * @param rSearchSpaceOffset
  1036. * @param rSearchSpaceResolution
  1037. * @param searchAngleResolution
  1038. * @param rCovariance
  1039. */
  1040. void ComputePositionalCovariance(const Pose2& rBestPose,
  1041. kt_double bestResponse,
  1042. const Pose2& rSearchCenter,
  1043. const Vector2<kt_double>& rSearchSpaceOffset,
  1044. const Vector2<kt_double>& rSearchSpaceResolution,
  1045. kt_double searchAngleResolution,
  1046. Matrix3& rCovariance);
  1047. /**
  1048. * Computes the angular covariance of the best pose
  1049. * @param rBestPose
  1050. * @param bestResponse
  1051. * @param rSearchCenter
  1052. * @param searchAngleOffset
  1053. * @param searchAngleResolution
  1054. * @param rCovariance
  1055. */
  1056. void ComputeAngularCovariance(const Pose2& rBestPose,
  1057. kt_double bestResponse,
  1058. const Pose2& rSearchCenter,
  1059. kt_double searchAngleOffset,
  1060. kt_double searchAngleResolution,
  1061. Matrix3& rCovariance);
  1062. /**
  1063. * Gets the correlation grid data (for debugging)
  1064. * @return correlation grid
  1065. */
  1066. inline CorrelationGrid* GetCorrelationGrid() const
  1067. {
  1068. return m_pCorrelationGrid;
  1069. }
  1070. private:
  1071. /**
  1072. * Marks cells where scans' points hit as being occupied
  1073. * @param rScans scans whose points will mark cells in grid as being occupied
  1074. * @param viewPoint do not add points that belong to scans "opposite" the view point
  1075. */
  1076. void AddScans(const LocalizedRangeScanVector& rScans, Vector2<kt_double> viewPoint);
  1077. /**
  1078. * Marks cells where scans' points hit as being occupied. Can smear points as they are added.
  1079. * @param pScan scan whose points will mark cells in grid as being occupied
  1080. * @param viewPoint do not add points that belong to scans "opposite" the view point
  1081. * @param doSmear whether the points will be smeared
  1082. */
  1083. void AddScan(LocalizedRangeScan* pScan, const Vector2<kt_double>& rViewPoint, kt_bool doSmear = true);
  1084. /**
  1085. * Compute which points in a scan are on the same side as the given viewpoint
  1086. * @param pScan
  1087. * @param rViewPoint
  1088. * @return points on the same side
  1089. */
  1090. PointVectorDouble FindValidPoints(LocalizedRangeScan* pScan, const Vector2<kt_double>& rViewPoint) const;
  1091. /**
  1092. * Get response at given position for given rotation (only look up valid points)
  1093. * @param angleIndex
  1094. * @param gridPositionIndex
  1095. * @return response
  1096. */
  1097. kt_double GetResponse(kt_int32u angleIndex, kt_int32s gridPositionIndex) const;
  1098. protected:
  1099. /**
  1100. * Default constructor
  1101. */
  1102. ScanMatcher(Mapper* pMapper)
  1103. : m_pMapper(pMapper)
  1104. , m_pCorrelationGrid(NULL)
  1105. , m_pSearchSpaceProbs(NULL)
  1106. , m_pGridLookup(NULL)
  1107. {
  1108. }
  1109. private:
  1110. Mapper* m_pMapper;
  1111. CorrelationGrid* m_pCorrelationGrid;
  1112. Grid<kt_double>* m_pSearchSpaceProbs;
  1113. GridIndexLookup<kt_int8u>* m_pGridLookup;
  1114. }; // ScanMatcher
  1115. ////////////////////////////////////////////////////////////////////////////////////////
  1116. ////////////////////////////////////////////////////////////////////////////////////////
  1117. ////////////////////////////////////////////////////////////////////////////////////////
  1118. /**
  1119. * Manages the scan data for a device
  1120. */
  1121. class ScanManager
  1122. {
  1123. public:
  1124. /**
  1125. * Default constructor
  1126. */
  1127. ScanManager(kt_int32u runningBufferMaximumSize, kt_double runningBufferMaximumDistance)
  1128. : m_pLastScan(NULL)
  1129. , m_RunningBufferMaximumSize(runningBufferMaximumSize)
  1130. , m_RunningBufferMaximumDistance(runningBufferMaximumDistance)
  1131. {
  1132. }
  1133. /**
  1134. * Destructor
  1135. */
  1136. virtual ~ScanManager()
  1137. {
  1138. Clear();
  1139. }
  1140. public:
  1141. /**
  1142. * Adds scan to vector of processed scans tagging scan with given unique id
  1143. * @param pScan
  1144. */
  1145. inline void AddScan(LocalizedRangeScan* pScan, kt_int32s uniqueId)
  1146. {
  1147. // assign state id to scan
  1148. pScan->SetStateId(static_cast<kt_int32u>(m_Scans.size()));
  1149. // assign unique id to scan
  1150. pScan->SetUniqueId(uniqueId);
  1151. // add it to scan buffer
  1152. m_Scans.push_back(pScan);
  1153. }
  1154. /**
  1155. * Gets last scan
  1156. * @param deviceId
  1157. * @return last localized range scan
  1158. */
  1159. inline LocalizedRangeScan* GetLastScan()
  1160. {
  1161. return m_pLastScan;
  1162. }
  1163. /**
  1164. * Sets the last scan
  1165. * @param pScan
  1166. */
  1167. inline void SetLastScan(LocalizedRangeScan* pScan)
  1168. {
  1169. m_pLastScan = pScan;
  1170. }
  1171. /**
  1172. * Gets scans
  1173. * @return scans
  1174. */
  1175. inline LocalizedRangeScanVector& GetScans()
  1176. {
  1177. return m_Scans;
  1178. }
  1179. /**
  1180. * Gets running scans
  1181. * @return running scans
  1182. */
  1183. inline LocalizedRangeScanVector& GetRunningScans()
  1184. {
  1185. return m_RunningScans;
  1186. }
  1187. /**
  1188. * Adds scan to vector of running scans
  1189. * @param pScan
  1190. */
  1191. void AddRunningScan(LocalizedRangeScan* pScan)
  1192. {
  1193. m_RunningScans.push_back(pScan);
  1194. // vector has at least one element (first line of this function), so this is valid
  1195. Pose2 frontScanPose = m_RunningScans.front()->GetSensorPose();
  1196. Pose2 backScanPose = m_RunningScans.back()->GetSensorPose();
  1197. // cap vector size and remove all scans from front of vector that are too far from end of vector
  1198. kt_double squaredDistance = frontScanPose.GetPosition().SquaredDistance(backScanPose.GetPosition());
  1199. while (m_RunningScans.size() > m_RunningBufferMaximumSize ||
  1200. squaredDistance > math::Square(m_RunningBufferMaximumDistance) - KT_TOLERANCE)
  1201. {
  1202. // remove front of running scans
  1203. m_RunningScans.erase(m_RunningScans.begin());
  1204. // recompute stats of running scans
  1205. frontScanPose = m_RunningScans.front()->GetSensorPose();
  1206. backScanPose = m_RunningScans.back()->GetSensorPose();
  1207. squaredDistance = frontScanPose.GetPosition().SquaredDistance(backScanPose.GetPosition());
  1208. }
  1209. }
  1210. /**
  1211. * Deletes data of this buffered device
  1212. */
  1213. void Clear()
  1214. {
  1215. m_Scans.clear();
  1216. m_RunningScans.clear();
  1217. }
  1218. private:
  1219. LocalizedRangeScanVector m_Scans;
  1220. LocalizedRangeScanVector m_RunningScans;
  1221. LocalizedRangeScan* m_pLastScan;
  1222. kt_int32u m_RunningBufferMaximumSize;
  1223. kt_double m_RunningBufferMaximumDistance;
  1224. }; // ScanManager
  1225. ////////////////////////////////////////////////////////////////////////////////////////
  1226. ////////////////////////////////////////////////////////////////////////////////////////
  1227. ////////////////////////////////////////////////////////////////////////////////////////
  1228. /**
  1229. * Manages the devices for the mapper
  1230. */
  1231. class KARTO_EXPORT MapperSensorManager // : public SensorManager
  1232. {
  1233. typedef std::map<Name, ScanManager*> ScanManagerMap;
  1234. public:
  1235. /**
  1236. * Constructor
  1237. */
  1238. MapperSensorManager(kt_int32u runningBufferMaximumSize, kt_double runningBufferMaximumDistance)
  1239. : m_RunningBufferMaximumSize(runningBufferMaximumSize)
  1240. , m_RunningBufferMaximumDistance(runningBufferMaximumDistance)
  1241. , m_NextScanId(0)
  1242. {
  1243. }
  1244. /**
  1245. * Destructor
  1246. */
  1247. virtual ~MapperSensorManager()
  1248. {
  1249. Clear();
  1250. }
  1251. public:
  1252. /**
  1253. * Registers a sensor (with given name); do
  1254. * nothing if device already registered
  1255. * @param rSensorName
  1256. */
  1257. void RegisterSensor(const Name& rSensorName);
  1258. /**
  1259. * Gets scan from given sensor with given ID
  1260. * @param rSensorName
  1261. * @param scanIndex
  1262. * @return localized range scan
  1263. */
  1264. LocalizedRangeScan* GetScan(const Name& rSensorName, kt_int32s scanIndex);
  1265. /**
  1266. * Gets names of all sensors
  1267. * @return sensor names
  1268. */
  1269. inline std::vector<Name> GetSensorNames()
  1270. {
  1271. std::vector<Name> deviceNames;
  1272. const_forEach(ScanManagerMap, &m_ScanManagers)
  1273. {
  1274. deviceNames.push_back(iter->first);
  1275. }
  1276. return deviceNames;
  1277. }
  1278. /**
  1279. * Gets last scan of given sensor
  1280. * @param rSensorName
  1281. * @return last localized range scan of sensor
  1282. */
  1283. inline LocalizedRangeScan* GetLastScan(const Name& rSensorName)
  1284. {
  1285. RegisterSensor(rSensorName);
  1286. return GetScanManager(rSensorName)->GetLastScan();
  1287. }
  1288. /**
  1289. * Sets the last scan of device of given scan
  1290. * @param pScan
  1291. */
  1292. inline void SetLastScan(LocalizedRangeScan* pScan)
  1293. {
  1294. GetScanManager(pScan)->SetLastScan(pScan);
  1295. }
  1296. /**
  1297. * Gets the scan with the given unique id
  1298. * @param id
  1299. * @return scan
  1300. */
  1301. inline LocalizedRangeScan* GetScan(kt_int32s id)
  1302. {
  1303. assert(math::IsUpTo(id, (kt_int32s)m_Scans.size()));
  1304. return m_Scans[id];
  1305. }
  1306. /**
  1307. * Adds scan to scan vector of device that recorded scan
  1308. * @param pScan
  1309. */
  1310. void AddScan(LocalizedRangeScan* pScan);
  1311. /**
  1312. * Adds scan to running scans of device that recorded scan
  1313. * @param pScan
  1314. */
  1315. inline void AddRunningScan(LocalizedRangeScan* pScan)
  1316. {
  1317. GetScanManager(pScan)->AddRunningScan(pScan);
  1318. }
  1319. /**
  1320. * Gets scans of device
  1321. * @param rSensorName
  1322. * @return scans of device
  1323. */
  1324. inline LocalizedRangeScanVector& GetScans(const Name& rSensorName)
  1325. {
  1326. return GetScanManager(rSensorName)->GetScans();
  1327. }
  1328. /**
  1329. * Gets running scans of device
  1330. * @param rSensorName
  1331. * @return running scans of device
  1332. */
  1333. inline LocalizedRangeScanVector& GetRunningScans(const Name& rSensorName)
  1334. {
  1335. return GetScanManager(rSensorName)->GetRunningScans();
  1336. }
  1337. /**
  1338. * Gets all scans of all devices
  1339. * @return all scans of all devices
  1340. */
  1341. LocalizedRangeScanVector GetAllScans();
  1342. /**
  1343. * Deletes all scan managers of all devices
  1344. */
  1345. void Clear();
  1346. private:
  1347. /**
  1348. * Get scan manager for localized range scan
  1349. * @return ScanManager
  1350. */
  1351. inline ScanManager* GetScanManager(LocalizedRangeScan* pScan)
  1352. {
  1353. return GetScanManager(pScan->GetSensorName());
  1354. }
  1355. /**
  1356. * Get scan manager for id
  1357. * @param rSensorName
  1358. * @return ScanManager
  1359. */
  1360. inline ScanManager* GetScanManager(const Name& rSensorName)
  1361. {
  1362. if (m_ScanManagers.find(rSensorName) != m_ScanManagers.end())
  1363. {
  1364. return m_ScanManagers[rSensorName];
  1365. }
  1366. return NULL;
  1367. }
  1368. private:
  1369. // map from device ID to scan data
  1370. ScanManagerMap m_ScanManagers;
  1371. kt_int32u m_RunningBufferMaximumSize;
  1372. kt_double m_RunningBufferMaximumDistance;
  1373. kt_int32s m_NextScanId;
  1374. std::vector<LocalizedRangeScan*> m_Scans;
  1375. }; // MapperSensorManager
  1376. ////////////////////////////////////////////////////////////////////////////////////////
  1377. ////////////////////////////////////////////////////////////////////////////////////////
  1378. ////////////////////////////////////////////////////////////////////////////////////////
  1379. /**
  1380. * Graph SLAM mapper. Creates a map given a set of LocalizedRangeScans
  1381. * The current Karto implementation is a proprietary, high-performance
  1382. * scan-matching algorithm that constructs a map from individual range scans and corrects for
  1383. * errors in the range and odometry data.
  1384. *
  1385. * The following parameters can be set on the Mapper.
  1386. *
  1387. * \a UseScanMatching (ParameterBool)\n
  1388. * When set to true, the mapper will use a scan matching algorithm. In most real-world situations
  1389. * this should be set to true so that the mapper algorithm can correct for noise and errors in
  1390. * odometry and scan data. In some simulator environments where the simulated scan and odometry
  1391. * data are very accurate, the scan matching algorithm can produce worse results. In those cases
  1392. * set to false to improve results.
  1393. * Default value is true.
  1394. *
  1395. * \a UseScanBarycenter (ParameterBool)\n
  1396. *
  1397. * \a UseResponseExpansion (ParameterBool)\n
  1398. *
  1399. * \a RangeThreshold (ParameterDouble - meters)\n
  1400. * The range threshold is used to truncate range scan distance measurement readings. The threshold should
  1401. * be set such that adjacent range readings in a scan will generally give "solid" coverage of objects.
  1402. *
  1403. * \image html doxygen/RangeThreshold.png
  1404. * \image latex doxygen/RangeThreshold.png "" width=3in
  1405. *
  1406. * Having solid coverage depends on the map resolution and the angular resolution of the range scan device.
  1407. * The following are the recommended threshold values for the corresponding map resolution and range finder
  1408. * resolution values:
  1409. *
  1410. * <table border=0>
  1411. * <tr>
  1412. * <td><b>Map Resolution</b></td>
  1413. * <td colspan=3 align=center><b>Laser Angular Resolution</b></td>
  1414. * </tr>
  1415. * <tr>
  1416. * <td></td>
  1417. * <td align=center><b>1.0 degree</b></td>
  1418. * <td align=center><b>0.5 degree</b></td>
  1419. * <td align=center><b>0.25 degree</b></td>
  1420. * </tr>
  1421. * <tr>
  1422. * <td align=center><b>0.1</b></td>
  1423. * <td align=center>5.7m</td>
  1424. * <td align=center>11.4m</td>
  1425. * <td align=center>22.9m</td>
  1426. * </tr>
  1427. * <tr>
  1428. * <td align=center><b>0.05</b></td>
  1429. * <td align=center>2.8m</td>
  1430. * <td align=center>5.7m</td>
  1431. * <td align=center>11.4m</td>
  1432. * </tr>
  1433. * <tr>
  1434. * <td align=center><b>0.01</b></td>
  1435. * <td align=center>0.5m</td>
  1436. * <td align=center>1.1m</td>
  1437. * <td align=center>2.3m</td>
  1438. * </tr>
  1439. * </table>
  1440. *
  1441. * Note that the value of RangeThreshold should be adjusted taking into account the values of
  1442. * MinimumTravelDistance and MinimumTravelHeading (see also below). By incorporating scans
  1443. * into the map more frequently, the RangeThreshold value can be increased as the additional scans
  1444. * will "fill in" the gaps of objects at greater distances where there is less solid coverage.
  1445. *
  1446. * Default value is 12.0 (meters).
  1447. *
  1448. * \a MinimumTravelDistance (ParameterDouble - meters)\n
  1449. * Sets the minimum travel between scans. If a new scan's position is more than minimumDistance from
  1450. * the previous scan, the mapper will use the data from the new scan. Otherwise, it will discard the
  1451. * new scan if it also does not meet the minimum change in heading requirement.
  1452. * For performance reasons, generally it is a good idea to only process scans if the robot
  1453. * has moved a reasonable amount.
  1454. * Default value is 0.3 (meters).
  1455. *
  1456. * \a MinimumTravelHeading (ParameterDouble - radians)\n
  1457. * Sets the minimum heading change between scans. If a new scan's heading is more than minimumHeading from
  1458. * the previous scan, the mapper will use the data from the new scan. Otherwise, it will discard the
  1459. * new scan if it also does not meet the minimum travel distance requirement.
  1460. * For performance reasons, generally it is a good idea to only process scans if the robot
  1461. * has moved a reasonable amount.
  1462. * Default value is 0.08726646259971647 (radians) - 5 degrees.
  1463. *
  1464. * \a ScanBufferSize (ParameterIn32u - size)\n
  1465. * Scan buffer size is the length of the scan chain stored for scan matching.
  1466. * "ScanBufferSize" should be set to approximately "ScanBufferMaximumScanDistance" / "MinimumTravelDistance".
  1467. * The idea is to get an area approximately 20 meters long for scan matching.
  1468. * For example, if we add scans every MinimumTravelDistance = 0.3 meters, then "ScanBufferSize"
  1469. * should be 20 / 0.3 = 67.)
  1470. * Default value is 67.
  1471. *
  1472. * \a ScanBufferMaximumScanDistance (ParameterDouble - meters)\n
  1473. * Scan buffer maximum scan distance is the maximum distance between the first and last scans
  1474. * in the scan chain stored for matching.
  1475. * Default value is 20.0.
  1476. *
  1477. * \a CorrelationSearchSpaceDimension (ParameterDouble - meters)\n
  1478. * The size of the correlation grid used by the matcher.
  1479. * Default value is 0.3 meters which tells the matcher to use a 30cm x 30cm grid.
  1480. *
  1481. * \a CorrelationSearchSpaceResolution (ParameterDouble - meters)\n
  1482. * The resolution (size of a grid cell) of the correlation grid.
  1483. * Default value is 0.01 meters.
  1484. *
  1485. * \a CorrelationSearchSpaceSmearDeviation (ParameterDouble - meters)\n
  1486. * The robot position is smeared by this value in X and Y to create a smoother response.
  1487. * Default value is 0.03 meters.
  1488. *
  1489. * \a LinkMatchMinimumResponseFine (ParameterDouble - probability (>= 0.0, <= 1.0))\n
  1490. * Scans are linked only if the correlation response value is greater than this value.
  1491. * Default value is 0.4
  1492. *
  1493. * \a LinkScanMaximumDistance (ParameterDouble - meters)\n
  1494. * Maximum distance between linked scans. Scans that are farther apart will not be linked
  1495. * regardless of the correlation response value.
  1496. * Default value is 6.0 meters.
  1497. *
  1498. * \a LoopSearchSpaceDimension (ParameterDouble - meters)\n
  1499. * Dimension of the correlation grid used by the loop closure detection algorithm
  1500. * Default value is 4.0 meters.
  1501. *
  1502. * \a LoopSearchSpaceResolution (ParameterDouble - meters)\n
  1503. * Coarse resolution of the correlation grid used by the matcher to determine a possible
  1504. * loop closure.
  1505. * Default value is 0.05 meters.
  1506. *
  1507. * \a LoopSearchSpaceSmearDeviation (ParameterDouble - meters)\n
  1508. * Smearing distance in the correlation grid used by the matcher to determine a possible
  1509. * loop closure match.
  1510. * Default value is 0.03 meters.
  1511. *
  1512. * \a LoopSearchMaximumDistance (ParameterDouble - meters)\n
  1513. * Scans less than this distance from the current position will be considered for a match
  1514. * in loop closure.
  1515. * Default value is 4.0 meters.
  1516. *
  1517. * \a LoopMatchMinimumChainSize (ParameterIn32s)\n
  1518. * When the loop closure detection finds a candidate it must be part of a large
  1519. * set of linked scans. If the chain of scans is less than this value we do not attempt
  1520. * to close the loop.
  1521. * Default value is 10.
  1522. *
  1523. * \a LoopMatchMaximumVarianceCoarse (ParameterDouble)\n
  1524. * The co-variance values for a possible loop closure have to be less than this value
  1525. * to consider a viable solution. This applies to the coarse search.
  1526. * Default value is 0.16.
  1527. *
  1528. * \a LoopMatchMinimumResponseCoarse (ParameterDouble - probability (>= 0.0, <= 1.0))\n
  1529. * If response is larger then this then initiate loop closure search at the coarse resolution.
  1530. * Default value is 0.7.
  1531. *
  1532. * \a LoopMatchMinimumResponseFine (ParameterDouble - probability (>= 0.0, <= 1.0))\n
  1533. * If response is larger then this then initiate loop closure search at the fine resolution.
  1534. * Default value is 0.5.
  1535. */
  1536. class KARTO_EXPORT Mapper : public Module
  1537. {
  1538. friend class MapperGraph;
  1539. friend class ScanMatcher;
  1540. public:
  1541. /**
  1542. * Default constructor
  1543. */
  1544. Mapper();
  1545. /**
  1546. * Constructor a mapper with a name
  1547. * @param rName mapper name
  1548. */
  1549. Mapper(const std::string& rName);
  1550. /**
  1551. * Destructor
  1552. */
  1553. virtual ~Mapper();
  1554. public:
  1555. /**
  1556. * Allocate memory needed for mapping
  1557. * @param rangeThreshold
  1558. */
  1559. void Initialize(kt_double rangeThreshold);
  1560. /**
  1561. * Resets the mapper.
  1562. * Deallocate memory allocated in Initialize()
  1563. */
  1564. void Reset();
  1565. /**
  1566. * Process a localized range scan for incorporation into the map. The scan must
  1567. * be identified with a range finder device. Once added to a map, the corrected pose information in the
  1568. * localized scan will be updated to the correct pose as determined by the mapper.
  1569. *
  1570. * @param pScan A localized range scan that has pose information associated directly with the scan data. The pose
  1571. * is that of the range device originating the scan. Note that the mapper will set corrected pose
  1572. * information in the scan object.
  1573. *
  1574. * @return true if the scan was added successfully, false otherwise
  1575. */
  1576. virtual kt_bool Process(LocalizedRangeScan* pScan);
  1577. /**
  1578. * Process an Object
  1579. */
  1580. virtual kt_bool Process(Object* pObject);
  1581. /**
  1582. * Returns all processed scans added to the mapper.
  1583. * NOTE: The returned scans have their corrected pose updated.
  1584. * @return list of scans received and processed by the mapper. If no scans have been processed,
  1585. * return an empty list.
  1586. */
  1587. virtual const LocalizedRangeScanVector GetAllProcessedScans() const;
  1588. /**
  1589. * Add a listener to mapper
  1590. * @param pListener
  1591. */
  1592. void AddListener(MapperListener* pListener);
  1593. /**
  1594. * Remove a listener to mapper
  1595. * @param pListener
  1596. */
  1597. void RemoveListener(MapperListener* pListener);
  1598. /**
  1599. * Set scan optimizer used by mapper when closing the loop
  1600. * @param pSolver
  1601. */
  1602. void SetScanSolver(ScanSolver* pSolver);
  1603. /**
  1604. * Get scan link graph
  1605. * @return graph
  1606. */
  1607. virtual MapperGraph* GetGraph() const;
  1608. /**
  1609. * Gets the sequential scan matcher
  1610. * @return sequential scan matcher
  1611. */
  1612. virtual ScanMatcher* GetSequentialScanMatcher() const;
  1613. /**
  1614. * Gets the loop scan matcher
  1615. * @return loop scan matcher
  1616. */
  1617. virtual ScanMatcher* GetLoopScanMatcher() const;
  1618. /**
  1619. * Gets the device manager
  1620. * @return device manager
  1621. */
  1622. inline MapperSensorManager* GetMapperSensorManager() const
  1623. {
  1624. return m_pMapperSensorManager;
  1625. }
  1626. /**
  1627. * Tries to close a loop using the given scan with the scans from the given sensor
  1628. * @param pScan
  1629. * @param rSensorName
  1630. */
  1631. inline kt_bool TryCloseLoop(LocalizedRangeScan* pScan, const Name& rSensorName)
  1632. {
  1633. return m_pGraph->TryCloseLoop(pScan, rSensorName);
  1634. }
  1635. private:
  1636. void InitializeParameters();
  1637. /**
  1638. * Test if the scan is "sufficiently far" from the last scan added.
  1639. * @param pScan scan to be checked
  1640. * @param pLastScan last scan added to mapper
  1641. * @return true if the scan is "sufficiently far" from the last scan added or
  1642. * the scan is the first scan to be added
  1643. */
  1644. kt_bool HasMovedEnough(LocalizedRangeScan* pScan, LocalizedRangeScan* pLastScan) const;
  1645. public:
  1646. /////////////////////////////////////////////
  1647. // fire information for listeners!!
  1648. /**
  1649. * Fire a general message to listeners
  1650. * @param rInfo
  1651. */
  1652. void FireInfo(const std::string& rInfo) const;
  1653. /**
  1654. * Fire a debug message to listeners
  1655. * @param rInfo
  1656. */
  1657. void FireDebug(const std::string& rInfo) const;
  1658. /**
  1659. * Fire a message upon checking for loop closure to listeners
  1660. * @param rInfo
  1661. */
  1662. void FireLoopClosureCheck(const std::string& rInfo) const;
  1663. /**
  1664. * Fire a message before loop closure to listeners
  1665. * @param rInfo
  1666. */
  1667. void FireBeginLoopClosure(const std::string& rInfo) const;
  1668. /**
  1669. * Fire a message after loop closure to listeners
  1670. * @param rInfo
  1671. */
  1672. void FireEndLoopClosure(const std::string& rInfo) const;
  1673. // FireRunningScansUpdated
  1674. // FireCovarianceCalculated
  1675. // FireLoopClosureChain
  1676. private:
  1677. /**
  1678. * Restrict the copy constructor
  1679. */
  1680. Mapper(const Mapper&);
  1681. /**
  1682. * Restrict the assignment operator
  1683. */
  1684. const Mapper& operator=(const Mapper&);
  1685. public:
  1686. void SetUseScanMatching(kt_bool val) { m_pUseScanMatching->SetValue(val); }
  1687. private:
  1688. kt_bool m_Initialized;
  1689. ScanMatcher* m_pSequentialScanMatcher;
  1690. MapperSensorManager* m_pMapperSensorManager;
  1691. MapperGraph* m_pGraph;
  1692. ScanSolver* m_pScanOptimizer;
  1693. std::vector<MapperListener*> m_Listeners;
  1694. /**
  1695. * When set to true, the mapper will use a scan matching algorithm. In most real-world situations
  1696. * this should be set to true so that the mapper algorithm can correct for noise and errors in
  1697. * odometry and scan data. In some simulator environments where the simulated scan and odometry
  1698. * data are very accurate, the scan matching algorithm can produce worse results. In those cases
  1699. * set this to false to improve results.
  1700. * Default value is true.
  1701. */
  1702. Parameter<kt_bool>* m_pUseScanMatching;
  1703. /**
  1704. * Default value is true.
  1705. */
  1706. Parameter<kt_bool>* m_pUseScanBarycenter;
  1707. /**
  1708. * Sets the minimum time between scans. If a new scan's time stamp is
  1709. * longer than MinimumTimeInterval from the previously processed scan,
  1710. * the mapper will use the data from the new scan. Otherwise, it will
  1711. * discard the new scan if it also does not meet the minimum travel
  1712. * distance and heading requirements. For performance reasons, it is
  1713. * generally it is a good idea to only process scans if a reasonable
  1714. * amount of time has passed. This parameter is particularly useful
  1715. * when there is a need to process scans while the robot is stationary.
  1716. * Default value is 3600 (seconds), effectively disabling this parameter.
  1717. */
  1718. Parameter<kt_double>* m_pMinimumTimeInterval;
  1719. /**
  1720. * Sets the minimum travel between scans. If a new scan's position is more than minimumTravelDistance
  1721. * from the previous scan, the mapper will use the data from the new scan. Otherwise, it will discard the
  1722. * new scan if it also does not meet the minimum change in heading requirement.
  1723. * For performance reasons, generally it is a good idea to only process scans if the robot
  1724. * has moved a reasonable amount.
  1725. * Default value is 0.2 (meters).
  1726. */
  1727. Parameter<kt_double>* m_pMinimumTravelDistance;
  1728. /**
  1729. * Sets the minimum heading change between scans. If a new scan's heading is more than minimumTravelHeading
  1730. * from the previous scan, the mapper will use the data from the new scan. Otherwise, it will discard the
  1731. * new scan if it also does not meet the minimum travel distance requirement.
  1732. * For performance reasons, generally it is a good idea to only process scans if the robot
  1733. * has moved a reasonable amount.
  1734. * Default value is 10 degrees.
  1735. */
  1736. Parameter<kt_double>* m_pMinimumTravelHeading;
  1737. /**
  1738. * Scan buffer size is the length of the scan chain stored for scan matching.
  1739. * "scanBufferSize" should be set to approximately "scanBufferMaximumScanDistance" / "minimumTravelDistance".
  1740. * The idea is to get an area approximately 20 meters long for scan matching.
  1741. * For example, if we add scans every minimumTravelDistance == 0.3 meters, then "scanBufferSize"
  1742. * should be 20 / 0.3 = 67.)
  1743. * Default value is 67.
  1744. */
  1745. Parameter<kt_int32u>* m_pScanBufferSize;
  1746. /**
  1747. * Scan buffer maximum scan distance is the maximum distance between the first and last scans
  1748. * in the scan chain stored for matching.
  1749. * Default value is 20.0.
  1750. */
  1751. Parameter<kt_double>* m_pScanBufferMaximumScanDistance;
  1752. /**
  1753. * Scans are linked only if the correlation response value is greater than this value.
  1754. * Default value is 0.4
  1755. */
  1756. Parameter<kt_double>* m_pLinkMatchMinimumResponseFine;
  1757. /**
  1758. * Maximum distance between linked scans. Scans that are farther apart will not be linked
  1759. * regardless of the correlation response value.
  1760. * Default value is 6.0 meters.
  1761. */
  1762. Parameter<kt_double>* m_pLinkScanMaximumDistance;
  1763. /**
  1764. * Enable/disable loop closure.
  1765. * Default is enabled.
  1766. */
  1767. Parameter<kt_bool>* m_pDoLoopClosing;
  1768. /**
  1769. * Scans less than this distance from the current position will be considered for a match
  1770. * in loop closure.
  1771. * Default value is 4.0 meters.
  1772. */
  1773. Parameter<kt_double>* m_pLoopSearchMaximumDistance;
  1774. /**
  1775. * When the loop closure detection finds a candidate it must be part of a large
  1776. * set of linked scans. If the chain of scans is less than this value we do not attempt
  1777. * to close the loop.
  1778. * Default value is 10.
  1779. */
  1780. Parameter<kt_int32u>* m_pLoopMatchMinimumChainSize;
  1781. /**
  1782. * The co-variance values for a possible loop closure have to be less than this value
  1783. * to consider a viable solution. This applies to the coarse search.
  1784. * Default value is 0.16.
  1785. */
  1786. Parameter<kt_double>* m_pLoopMatchMaximumVarianceCoarse;
  1787. /**
  1788. * If response is larger then this, then initiate loop closure search at the coarse resolution.
  1789. * Default value is 0.7.
  1790. */
  1791. Parameter<kt_double>* m_pLoopMatchMinimumResponseCoarse;
  1792. /**
  1793. * If response is larger then this, then initiate loop closure search at the fine resolution.
  1794. * Default value is 0.7.
  1795. */
  1796. Parameter<kt_double>* m_pLoopMatchMinimumResponseFine;
  1797. //////////////////////////////////////////////////////////////////////////////
  1798. // CorrelationParameters correlationParameters;
  1799. /**
  1800. * The size of the search grid used by the matcher.
  1801. * Default value is 0.3 meters which tells the matcher to use a 30cm x 30cm grid.
  1802. */
  1803. Parameter<kt_double>* m_pCorrelationSearchSpaceDimension;
  1804. /**
  1805. * The resolution (size of a grid cell) of the correlation grid.
  1806. * Default value is 0.01 meters.
  1807. */
  1808. Parameter<kt_double>* m_pCorrelationSearchSpaceResolution;
  1809. /**
  1810. * The point readings are smeared by this value in X and Y to create a smoother response.
  1811. * Default value is 0.03 meters.
  1812. */
  1813. Parameter<kt_double>* m_pCorrelationSearchSpaceSmearDeviation;
  1814. //////////////////////////////////////////////////////////////////////////////
  1815. // CorrelationParameters loopCorrelationParameters;
  1816. /**
  1817. * The size of the search grid used by the matcher.
  1818. * Default value is 0.3 meters which tells the matcher to use a 30cm x 30cm grid.
  1819. */
  1820. Parameter<kt_double>* m_pLoopSearchSpaceDimension;
  1821. /**
  1822. * The resolution (size of a grid cell) of the correlation grid.
  1823. * Default value is 0.01 meters.
  1824. */
  1825. Parameter<kt_double>* m_pLoopSearchSpaceResolution;
  1826. /**
  1827. * The point readings are smeared by this value in X and Y to create a smoother response.
  1828. * Default value is 0.03 meters.
  1829. */
  1830. Parameter<kt_double>* m_pLoopSearchSpaceSmearDeviation;
  1831. //////////////////////////////////////////////////////////////////////////////
  1832. // ScanMatcherParameters;
  1833. // Variance of penalty for deviating from odometry when scan-matching.
  1834. // The penalty is a multiplier (less than 1.0) is a function of the
  1835. // delta of the scan position being tested and the odometric pose
  1836. Parameter<kt_double>* m_pDistanceVariancePenalty;
  1837. Parameter<kt_double>* m_pAngleVariancePenalty;
  1838. // The range of angles to search during a coarse search and a finer search
  1839. Parameter<kt_double>* m_pFineSearchAngleOffset;
  1840. Parameter<kt_double>* m_pCoarseSearchAngleOffset;
  1841. // Resolution of angles to search during a coarse search
  1842. Parameter<kt_double>* m_pCoarseAngleResolution;
  1843. // Minimum value of the penalty multiplier so scores do not
  1844. // become too small
  1845. Parameter<kt_double>* m_pMinimumAnglePenalty;
  1846. Parameter<kt_double>* m_pMinimumDistancePenalty;
  1847. // whether to increase the search space if no good matches are initially found
  1848. Parameter<kt_bool>* m_pUseResponseExpansion;
  1849. public:
  1850. /* Abstract methods for parameter setters and getters */
  1851. /* Getters */
  1852. // General Parameters
  1853. bool getParamUseScanMatching();
  1854. bool getParamUseScanBarycenter();
  1855. double getParamMinimumTimeInterval();
  1856. double getParamMinimumTravelDistance();
  1857. double getParamMinimumTravelHeading();
  1858. int getParamScanBufferSize();
  1859. double getParamScanBufferMaximumScanDistance();
  1860. double getParamLinkMatchMinimumResponseFine();
  1861. double getParamLinkScanMaximumDistance();
  1862. double getParamLoopSearchMaximumDistance();
  1863. bool getParamDoLoopClosing();
  1864. int getParamLoopMatchMinimumChainSize();
  1865. double getParamLoopMatchMaximumVarianceCoarse();
  1866. double getParamLoopMatchMinimumResponseCoarse();
  1867. double getParamLoopMatchMinimumResponseFine();
  1868. // Correlation Parameters - Correlation Parameters
  1869. double getParamCorrelationSearchSpaceDimension();
  1870. double getParamCorrelationSearchSpaceResolution();
  1871. double getParamCorrelationSearchSpaceSmearDeviation();
  1872. // Correlation Parameters - Loop Closure Parameters
  1873. double getParamLoopSearchSpaceDimension();
  1874. double getParamLoopSearchSpaceResolution();
  1875. double getParamLoopSearchSpaceSmearDeviation();
  1876. // Scan Matcher Parameters
  1877. double getParamDistanceVariancePenalty();
  1878. double getParamAngleVariancePenalty();
  1879. double getParamFineSearchAngleOffset();
  1880. double getParamCoarseSearchAngleOffset();
  1881. double getParamCoarseAngleResolution();
  1882. double getParamMinimumAnglePenalty();
  1883. double getParamMinimumDistancePenalty();
  1884. bool getParamUseResponseExpansion();
  1885. /* Setters */
  1886. // General Parameters
  1887. void setParamUseScanMatching(bool b);
  1888. void setParamUseScanBarycenter(bool b);
  1889. void setParamMinimumTimeInterval(double d);
  1890. void setParamMinimumTravelDistance(double d);
  1891. void setParamMinimumTravelHeading(double d);
  1892. void setParamScanBufferSize(int i);
  1893. void setParamScanBufferMaximumScanDistance(double d);
  1894. void setParamLinkMatchMinimumResponseFine(double d);
  1895. void setParamLinkScanMaximumDistance(double d);
  1896. void setParamLoopSearchMaximumDistance(double d);
  1897. void setParamDoLoopClosing(bool b);
  1898. void setParamLoopMatchMinimumChainSize(int i);
  1899. void setParamLoopMatchMaximumVarianceCoarse(double d);
  1900. void setParamLoopMatchMinimumResponseCoarse(double d);
  1901. void setParamLoopMatchMinimumResponseFine(double d);
  1902. // Correlation Parameters - Correlation Parameters
  1903. void setParamCorrelationSearchSpaceDimension(double d);
  1904. void setParamCorrelationSearchSpaceResolution(double d);
  1905. void setParamCorrelationSearchSpaceSmearDeviation(double d);
  1906. // Correlation Parameters - Loop Closure Parameters
  1907. void setParamLoopSearchSpaceDimension(double d);
  1908. void setParamLoopSearchSpaceResolution(double d);
  1909. void setParamLoopSearchSpaceSmearDeviation(double d);
  1910. // Scan Matcher Parameters
  1911. void setParamDistanceVariancePenalty(double d);
  1912. void setParamAngleVariancePenalty(double d);
  1913. void setParamFineSearchAngleOffset(double d);
  1914. void setParamCoarseSearchAngleOffset(double d);
  1915. void setParamCoarseAngleResolution(double d);
  1916. void setParamMinimumAnglePenalty(double d);
  1917. void setParamMinimumDistancePenalty(double d);
  1918. void setParamUseResponseExpansion(bool b);
  1919. };
  1920. } // namespace karto
  1921. #endif // OPEN_KARTO_MAPPER_H