Skip to main content
Open In Colab EKF localization assumes that we know the positions of the landmarks. Suppose instead that we place a TurtleBot in a room for which we have no map. When its sensor detects a landmark, how do we assign world coordinates to that landmark? We need the pose of the robot. But without a map, the robot cannot use the landmark to determine its pose. In simultaneous localization and mapping (SLAM), we estimate the pose and the map together. The distinction from localization is that landmark positions are now unknown quantities in the estimation problem. Let a1:ta_{1:t} denote the actions and z1:tz_{1:t} the measurements collected up to time tt. We distinguish two estimation problems. In full SLAM, we estimate the entire trajectory s1:ts_{1:t} and the map mm: p(s1:t,m∣z1:t,a1:t).p(s_{1:t},m\mid z_{1:t},a_{1:t}). In online SLAM, we retain the current pose and the map: p(st,m∣z1:t,a1:t).p(s_t,m\mid z_{1:t},a_{1:t}). EKF SLAM is an online method. We will study it for a planar robot and a map of point landmarks, using the formulation in Chapter 10 of Thrun, Burgard and Fox (Thrun et al., 2005). The implementation in the next section considers two sensors, a LIDAR and a D435i depth camera, which supply landmark measurements to the same estimator.

A robot and a landmark on a line

Before considering the planar robot, we use the simplest possible case to show why SLAM needs one joint belief over the pose and the map. A range measurement depends only on the difference between the landmark and robot positions. The robot and landmark estimates are independent before the measurement. When the robot moves, the landmark stays where it is, so motion changes the robot estimate and leaves the landmark estimate alone. Motion therefore cannot create a correlation between the two estimates. A measurement that involves both positions can, and after the update the estimates are correlated. EKF SLAM stores such correlations in the off-diagonal entries of its covariance matrix. The example also shows how the filter divides a correction between the two positions. The robot is at an estimated position of 1 m, with a standard deviation of 20 cm. A landmark is estimated to be at 4 m, with a standard deviation of 10 cm. Assume that the two estimates are initially independent. We would therefore expect the sensor to report a distance of 3 m. Suppose it reports 3.1 m instead. We could account for the extra 10 cm by moving the estimated landmark position to 4.1 m, by moving the estimated robot position to 0.9 m, or by changing both estimates. The Kalman filter must decide how to distribute the correction, taking into account the uncertainty in each position and in the measurement. Output from cell 2 The figure shows the two prior probability densities on the same physical line. The dashed arrows illustrate two possible explanations for the measurement; neither is necessarily the actual configuration. Since the landmark is ahead of the robot, we model the measurement as z=m−x+ϵ,ϵ∼N(0,R),z=m-x+\epsilon,\qquad \epsilon\sim\mathcal N(0,R), where xx and mm denote the robot and landmark positions, respectively. More precisely, we use a signed distance model, which agrees with range when m>xm>x. Here the measurement standard deviation is 0.1 m, so R=0.01 m2R=0.01\,\mathrm{m}^2. Let us apply the Kalman filter from the earlier sections, with the state ordered as (x,m)⊤(x,m)^\top: μˉ=[14],Σˉ=[0.04000.01].\bar\mu=\begin{bmatrix}1\\4\end{bmatrix},\qquad \bar\Sigma=\begin{bmatrix}0.04&0\\0&0.01\end{bmatrix}. The measurement function is linear, with H=[−1    1]H=[-1\;\;1]. The innovation is ν=3.1−3.0=0.1\nu=3.1-3.0=0.1 m. No EKF linearization is needed in this example.
The posterior mean and covariance are μ=[0.93334.0167],Σ=[0.01330.00670.00670.0083].\mu=\begin{bmatrix}0.9333\\4.0167\end{bmatrix},\qquad \Sigma=\begin{bmatrix}0.0133&0.0067\\0.0067&0.0083\end{bmatrix}. The robot position estimate decreases by 6.7 cm and the landmark position estimate increases by 1.7 cm. The robot receives the larger correction because its prior variance is four times that of the landmark. The combined correction is less than 10 cm because the measurement itself is uncertain.

Correlation from a single update

