Main MRPT website > C++ reference for MRPT 1.9.9
CHMTSLAM_TBI.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "hmtslam-precomp.h" // Precomp header
11 
12 #include <mrpt/utils/CTicTac.h>
13 #include <mrpt/random.h>
14 #include <mrpt/utils/CFileStream.h>
15 #include <mrpt/system/os.h>
16 
17 using namespace mrpt::slam;
18 using namespace mrpt::hmtslam;
19 using namespace mrpt::utils;
20 using namespace mrpt::random;
21 using namespace mrpt::poses;
22 using namespace mrpt::system;
23 using namespace std;
24 
25 /*---------------------------------------------------------------
26 
27  CHMTSLAM_TBI
28 
29  Topological Bayesian Inference (TBI) process within HMT-SLAM
30 
31  ---------------------------------------------------------------*/
32 void CHMTSLAM::thread_TBI()
33 {
34  CHMTSLAM* obj = this;
35  CTicTac tictac;
36 
37  // Seems that must be called in each thread??
38  if (obj->m_options.random_seed)
39  getRandomGenerator().randomize(obj->m_options.random_seed);
40  else
42 
43  try
44  {
45  // Start thread:
46  // -------------------------
47  obj->logFmt(
48  mrpt::utils::LVL_DEBUG,
49  "[thread_TBI] Thread started (ID=0x%08lX)\n",
50  std::this_thread::get_id());
51 
52  // --------------------------------------------
53  // The main loop
54  // Executes until termination is signaled
55  // --------------------------------------------
56  while (!obj->m_terminateThreads)
57  {
58  std::this_thread::sleep_for(100ms);
59  }; // end while execute thread
60 
61  // Finish thread:
62  // -------------------------
63  time_t timCreat, timExit;
64  double timCPU = 0;
65  MRPT_TODO("Fix thread times");
66  // try { mrpt::system::getCurrentThreadTimes(
67  // timCreat,timExit,timCPU); } catch(...) {};
68  obj->logFmt(
69  mrpt::utils::LVL_DEBUG,
70  "[thread_TBI] Thread finished. CPU time used:%.06f secs \n",
71  timCPU);
72  obj->m_terminationFlag_TBI = true;
73  }
74  catch (std::exception& e)
75  {
76  obj->m_terminationFlag_TBI = true;
77 
78  // Release semaphores:
79 
80  obj->logFmt(mrpt::utils::LVL_ERROR, "%s", e.what());
81 
82  // DEBUG: Terminate application:
83  obj->m_terminateThreads = true;
84  }
85  catch (...)
86  {
87  obj->m_terminationFlag_TBI = true;
88 
89  obj->logFmt(
90  mrpt::utils::LVL_ERROR,
91  "\n---------------------- EXCEPTION CAUGHT! ---------------------\n"
92  " In CHierarchicalMappingFramework::thread_TBI. Unexpected runtime "
93  "error!!\n");
94 
95  // Release semaphores:
96 
97  // DEBUG: Terminate application:
98  obj->m_terminateThreads = true;
99  }
100 }
101 
102 /*---------------------------------------------------------------
103 
104  TBI_main_method
105 
106  Topological Bayesian Inference (TBI) process within HMT-SLAM
107 
108  ---------------------------------------------------------------*/
109 CHMTSLAM::TMessageLSLAMfromTBI::Ptr CHMTSLAM::TBI_main_method(
110  CLocalMetricHypothesis* LMH, const CHMHMapNode::TNodeID& areaID)
111 {
112  MRPT_START
113 
114  CHMTSLAM* obj = (CHMTSLAM*)LMH->m_parent.get();
115 
116  const THypothesisID LMH_ID = LMH->m_ID;
117 
118  // Lock the map:
119  std::lock_guard<std::mutex>(obj->m_map_cs);
120 
123 
124  // Fill out easy data:
125  msg->hypothesisID = LMH_ID;
126  msg->cur_area = areaID;
127 
128  // get a pointer to the current area:
129  const CHMHMapNode::Ptr currentArea = obj->m_map.getNodeByID(areaID);
130  ASSERT_(currentArea);
131 
132  obj->logFmt(
133  mrpt::utils::LVL_DEBUG, "[TBI] Request for area id=%i\n", (int)areaID);
134 
135  // --------------------------------------------------------
136  // 1) Use bounding-boxes to get a first list of candidates
137  // The candidates are saved in "msg->loopClosureData"
138  // -------------------------------------------------------
139  // But first: if the areas are within the LMH, then we have to update the
140  // maps in the HMAP!
141  if (LMH->m_neighbors.find(areaID) != LMH->m_neighbors.end())
142  {
143  // Update:
144  LMH->updateAreaFromLMH(areaID);
145  }
146 
147  for (CHierarchicalMapMHPartition::iterator a = obj->m_map.begin();
148  a != obj->m_map.end(); ++a)
149  {
150  // Only for other areas!
151  if (a->first == areaID) continue;
152 
153  // Test hypothesis: LMH_ID
154  if (a->second->m_hypotheses.has(LMH_ID))
155  {
156  // Not neighbors:
157  if (a->second->isNeighbor(areaID, LMH_ID)) continue;
158 
159  // OK, check:
160  // But first: if the areas are within the LMH, then we have to
161  // update the maps in the HMAP!
162  if (LMH->m_neighbors.find(a->first) != LMH->m_neighbors.end())
163  {
164  // Update:
165  LMH->updateAreaFromLMH(a->first);
166  }
167 
168  // Compute it:
169  double match = obj->m_map.computeOverlapProbabilityBetweenNodes(
170  areaID, // From
171  a->first, // To
172  LMH_ID);
173 
174  obj->logFmt(
175  mrpt::utils::LVL_DEBUG, "[TBI] %i-%i -> overlap prob=%f\n",
176  (int)areaID, (int)a->first, match);
177 
178  if (match > 0.9)
179  {
180  // Initialize the new entry in "msg->loopClosureData" for the
181  // areas:
182  // "areaID" <-> "a->first"
184  msg->loopClosureData[a->first];
185 
186  tbi_info.log_lik = 0;
187  tbi_info.delta_new_cur.clear();
188  }
189  }
190  } // end for each node in the graph.
191 
192  // ----------------------------------------------------
193  // 2) Use the TBI engines
194  // ----------------------------------------------------
195  std::set<CHMHMapNode::TNodeID> lstNodesToErase;
196  {
197  std::lock_guard<std::mutex> lock(obj->m_topLCdets_cs);
198 
200  obj->m_topLCdets.begin();
201  it != obj->m_topLCdets.end(); ++it)
202  {
203  for (map<CHMHMapNode::TNodeID,
205  msg->loopClosureData.begin();
206  candidate != msg->loopClosureData.end(); ++candidate)
207  {
208  // If the current log_lik of this area is reaaaally low, we
209  // could skip the computation with other LC detectors...
210  // ----------------------------------------------------------------------------------------------------------------
211  // TODO: ...
212 
213  // Proceed:
214  // ----------------------------------------------------------------------------------------------------------------
215  const CHMHMapNode::Ptr refArea =
216  obj->m_map.getNodeByID(candidate->first);
217  double this_log_lik;
218 
219  // get the output from this LC detector:
220  CPose3DPDF::Ptr pdf = (*it)->computeTopologicalObservationModel(
221  LMH->m_ID, currentArea, refArea, this_log_lik);
222 
223  // Add to the output:
224  candidate->second.log_lik += this_log_lik;
225 
226  // This is because not all LC detector MUST return a pose PDF
227  // (i.e. image-based detectors)
228  if (pdf)
229  {
231  CPose3DPDFSOG::Ptr SOG =
232  std::dynamic_pointer_cast<CPose3DPDFSOG>(pdf);
233 
234  // Mix (append) the modes, if any:
235  if (SOG->size() > 0)
236  candidate->second.delta_new_cur.appendFrom(*SOG);
237  else
238  lstNodesToErase.insert(candidate->first);
239  }
240  } // end for each candidate area
241  } // end for each LC detector
242 
243  } // end of m_topLCdets_cs lock
244 
245  // Delete candidates which had no PDF when they should.
246  for (set<CHMHMapNode::TNodeID>::const_iterator it = lstNodesToErase.begin();
247  it != lstNodesToErase.end(); ++it)
248  msg->loopClosureData.erase(*it);
249 
250  obj->logFmt(
251  mrpt::utils::LVL_DEBUG, "[TBI_main] Done. %u candidates found.\n",
252  (unsigned int)msg->loopClosureData.size());
253 
254  return msg;
255  MRPT_END
256 }
A namespace of pseudo-random numbers genrators of diferent distributions.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
THypothesisID m_ID
The unique ID of the hypothesis (Used for accessing mrpt::slam::CHierarchicalMHMap).
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
void appendFrom(const CPose3DPDFSOG &o)
Append the Gaussian modes from "o" to the current set of modes of "this" density. ...
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Scalar * iterator
Definition: eigen_plugins.h:26
Declares a class that represents a Probability Density function (PDF) of a 3D(6D) pose ...
Definition: CPose3DPDFSOG.h:35
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::poses::CPose3DPDFSOG delta_new_cur
Depending on the loop-closure engine, an guess of the relative pose of "area_new" relative to "cur_ar...
Definition: CHMTSLAM.h:145
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
An implementation of Hybrid Metric Topological SLAM (HMT-SLAM).
Definition: CHMTSLAM.h:65
#define MRPT_TODO(x)
int64_t THypothesisID
An integer number uniquely identifying each of the concurrent hypotheses for the robot topological pa...
#define MRPT_END
std::shared_ptr< TMessageLSLAMfromTBI > Ptr
Definition: CHMTSLAM.h:127
This class implements a high-performance stopwatch.
Definition: CTicTac.h:23
mrpt::utils::safe_ptr< CHMTSLAM > m_parent
For quick access to our parent object.
std::shared_ptr< CPose3DPDF > Ptr
Definition: CPose3DPDF.h:45
This class is used in HMT-SLAM to represent each of the Local Metric Hypotheses (LMHs).
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
#define MRPT_START
double log_lik
Log likelihood for this loop-closure.
Definition: CHMTSLAM.h:138
void clear()
Clear all the gaussian modes.
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:103
#define ASSERT_(f)
void updateAreaFromLMH(const CHMHMapNode::TNodeID areaID, bool eraseSFsFromLMH=false)
The corresponding node in the HMT map is updated with the robot poses & SFs in the LMH: the poses are ref...
std::shared_ptr< CHMHMapNode > Ptr
Definition: CHMHMapNode.h:42
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
TNodeIDSet m_neighbors
The list of all areas sourronding the current one (this includes the current area itself)...
mrpt::utils::TNodeID TNodeID
The type of the IDs of nodes.
Definition: CHMHMapNode.h:47
std::shared_ptr< CPose3DPDFSOG > Ptr
Definition: CPose3DPDFSOG.h:37



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019