build map from 2-凯发k8网页登录
this example shows how to implement the slam algorithm on a series of 2-d lidar scans using scan processing and pose graph optimization (pgo). the goal of this example is to estimate the trajectory of the robot and build a map of the environment.
slam stands for simultaneous localization and mapping.
localization — estimating the pose of a robot in a known environment.
mapping — building the map of an unknown environment from a known robot pose and sensor data.
in the slam process, a robot creates a map of an environment while localizing itself. slam has a wide range of applications in robotics, self-driving cars, and uavs.
in offline slam, a robot steers through an environment and records the sensor data. the slam algorithm processes this data to compute a map of the environment. the map is stored and used for localization, path-planning during the actual robot operation.
this example uses a 2-d offline slam algorithm. the algorithm incrementally processes recorded lidar scans and builds a pose graph to create a map of the environment. to overcome the drift accumulated in the estimated robot trajectory, the example uses scan matching to recognize previously visited places and then uses this loop closure information to optimize poses and update the map of the environment. to optimize the pose graph, this example uses a 2-d pose graph optimization function from navigation toolbox™.
in this example, you learn how to:
estimate robot trajectory from a series of scans using scan registration algorithms.
optimize the drift in the estimated robot trajectory by identifying previously visited places (loop closures).
visualize the map of the environment using scans and their absolute poses.
load laser scans
this example uses data collected in an indoor environment using a jackal™ robot from clearpath robotics™. the robot is equipped with a sick™ tim-511 laser scanner with a maximum range of 10 meters. load the warehouse.mat
file containing the laser scans into the workspace.
data = load("warehouse.mat");
scans = data.warehousescans;
estimate robot trajectory
create a object. using this object, you can:
store and add lidar scans incrementally.
detect, add, and delete loop closures.
find and update the absolute poses of the scans.
generate and visualize a pose graph.
specify the maximum lidar range and grid resolution values. you can modify these values to fine-tune the map of the environment. use these values to create the lidar scan map.
maxlidarrange = 8; gridresolution = 20; mapobj = lidarscanmap(gridresolution,maxlidarrange);
incrementally add scans from the input data to the lidar scan map object by using the function. this function rejects scans if they are too close to consecutive scans.
for i = 1:numel(scans) isscanaccepted = addscan(mapobj,scans{i}); if ~isscanaccepted continue; end end
reconstruct the scene by plotting the scans and poses tracked by the lidar scan map.
hfigmap = figure;
axmap = axes(parent=hfigmap);
show(mapobj,parent=axmap);
title(axmap,"map of the environment and robot trajectory")
notice that the estimated robot trajectory drifts over time. the drift can be due to any of these reasons:
noisy scans from the sensor, without sufficient overlap.
absence of significant features in the environment.
an inaccurate initial transformation, especially when rotation is significant
drift in the estimated trajectory results in an inaccurate map of the environment.
drift correction
correct the drift in trajectory by accurately detecting the loops, which are the places the robot returns to, after previously visiting. add the loop closure edges to the object to correct the drift in trajectory during pose graph optimization.
loop closure detection
loop closure detection determines whether, for a given scan, the robot has previously visited the current location. the search consists of matching the current scan against the previous scans around the current robot location, within the specified radius loopclosuresearchradius
. accept a scan as a match if the match score is greater than the specified threshold loopclosurethreshold
.
you can detect loop closures by using the function of the object and the add them to the map object by using the function.
you can increase the loopclosurethreshold
value to avoid false positives in loop closure detection, but the function can still return bad matches in environments with similar or repeated features. to address this, increase the loopclosuresearchradius
value to search a larger radius around the current scan for loop closures, though this increases computation time.
you can also specify the number of loop closure matches loopclosurenummatches.
all of these parameters help in fine-tuning the loop closure detections.
loopclosurethreshold = 110; loopclosuresearchradius = 2; loopclosurenummatches = 1; mapobjloop = lidarscanmap(gridresolution,maxlidarrange); for i = 1:numel(scans) isscanaccepted = addscan(mapobjloop,scans{i}); % detect loop closure if scan is accepted if isscanaccepted [relpose,matchscanid] = detectloopclosure(mapobjloop, ... matchthreshold=loopclosurethreshold, ... searchradius=loopclosuresearchradius, ... nummatches=loopclosurenummatches); % add loop closure to map object if relpose is estimated if ~isempty(relpose) addloopclosure(mapobjloop,matchscanid,i,relpose); end end end
optimize trajectory
create a pose graph object from the drift-corrected lidar scan map by using the function. use the (navigation toolbox) function to optimize the pose graph.
pgraph = posegraph(mapobjloop); updatedpgraph = optimizeposegraph(pgraph);
extract the optimized absolute poses from the pose graph by using the (navigation toolbox) function, and update the trajectory to build an accurate map of the environment.
optimizedscanposes = nodeestimates(updatedpgraph); updatescanposes(mapobjloop,optimizedscanposes);
visualize results
visualize the change in robot trajectory before and after pose graph optimization. the red lines represent loop closure edges.
hfigtraj = figure(position=[0 0 900 450]); % visualize robot trajectory before optimization axpgraph = subplot(1,2,1,parent=hfigtraj); axpgraph.position = [0.04 0.1 0.45 0.8]; show(pgraph,ids="off",parent=axpgraph); title(axpgraph,"before pgo") % visualize robot trajectory after optimization axupdatedpgraph = subplot(1,2,2,parent=hfigtraj); axupdatedpgraph.position = [0.54 0.1 0.45 0.8]; show(updatedpgraph,ids="off",parent=axupdatedpgraph); title(axupdatedpgraph,"after pgo") axis([axpgraph axupdatedpgraph],[-6 10 -7 3]) sgtitle("robot trajectory",fontweight="bold")
visualize the map of the environment and robot trajectory before and after pose graph optimization.
hfigmaptraj = figure(position=[0 0 900 450]); % visualize map and robot trajectory before optimization axoldmap = subplot(1,2,1,parent=hfigmaptraj); axoldmap.position = [0.05 0.1 0.44 0.8]; show(mapobj,parent=axoldmap); title(axoldmap,"before pgo") % visualize map and robot trajectory after optimization axupdatedmap = subplot(1,2,2,parent=hfigmaptraj); axupdatedmap.position = [0.56 0.1 0.44 0.8]; show(mapobjloop,parent=axupdatedmap); title(axupdatedmap,"after pgo") axis([axoldmap axupdatedmap],[-9 18 -10 9]) sgtitle("map of the environment and robot trajectory",fontweight="bold")
see also
functions
- | | | |