Notice the change in the covariance matrix. Its off-diagonal entries, which were initially zero, are now positive. To interpret these entries, we need a different plot from the previous one. Put the robot position xx on the horizontal axis and the landmark position mm on the vertical axis. A point in this plot specifies an entire configuration of our one-dimensional world. If the sensor were exact, all configurations consistent with the measurement would satisfy m=x+3.1.m=x+3.1. This line is the measurement line. For instance, (0.9,4.0)(0.9,4.0) and (1.0,4.1)(1.0,4.1) both satisfy this equation. We cannot tell them apart using range alone. With a noisy sensor, configurations near the line are more likely than those far from it. Output from cell 4 The blue ellipse describes the prior joint distribution, and the orange ellipse describes the posterior. Both are contours at Mahalanobis distance 2, enclosing about 86.5% of the probability of their respective two-dimensional Gaussians. The gray band indicates configurations within two measurement standard deviations of the observed range; it is not a boundary beyond which configurations are impossible. The orange ellipse tilts toward the measurement line but is not parallel to it. Its long axis makes an angle of about 35∘35^\circ with the xx axis, while the line makes 45∘45^\circ. The measurement constrains only the difference m−xm-x, so it narrows the ellipse across the line and leaves it long along the line. The prior pulls the long axis toward the xx axis, because the robot’s prior variance of 0.04 m20.04\,\mathrm{m}^2 is four times the landmark’s. If the robot is farther to the right than we estimate, the landmark is likely to be farther to the right as well. This is what the positive covariance Σxm=0.006667 m2\Sigma_{xm}=0.006667\,\mathrm{m}^2 tells us. The correlation coefficient is about 0.63. We can also explain the result directly from Bayes’ rule. The independent prior factors into p(x)p(m)p(x)p(m), so p(x,m∣z)∝p(z∣x,m)p(x)p(m).p(x,m\mid z)\propto p(z\mid x,m)p(x)p(m). The likelihood depends on the difference m−xm-x. Multiplying by this likelihood couples the two variables, and in this example the posterior no longer factors into separate robot and landmark distributions. Suppose the measured range had been 3.0 m instead of 3.1 m. The innovation would have been zero and the two means would have stayed where they were. Nevertheless, the covariance matrix would have changed by exactly the same amount: in this linear model, the covariance update depends on HH, RR and the prior covariance, not on the value of zz. The measurement creates correlation even when there is no correction to the mean.

What did the measurement tell us about the map?

