Skip to main content
Open In Colab The localization of the robot is the Bayes filter estimator of its pose when a map mm of its environment is provided. This section treats the EKF approximation to the localization problem.

Localization with a known map

Graphical model for localization: each state depends on the previous state, the current action and the map, and each measurement depends on the current state, its correspondence and the map. Graphical model for localization of a mobile agent with known correspondences. Shaded nodes are observed: the actions aa, the measurements zz, the correspondences cc and the known map mm. The dashed edges from the map to the states belong to a map-based motion model, which the EKF below leaves out. Maps take several forms. An indoor robot can use a floor plan or an occupancy grid. The EKF in this section uses a landmark map, a list of point landmarks with known positions. A car uses a lane-level road map that records the lane markings, traffic signs and other landmarks along each road. In the end we describe how such maps are built. Localization problems are non trivial as they suffer from ambiguities as shown in the figure below. Floor plan of a corridor with offices above and below whose layout repeats every 6 meters. The belief over the robot's position has two equal peaks, one at the true position and one 6 meters further along. Belief over the robot’s position after one range scan in a corridor whose layout repeats every 6 m (darker is more probable). The scan from the true position (square) matches equally well 6 m further along, so the belief has two peaks. The Bayes filter for localization is almost identical to the Bayes filter we have seen earlier but we now also condition on the map. One step of the filter takes the previous belief bel(st−1)\mathtt{bel}(s_{t-1}), the action ata_t, the measurement ztz_t and the map mm, and returns the new belief bel(st)\mathtt{bel}(s_t): 1:for all st do2:bel^(st)=∫p(st∣at,st−1,m) bel(st−1) dst−1prediction3:bel(st)=η p(zt∣st,m) bel^(st)measurement update4:end for5:return bel(st)\begin{array}{lll} 1: & \textbf{for all } s_t \textbf{ do} & \\ 2: & \qquad \widehat{\mathtt{bel}}(s_t) = \int p(s_t \mid a_t, s_{t-1}, m)\, \mathtt{bel}(s_{t-1})\, ds_{t-1} & \text{prediction} \\ 3: & \qquad \mathtt{bel}(s_t) = \eta\, p(z_t \mid s_t, m)\, \widehat{\mathtt{bel}}(s_t) & \text{measurement update} \\ 4: & \textbf{end for} & \\ 5: & \textbf{return } \mathtt{bel}(s_t) & \end{array} Table 7.1 of Probabilistic Robotics. In the measurement update, the map determines the measurement the robot should observe from each pose. In the prediction, the map makes some poses impossible: the robot cannot end up inside a wall. Section 5.5 of the book approximates this map-based motion model for short steps by multiplying the map-free model by the consistency of the pose with the map, p(st∣at,st−1,m)=η p(st∣at,st−1) p(st∣m),p(s_t \mid a_t, s_{t-1}, m) = \eta\, p(s_t \mid a_t, s_{t-1})\, p(s_t \mid m), where p(st∣m)p(s_t \mid m) is zero for poses that collide with an obstacle and constant elsewhere. The EKF below uses the map-free velocity model gg, which amounts to assuming p(st∣at,st−1,m)=p(st∣at,st−1)p(s_t \mid a_t, s_{t-1}, m) = p(s_t \mid a_t, s_{t-1}). That is reasonable for a robot driving in open space among point landmarks. A Gaussian could not represent a belief cut off by a wall anyway.

EKF localization with known correspondences

EKF localization tracks the pose of a ground robot such as the TurtleBot, st=(x,y,θ)s_t = (x, y, \theta), with the extended Kalman filter. Its belief is a single Gaussian, which has one peak, so the filter has to start from a rough estimate of where the robot is. It cannot express a belief like “the robot is in one of these two places”, which is what you get when the starting pose is unknown. The particle filter can represent such a belief.

Landmarks, observations and signatures

There are two fundamentally different ways to construct a measurement model from sensor data. A beam-based measurement model, operates directly on the ranges returned by a LIDAR. Given a robot pose and an occupancy map, it predicts each range by ray casting and evaluates how well the predicted scan agrees with the observed scan. This approach is not naturally suited to the EKF. A small change in the robot pose can cause a beam to intersect a different obstacle, producing an abrupt change in the predicted range. Consequently, a local linear approximation of the measurement function may be unreliable. Furthermore, the beam measurement model accounts for several types of measurement errors, resulting in a likelihood that is not Gaussian. An alternative is a landmark detector, which Probabilistic Robotics (Section 6.6) calls a feature extractor. It reduces each raw measurement to a short list of landmark observations, such as the range and bearing of each identifiable landmark in view. Once an observation is associated with a known landmark, its expected range and bearing are differentiable functions of the pose, and their errors are close to Gaussian, which is what the EKF needs. This section takes that approach. Scan-based localization usually runs on a particle filter, as Nav2’s AMCL does. Five terms describe the landmarks and the robot’s observations of them:
  • A landmark is a distinct object in the world that the robot uses for navigation, such as a pole, a door post or a chair.
  • The map m={m1,…,mN}m = \{m_1, \dots, m_N\} lists the known landmarks, indexed by jj. Landmark jj has a position (mj,x,mj,y)(m_{j,x}, m_{j,y}) in the map frame and a signature κj\kappa_j. Such a landmark map, which Thrun, Burgard and Fox call a feature-based map, records where the landmarks are and what they look like, and nothing about the space between them.
  • A signature κ\kappa describes how a landmark looks. Thrun, Burgard and Fox allow a number such as an average color, an integer for the landmark’s type, or a vector such as height and color (Probabilistic Robotics, Section 6.6).
  • A landmark observation is one perceptive detection of a landmark at one time step, in the robot’s frame: oti=(zti,κti)o^i_t = (z^i_t, \kappa^i_t). Its geometric measurement zti=(rti,ϕti)z^i_t = (r^i_t, \phi^i_t) is the range and bearing of the landmark, and κti\kappa^i_t is its observed signature. The index ii counts the observations of step tt. The detector turns the raw measurement ztz_t, a whole scan or image, into the list o(zt)={ot1,ot2,… }o(z_t) = \{o^1_t, o^2_t, \dots\}. A superscript ii therefore marks the range and bearing of one observation, while ztz_t without it is the raw measurement of the Bayes filter. Landmarks stay in the map, while observations are produced anew at every step, and their number changes as landmarks come into and out of view.
  • The correspondence ct(i)=jc_t(i) = j says that observation ii came from landmark jj: ii counts the observations of one step, and jj counts the landmarks of the map. Thrun, Burgard and Fox also allow ct(i)=N+1c_t(i) = N + 1 for an observation that matches no landmark, such as a spurious detection. Nothing in the definition stops two observations from having the same landmark. For observations from one camera image that assignment is wrong, since two image regions are two places in the world, and data association usually adds this constraint, called mutual exclusion (Probabilistic Robotics, Section 7.8).
