monte_carlo_scan_matcher.h
Go to the documentation of this file.
1 
11 #ifndef __MONTE_CARLO_SCAN_MATCHER_H
12 #define __MONTE_CARLO_SCAN_MATCHER_H
13 
14 #include <functional>
15 #include <limits>
16 #include <memory>
17 
18 #include "state_data.h"
19 #include "sensor_data.h"
20 #include "grid_scan_matcher.h"
21 
22 // TODO: move publish transform to observer
23 
31 public:
32  using ObsPtr = std::shared_ptr<GridScanMatcherObserver>;
33 public:
34 
42  MonteCarloScanMatcher(std::shared_ptr<ScanCostEstimator> estimator,
43  unsigned failed_iter, unsigned max_iter):
44  GridScanMatcher{estimator},
45  _failed_tries_limit(failed_iter), _total_tries_limit(max_iter) {}
46 
56  virtual double process_scan(const RobotState &init_pose,
57  const TransformedLaserScan &scan,
58  const GridMap &map,
59  RobotState &pose_delta) override {
60  do_for_each_observer([init_pose, scan, map](ObsPtr obs) {
61  obs->on_matching_start(init_pose, scan, map);
62  });
63 
64  unsigned failed_tries = 0, total_tries = 0;
65  std::shared_ptr<ScanCostEstimator> sce = GridScanMatcher::cost_estimator();
66  double min_scan_cost = std::numeric_limits<double>::max();
67  RobotState optimal_pose = init_pose;
68 
69  min_scan_cost = sce->estimate_scan_cost(optimal_pose, scan,
70  map, min_scan_cost);
71 
72  do_for_each_observer([optimal_pose, scan, min_scan_cost](ObsPtr obs) {
73  obs->on_scan_test(optimal_pose, scan, min_scan_cost);
74  obs->on_pose_update(optimal_pose, scan, min_scan_cost);
75  });
76 
77  while (failed_tries < _failed_tries_limit &&
78  total_tries < _total_tries_limit) {
79  total_tries++;
80 
81  RobotState sampled_pose = optimal_pose;
82  sample_pose(sampled_pose);
83  double sampled_scan_cost = sce->estimate_scan_cost(sampled_pose, scan,
84  map, min_scan_cost);
85  do_for_each_observer([sampled_pose, scan, sampled_scan_cost](ObsPtr obs) {
86  obs->on_scan_test(sampled_pose, scan, sampled_scan_cost);
87  });
88 
89  if (min_scan_cost <= sampled_scan_cost) {
90  failed_tries++;
91  continue;
92  }
93 
94  min_scan_cost = sampled_scan_cost;
95  optimal_pose = sampled_pose;
96  failed_tries = on_estimate_update(failed_tries, _failed_tries_limit);
97 
98  do_for_each_observer([optimal_pose, scan, min_scan_cost](ObsPtr obs) {
99  obs->on_pose_update(optimal_pose, scan, min_scan_cost);
100  });
101  }
102 
103  pose_delta.x = optimal_pose.x - init_pose.x;
104  pose_delta.y = optimal_pose.y - init_pose.y;
105  pose_delta.theta = optimal_pose.theta - init_pose.theta;
106 
107  do_for_each_observer([pose_delta, min_scan_cost](ObsPtr obs) {
108  obs->on_matching_end(pose_delta, min_scan_cost);
109  });
110  return min_scan_cost;
111  }
112 
113 protected:
118  virtual void sample_pose(RobotState &base_pose) = 0;
119 
125  virtual unsigned on_estimate_update(unsigned sample_num,
126  unsigned sample_limit) = 0;
127 
128 private:
129  void do_for_each_observer(std::function<void(ObsPtr)> op) {
130  for (auto &obs : GridScanMatcher::observers()) {
131  if (auto obs_ptr = obs.lock()) {
132  op(obs_ptr);
133  }
134  }
135  }
136 
137 private:
140 };
141 
142 #endif
double x
Definition: state_data.h:38
std::shared_ptr< ScanCostEstimator > cost_estimator()
Returns a pointer to the cost estimator.
An occupancy grid implementation.
Definition: grid_map.h:22
void do_for_each_observer(std::function< void(ObsPtr)> op)
Defines some interfaces for Scan Matchers. There are class GridScanMatcherObserver, ScanCostEstimator, GridScanMatcher.
virtual unsigned on_estimate_update(unsigned sample_num, unsigned sample_limit)=0
Defines some structures related to data obtained from sensors. There are structures ScanPoint and Tra...
Class that matches scans. Performes a scan adjustment by altering a robot pose in order to maximize t...
double theta
The position of robot.
Definition: state_data.h:38
double y
Definition: state_data.h:38
std::vector< std::weak_ptr< GridScanMatcherObserver > > & observers()
Returns a reference to the vector of pointers on observers.
Defines a robot position in cartesian coordinates and an angle of rotation.
Definition: state_data.h:14
Defines some classes related to a robot state. There are classes RobotState and World.
virtual double process_scan(const RobotState &init_pose, const TransformedLaserScan &scan, const GridMap &map, RobotState &pose_delta) override
virtual void sample_pose(RobotState &base_pose)=0
MonteCarloScanMatcher(std::shared_ptr< ScanCostEstimator > estimator, unsigned failed_iter, unsigned max_iter)
Initializes the scan matcher with a certain scan cost estimator.
std::shared_ptr< GridScanMatcherObserver > ObsPtr
Scan Matcher based on the Monte Carlo method. The focus of the scan matcher is to compare a scan and ...
Framework internal representation of a laser scan.
Definition: sensor_data.h:33


tiny_slam
Author(s):
autogenerated on Mon Jun 10 2019 15:30:57