The quantity measured by the sensor was the relative position m−xm-x. Before the update its variance was Var⁡(m−x)=0.04+0.01=0.05 m2.\operatorname{Var}(m-x)=0.04+0.01=0.05\,\mathrm{m}^2. After the update we must account for the cross-covariance: Var⁡(m−x)=Σmm+Σxx−2Σxm=0.00833 m2.\operatorname{Var}(m-x)=\Sigma_{mm}+\Sigma_{xx}-2\Sigma_{xm} =0.00833\,\mathrm{m}^2. The standard deviation of the relative position has gone from 22.4 cm to 9.1 cm. Compare this with the uncertainty in the individual positions:
Write each position as its mean plus an error, x=μx+exx = \mu_x + e_x and m=μm+emm = \mu_m + e_m. The error of m−xm - x is then em−exe_m - e_x, and its variance is the expected square of that error:Var⁡(m−x)=E[(em−ex)2]=E[em2]+E[ex2]−2 E[emex]=Σmm+Σxx−2Σxm.\begin{aligned} \operatorname{Var}(m-x) &= \mathbb E\big[(e_m - e_x)^2\big] \\ &= \mathbb E[e_m^2] + \mathbb E[e_x^2] - 2\,\mathbb E[e_m e_x] \\ &= \Sigma_{mm} + \Sigma_{xx} - 2\Sigma_{xm}. \end{aligned}The last term is the cross-covariance, the off-diagonal entry of Σ\Sigma. Before the update the two estimates are independent, so Σxm=0\Sigma_{xm} = 0 and the two variances simply add: 0.01+0.04=0.05 m20.01 + 0.04 = 0.05\ \mathrm{m}^2, a standard deviation of 22.4 cm.After the update, with Σxx=0.0133\Sigma_{xx} = 0.0133, Σmm=0.0083\Sigma_{mm} = 0.0083 and Σxm=0.0067\Sigma_{xm} = 0.0067,Var⁡(m−x)=0.0083+0.0133−2(0.0067)=0.0083 m2,\operatorname{Var}(m-x) = 0.0083 + 0.0133 - 2(0.0067) = 0.0083\ \mathrm{m}^2,a standard deviation of 9.1 cm. The positive covariance enters with a minus sign. It means the errors exe_x and eme_m tend to have the same sign: if the robot is estimated too far to the right, the landmark probably is too. In the difference em−exe_m - e_x the two errors largely cancel, so the gap between robot and landmark is known much better than either position.The same number follows without Σ\Sigma. Before the update the relative position is known with variance 0.05. The sensor measures that same quantity with variance R=0.01R = 0.01. Combining two independent estimates of one quantity adds their inverse variances:Var⁡(m−x)=(10.05+10.01)−1=120+100=0.0083 m2.\operatorname{Var}(m-x) = \left(\frac{1}{0.05} + \frac{1}{0.01}\right)^{-1} = \frac{1}{20 + 100} = 0.0083\ \mathrm{m}^2.This is the one-dimensional Kalman update applied to m−xm - x directly. The result, 9.1 cm, is slightly better than the sensor’s 10 cm, because the prior adds a little information.In matrix form, Var⁡(m−x)=w⊤Σw\operatorname{Var}(m-x) = w^\top \Sigma w with w=(−1,1)⊤w = (-1, 1)^\top, which is HΣH⊤H \Sigma H^\top. For the prior, HΣˉH⊤=0.05H \bar\Sigma H^\top = 0.05 is the term that appears in the innovation variance S=HΣˉH⊤+R=0.06S = H \bar\Sigma H^\top + R = 0.06.
This distinction will matter when we have many landmarks. We may know the distances between landmarks quite accurately even when their coordinates in the world frame remain uncertain.

The EKF SLAM state

We now return to a planar robot. Let st=(xt,yt,θt)⊤s_t=(x_t,y_t,\theta_t)^\top be its pose and let mk=(mk,x,mk,y)⊤m_k=(m_{k,x},m_{k,y})^\top be the coordinates of landmark kk. With NN landmarks in the map, the state is yt=[stm]=(xt,yt,θt,m1,x,m1,y,…,mN,x,mN,y)⊤.y_t=\begin{bmatrix}s_t\\m\end{bmatrix} =(x_t,y_t,\theta_t,m_{1,x},m_{1,y},\ldots,m_{N,x},m_{N,y})^\top. As in EKF localization, the filter approximates its belief by a Gaussian, bel(yt)=N(μt,Σt)\mathtt{bel}(y_t)=\mathcal N(\mu_t,\Sigma_t). The difference is that the Gaussian now describes a (3+2N)(3+2N)-dimensional state. Thrun, Burgard and Fox also store a signature for each landmark (equation 10.7); we omit that component because the landmarks in our example are indistinguishable by appearance. It is useful to partition the covariance according to the pose and map: Σt=[ΣssΣsmΣmsΣmm].\Sigma_t=\begin{bmatrix} \Sigma_{ss}&\Sigma_{sm}\\ \Sigma_{ms}&\Sigma_{mm} \end{bmatrix}. The 3×33\times3 block Σss\Sigma_{ss} describes pose uncertainty. The 2N×2N2N\times2N block Σmm\Sigma_{mm} includes the uncertainty of individual landmarks as well as their mutual cross-covariances. The rectangular block Σsm\Sigma_{sm} describes how the pose estimate is correlated with each of the landmark estimates. By symmetry, Σms=Σsm⊤\Sigma_{ms}=\Sigma_{sm}^\top. The small example above explains why we cannot simply discard the off-diagonal blocks. They describe dependencies produced by past measurements. In a SLAM map, these dependencies also arise when several landmark positions are inferred from an uncertain pose.

Prediction