Thrun, Burgard and Fox call the whole triple (rti,ϕti,κti)(r^i_t, \phi^i_t, \kappa^i_t) a feature and write it ftif^i_t (Probabilistic Robotics, Section 6.6). In computer vision, a feature is a vector that a network computes from an image, such as the appearance embedding in the example below. We stick to the term landmark observation for the triplet. The probabilistic model is the same as in the book.

Example: detected objects as landmark observations

An office map has four landmarks, each specified by the position of its center and a signature κj=(ℓj,ej)\kappa_j = (\ell_j, e_j) with two parts. The class label ℓj\ell_j names the object class. The appearance embedding eje_j is a vector of a few hundred numbers that a CNN computes from an image of the object. Embeddings of two views of the same object point in nearly the same direction. Embeddings of two different objects of the same class point in different directions only if the network was trained to tell individual objects apart, not just classes. The robot stands at the origin, facing along the xx axis. Its RGB-D camera sees 35°35° to either side, so only the gray office chair (m2m_2) and the trash can (m3m_3) are in view. The fire extinguisher, at a bearing of 78.7°78.7°, and the armchair, at 116.6°116.6°, produce no observations. An object detector draws a box around each object it recognizes, and the depth pixels inside a box give the centroid of the visible part of the object. The centroid’s range and bearing, measured counterclockwise from the heading so that positive is to the left, form the geometric measurement ztiz^i_t of an observation. The class label ℓti\ell^i_t and the embedding etie^i_t of the image inside the box form its signature κti=(ℓti,eti)\kappa^i_t = (\ell^i_t, e^i_t). The detector reports the trash can first and the chair second:
Top view of the office map: walls, a fire extinguisher by the top wall, a gray office chair, a dark trash can and a red armchair at their numbered map positions, the robot at the origin facing along x, the camera's field of view as a wedge covering the chair and the trash can, and two crosses at the measured range and bearing of observations 1 and 2. The office map from above, with each landmark at its numbered map position (gray cross). The robot (pink) faces along xx, and the blue wedge is its camera’s field of view. Gold crosses mark the two observations at their measured range and bearing.
Data association
Data association finds ct(i)c_t(i) for each observation. The first observation is a trash can, and the map has only one, so ct(1)=3c_t(1) = 3: the order in which the detector reports the observations says nothing about the order of the map. The second observation is a chair, and the map has two. The class label cannot decide between them, so we compare the observed embedding et2e^2_t with the stored ones. Say their cosine similarities are sim⁡(et2,e2)=0.91,sim⁡(et2,e4)=0.34,\operatorname{sim}(e^2_t, e_2) = 0.91, \qquad \operatorname{sim}(e^2_t, e_4) = 0.34, so ct(2)=2c_t(2) = 2. The predicted range and bearing can also decide a correspondence, but only when the pose is already well known. If the two chairs were of the same model, their embeddings would be nearly equal, and only the predicted range and bearing could tell them apart. Localization solves the reverse problem. It estimates the pose from which the associated landmarks would appear at the measured ranges and bearings. From (1,0)(1, 0), for example, the chair would appear at 1.41 m and 45°45°, far from the measured 2.27 m and 27.5°27.5°, so that pose is unlikely.

Probabilistic measurement model

