MRPT  1.9.9
CHolonomicFullEval.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "nav-precomp.h" // Precomp header
11 
12 #include <mrpt/core/round.h>
13 #include <mrpt/math/geometry.h>
18 #include <Eigen/Dense> // col(),...
19 #include <cmath>
20 
21 using namespace mrpt;
22 using namespace mrpt::math;
23 using namespace mrpt::nav;
24 using namespace std;
25 
30 
31 const unsigned int INVALID_K = std::numeric_limits<unsigned int>::max();
32 
34  const mrpt::config::CConfigFileBase* INI_FILE)
36  m_last_selected_sector(std::numeric_limits<unsigned int>::max())
37 {
38  if (INI_FILE != nullptr) initialize(*INI_FILE);
39 }
40 
41 void CHolonomicFullEval::saveConfigFile(mrpt::config::CConfigFileBase& c) const
42 {
43  options.saveToConfigFile(c, getConfigFileSectionName());
44 }
45 
46 void CHolonomicFullEval::initialize(const mrpt::config::CConfigFileBase& c)
47 {
48  options.loadFromConfigFile(c, getConfigFileSectionName());
49 }
50 
51 struct TGap
52 {
53  int k_from{-1}, k_to{-1};
54  double min_eval, max_eval;
55  bool contains_target_k{false};
56  /** Direction with the best evaluation inside the gap. */
57  int k_best_eval{-1};
58 
59  TGap()
60  : min_eval(std::numeric_limits<double>::max()),
61  max_eval(-std::numeric_limits<double>::max())
62 
63  {
64  }
65 };
66 
67 CHolonomicFullEval::EvalOutput::EvalOutput() : best_k(INVALID_K) {}
69  unsigned int target_idx, const NavInput& ni, EvalOutput& eo)
70 {
71  ASSERT_(target_idx < ni.targets.size());
72  const auto target = ni.targets[target_idx];
73 
74  using mrpt::square;
75 
76  eo = EvalOutput();
77 
78  const auto ptg = getAssociatedPTG();
79  const double ptg_ref_dist = ptg ? ptg->getRefDistance() : 1.0;
80  const size_t nDirs = ni.obstacles.size();
81 
82  const double target_dir = ::atan2(target.y, target.x);
83  const unsigned int target_k =
85  const double target_dist = target.norm();
86 
87  m_dirs_scores.resize(nDirs, options.factorWeights.size() + 2);
88 
89  // TP-Obstacles in 2D:
90  std::vector<mrpt::math::TPoint2D> obstacles_2d(nDirs);
91 
93  sp.aperture = 2.0 * M_PI;
94  sp.nRays = nDirs;
95  sp.rightToLeft = true;
96  const auto& sc_lut = m_sincos_lut.getSinCosForScan(sp);
97 
98  for (unsigned int i = 0; i < nDirs; i++)
99  {
100  obstacles_2d[i].x = ni.obstacles[i] * sc_lut.ccos[i];
101  obstacles_2d[i].y = ni.obstacles[i] * sc_lut.csin[i];
102  }
103 
104  const int NUM_FACTORS = 5;
105 
106  ASSERT_(options.factorWeights.size() == NUM_FACTORS);
107 
108  for (unsigned int i = 0; i < nDirs; i++)
109  {
110  double scores[NUM_FACTORS]; // scores for each criterion
111 
112  if (ni.obstacles[i] < options.TOO_CLOSE_OBSTACLE &&
113  !(i == target_k &&
114  ni.obstacles[i] > 1.02 * target_dist)) // Too close to obstacles?
115  // (unless target is in
116  // between obstacles and
117  // the robot)
118  {
119  for (int l = 0; l < NUM_FACTORS; l++) m_dirs_scores(i, l) = .0;
120  continue;
121  }
122 
123  const double d = std::min(ni.obstacles[i], 0.95 * target_dist);
124 
125  // The TP-Space representative coordinates for this direction:
126  const double x = d * sc_lut.ccos[i];
127  const double y = d * sc_lut.csin[i];
128 
129  // Factor #1: collision-free distance
130  // -----------------------------------------------------
131  if (mrpt::abs_diff(i, target_k) <= 1 &&
132  target_dist < 1.0 - options.TOO_CLOSE_OBSTACLE &&
133  ni.obstacles[i] > 1.05 * target_dist)
134  {
135  // Don't count obstacles ahead of the target.
136  scores[0] =
137  std::max(target_dist, ni.obstacles[i]) / (target_dist * 1.05);
138  }
139  else
140  {
141  scores[0] =
142  std::max(0.0, ni.obstacles[i] - options.TOO_CLOSE_OBSTACLE);
143  }
144 
145  // Discount "circular loop aparent free distance" here, but don't count
146  // it for clearance, since those are not real obstacle points.
147  if (ptg != nullptr)
148  {
149  const double max_real_freespace =
150  ptg->getActualUnloopedPathLength(i);
151  const double max_real_freespace_norm =
152  max_real_freespace / ptg->getRefDistance();
153 
154  mrpt::keep_min(scores[0], max_real_freespace_norm);
155  }
156 
157  // Factor #2: Closest approach to target along straight line (Euclidean)
158  // -------------------------------------------
160  sg.point1.x = 0;
161  sg.point1.y = 0;
162  sg.point2.x = x;
163  sg.point2.y = y;
164 
165  // Range of attainable values: 0=passes thru target. 2=opposite
166  // direction
167  double min_dist_target_along_path = sg.distance(target);
168 
169  // Idea: if this segment is taking us *away* from target, don't make the
170  // segment to start at (0,0), since all
171  // paths "running away" will then have identical minimum distances to
172  // target. Use the middle of the segment instead:
173  const double endpt_dist_to_target = (target - TPoint2D(x, y)).norm();
174  const double endpt_dist_to_target_norm =
175  std::min(1.0, endpt_dist_to_target);
176 
177  if ((endpt_dist_to_target_norm > target_dist &&
178  endpt_dist_to_target_norm >= 0.95 * target_dist) &&
179  min_dist_target_along_path >
180  1.05 * std::min(
181  target_dist,
182  endpt_dist_to_target_norm) // the path does not get
183  // any closer to trg
184  //|| (ni.obstacles[i]<0.95*target_dist)
185  )
186  {
187  // path takes us away or way blocked:
188  sg.point1.x = x * 0.5;
189  sg.point1.y = y * 0.5;
190  min_dist_target_along_path = sg.distance(target);
191  }
192 
193  scores[1] = 1.0 / (1.0 + square(min_dist_target_along_path));
194 
195  // Factor #3: Distance of end collision-free point to target (Euclidean)
196  // -----------------------------------------------------
197  {
198  scores[2] = std::sqrt(
199  1.01 - endpt_dist_to_target_norm); // the 1.01 instead of 1.0
200  // is to be 100% sure we
201  // don't get a domain error
202  // in sqrt()
203  }
204 
205  // Factor #4: Stabilizing factor (hysteresis) to avoid quick switch
206  // among very similar paths:
207  // ------------------------------------------------------------------------------------------
208  if (m_last_selected_sector != std::numeric_limits<unsigned int>::max())
209  {
210  const unsigned int hist_dist = mrpt::abs_diff(
212  i); // It's fine here to consider that -PI is far from +PI.
213 
214  if (hist_dist >= options.HYSTERESIS_SECTOR_COUNT)
215  scores[3] = square(
216  1.0 - (hist_dist - options.HYSTERESIS_SECTOR_COUNT) /
217  double(nDirs));
218  else
219  scores[3] = 1.0;
220  }
221  else
222  {
223  scores[3] = 1.0;
224  }
225 
226  // Factor #5: clearance to nearest obstacle along path
227  // ------------------------------------------------------------------------------------------
228  {
229  const double query_dist_norm = std::min(0.99, target_dist * 0.95);
230  const double avr_path_clearance = ni.clearance->getClearance(
231  i /*path index*/, query_dist_norm, true /*interpolate path*/);
232  const double point_clearance = ni.clearance->getClearance(
233  i /*path index*/, query_dist_norm, false /*interpolate path*/);
234  scores[4] = 0.5 * (avr_path_clearance + point_clearance);
235  }
236 
237  // Save stats for debugging:
238  for (int l = 0; l < NUM_FACTORS; l++) m_dirs_scores(i, l) = scores[l];
239  }
240 
241  // Normalize factors?
242  ASSERT_(options.factorNormalizeOrNot.size() == NUM_FACTORS);
243  for (int l = 0; l < NUM_FACTORS; l++)
244  {
245  if (!options.factorNormalizeOrNot[l]) continue;
246 
247  const double mmax = m_dirs_scores.col(l).maxCoeff();
248  const double mmin = m_dirs_scores.col(l).minCoeff();
249  const double span = mmax - mmin;
250  if (span <= .0) continue;
251 
252  m_dirs_scores.col(l).array() -= mmin;
253  m_dirs_scores.col(l).array() /= span;
254  }
255 
256  // Phase 1: average of PHASE1_FACTORS and thresholding:
257  // ----------------------------------------------------------------------
258  const unsigned int NUM_PHASES = options.PHASE_FACTORS.size();
259  ASSERT_(NUM_PHASES >= 1);
260 
261  std::vector<double> weights_sum_phase(NUM_PHASES, .0),
262  weights_sum_phase_inv(NUM_PHASES);
263  for (unsigned int i = 0; i < NUM_PHASES; i++)
264  {
265  for (unsigned int l : options.PHASE_FACTORS[i])
266  weights_sum_phase[i] += options.factorWeights[l];
267  ASSERT_(weights_sum_phase[i] > .0);
268  weights_sum_phase_inv[i] = 1.0 / weights_sum_phase[i];
269  }
270 
271  eo.phase_scores = std::vector<std::vector<double>>(
272  NUM_PHASES, std::vector<double>(nDirs, .0));
273  auto& phase_scores = eo.phase_scores; // shortcut
274  double last_phase_threshold = -1.0; // don't threshold for the first phase
275 
276  for (unsigned int phase_idx = 0; phase_idx < NUM_PHASES; phase_idx++)
277  {
278  double phase_min = std::numeric_limits<double>::max(), phase_max = .0;
279 
280  for (unsigned int i = 0; i < nDirs; i++)
281  {
282  double this_dir_eval = 0;
283 
284  if (ni.obstacles[i] <
285  options.TOO_CLOSE_OBSTACLE || // Too close to obstacles ?
286  (phase_idx > 0 &&
287  phase_scores[phase_idx - 1][i] <
288  last_phase_threshold) // thresholding of the previous
289  // phase
290  )
291  {
292  this_dir_eval = .0;
293  }
294  else
295  {
296  // Weighted avrg of factors:
297  for (unsigned int l : options.PHASE_FACTORS[phase_idx])
298  this_dir_eval +=
300  std::log(std::max(1e-6, m_dirs_scores(i, l)));
301 
302  this_dir_eval *= weights_sum_phase_inv[phase_idx];
303  this_dir_eval = std::exp(this_dir_eval);
304  }
305  phase_scores[phase_idx][i] = this_dir_eval;
306 
307  mrpt::keep_max(phase_max, phase_scores[phase_idx][i]);
308  mrpt::keep_min(phase_min, phase_scores[phase_idx][i]);
309 
310  } // for each direction
311 
312  ASSERT_(options.PHASE_THRESHOLDS.size() == NUM_PHASES);
313  ASSERT_(
314  options.PHASE_THRESHOLDS[phase_idx] > .0 &&
315  options.PHASE_THRESHOLDS[phase_idx] < 1.0);
316 
317  last_phase_threshold =
318  options.PHASE_THRESHOLDS[phase_idx] * phase_max +
319  (1.0 - options.PHASE_THRESHOLDS[phase_idx]) * phase_min;
320  } // end for each phase
321 
322  // Give a chance for a derived class to manipulate the final evaluations:
323  auto& dirs_eval = *phase_scores.rbegin();
324 
325  postProcessDirectionEvaluations(dirs_eval, ni, target_idx);
326 
327  // Recalculate the threshold just in case the postProcess function above
328  // changed things:
329  {
330  double phase_min = std::numeric_limits<double>::max(), phase_max = .0;
331  for (unsigned int i = 0; i < nDirs; i++)
332  {
333  mrpt::keep_max(phase_max, phase_scores[NUM_PHASES - 1][i]);
334  mrpt::keep_min(phase_min, phase_scores[NUM_PHASES - 1][i]);
335  }
336  last_phase_threshold =
337  options.PHASE_THRESHOLDS[NUM_PHASES - 1] * phase_max +
338  (1.0 - options.PHASE_THRESHOLDS[NUM_PHASES - 1]) * phase_min;
339  }
340 
341  // Search for best direction:
342 
343  // Of those directions above "last_phase_threshold", keep the GAP with the
344  // largest maximum value within;
345  // then pick the MIDDLE point as the final selection.
346  std::vector<TGap> gaps;
347  int best_gap_idx = -1;
348  int gap_idx_for_target_dir = -1;
349  {
350  bool inside_gap = false;
351  for (unsigned int i = 0; i < nDirs; i++)
352  {
353  const double val = dirs_eval[i];
354  if (val < last_phase_threshold)
355  {
356  if (inside_gap)
357  {
358  // We just ended a gap:
359  auto& active_gap = *gaps.rbegin();
360  active_gap.k_to = i - 1;
361  inside_gap = false;
362  }
363  }
364  else
365  {
366  // higher or EQUAL to the treshold (equal is important just in
367  // case we have a "flat" diagram...)
368  if (!inside_gap)
369  {
370  // We just started a gap:
371  TGap new_gap;
372  new_gap.k_from = i;
373  gaps.emplace_back(new_gap);
374  inside_gap = true;
375  }
376  }
377 
378  if (inside_gap)
379  {
380  auto& active_gap = *gaps.rbegin();
381  if (val >= active_gap.max_eval)
382  {
383  active_gap.k_best_eval = i;
384  }
385  mrpt::keep_max(active_gap.max_eval, val);
386  mrpt::keep_min(active_gap.min_eval, val);
387 
388  if (target_k == i)
389  {
390  active_gap.contains_target_k = true;
391  gap_idx_for_target_dir = gaps.size() - 1;
392  }
393 
394  if (best_gap_idx == -1 || val > gaps[best_gap_idx].max_eval)
395  {
396  best_gap_idx = gaps.size() - 1;
397  }
398  }
399  } // end for i
400 
401  // Handle the case where we end with an open, active gap:
402  if (inside_gap)
403  {
404  auto& active_gap = *gaps.rbegin();
405  active_gap.k_to = nDirs - 1;
406  }
407  }
408 
409  ASSERT_(!gaps.empty());
410  ASSERT_(best_gap_idx >= 0 && best_gap_idx < int(gaps.size()));
411 
412  const TGap& best_gap = gaps[best_gap_idx];
413 
414  eo.best_eval = best_gap.max_eval;
415 
416  // Different qualitative situations:
417  if (best_gap_idx == gap_idx_for_target_dir) // Gap contains target, AND
418  {
419  // the way seems to have clearance enought:
420  const auto cl_left =
421  mrpt::abs_diff(target_k, (unsigned int)best_gap.k_from);
422  const auto cl_right =
423  mrpt::abs_diff(target_k, (unsigned int)best_gap.k_to);
424 
425  const auto smallest_clearance_in_k_units = std::min(cl_left, cl_right);
426  const unsigned int clearance_threshold =
428 
429  const unsigned int gap_width = best_gap.k_to - best_gap.k_from;
430  const unsigned int width_threshold =
432 
433  // Move straight to target?
434  if (smallest_clearance_in_k_units >= clearance_threshold &&
435  gap_width >= width_threshold &&
436  ni.obstacles[target_k] > target_dist * 1.01)
437  {
438  eo.best_k = target_k;
439  }
440  }
441 
442  if (eo.best_k == INVALID_K) // did not fulfill conditions above
443  {
444  // Not heading to target: go thru the "middle" of the gap to maximize
445  // clearance
446  eo.best_k = mrpt::round(0.5 * (best_gap.k_to + best_gap.k_from));
447  }
448 
449  // Alternative, simpler method to decide motion:
450  // If target can be reached without collision *and* with a minimum of
451  // clearance,
452  // then select that direction, with the score as computed with the regular
453  // formulas above
454  // (even if that score was not the maximum!).
455  if (target_dist < 0.99 &&
456  (
457  /* No obstacles to target + enough clearance: */
458  (ni.obstacles[target_k] > target_dist * 1.01 &&
460  target_k /*path index*/, std::min(0.99, target_dist * 0.95),
461  true /*interpolate path*/) > options.TOO_CLOSE_OBSTACLE) ||
462  /* Or: no obstacles to target with extra margin, target is really
463  near, dont check clearance: */
464  (ni.obstacles[target_k] >
465  (target_dist + 0.15 /*meters*/ / ptg_ref_dist) &&
466  target_dist < (1.5 /*meters*/ / ptg_ref_dist))) &&
467  dirs_eval[target_k] >
468  0 /* the direct target direction has at least a minimum score */
469  )
470  {
471  eo.best_k = target_k;
472  eo.best_eval = dirs_eval[target_k];
473 
474  // Reflect this decision in the phase score plots:
475  phase_scores[NUM_PHASES - 1][target_k] += 2.0;
476  }
477 }
478 
480 {
481  using mrpt::square;
482 
483  ASSERT_(ni.clearance != nullptr);
484  ASSERT_(!ni.targets.empty());
485 
486  // Create a log record for returning data.
488  std::make_shared<CLogFileRecord_FullEval>();
489  no.logRecord = log;
490 
491  const size_t numTrgs = ni.targets.size();
492 
493  std::vector<EvalOutput> evals(numTrgs);
494  double best_eval = .0;
495  unsigned int best_trg_idx = 0;
496 
497  for (unsigned int trg_idx = 0; trg_idx < numTrgs; trg_idx++)
498  {
499  auto& eo = evals[trg_idx];
500  evalSingleTarget(trg_idx, ni, eo);
501 
502  if (eo.best_eval >=
503  best_eval) // >= because we prefer the most advanced targets...
504  {
505  best_eval = eo.best_eval;
506  best_trg_idx = trg_idx;
507  }
508  }
509 
510  // Prepare NavigationOutput data:
511  if (best_eval == .0)
512  {
513  // No way found!
514  no.desiredDirection = 0;
515  no.desiredSpeed = 0;
516  }
517  else
518  {
519  // A valid movement:
520  const auto ptg = getAssociatedPTG();
521  const double ptg_ref_dist = ptg ? ptg->getRefDistance() : 1.0;
522 
524  evals[best_trg_idx].best_k, ni.obstacles.size());
525 
526  // Speed control: Reduction factors
527  // ---------------------------------------------
528  const double targetNearnessFactor =
530  ? std::min(
531  1.0, ni.targets[best_trg_idx].norm() /
533  ptg_ref_dist))
534  : 1.0;
535 
536  const double obs_dist =
537  ni.obstacles[evals[best_trg_idx].best_k]; // Was: min with
538  // obs_clearance too.
539  const double obs_dist_th = std::max(
541  (options.OBSTACLE_SLOW_DOWN_DISTANCE / ptg_ref_dist) *
542  ni.maxObstacleDist);
543  double riskFactor = 1.0;
544  if (obs_dist <= options.TOO_CLOSE_OBSTACLE)
545  {
546  riskFactor = 0.0;
547  }
548  else if (
549  obs_dist < obs_dist_th && obs_dist_th > options.TOO_CLOSE_OBSTACLE)
550  {
551  riskFactor = (obs_dist - options.TOO_CLOSE_OBSTACLE) /
552  (obs_dist_th - options.TOO_CLOSE_OBSTACLE);
553  }
554  no.desiredSpeed =
555  ni.maxRobotSpeed * std::min(riskFactor, targetNearnessFactor);
556  }
557 
558  m_last_selected_sector = evals[best_trg_idx].best_k;
559 
560  // LOG --------------------------
561  if (log)
562  {
563  log->selectedTarget = best_trg_idx;
564  log->selectedSector = evals[best_trg_idx].best_k;
565  log->evaluation = evals[best_trg_idx].best_eval;
566  log->dirs_eval = evals[best_trg_idx].phase_scores;
567 
569  {
570  log->dirs_scores = m_dirs_scores;
571  }
572  }
573 }
574 
576  const double a, const unsigned int N)
577 {
578  const int idx = round(0.5 * (N * (1 + mrpt::math::wrapToPi(a) / M_PI) - 1));
579  if (idx < 0)
580  return 0;
581  else
582  return static_cast<unsigned int>(idx);
583 }
584 
589 {
591  << evaluation << selectedTarget /*v3*/;
592 }
593 
594 /*---------------------------------------------------------------
595  readFromStream
596  ---------------------------------------------------------------*/
599 {
600  switch (version)
601  {
602  case 0:
603  case 1:
604  case 2:
605  case 3:
606  {
607  if (version >= 2)
608  {
610  }
611  else
612  {
615  if (version >= 1)
616  {
618  }
619  }
621  if (version >= 3)
622  {
623  in >> selectedTarget;
624  }
625  else
626  {
627  selectedTarget = 0;
628  }
629  }
630  break;
631  default:
633  };
634 }
635 
636 /*---------------------------------------------------------------
637  TOptions
638  ---------------------------------------------------------------*/
640  : factorWeights{0.1, 0.5, 0.5, 0.01, 1},
641  factorNormalizeOrNot{0, 0, 0, 0, 1},
642  PHASE_FACTORS{{1, 2}, {4}, {0, 2}},
643  PHASE_THRESHOLDS{0.5, 0.6, 0.7}
644 
645 {
646 }
647 
650 {
651  MRPT_START
652 
653  // Load from config text:
654  MRPT_LOAD_CONFIG_VAR(TOO_CLOSE_OBSTACLE, double, c, s);
655  MRPT_LOAD_CONFIG_VAR(TARGET_SLOW_APPROACHING_DISTANCE, double, c, s);
656  MRPT_LOAD_CONFIG_VAR(OBSTACLE_SLOW_DOWN_DISTANCE, double, c, s);
657  MRPT_LOAD_CONFIG_VAR(HYSTERESIS_SECTOR_COUNT, double, c, s);
658  MRPT_LOAD_CONFIG_VAR(LOG_SCORE_MATRIX, bool, c, s);
659  MRPT_LOAD_CONFIG_VAR(clearance_threshold_ratio, double, c, s);
660  MRPT_LOAD_CONFIG_VAR(gap_width_ratio_threshold, double, c, s);
661 
662  c.read_vector(
663  s, "factorWeights", std::vector<double>(), factorWeights, true);
664  ASSERT_(factorWeights.size() == 5);
665 
666  c.read_vector(
667  s, "factorNormalizeOrNot", factorNormalizeOrNot, factorNormalizeOrNot);
668  ASSERT_(factorNormalizeOrNot.size() == factorWeights.size());
669 
670  // Phases:
671  int PHASE_COUNT = 0;
672  MRPT_LOAD_CONFIG_VAR_NO_DEFAULT(PHASE_COUNT, int, c, s);
673 
674  PHASE_FACTORS.resize(PHASE_COUNT);
675  PHASE_THRESHOLDS.resize(PHASE_COUNT);
676  for (int i = 0; i < PHASE_COUNT; i++)
677  {
678  c.read_vector(
679  s, mrpt::format("PHASE%i_FACTORS", i + 1), PHASE_FACTORS[i],
680  PHASE_FACTORS[i], true);
681  ASSERT_(!PHASE_FACTORS[i].empty());
682 
683  PHASE_THRESHOLDS[i] = c.read_double(
684  s, mrpt::format("PHASE%i_THRESHOLD", i + 1), .0, true);
685  ASSERT_(PHASE_THRESHOLDS[i] >= .0 && PHASE_THRESHOLDS[i] <= 1.0);
686  }
687 
688  MRPT_END
689 }
690 
693 {
694  MRPT_START;
695 
696  const int WN = mrpt::config::MRPT_SAVE_NAME_PADDING(),
698 
700  TOO_CLOSE_OBSTACLE,
701  "Directions with collision-free distances below this threshold are not "
702  "elegible.");
704  TARGET_SLOW_APPROACHING_DISTANCE,
705  "Start to reduce speed when closer than this to target.");
707  OBSTACLE_SLOW_DOWN_DISTANCE,
708  "Start to reduce speed when clearance is below this value ([0,1] ratio "
709  "wrt obstacle reference/max distance)");
711  HYSTERESIS_SECTOR_COUNT,
712  "Range of `sectors` (directions) for hysteresis over successive "
713  "timesteps");
715  LOG_SCORE_MATRIX, "Save the entire score matrix in log files");
717  clearance_threshold_ratio,
718  "Ratio [0,1], times path_count, gives the minimum number of paths at "
719  "each side of a target direction to be accepted as desired direction");
721  gap_width_ratio_threshold,
722  "Ratio [0,1], times path_count, gives the minimum gap width to accept "
723  "a direct motion towards target.");
724 
725  ASSERT_EQUAL_(factorWeights.size(), 5);
726  c.write(
727  s, "factorWeights",
728  mrpt::system::sprintf_container("%.2f ", factorWeights), WN, WV,
729  "[0]=Free space, [1]=Dist. in sectors, [2]=Closer to target "
730  "(Euclidean), [3]=Hysteresis, [4]=clearance along path");
731  c.write(
732  s, "factorNormalizeOrNot",
733  mrpt::system::sprintf_container("%u ", factorNormalizeOrNot), WN, WV,
734  "Normalize factors or not (1/0)");
735 
736  c.write(
737  s, "PHASE_COUNT", PHASE_FACTORS.size(), WN, WV,
738  "Number of evaluation phases to run (params for each phase below)");
739 
740  for (unsigned int i = 0; i < PHASE_FACTORS.size(); i++)
741  {
742  c.write(
743  s, mrpt::format("PHASE%u_THRESHOLD", i + 1), PHASE_THRESHOLDS[i],
744  WN, WV,
745  "Phase scores must be above this relative range threshold [0,1] to "
746  "be considered in next phase (Default:`0.75`)");
747  c.write(
748  s, mrpt::format("PHASE%u_FACTORS", i + 1),
749  mrpt::system::sprintf_container("%d ", PHASE_FACTORS[i]), WN, WV,
750  "Indices of the factors above to be considered in this phase");
751  }
752 
753  MRPT_END;
754 }
755 
758 {
759  // Params:
761  << options.PHASE_FACTORS << // v3
763  << options.PHASE_THRESHOLDS // v3
768  ;
769  // State:
770  out << m_last_selected_sector;
771 }
774 {
775  switch (version)
776  {
777  case 0:
778  case 1:
779  case 2:
780  case 3:
781  case 4:
782  {
783  // Params:
785 
786  if (version >= 3)
787  {
789  }
790  else
791  {
792  options.PHASE_THRESHOLDS.resize(2);
794  }
797 
798  if (version >= 3)
799  {
801  }
802  else
803  {
804  options.PHASE_THRESHOLDS.resize(1);
806  }
807 
808  if (version >= 1) in >> options.OBSTACLE_SLOW_DOWN_DISTANCE;
809  if (version >= 2) in >> options.factorNormalizeOrNot;
810 
811  if (version >= 4)
812  {
815  }
816 
817  // State:
819  }
820  break;
821  default:
823  };
824 }
825 
827  std::vector<double>& dir_evals, const NavInput& ni, unsigned int trg_idx)
828 {
829  // Default: do nothing
830 }
double index2alpha(uint16_t k) const
Alpha value for the discrete corresponding value.
unsigned int direction2sector(const double a, const unsigned int N)
double gap_width_ratio_threshold
Ratio [0,1], times path_count, gives the minimum gap width to accept a direct motion towards target...
virtual void postProcessDirectionEvaluations(std::vector< double > &dir_evals, const NavInput &ni, unsigned int trg_idx)
double x
X,Y coordinates.
Definition: TPoint2D.h:23
double min_eval
mrpt::obs::CSinCosLookUpTableFor2DScans m_sincos_lut
#define MRPT_START
Definition: exceptions.h:241
A class for storing extra information about the execution of CHolonomicFullEval navigation.
const TSinCosValues & getSinCosForScan(const CObservation2DRangeScan &scan) const
Return two vectors with the cos and the sin of the angles for each of the rays in a scan...
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
#define min(a, b)
void resize(size_t row, size_t col)
double max_eval
double TOO_CLOSE_OBSTACLE
Directions with collision-free distances below this threshold are not elegible.
auto col(int colIdx)
Definition: MatrixBase.h:89
void saveToConfigFile(mrpt::config::CConfigFileBase &cfg, const std::string &section) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
int MRPT_SAVE_NAME_PADDING()
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
std::vector< double > obstacles
Distance to obstacles in polar coordinates, relative to the robot.
double distance(const TPoint2D &point) const
Distance to point.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
This file implements several operations that operate element-wise on individual or pairs of container...
A base class for holonomic reactive navigation methods.
double TARGET_SLOW_APPROACHING_DISTANCE
Start to reduce speed when closer than this to target [m].
STL namespace.
mrpt::nav const unsigned int INVALID_K
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
std::vector< int32_t > factorNormalizeOrNot
0/1 to normalize factors.
IMPLEMENTS_SERIALIZABLE(CLogFileRecord_FullEval, CHolonomicLogFileRecord, mrpt::nav) IMPLEMENTS_SERIALIZABLE(CHolonomicFullEval
GLdouble s
Definition: glext.h:3682
bool m_enableApproachTargetSlowDown
Whether to decrease speed when approaching target.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
double clearance_threshold_ratio
Ratio [0,1], times path_count, gives the minimum number of paths at each side of a target direction t...
std::vector< double > PHASE_THRESHOLDS
Phase 1,2,N-1...
std::vector< mrpt::math::TPoint2D > targets
Relative location (x,y) of target point(s).
unsigned char uint8_t
Definition: rptypes.h:44
TOptions options
Parameters of the algorithm (can be set manually or loaded from CHolonomicFullEval::initialize or opt...
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
double OBSTACLE_SLOW_DOWN_DISTANCE
Start to reduce speed when clearance is below this value ([0,1] ratio wrt obstacle reference/max dist...
double getClearance(uint16_t k, double TPS_query_distance, bool integrate_over_path) const
Gets the clearance for path k and distance TPS_query_distance in one of two modes: ...
T square(const T x)
Inline function for the square of a number.
int mmin(const int t1, const int t2)
Definition: xmlParser.cpp:37
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
double maxRobotSpeed
Maximum robot speed, in the same units than obstacles, per second.
This class allows loading and storing values and vectors of different types from a configuration text...
This base provides a set of functions for maths stuff.
2D segment, consisting of two points.
Definition: TSegment2D.h:20
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
Auxiliary struct that holds all the relevant geometry information about a 2D scan.
const GLubyte * c
Definition: glext.h:6406
A base class for log records for different holonomic navigation methods.
double desiredDirection
The desired motion direction, in the range [-PI, PI].
mrpt::config::CConfigFileBase CConfigFileBase
std::vector< double > factorWeights
See docs above.
T abs_diff(const T a, const T b)
Efficient and portable evaluation of the absolute difference of two unsigned integer values (but will...
int val
Definition: mrpt_jpeglib.h:957
int MRPT_SAVE_VALUE_PADDING()
void evalSingleTarget(unsigned int target_idx, const NavInput &ni, EvalOutput &eo)
Evals one single target of the potentially many of them in NavInput.
#define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT( variableName, variableType, configFileObject, sectionNameStr)
CHolonomicLogFileRecord::Ptr logRecord
The navigation method will create a log record and store it here via a smart pointer.
TPoint2D point2
Destiny point.
Definition: TSegment2D.h:30
double maxObstacleDist
Maximum expected value to be found in obstacles.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
GLsizei const GLchar ** string
Definition: glext.h:4116
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:50
Full evaluation of all possible directions within the discrete set of input directions.
bool empty() const
Definition: ts_hash_map.h:190
std::vector< std::vector< double > > dirs_eval
Final [N-1] and earlier stages [0...N-1] evaluation scores for each direction, in the same order of T...
TPoint2D point1
Origin point.
Definition: TSegment2D.h:26
double desiredSpeed
The desired motion speed in that direction, from 0 up to NavInput::maxRobotSpeed. ...
#define MRPT_LOAD_CONFIG_VAR( variableName, variableType, configFileObject, sectionNameStr)
An useful macro for loading variables stored in a INI-like file under a key with the same name that t...
const mrpt::nav::ClearanceDiagram * clearance
The computed clearance for each direction (optional in some implementations).
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
void navigate(const NavInput &ni, NavOutput &no) override
Invokes the holonomic navigation algorithm itself.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
mrpt::math::CMatrixD m_dirs_scores
Individual scores for each direction: (i,j), i (row) are directions, j (cols) are scores...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
double HYSTERESIS_SECTOR_COUNT
Range of "sectors" (directions) for hysteresis over successive timesteps.
#define MRPT_SAVE_CONFIG_VAR_COMMENT(variableName, __comment)
GLenum GLint GLint y
Definition: glext.h:3542
Input parameters for CAbstractHolonomicReactiveMethod::navigate()
GLenum GLint x
Definition: glext.h:3542
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
bool rightToLeft
Angles storage order: true=counterclockwise; false=clockwise.
Lightweight 2D point.
Definition: TPoint2D.h:31
GLenum GLenum GLvoid GLvoid GLvoid * span
Definition: glext.h:3580
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
Output for CAbstractHolonomicReactiveMethod::navigate()
mrpt::math::CMatrixD dirs_scores
Individual scores for each direction: (i,j), i (row) are directions, j (cols) are scores...
std::string sprintf_container(const char *fmt, const T &V)
Generates a string for a container in the format [A,B,C,...], and the fmt string for each vector elem...
Definition: string_utils.h:125
std::vector< std::vector< double > > phase_scores
mrpt::nav::CParameterizedTrajectoryGenerator * getAssociatedPTG() const
Returns the pointer set by setAssociatedPTG()
std::vector< std::vector< int32_t > > PHASE_FACTORS
Factor indices [0,4] for the factors to consider in each phase 1,2,...N of the movement decision (Def...
bool LOG_SCORE_MATRIX
(default:false, to save space)
uint16_t alpha2index(double alpha) const
Discrete index value for the corresponding alpha value.
CONTAINER::Scalar norm(const CONTAINER &v)
void loadFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019