Suppose the robot executes an action ata_t. We already know from EKF localization how to predict the new pose using the motion model gg. What should happen to the map? The landmarks are stationary, so their estimated coordinates do not change during prediction. μˉt=[g(at,μt−1,s)μt−1,m].\bar\mu_t=\begin{bmatrix}g(a_t,\mu_{t-1,s})\\\mu_{t-1,m}\end{bmatrix}. Let GtG_t and VtV_t be the Jacobians of the motion model with respect to the pose and the wheel speeds, respectively, and let MtM_t be the wheel-speed noise covariance. The pose process noise is Qt=VtMtVt⊤Q_t=V_tM_tV_t^\top. Assuming independent process noise and a static map, the augmented prediction is Σˉt=[Gt00I]Σt−1[Gt00I]⊤+[Qt000].\bar\Sigma_t=\begin{bmatrix}G_t&0\\0&I\end{bmatrix} \Sigma_{t-1}\begin{bmatrix}G_t&0\\0&I\end{bmatrix}^\top +\begin{bmatrix}Q_t&0\\0&0\end{bmatrix}. Multiplying out the blocks gives Σˉss=GtΣssGt⊤+Qt,Σˉsm=GtΣsm,Σˉmm=Σmm.\begin{aligned} \bar\Sigma_{ss}&=G_t\Sigma_{ss}G_t^\top+Q_t,\\ \bar\Sigma_{sm}&=G_t\Sigma_{sm},\\ \bar\Sigma_{mm}&=\Sigma_{mm}. \end{aligned} Here the blocks on the right belong to time t−1t-1. The map’s marginal distribution remains unchanged, but its relationship to the robot’s new pose generally changes. This is why propagating only the 3×33\times3 pose covariance is insufficient.

Measurements of known landmarks

Suppose the robot detects landmark kk and that we know which landmark produced the measurement. As in localization, the measurement consists of a range and bearing: h(s,mk)=[(mk,x−x)2+(mk,y−y)2atan2⁡(mk,y−y,mk,x−x)−θ].h(s,m_k)=\begin{bmatrix} \sqrt{(m_{k,x}-x)^2+(m_{k,y}-y)^2}\\ \operatorname{atan2}(m_{k,y}-y,m_{k,x}-x)-\theta \end{bmatrix}. In localization, the landmark coordinates were constants when we differentiated hh. Now both ss and mkm_k belong to the state. The full measurement Jacobian has two potentially nonzero blocks: Htk=[Hs02×2(k−1)Hmk02×2(N−k)].H_t^k=\begin{bmatrix}H_s&0_{2\times 2(k-1)}&H_{m_k}&0_{2\times 2(N-k)}\end{bmatrix}. The two nonzero blocks are Hs=∂h/∂sH_s=\partial h/\partial s, of size 2×32\times 3, and Hmk=∂h/∂mkH_{m_k}=\partial h/\partial m_k, of size 2×22\times 2. Both are evaluated at the predicted mean. Here 0a×b0_{a\times b} is a block of zeros with aa rows and bb columns. The first zero block covers landmarks 11 to k−1k-1 and the second covers landmarks k+1k+1 to NN. These columns are zero because a measurement of landmark kk does not depend on where the other landmarks are. Write z^tk=h(μˉt,s,μˉt,mk)\hat z_t^k=h(\bar\mu_{t,s},\bar\mu_{t,m_k}) and νt=zt−z^tk\nu_t=z_t-\hat z_t^k, wrapping the bearing component of the innovation to [−π,π)[-\pi,\pi). For example, bearings just below π\pi and just above −π-\pi are close to one another, not almost a full revolution apart. The EKF correction is Ψk=HtkΣˉtHtk⊤+Rt,Kt=ΣˉtHtk⊤Ψk−1,μt=μˉt+Ktνt,Σt=Σˉt−KtΨkKt⊤.\begin{aligned} \Psi_k&=H_t^k\bar\Sigma_tH_t^{k\top}+R_t,\\ K_t&=\bar\Sigma_tH_t^{k\top}\Psi_k^{-1},\\ \mu_t&=\bar\mu_t+K_t\nu_t,\\ \Sigma_t&=\bar\Sigma_t-K_t\Psi_kK_t^\top. \end{aligned} The first two equations are familiar from localization. Look more closely at the rows of KtK_t corresponding to a different landmark jj. Write Σˉmj,s\bar\Sigma_{m_j,s} for the block of Σˉt\bar\Sigma_t with the rows of mjm_j and the columns of ss, and Σˉmj,mk\bar\Sigma_{m_j,m_k} for the block with the rows of mjm_j and the columns of mkm_k. Since HtkH_t^k is nonzero only in the columns of ss and mkm_k, these rows are Kmj=(Σˉmj,sHs⊤+Σˉmj,mkHmk⊤)Ψk−1.K_{m_j}=(\bar\Sigma_{m_j,s}H_s^\top+\bar\Sigma_{m_j,m_k}H_{m_k}^\top)\Psi_k^{-1}. Although the measurement function does not depend on mjm_j, this gain need not be zero. If it is nonzero, an observation of landmark kk can change our estimate of landmark jj through the correction KmjνtK_{m_j}\nu_t. We do not need to observe jj again in order to refine its position. Think of walking through a town without GPS. You count your steps and mark each shop on a paper map where you think it is. Your position error grows as you walk, and each shop gets the error you had when you marked it. After an hour you return to your hotel and find that your map puts you 200 m east of it. You correct your own position, and you also move the shops you marked late in the walk west, because they carry the same error. You never saw those shops again. The cross-covariances Σˉmj,s\bar\Sigma_{m_j,s} and Σˉmj,mk\bar\Sigma_{m_j,m_k} hold this shared error. When the robot returns to a previously visited area, observations of familiar landmarks can therefore correct portions of the map that are no longer visible. This is the role of loop closure in EKF SLAM. The correction depends on the cross-covariances and on the quality of the association; it is not guaranteed to improve every estimate in a particular realization.