With the robot at st=(x,y,θ)s_t = (x, y, \theta) and a known correspondence ct(i)=jc_t(i) = j, the range and bearing1 that landmark jj would produce without noise are h(st,j,m)=[(mj,x−x)2+(mj,y−y)2atan2⁡(mj,y−y,  mj,x−x)−θ].h(s_t, j, m) = \begin{bmatrix} \sqrt{(m_{j,x} - x)^2 + (m_{j,y} - y)^2} \\ \operatorname{atan2}(m_{j,y} - y,\; m_{j,x} - x) - \theta \end{bmatrix}. The measured range and bearing are these values plus noise: zti=h(st,j,m)+εti,εti∼N(0,Rt),Rt=[σr200σϕ2].z^i_t = h(s_t, j, m) + \varepsilon^i_t, \qquad \varepsilon^i_t \sim \mathcal{N}(0, R_t), \qquad R_t = \begin{bmatrix} \sigma_r^2 & 0 \\ 0 & \sigma_\phi^2 \end{bmatrix}. This is the probabilistic model of linear regression with a nonlinear mean: hh gives the conditional mean of the measurement given the pose, and RtR_t gives its covariance. The range and bearing noises are taken as independent, so RtR_t is diagonal. The bearing error is wrapped to [−π,π)[-\pi, \pi), since bearings of 179°179° and −179°-179° are only 2°2° apart. A Gaussian on the wrapped bearing error is a local approximation to a distribution on the circle, and it is accurate when σϕ\sigma_\phi is a few degrees. For a real sensor, σr\sigma_r and σϕ\sigma_\phi are estimated from calibration data, by measuring landmarks at known positions. An observation also carries the signature κti\kappa^i_t, so its full likelihood has a second factor, p(κti∣κj)p(\kappa^i_t \mid \kappa_j), which scores how well the observed signature matches the one stored for landmark jj. The model assumes that this appearance factor is independent of the geometric error and of the pose. The second assumption is an approximation, since a real image of an object, and its embedding, change with the viewpoint. Under it, and with a known correspondence, the appearance factor is constant in the pose and cancels when the posterior is normalized, so the EKF uses only N(zti; h(st,j,m), Rt)\mathcal{N}\big(z^i_t;\, h(s_t, j, m),\, R_t\big). The appearance factor is what finds the correspondence, as in the example. Correspondence estimation defines it for a class label and an embedding and adds it to the score of each candidate landmark. Given the correspondences, the errors of different observations are taken as independent. The likelihood of all the observations of a step is then the product of their likelihoods, and the filter can process the observations one at a time. The product takes the number of observations as given. It does not model which landmarks the detector finds and which it misses.
This model assumes a sensor that detects every landmark in view and whose only error is zero-mean noise on the measured position. A detector also misses objects, reports objects that are not there and confuses classes. Its centroid lies on the visible surface of the object, closer to the camera than the object’s center. That offset depends on the size of the object and the viewing angle, so it is a bias, which zero-mean noise does not describe. The confusion matrix covers class confusion. The filter handles the other errors outside the model. A missed object gives no observation, the validation gate of line 7 below rejects observations far from their prediction, and correspondence estimation assigns observations that carry no ID.The filter is the same for every sensor. What changes is RtR_t, which landmarks are visible, and whether the signature gives the correspondence. A camera that reads fiducial markers gets each landmark’s ID, and with it ct(i)c_t(i). A 2D LIDAR such as the TurtleBot3’s LDS-01 measures range and bearing more precisely than a depth camera, but it reports no class label or embedding, so landmarks extracted from its scan need correspondence estimation. A dark object that absorbs the laser returns nothing at all. The Perception overview lists what each sensor supplies.The landmark positions are treated as exact. If each landmark’s position error is independent of the others, its covariance can be propagated through hh and added to RtR_t. When the landmark positions are estimated together with the pose, the problem becomes SLAM.

Linearization of the measurement model

The EKF update needs hh linearized at the predicted mean μˉt\bar\mu_t. With the landmark jj and the map mm fixed, hh maps the 3-dimensional pose to the 2-dimensional geometric measurement, R3→R2\mathbb R^3 \to \mathbb R^2. Write δ=(mj,x−x,  mj,y−y)\delta = (m_{j,x} - x,\; m_{j,y} - y) and q=δ⊤δq = \delta^\top\delta. Differentiating the two rows of hh with respect to (x,y,θ)(x, y, \theta) gives the 2×32 \times 3 Jacobian Hti=1q[−q δx−q δy0δy−δx−q].H^i_t = \frac{1}{q}\begin{bmatrix} -\sqrt{q}\,\delta_x & -\sqrt{q}\,\delta_y & 0 \\ \delta_y & -\delta_x & -q \end{bmatrix}. Each row says how one measurement changes for a small change of the pose. The zero in the range row says that turning in place does not change the distance to a landmark. The last entry of the bearing row is −1-1: the bearing falls by one radian for each radian the robot turns counterclockwise. Both hh and HtiH^i_t are undefined when the robot stands on the landmark, q=0q = 0, and the bearing row grows as 1/q1/\sqrt q close to it. A real system rejects observations with a very small predicted range. With HtiH^i_t of size 2×32 \times 3, RtR_t and StiS^i_t are 2×22 \times 2 and the gain KtiK^i_t is 3×23 \times 2, as in the size table of the EKF section.
Table 7.2 of Probabilistic Robotics takes a scalar numerical signature with Gaussian noise. It predicts all three parts of the observation and linearizes them at the predicted mean μˉt\bar\mu_t:h(st,j,m)=[qatan2⁡(δy,δx)−θκj],Hti=1q[−q δx−q δy0δy−δx−q000],h(s_t, j, m) = \begin{bmatrix} \sqrt{q} \\ \operatorname{atan2}(\delta_y, \delta_x) - \theta \\ \kappa_j \end{bmatrix}, \qquad H^i_t = \frac{1}{q}\begin{bmatrix} -\sqrt{q}\,\delta_x & -\sqrt{q}\,\delta_y & 0 \\ \delta_y & -\delta_x & -q \\ 0 & 0 & 0 \end{bmatrix},with measurement noise covariance Rt=diag⁡(σr2,σϕ2,σκ2)R_t = \operatorname{diag}(\sigma_r^2, \sigma_\phi^2, \sigma_\kappa^2). The last row of HtiH^i_t is zero because the signature of a landmark does not depend on where the robot stands.The zero row means the signature cannot move the estimate. Since RtR_t is diagonal, the innovation covariance Sti=HtiΣˉtHti⊤+RtS^i_t = H^i_t \bar\Sigma_t H^{i\top}_t + R_t is block diagonal, with the 2×22 \times 2 range-bearing block in the top left and σκ2\sigma_\kappa^2 in the bottom right. The third column of the gain Kti=ΣˉtHti⊤(Sti)−1K^i_t = \bar\Sigma_t H^{i\top}_t (S^i_t)^{-1} is then zero. In line 9 of the algorithm below, the signature innovation κti−κj\kappa^i_t - \kappa_j is multiplied by that zero column. In line 10, the term KtiRtKti⊤K^i_t R_t K^{i\top}_t picks up σκ2\sigma_\kappa^2 only through the same zero column, and KtiHtiK^i_t H^i_t uses only the first two rows of HtiH^i_t. Deleting the third row of hh, HtiH^i_t and RtR_t therefore gives exactly the same mean and covariance. The one place the two forms differ is the gate of line 7: with the third row, the squared Mahalanobis distance gains the term (κti−κj)2/σκ2(\kappa^i_t - \kappa_j)^2 / \sigma_\kappa^2 and has 3 degrees of freedom instead of 2.The label and embedding of the example have no such row, because there is no difference κti−κj\kappa^i_t - \kappa_j to linearize, so for them the gate keeps 2 degrees of freedom. The code uses the two-row form above.

