00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include <laser_slam/scan_matching.h>
00040 #include <graph_mapping_utils/geometry.h>
00041 #include <pose_graph/exception.h>
00042 #include <graph_mapping_utils/to_string.h>
00043 #include <boost/foreach.hpp>
00044 #include <boost/optional.hpp>
00045
00046
00047 namespace laser_slam
00048 {
00049
00050 namespace msg=graph_mapping_msgs;
00051 namespace ksm=karto_scan_matcher;
00052 namespace util=graph_mapping_utils;
00053 namespace gm=geometry_msgs;
00054 namespace sm=sensor_msgs;
00055 namespace pg=pose_graph;
00056
00057 typedef std::vector<ksm::ScanWithPose> ScanVec;
00058
00059 ScanVec makeRefScans(const pg::ConstraintGraph& g, const ScanMap& scans, const pg::NodeSet& nodes,
00060 const tf::Pose& laser_offset)
00061 {
00062 ScanVec v;
00063 const tf::Pose base_in_laser_frame = laser_offset.inverse();
00064 BOOST_FOREACH (const unsigned n, nodes) {
00065 try {
00066 ROS_ASSERT (g.hasOptimizedPose(n));
00067 LocalizedScan::ConstPtr loc_scan = scans.get(n);
00068
00069
00070
00071 const tf::Pose laser_pose = util::absolutePose(g.getOptimizedPose(n), loc_scan->sensor_pose);
00072 const tf::Pose base_pose = laser_pose*base_in_laser_frame;
00073
00074 v.push_back(ksm::ScanWithPose(loc_scan->scan, util::projectToPose2D(base_pose)));
00075 }
00076 catch (pg::DataNotFoundException& e) {
00077 ROS_WARN_STREAM ("Didn't find scan for node " << n << "; skipping");
00078 }
00079 }
00080 return v;
00081 }
00082
00083
00084
00085
00086 ksm::ScanMatchResult scanMatchNodes (const pg::ConstraintGraph& g, MatcherPtr matcher, const pg::NodeSet& nodes,
00087 const ScanMap& scans, const sm::LaserScan& scan, const tf::Pose& init_estimate,
00088 const tf::Pose& laser_offset)
00089 {
00090 const ScanVec ref_scans = makeRefScans(g, scans, nodes, laser_offset);
00091 ROS_DEBUG_STREAM_NAMED ("scan_matching", "Scan matching using " << ref_scans.size() << " reference scans");
00092 ROS_WARN_STREAM_COND (nodes.size() > ref_scans.size(), "Skipped " << nodes.size() - ref_scans.size() <<
00093 " nodes due to lack of optimized poses");
00094 ksm::ScanMatchResult res = matcher->scanMatch(scan, util::projectToPose2D(util::toPose(init_estimate)), ref_scans);
00095 return res;
00096 }
00097
00098
00099
00100 karto_scan_matcher::ScanMatchResult globalLocalization (const pose_graph::ConstraintGraph& g, MatcherPtr local_matcher,
00101 const pg::NodeSet& nodes, const ScanMap& scans,
00102 const sm::LaserScan& scan, const tf::Pose& laser_offset,
00103 double global_resolution, double angular_resolution)
00104 {
00105 using util::toString;
00106 boost::optional<double> min_x, min_y, max_x, max_y;
00107 ROS_ASSERT(!nodes.empty());
00108 BOOST_FOREACH (const unsigned& n, nodes) {
00109 if (g.hasOptimizedPose(n)) {
00110 tf::Pose p = g.getOptimizedPose(n);
00111 const double x = p.getOrigin().x();
00112 const double y = p.getOrigin().y();
00113 if (!min_x || x<min_x)
00114 min_x = x;
00115 if (!min_y || y<min_y)
00116 min_y = y;
00117 if (!max_x || x>max_x)
00118 max_x = x;
00119 if (!max_y || y>max_y)
00120 max_y = y;
00121 }
00122 }
00123 ROS_ASSERT_MSG(min_x, "No optimized poses found in global location");
00124 ROS_DEBUG_NAMED ("global_matching", "Performing global matching between (%.2f, %.2f) and (%.2f, %.2f)",
00125 *min_x, *min_y, *max_x, *max_y);
00126 return globalLocalization(g, local_matcher, nodes, scans, scan, laser_offset, global_resolution,
00127 angular_resolution, *min_x, *min_y, *max_x, *max_y);
00128 }
00129
00130
00131 karto_scan_matcher::ScanMatchResult globalLocalization (const pose_graph::ConstraintGraph& g, MatcherPtr local_matcher,
00132 const pg::NodeSet& nodes, const ScanMap& scans,
00133 const sm::LaserScan& scan, const tf::Pose& laser_offset,
00134 double global_resolution, double angular_resolution,
00135 const double min_x, const double min_y,
00136 const double max_x, const double max_y)
00137 {
00138 const ScanVec ref_scans = makeRefScans(g, scans, nodes, laser_offset);
00139 boost::optional<ksm::ScanMatchResult> best;
00140 double best_response=-42;
00141 const double nx = 1+(max_x-min_x)/global_resolution;
00142 const double ny = 1+(max_y-min_y)/global_resolution;
00143 const double nt = 1+6.28/angular_resolution;
00144 const double num_iters = nx*ny*nt;
00145 unsigned count=0;
00146 for (double x=min_x; x<=max_x; x+=global_resolution) {
00147 for (double y=min_y; y<=max_y; y+=global_resolution) {
00148 for (double theta=0; theta<6.28; theta += angular_resolution) {
00149 {
00150 count++;
00151 ROS_INFO_THROTTLE (1.0, "Performing global localization. %.2f%% done.", 100.0*count/num_iters);
00152 }
00153 const gm::Pose2D p = util::makePose2D(x, y, theta);
00154 ksm::ScanMatchResult res = local_matcher->scanMatch(scan, p, ref_scans);
00155 if (!best || res.response > best_response) {
00156 ROS_DEBUG_STREAM_NAMED ("global_matching", "Result " << util::toString(res.pose) <<
00157 " with response " << res.response << " is a new best");
00158 best = res;
00159 best_response = res.response;
00160 }
00161 }
00162 }
00163 }
00164
00165 ROS_ASSERT(best);
00166 ROS_DEBUG_STREAM_NAMED ("global_matching", "Final result is " << best->pose <<
00167 " with response " << best->response);
00168 return *best;
00169 }
00170
00171
00172 }