Unknown correspondences

In practice, the sensor reports a range and bearing but may not identify the landmark. If several landmarks look alike, we must first decide which entry in the map corresponds to the observed landmark. We may also be seeing a landmark for the first time. For an observation ztiz_t^i and a candidate landmark kk, define the squared Mahalanobis distance Dik2=(zti−z^tk)⊤Ψk−1(zti−z^tk),D_{ik}^2=(z_t^i-\hat z_t^k)^\top\Psi_k^{-1}(z_t^i-\hat z_t^k), again wrapping the bearing residual. In the correspondence estimation section, we derived the maximum likelihood criterion Dik2+ln⁡det⁡ΨkD_{ik}^2+\ln\det\Psi_k. The EKF SLAM algorithm in Table 10.2 of Thrun, Burgard and Fox uses just Dik2D_{ik}^2. The criteria agree when the innovation covariances have the same determinant, and may give different associations otherwise. The algorithm uses a threshold α\alpha to decide whether any existing landmark is a plausible match. If the smallest distance exceeds α\alpha, the implementation initializes a new landmark. For approximately Gaussian, correctly associated innovations, Dik2D_{ik}^2 is approximately chi-squared with two degrees of freedom. The implementation uses α=13.8\alpha=13.8, close to the 99.9th percentile. Thus a correct association would fail this gate about once in a thousand measurements under the ideal model. This does not mean that a newly initialized landmark has a 99.9% probability of being real, or that a false association is equally unlikely.

Adding a landmark

Suppose the measurement comes from a new landmark. Given a range rr and bearing ϕ\phi, we can estimate its position from the current pose by inverting the observation model: m^N+1=[μˉt,x+rcos⁡(ϕ+μˉt,θ)μˉt,y+rsin⁡(ϕ+μˉt,θ)].\hat m_{N+1}=\begin{bmatrix} \bar\mu_{t,x}+r\cos(\phi+\bar\mu_{t,\theta})\\ \bar\mu_{t,y}+r\sin(\phi+\bar\mu_{t,\theta}) \end{bmatrix}. This construction requires both range and bearing. A monocular bearing-only measurement does not determine depth from a single view; triangulation requires another view with sufficient parallax (or additional information). We return to this in visual SLAM. The new landmark is uncertain for two reasons: the robot pose is uncertain, and the range and bearing are noisy. Both must enter its covariance. Let GsG_s and GzG_z be the Jacobians of the placement function above with respect to the pose and measurement. Using first-order uncertainty propagation, assuming the measurement noise is independent of the existing state, we obtain C=GsΣˉs,⋅,P=GsΣˉssGs⊤+GzRtGz⊤.\begin{aligned} C&=G_s\bar\Sigma_{s,\cdot},\\ P&=G_s\bar\Sigma_{ss}G_s^\top+G_zR_tG_z^\top. \end{aligned} Here Σˉs,⋅\bar\Sigma_{s,\cdot} consists of the three pose rows of the existing covariance. The block CC therefore includes the new landmark’s cross-covariance with every existing state component. Append the two new coordinates to the mean and augment the covariance: Σaug=[ΣˉC⊤CP].\Sigma_{\mathrm{aug}}=\begin{bmatrix}\bar\Sigma&C^\top\\C&P\end{bmatrix}. This is the inverse-measurement initialization used in the implementation, following Corke (Corke, 2023). Thrun, Burgard and Fox instead start each new landmark from a diffuse prior. For subsequent observations in the same time step, use the current estimate and covariance in place of the predicted quantities above. The first observation of an otherwise unconstrained new landmark cannot tell us where the robot is in the world. Any shift in the robot pose can be accommodated by placing the landmark accordingly. We use this measurement to initialize the landmark; we must not then reuse the same measurement as an independent correction. A later observation provides a constraint between the new robot pose and the stored landmark estimate.