Motion model

The action is the pair of wheel speeds at=(uL,uR)a_t = (u_L, u_R). As derived in nonholonomic robots, they set the forward speed v=r2(uL+uR)v = \frac{r}{2}(u_L + u_R) and the turning rate ω=r2d(uR−uL)\omega = \frac{r}{2d}(u_R - u_L). Holding vv and ω\omega constant for Δt\Delta t is a constant body twist Vb=(ω,v,0)\mathcal{V}_b = (\omega, v, 0). The new pose is then the planar screw motion of motion representations, Tt=Tt−1exp⁡([Vb] Δt)T_t = T_{t-1}\exp([\mathcal{V}_b]\,\Delta t), which moves the robot along an arc of radius v/ωv/\omega. Multiplying out the two matrices and reading off the position and the heading gives the motion model g(at,st−1)=[x−vωsin⁡θ+vωsin⁡(θ+ωΔt)y+vωcos⁡θ−vωcos⁡(θ+ωΔt)θ+ωΔt].g(a_t, s_{t-1}) = \begin{bmatrix} x - \frac{v}{\omega}\sin\theta + \frac{v}{\omega}\sin(\theta + \omega\Delta t) \\ y + \frac{v}{\omega}\cos\theta - \frac{v}{\omega}\cos(\theta + \omega\Delta t) \\ \theta + \omega\Delta t \end{bmatrix}. The noise comes from the wheels. Each wheel turns at its commanded speed plus Gaussian noise that stays constant during the step. MtM_t is the 2×22 \times 2 covariance of that noise, in wheel-speed units of (rad/s)2(\text{rad/s})^2. The two wheels have separate motors and encoders, so their noises are taken as independent and MtM_t is diagonal. The standard deviation of each wheel grows with its commanded speed, on top of a floor: Mt=[σL200σR2],σL=α ∣uL∣+β,σR=α ∣uR∣+β.M_t = \begin{bmatrix} \sigma_L^2 & 0 \\ 0 & \sigma_R^2 \end{bmatrix}, \qquad \sigma_L = \alpha\,|u_L| + \beta, \qquad \sigma_R = \alpha\,|u_R| + \beta. The parameter α\alpha scales the noise with the speed, and β\beta is the noise that remains when a wheel is nearly still. Both depend on the robot and are estimated from driving data. Probabilistic Robotics (Section 5.3) puts the noise on the velocities (v,ω)(v, \omega), with variances that grow with the commanded velocities through four robot-specific parameters α1\alpha_1 to α4\alpha_4. Noise on the wheel speeds has the same diagonal structure, one entry per wheel. The two terms of σ\sigma stand for two kinds of error. The term α ∣u∣\alpha\,|u| covers errors that are a fixed fraction of the motion. If the real wheel radius is 1% smaller than the rr in the model, because the tire is worn or the robot carries a load, each wheel turn covers 1% less ground at any speed. Slip on the floor and the lag of the motor’s speed controller also grow with the speed. These act like a percentage error, so the standard deviation is proportional to ∣u∣|u|: a wheel that turns twice as far during the step has twice the error. The floor β\beta covers errors that do not shrink as the wheel slows down. The encoder counts whole ticks, so it measures the speed only to within about one tick per sampling period. Near zero speed the gears have play and the motor needs a minimum current to overcome static friction, so a wheel commanded to turn slowly may stall and then jerk forward. The floor also keeps MtM_t positive definite when a wheel is commanded to stop. Without it, a robot told to stand still would add no uncertainty to its pose, and the filter would become more confident the longer it waits. With the values of the example below, α=0.05\alpha = 0.05 and β=0.1\beta = 0.1 rad/s, the robot that drives a circle of radius 1.5 m at 0.2 m/s commands uL≈5.5u_L \approx 5.5 and uR≈6.6u_R \approx 6.6 rad/s. Then σL≈0.37\sigma_L \approx 0.37 and σR≈0.43\sigma_R \approx 0.43 rad/s, about 7% of each wheel’s speed, and the floor contributes about a quarter of each. Errors on the two wheels affect the pose in different ways. A common error, both wheels too fast, changes how far the robot goes. A difference between them changes how much it turns. For this robot and a step of Δt=0.5\Delta t = 0.5 s, the wheel noise above gives a distance error of about 5 mm and a heading error of about 1.9∘1.9^\circ per step. The heading error matters more: driving on with a wrong heading turns it into a sideways position error that grows with the distance driven. That is where the banana shape of the predicted poses comes from. Diagonal MtM_t is an approximation. A slippery patch of floor or a low battery slows both wheels at once, which would add an off-diagonal term.