Limitations of EKF SLAM

The preceding discussion assumes correct landmark associations, stationary landmarks, and a sufficiently accurate linearization. Each assumption has practical consequences. The pose and map are defined relative to a reference frame. Without an external position or heading reference, translating or rotating the entire trajectory and map together leaves the relative measurements unchanged. We usually choose the initial pose to define the world frame. Loop closure improves the internally estimated geometry, but does not supply an absolute geographical reference. The EKF approximates a nonlinear posterior by a Gaussian. Large uncertainty, particularly in heading, can make the local linearization inaccurate. Repeated linearization can also make the estimator overconfident in directions that are not observable (Huang et al., 2010). A small reported covariance is not by itself evidence of a correct map. A wrong correspondence can cause a large correction to the pose and map. The nearest-neighbor method described here does not revisit earlier association decisions. Practical systems may maintain provisional landmarks, reject ambiguous observations, or remove landmarks that repeatedly fail to be detected. The simple implementation does not use missed detections as measurements. That is a limitation of its observation model, not a general prohibition on incorporating negative information into probabilistic estimation. Finally, with NN landmarks the covariance has (3+2N)2(3+2N)^2 entries. A single measurement has a two-dimensional innovation, so the correction KtΨkKt⊤K_t\Psi_kK_t^\top can be computed as a rank-at-most-two update in O(N2)O(N^2) time. Computing (I−KtHt)Σˉt(I-K_tH_t)\bar\Sigma_t as a general dense matrix product instead can cost O(N3)O(N^3). A full search through candidate landmarks also adds work. The implementation uses only eight landmarks, so clarity of the code is more important than exploiting every available sparsity. For larger maps, methods such as FastSLAM, submaps, and sparse information filters avoid some of the costs of the dense joint covariance. Graph-based SLAM takes a different approach, retaining pose and measurement constraints and exploiting the sparsity of the resulting optimization problem. See Thrun, Burgard and Fox, Chapters 12 and 13, for the former approaches.

From theory to implementation

In the next section, we will run EKF SLAM on a TurtleBot in a room whose landmarks are eight identical pillars. The LIDAR and depth camera provide different ways of obtaining range-bearing observations, but the prediction, correction, and landmark initialization equations are the same. As you examine the resulting maps, pay particular attention to what happens when a landmark is observed again after a period of motion.

Notation reference

Lectures

Key references: (Thrun et al., 2005; Corke, 2023; Dissanayake et al., 2001; Huang et al., 2010)

References

  • Corke, P. (2023). Robotics, vision and control: Fundamental algorithms in python. DOI
  • Dissanayake, M., Newman, P., Clark, S., Durrant-Whyte, H., Csorba, M. (2001). A solution to the simultaneous localization and map building (SLAM) problem. IEEE Trans. Rob. Autom. DOI
  • Huang, G., Mourikis, A., Roumeliotis, S. (2010). Observability-based rules for designing consistent EKF SLAM estimators. Int. J. Rob. Res. DOI
  • Thrun, S., Burgard, W., Fox, D. (2005). Probabilistic Robotics. Link

Found an error on this page? Report an issue.