Linearization of the motion model

The prediction step needs gg linearized at the previous mean μt−1\mu_{t-1} and the commanded wheel speeds, once with respect to the state and once with respect to the action. The Jacobian with respect to the state is Gt=[10vω(cos⁡(θ+ωΔt)−cos⁡θ)01vω(sin⁡(θ+ωΔt)−sin⁡θ)001].G_t = \begin{bmatrix} 1 & 0 & \frac{v}{\omega}\left(\cos(\theta + \omega\Delta t) - \cos\theta\right) \\ 0 & 1 & \frac{v}{\omega}\left(\sin(\theta + \omega\Delta t) - \sin\theta\right) \\ 0 & 0 & 1 \end{bmatrix}. It is derived the same way as the simpler model of the overconfident EKF prediction section, one row of gg at a time. Only the heading enters gg nonlinearly, so the third column holds the derivatives of the first two rows with respect to θ\theta, and the rest of GtG_t is the identity. Line 2 of the algorithm uses it to propagate the previous covariance, GtΣt−1Gt⊤G_t \Sigma_{t-1} G_t^\top. The Jacobian with respect to the wheel speeds, Vt=∂g/∂atV_t = \partial g / \partial a_t, maps the wheel noise into the state, so the motion noise covariance is Qt=VtMtVt⊤Q_t = V_t M_t V_t^\top.
The wheel speeds enter gg only through vv and ω\omega, so the chain rule splits VtV_t into two factors:Vt=∂g∂at=∂g∂(v,ω) ∂(v,ω)∂(uL,uR).V_t = \frac{\partial g}{\partial a_t} = \frac{\partial g}{\partial (v, \omega)} \, \frac{\partial (v, \omega)}{\partial (u_L, u_R)} .The second factor is a constant matrix, because vv and ω\omega are linear in the wheel speeds:J=∂(v,ω)∂(uL,uR)=[r2r2−r2dr2d].J = \frac{\partial (v, \omega)}{\partial (u_L, u_R)} = \begin{bmatrix} \dfrac{r}{2} & \dfrac{r}{2} \\[6pt] -\dfrac{r}{2d} & \dfrac{r}{2d} \end{bmatrix}.Each column of JJ belongs to one wheel. An error in one wheel’s speed changes vv by r/2r/2 times that error and ω\omega by ∓r/(2d)\mp r/(2d) times it, so noise on a single wheel both moves the robot forward and turns it.The first factor is the derivative of the arc. Write θ′=θ+ωΔt\theta' = \theta + \omega\Delta t for the heading at the end of the step. Differentiating gg with respect to vv gives∂g∂v=1ω[sin⁡θ′−sin⁡θcos⁡θ−cos⁡θ′0],\frac{\partial g}{\partial v} = \frac{1}{\omega} \begin{bmatrix} \sin\theta' - \sin\theta \\ \cos\theta - \cos\theta' \\ 0 \end{bmatrix},and with respect to ω\omega, where θ′\theta' also depends on ω\omega,∂gx∂ω=vω2(sin⁡θ−sin⁡θ′)+v Δtωcos⁡θ′,∂gy∂ω=vω2(cos⁡θ′−cos⁡θ)+v Δtωsin⁡θ′,∂gθ∂ω=Δt.\begin{aligned} \frac{\partial g_x}{\partial \omega} &= \frac{v}{\omega^2}\left(\sin\theta - \sin\theta'\right) + \frac{v\,\Delta t}{\omega}\cos\theta', \\ \frac{\partial g_y}{\partial \omega} &= \frac{v}{\omega^2}\left(\cos\theta' - \cos\theta\right) + \frac{v\,\Delta t}{\omega}\sin\theta', \\ \frac{\partial g_\theta}{\partial \omega} &= \Delta t . \end{aligned}The two columns form the 3×23 \times 2 matrix ∂g/∂(v,ω)\partial g / \partial (v, \omega), andVt=[∂g∂v∂g∂ω]J,V_t = \begin{bmatrix} \dfrac{\partial g}{\partial v} & \dfrac{\partial g}{\partial \omega} \end{bmatrix} J ,a 3×23 \times 2 matrix evaluated at μt−1\mu_{t-1} and the commanded wheel speeds. With MtM_t of size 2×22 \times 2, the motion noise Qt=VtMtVt⊤Q_t = V_t M_t V_t^\top is 3×33 \times 3, the size of the state covariance. VtV_t already contains Δt\Delta t, so it turns the wheel-speed covariance into a pose covariance directly, and QtQ_t needs no extra factor of Δt\Delta t. For the same reason MtM_t must describe wheel speeds, not wheel angles.When both wheels turn at the same speed, ω=0\omega = 0 and the formulas above divide by zero. Their limit as ω→0\omega \to 0 is the straight drive:∂g∂v=Δt[cos⁡θsin⁡θ0],∂g∂ω=[−12v Δt2sin⁡θ12v Δt2cos⁡θΔt].\frac{\partial g}{\partial v} = \Delta t \begin{bmatrix} \cos\theta \\ \sin\theta \\ 0 \end{bmatrix}, \qquad \frac{\partial g}{\partial \omega} = \begin{bmatrix} -\frac{1}{2} v\,\Delta t^2 \sin\theta \\ \frac{1}{2} v\,\Delta t^2 \cos\theta \\ \Delta t \end{bmatrix}.A turn-rate error during a straight drive moves the robot sideways by an amount that grows with Δt2\Delta t^2, because the heading error it causes grows during the step. The code uses these limits when the turn per step is tiny, ∣ωΔt∣<10−6|\omega\Delta t| < 10^{-6}. Near ω=0\omega = 0 the arc formulas subtract nearly equal numbers and divide by a small ω\omega, which loses precision. The function V in the code below implements both cases.

The algorithm

One step takes the previous belief, the action, the observations and their correspondences, and the map: 1:μˉt=g(at,μt−1)2:Σˉt=Gt Σt−1 Gt⊤+VtMtVt⊤3:for each observation oti=(zti,κti) with j=ct(i):4:z^ti=h(μˉt,j,m),and Hti at μˉt5:Sti=HtiΣˉtHti⊤+Rt6:νti=zti−z^ti,with its bearing wrapped to [−π,π)7:if νti⊤(Sti)−1νti>γ: skip this observation8:Kti=ΣˉtHti⊤(Sti)−19:μˉt←μˉt+Ktiνti10:Σˉt←(I−KtiHti)Σˉt(I−KtiHti)⊤+KtiRtKti⊤11:μt=μˉt,Σt=Σˉt\begin{array}{ll} 1: & \bar\mu_t = g(a_t, \mu_{t-1}) \\ 2: & \bar\Sigma_t = G_t\, \Sigma_{t-1}\, G_t^\top + V_t M_t V_t^\top \\ 3: & \text{for each observation } o^i_t = (z^i_t, \kappa^i_t) \text{ with } j = c_t(i): \\ 4: & \qquad \hat z^i_t = h(\bar\mu_t, j, m), \quad \text{and } H^i_t \text{ at } \bar\mu_t \\ 5: & \qquad S^i_t = H^i_t \bar\Sigma_t H^{i\top}_t + R_t \\ 6: & \qquad \nu^i_t = z^i_t - \hat z^i_t, \quad \text{with its bearing wrapped to } [-\pi, \pi) \\ 7: & \qquad \text{if } \nu^{i\top}_t (S^i_t)^{-1} \nu^i_t > \gamma: \text{ skip this observation} \\ 8: & \qquad K^i_t = \bar\Sigma_t H^{i\top}_t (S^i_t)^{-1} \\ 9: & \qquad \bar\mu_t \leftarrow \bar\mu_t + K^i_t \nu^i_t \\ 10: & \qquad \bar\Sigma_t \leftarrow \left(I - K^i_t H^i_t\right) \bar\Sigma_t \left(I - K^i_t H^i_t\right)^\top + K^i_t R_t K^{i\top}_t \\ 11: & \mu_t = \bar\mu_t, \quad \Sigma_t = \bar\Sigma_t \end{array} Lines 1 and 2 are the prediction step of the EKF, with the motion model and its noise MtM_t. Line 3 loops over the observations of step tt in the order the sensor reported them. Each observation arrives with its landmark j=ct(i)j = c_t(i), so no search over the landmarks is needed. Lines 9 and 10 overwrite μˉt\bar\mu_t and Σˉt\bar\Sigma_t, so each observation starts from the belief the previous one left. When no landmark is in view the loop does not run, and line 11 returns the prediction. Line 4 computes the predicted measurement z^ti\hat z^i_t, the range and bearing the sensor would report from the current μˉt\bar\mu_t if there were no noise, and the Jacobian HtiH^i_t at the same point, so each observation is linearized at the mean the previous one left. For a linear-Gaussian model with independent measurement noises, updating with one observation at a time gives exactly the same result as one update with all of them stacked. For the EKF the two can differ, because a stacked update linearizes once, at the predicted mean, and the sequential result can also depend on the order of the observations. The difference grows with how strongly hh bends over the spread of the belief, and it is not always small. In the office example, say the prediction step puts the robot at the origin facing along the xx axis, μˉt=(0,0,0°)\bar\mu_t = (0, 0, 0°). Observation 1 came from the trash can at (3,−1)(3, -1) m, so line 4 gives z^t1=[32+(−1)2atan2⁡(−1, 3)−0°]=[3.16 m−18.4°].\hat z^1_t = \begin{bmatrix} \sqrt{3^2 + (-1)^2} \\ \operatorname{atan2}(-1,\, 3) - 0° \end{bmatrix} = \begin{bmatrix} 3.16 \text{ m} \\ -18.4° \end{bmatrix}. The code below computes line 4 for both observations, and the figure draws the result.
Top view of the office: walls, a fire extinguisher by the top wall, a gray office chair, a dark trash can and a red armchair drawn at their map positions, the robot at the origin facing along x with the camera's field of view as a wedge, dashed lines with range and bearing labels from the robot to the chair and the trash can, and crosses for the measured positions next to them. The predicted measurements from the predicted pose μˉt=(0,0,0°)\bar\mu_t = (0, 0, 0°). Each dashed line runs to a landmark in view: its length is the range r^ti\hat r^i_t, and its angle from the heading is the bearing ϕ^ti\hat\phi^i_t. Gold crosses mark the measured range and bearing. Each lies about 5 cm from the end of its dashed line, so both innovations are small. The camera reported zt1=(3.12 m,−17.9°)z^1_t = (3.12 \text{ m}, -17.9°), so line 6 gives the innovation νt1=(−0.04 m,0.5°)\nu^1_t = (-0.04 \text{ m}, 0.5°): the trash can is slightly closer and slightly further to the left than the predicted pose implies, and the update moves the pose a little to explain that. The single-step run below shows a large innovation. There the prediction puts the robot at (0,−1)(0, -1) facing −90°-90°, so it expects the landmark at (0,−2)(0, -2) dead ahead, z^=(1 m,0°)\hat z = (1 \text{ m}, 0°), and instead measures it at 47°47° to its right. Line 6 wraps the bearing of the innovation, as in the measurement model, and the heading in the state is wrapped the same way after each update. Otherwise the filter treats (x,y,θ)(x, y, \theta) as ordinary coordinates near its mean, and the wrapping matters only where the heading crosses ±π\pm\pi. Line 7 is a validation gate. The squared Mahalanobis distance ν⊤S−1ν\nu^\top S^{-1} \nu measures how far an observation is from its prediction, in units of the predicted spread. If the models are right it follows a chi-square distribution with 2 degrees of freedom and exceeds γ=9.21\gamma = 9.21 only 1% of the time. An observation can be wrong even when its ID is known. A misdetection, a partly hidden marker or a reflection can produce an observation far from its prediction, and the update would move the estimate toward it. The gate rejects such observations. The examples below run with the gate off, and the last of them shows what the gate does. Line 10 is the Joseph form of the covariance update. For the optimal gain it equals (I−KtiHti)Σˉt(I - K^i_t H^i_t)\bar\Sigma_t. It is written as a sum of two positive semidefinite terms, so rounding errors over many updates cannot make the covariance lose its symmetry or become indefinite. This is the EKF localization algorithm with known correspondences, Table 7.2 of Probabilistic Robotics (Thrun, Burgard and Fox), with ss for the state, wheel speeds for the action, QQ and RR as named in this course, and the gate and the Joseph form added.

One step with one landmark

We revisit the robot from the overconfident EKF prediction example, but now introduce a landmark observation. The robot drives straight for 1 m with a nominal heading of −90∘-90^\circ. Its initial position uncertainty is 2 cm and its heading uncertainty is 30∘30^\circ. A landmark is located at m1=(0,−2)m_1=(0,-2) m. The robot’s actual heading differs from the nominal heading by 25∘25^\circ, so it stops at approximately (0.42,−0.91)(0.42,-0.91) m. It then measures the range and bearing to the landmark, with measurement noise of σr=5\sigma_r=5 cm and σϕ=2∘\sigma_\phi=2^\circ. We compare the EKF’s prediction and measurement update against a Monte Carlo approximation of the corresponding distributions. Our objective is to understand how much a landmark observation can correct an inaccurate prediction.
Two panels. Left: predicted pose samples form a banana arcing upward, and the EKF prediction ellipse is wide and flat at y = -1 meter. Right: the Monte Carlo posterior is a small cluster near the true position, and the EKF mean sits several centimeters below it. Left: the predicted position distribution and its EKF approximation. Right: the posterior position distribution after observing the landmark. The star represents the landmark and the green square the true robot position. The prediction exhibits the same banana-shaped distribution encountered in the overconfident EKF prediction section. The EKF approximates this distribution with a Gaussian, substantially underestimating the uncertainty along the direction of travel. The landmark observation provides two complementary pieces of information:
  • The bearing helps determine which side of the predicted trajectory the robot is on.
  • The range helps determine how far the robot is from the landmark.
The measurement update substantially improves the estimated position. However, the EKF corrects the position much more effectively along the xx direction than along the yy direction. Why? The Kalman gain depends on the predicted uncertainty. Because the EKF was already overconfident about the yy coordinate, it gives insufficient weight to the range measurement when correcting that coordinate.
After the prediction, xx and θ\theta carry the same error. A heading error of δ\delta radians moves the robot δ\delta meters sideways over the 1 m drive, so the predicted covariance correlates xx and θ\theta almost perfectly. At the predicted mean, the bearing row of HtH_t is (−1,0,−1)(-1, 0, -1). A heading error δ\delta therefore changes the predicted bearing by about 2δ2\delta: once because the robot is turned, and once because the robot sits to the side of the landmark. With σθ=30°\sigma_\theta = 30°, the predicted bearing has a standard deviation of about 60°60°. The sensor measures the bearing to 2°2°, about 30 times finer. Since one error δ\delta drives both xx and θ\theta, a bearing that fixes δ\delta fixes both of them, and both standard deviations shrink by about that factor of 30. The update leaves xx with about 2 cm and θ\theta with about 1°1°.
The Monte Carlo posterior does not suffer from this particular linearization error and remains centered closer to the true position. This example illustrates an important limitation of the EKF: an inaccurate representation of uncertainty during prediction can compromise the subsequent measurement update. The consequences can be even more severe when measurement validation is enabled. In this example, the innovation exceeds the Mahalanobis distance threshold, meaning that the EKF would reject a valid observation because it is inconsistent with its overconfident prediction.

Driving among landmarks

Over many steps, the code below drives a simulated TurtleBot3 Waffle around a circle of radius 1.5 m at 0.2 m/s for 70 s, about one and a half laps. Three landmarks stand outside the circle. The sensor is idealized: it sees every landmark within 1.5 m, in any direction, with nothing in the way. A camera has a limited field of view, and one landmark can occlude another, which would leave more steps without a measurement. Even so, on parts of the circle no landmark is within range. Each wheel of the simulated robot turns at its commanded speed plus noise drawn from the motion model’s MtM_t, with α=0.05\alpha = 0.05 and β=0.1\beta = 0.1 rad/s, and the filter uses the same MtM_t. The sensor noise is the same as in the single step. The filter starts from the nominal starting pose, and the true starting pose is drawn from the filter’s initial belief, so the initial error matches the initial covariance. For comparison, the same filter also runs without any measurements, which is dead reckoning from the wheel speeds alone. The gate is off.
Circular true path with three landmarks outside it and their sensing disks. The EKF mean follows the true path closely while dead reckoning drifts inward; uncertainty ellipses are larger where no landmark is in view. Paths of one run. Each ellipse is the EKF’s 2σ2\sigma position uncertainty, drawn every 7 s: solid where a landmark is in view, dashed where none is. The shaded disks are the sensing ranges. Three stacked plots of the x, y and heading errors over 70 seconds with the EKF two-sigma band. The band widens through gray stretches with no landmark in view and narrows when one comes back into range. Errors of the same run, estimate minus truth, with the EKF’s ±2σ\pm 2\sigma band. Gray stretches have no landmark in view. Without measurements the estimate drifts: after 70 s dead reckoning is 34 cm from the true position, against 9 cm for the EKF. The dashed ellipses, drawn where no landmark is in view, are larger than the solid ones. The error figure shows the same thing over time: the band widens through each gray stretch and narrows within a step or two of a landmark coming back into range. The error stays inside the band at 98 to 100% of the steps of this run. That does not show that the filter is consistent. The steps of one run are strongly correlated, since an error at one step is still there at the next, and a band per coordinate does not test the joint covariance of xx, yy and θ\theta.

Checking consistency over many runs

A filter is consistent when its error has the covariance the filter claims. The normalized estimation error squared (NEES) tests this for the whole state at once: ϵt=(μt−st)⊤Σt−1(μt−st).\epsilon_t = (\mu_t - s_t)^\top \Sigma_t^{-1} (\mu_t - s_t). For a consistent filter ϵt\epsilon_t follows a chi-square distribution with 3 degrees of freedom, the size of the state, so its mean is 3. The NEES needs the true state, which only a simulation has. The normalized innovation squared (NIS) needs only the measurements, so a real robot can compute it. It is the squared Mahalanobis distance ν⊤S−1ν\nu^\top S^{-1} \nu of line 7, with mean 2 for a consistent filter. The code below repeats the drive 50 times with different noise and averages the NEES over the runs at each time step. A consistent filter’s average falls inside a band given by the chi-square distribution 95% of the time. The code does this once with the gate off and once with it on.
Average NEES over 50 runs against time. Without the gate it stays close to the consistency band around 3 and slightly above it in the second half; with the gate it rises to between 4 and 6 in the second half. Average NEES over 50 runs at each time step, with the gate off (solid) and on (dashed). The shaded band is where the average of a consistent filter falls 95% of the time. Without the gate the average NIS is 2.05, close to 2, so the innovations are as large as the filter predicts. The average NEES is 3.42, above the expected value of 3. It is close to the band for the first 40 s and mostly above it afterwards, and it is inside the band at 77% of the time steps, so the filter is mildly overconfident in the second lap. This section does not isolate the cause. A likely one is the linearization, since the heading uncertainty grows in each stretch with no landmark in view. With the gate the filter does worse, although the simulation produces no bad observations. The gate rejected 127 of 3,590 observations, in 27 of the 50 runs. If the filter were consistent it would reject about 1% of them, 36. In 4 runs it rejected 10 or more. Once the estimate has drifted, correct observations fall outside the gate, the filter rejects the measurements that would pull it back, and the error grows further. The average NIS rises to 4.80 and the average NEES to 3.98. Because the gate also rejects correct observations once the estimate has drifted, a filter with a gate needs a check for repeated rejections, for example counting them and relocalizing with a particle filter.

Where road maps come from

Lane-level road maps have been built by surveying. The markings are either measured with surveying instruments such as a total station, which takes a long time when an area has many markings (arXiv:2209.07737), or extracted from the point clouds of vehicles that carry 3D LIDAR, which is expensive (arXiv:2601.19536). A pipeline that uses only cameras costs less than either. In such a pipeline, each frame is warped to a bird’s-eye view with the homography of the road plane, and the lane markings are segmented in that view. The marking positions and the homography are then refined until a marking seen in many frames has the same position on the road in all of them. In the first paper, the vehicle poses come from RTK-GNSS, and only the markings and the homography are refined. In the second paper, the vehicle poses are refined as well. Mobileye’s REM collects lane markings and landmarks from the cameras of production cars and merges them in the cloud into one map. Cars then localize against that map by detecting its landmarks (Mobileye). The REM map also contains lane markings, while the map in this section contains only point landmarks. Localization against it has the same predict and correct structure as the filter in this section: the car predicts where each map landmark should appear, and it corrects its pose from the difference between the predicted and the observed positions.
Found an error on this page? Report an issue.

Footnotes

  1. atan2⁡(y,x)\operatorname{atan2}(y, x) is the two-argument arctangent: the angle of the point (x,y)(x, y), measured counterclockwise from the positive xx axis, in (−π,π](-\pi, \pi]. In terms of the ordinary arctangent, atan2⁡(y,x)={arctan⁡(y/x)x>0arctan⁡(y/x)+πx<0, y≥0arctan⁡(y/x)−πx<0, y<0π/2x=0, y>0−π/2x=0, y<0\operatorname{atan2}(y, x) = \begin{cases} \arctan(y/x) & x > 0 \\ \arctan(y/x) + \pi & x < 0,\ y \ge 0 \\ \arctan(y/x) - \pi & x < 0,\ y < 0 \\ \pi/2 & x = 0,\ y > 0 \\ -\pi/2 & x = 0,\ y < 0 \end{cases}, and it is undefined at (0,0)(0, 0). It differs from arctan⁡(y/x)\arctan(y/x), which depends only on the ratio y/xy/x and returns angles in (−π/2,π/2)(-\pi/2, \pi/2). The points (1,1)(1, 1) and (−1,−1)(-1, -1) both give arctan⁡(1)=45°\arctan(1) = 45°, while atan2⁡\operatorname{atan2} gives 45°45° and −135°-135°. The two agree when x>0x > 0, and atan2⁡\operatorname{atan2} is also defined at x=0x = 0. ↩