A LiDAR draws walls to the centimetre, but it smears when it moves and goes blind in a plain corridor. An IMU never blinks, but it drifts. On this toy building with a 30 m corridor, LiDAR alone ends its loop 29.8 m off and the IMU alone 44.7 m off. Fused, the loop ends 2.2 m off, and one loop-closure factor brings the worst error down to 0.69 m.
Pick a navigator and set the corridor length. Watch where the map folds. Then we build it piece by piece, from un-bending one sweep to solving the whole loop as one least-squares problem.
You need the Kalman filter and strapdown navigation. We build the rest from zero.
Lesson 17 of 22 in Sensor Fusion: Classical to Modern. It follows Visual-Inertial Odometry and leads to Multimodal Driving Fusion.
Illustrative keyframe toy, seeded: one keyframe per metre around a 20 m wide building. Scan matching 2 cm and 0.25° per step where walls have features; in the corridor this toy matcher keeps its start guess of zero motion. IMU alone: a constant 0.025 m/s² bias. Fused: the IMU carries the corridor with a 0.004 m/s² bias left over. Gauss-Newton solves the loop closure on the whole pose graph.
Chapter 0
Measure how far the IMU drifts between two LiDAR scans, and how far it drifts alone
A delivery robot rolls down a warehouse aisle. On its roof a LiDAR spins: a laser rangefinder that fires beams and times each echo. Each echo gives a distance to the centimetre. One full turn gives a point cloud, a metric sketch of the room.
The LiDAR has three flaws. It is slow: a spinning unit turns about 10 times a second, so a scan arrives every 100 ms. It smears: the robot moves during each 100 ms sweep. And it can go blind: in a long, plain corridor every scan looks the same, so scans cannot show how far the robot moved along it.
The IMU of Lesson 13 has the opposite profile. It reports hundreds of times a second and works in any corridor. Its flaw is drift. Position comes from integrating acceleration twice, so a constant accelerometer bias grows into an error that grows with time squared.
Warm is the IMU alone. Teal is the IMU reset by every LiDAR scan. Blue marks the scan-match noise. Move the scan rate and the bias.
Illustrative; exact for a constant bias and a perfect start: the IMU error is one half b t squared. We made up the 2 cm scan-match noise. Real systems also carry velocity error between scans.
Call the accelerometer bias b and the time since the last correction t. With a perfect start, the position error from a constant bias is:
e of t equals one half b t squared.
Square the time t since the last correction. Multiply by the bias b and halve it. That is how far the IMU has wandered.
With b = 0.05 m/s² and one 10 Hz scan gap, t = 0.1 s: e = 0.5 × 0.05 × 0.01 = 0.00025 m, a quarter of a millimetre. Alone for t = 60 s: e = 0.5 × 0.05 × 3600 = 90 m.
So the IMU is close to perfect for one scan gap and useless for a minute. The LiDAR is the reverse. Each scan anchors the robot to the walls, but only ten times a second and only in directions the walls pin down. Fused, the IMU un-bends each sweep and carries the robot between scans and through blind spots. The LiDAR resets the IMU drift at every scan.
Chapter 1
Move every point back to one moment, using where the IMU says the robot was when that beam fired
A spinning LiDAR does not take its cloud in one instant. The first beam fires at the start of the 100 ms sweep and the last fires at the end. If the robot moves, the sensor sees each point from a different place. The driver stamps them all as if from one pose, so a straight wall comes back bent. This is motion distortion.
The fix is deskewing. The LiDAR stamps each point with its firing time, tau. The IMU at 200 Hz gives about 20 poses inside each sweep, so we know where the robot was at every tau. Call the raw point p, in the sensor frame at the moment it fired. Call the robot pose at that moment T of tau, and the pose at the start of the sweep T-ref. A pose is a rotation plus a shift, written as one 3 by 3 matrix in 2-D.
p-ref equals T-ref-inverse, times T of tau, times p.
Push the raw point p into the world with the pose T of tau where it fired. Pull it back into the reference pose with T-ref-inverse. Every point gets the pose of its own firing time.
The robot drives toward a wall at 1.5 m/s. A beam fires at tau = 0.08 s and reads 3.00 m. By then the robot has moved 1.5 × 0.08 = 0.12 m, so T of tau is a 0.12 m shift and p-ref = 3.00 + 0.12 = 3.12 m. Without deskew that point is 12 cm wrong.
The dashed lines are the real walls. The robot sweeps once while it drives and turns. Choose raw or deskewed, then raise the speed and the turn rate.
Illustrative toy with exact geometry: a 10 m by 6 m room, 180 beams over one 100 ms sweep, constant speed and turn rate, and a perfect IMU. The raw sweep places every point as if the robot had not moved since the sweep began.
Turning hurts more than driving. At 90°/s the robot turns 9° in one sweep, and a wall 10 m away measured at the end of the sweep lands 1.57 m sideways. So deskew needs the gyroscope at full rate, not one velocity per scan: the correction depends on each point's angle and range.
Chapter 2
Iterate closest points, and measure each gap along the surface normal instead of straight to a neighbour
With clean scans, the next question is how far the robot moved between two of them. Registration finds the rotation and shift that lay the new scan onto the old one, the map. The world did not move, so that transform is the robot's motion.
The classic method is Iterative Closest Point (ICP) (Besl and McKay 1992). Pair each new point with its nearest map point. Find the transform that best aligns the pairs. Apply it, pair again, and repeat until nothing changes.
Point-to-point ICP minimises the straight distance of each pair. But a LiDAR never samples the same spot twice: the new scan hits a wall between the old scan's points. Demanding point on point drags the scan along the wall and can lock it in a wrong place. Chen and Medioni (1992) measured the gap along the surface normal instead, so a point only needs to land on the surface.
Call the new point after the candidate move p-prime and its matched map point q. Call the map surface's unit normal n: the direction straight out of the surface.
r equals n dot, p-prime minus q.
Take the gap from the map point q to the moved point p-prime. Keep only its part along the normal n. That part is r, so sliding along the wall costs nothing.
p-prime = (2.1, 0.3), q = (2.0, 0.0), n = (1, 0). The gap is (0.1, 0.3). Point-to-point scores its length, 0.316 m. Point-to-plane keeps r = 1 × 0.1 + 0 × 0.3 = 0.1 m, the true distance into the wall.
Pink is the map: two walls meeting at a corner. Blue is the new scan, sampled at other spots. Thin lines are this iteration's pairs. Choose a residual and press Step.
Illustrative toy, exact algebra: 15 map points per wall, 14 scan points per wall placed between them. At ×1.0 the scan starts turned 0.18 rad and shifted 0.35 m and −0.25 m, as in the Code Lab. Point-to-point uses the closed-form best fit of the pairs; point-to-plane solves the linearised rows. Error is the RMS distance to the real walls.
Each point-to-plane iteration is one small least-squares solve. Linearise the rotation, stack one row per point, solve for the twist (a small rotation plus a shift), and apply it. In the device it lands in two iterations. Point-to-point stalls 10.5 cm away, because every point keeps pairing with the same wrong neighbour.
Chapter 3
Score each point by how sharply its neighbours bend, and match only the sharpest and the flattest
A scan holds tens of thousands of points, and most lie on flat surfaces that say the same thing. LOAM, LiDAR Odometry and Mapping (Zhang and Singh 2014), registers only a few informative ones. It scores how much the surface bends at each point. It keeps the sharpest as edge points and the flattest as planar points.
Take a point p-i on one scan line. Take a set S of its neighbours on the same line, half on each side. Call the score c.
c equals the length of the sum over j in S of p-i minus p-j, divided by the size of S times the length of p-i.
Add up the vectors from each neighbour p-j to the point p-i. On a flat surface they cancel; at a corner they do not. Divide the length of the sum by the neighbour count and by the range. That is c.
A point at (5, 0) on a straight wall, with neighbours 0.1 m and 0.2 m away on each side: the four vectors cancel and c = 0. At a corner with neighbours (5, −0.2), (5, −0.1), (4.9, 0) and (4.8, 0), the sum is (0.3, 0.3), of length 0.424, so c = 0.424 / (4 × 5) = 0.021.
Top: one scan line seen from above. Rings are edge points, filled dots are planar points, faint dots are points LOAM drops. Bottom: each point's score c and the edge threshold. Choose a scene, then move the threshold.
Illustrative toy: 360 beams at 1° on one line, five neighbours on each side, as in common LOAM code. Planar points are those scoring under a tenth of the edge threshold. LOAM also caps the picks per region and skips occluded points; this toy does not.
Edge points match with a point-to-line residual, and planar points with the point-to-plane residual of Chapter 2. LIO-SAM writes the same two distances as its equations 10 and 11. A plane pins motion along its normal, and an edge pins the directions a plane leaves free. LOAM ran its odometry at about 10 Hz and its mapping at 1 Hz.
Chapter 4
Write each reading as a weighted squared error, and find the trajectory with the smallest total
One scan match gives one step. A robot takes thousands, and the drift piles up. Now and then it gets a GNSS fix, or sees a place it has seen before. A factor graph holds all of these in one picture.
It has two kinds of node. Variables are the unknowns: the robot poses, and often velocities and IMU biases. Factors are the measurements, and each one connects only the variables it talks about. An odometry factor links two poses in a row. A GNSS fix touches one pose. A loop closure links two poses far apart in time.
Each factor scores a guess. With Gaussian noise, its negative log-likelihood is a squared residual over the noise variance. Probabilities multiply, so their logs add, and the most probable trajectory, the MAP (maximum a posteriori) estimate, minimises a sum. Call the stacked unknowns x, the residual of factor i r-i of x, and its noise sigma-i.
x-hat equals the argmin over x of the sum over i of r-i of x squared, over sigma-i squared.
For a guess x, compute each factor's residual r-i. Square it and divide by its variance sigma-i squared. Add them all. x-hat is the guess with the smallest total.
Two poses. A prior says x0 = 0 (variance 1). Odometry says x1 − x0 = 10 (variance 1). A GNSS fix says x1 = 11 (variance 4). Setting both derivatives to zero gives 2 x0 − x1 = −10 and −x0 + 1.25 x1 = 12.75. So x0 = 0.167 and x1 = 10.333. x1 sits nearer the odometry because the fix is four times noisier.
Top: the graph. Circles are variables, squares are factors. Bottom: where each vote and the estimate sit. Move the fix and its noise.
Exact solution of the three-factor least squares in the With numbers line. The filter row is a Kalman filter run forward on the same chain.
A Kalman filter on this chain gives the same x1 = 10.333, with variance 1.333. But it leaves x0 at 0, because it never revisits the past. The graph moves x0 to 0.167. A Kalman filter is a factor graph shaped like a chain and solved forward once, the view Dellaert and Kaess (2017) build on. The graph is the general object: it can keep every past pose and add factors that reach back.
Chapter 5
Add one factor between the end and the start, and watch the correction spread back along the loop
Drive a loop and the odometry drift leaves the end short of the start. Then place recognition fires: this scan matches a scan from the start. That match becomes a loop-closure factor between the last pose and the first. A filter cannot use it, because it dropped the old pose. The graph kept every pose, so it can.
Gauss-Newton solves the graph: linearise each residual, then solve one linear system for the correction. Call each factor's Jacobian J-i, how its residual moves when the variables move. Call its weight Omega-i, one over its variance, and the system matrix H.
H equals the sum over i of J-i-transpose, Omega-i, J-i.
For each factor, multiply its Jacobian J-i by its weight Omega-i and by itself. That block lands only on the rows and columns of the variables the factor touches. Add every block to get H, then solve H times the correction equals minus the gradient.
An odometry factor between x-k and x-k-plus-one has J = [−1, 1] and Omega = 1, so it adds [[1, −1], [−1, 1]] on rows k and k+1 only. For 9 poses in a chain with a prior, H has 25 of 81 entries filled. A loop closure adds 2 more, at the corners. With 101 poses: 303 of 10,201, or 3%.
Left: the poses, the odometry factors (blue squares) and the true ring (dashed). Right: the filled entries of H. Switch the loop closure, then raise the drift.
Illustrative linear toy: 12 steps around a ring of radius 4 m, positions only. Each odometry step carries the same drift vector plus 3 cm of seeded noise. Odometry sigma 0.1 m, loop sigma 0.05 m, a tight prior on the first pose. Solved exactly; the grid shows one axis.
That sparsity is why smoothing runs in real time. iSAM2 (Kaess et al. 2012) keeps the factored H as a Bayes tree. A new factor edits only the part of the tree that holds its variables, up to the root, and leaves the rest. It also relinearises only the variables whose estimate moved enough. An odometry factor at the end touches a few variables; a loop closure touches the whole loop, once.
Chapter 6
Sum the IMU between two keyframes once, and correct the sum for a new bias without integrating again
Between two keyframes 1 s apart, a 200 Hz IMU gives 200 readings. One factor per reading would bloat the graph. Preintegration sums them once into one relative motion: the change in velocity, position and rotation from keyframe i to keyframe j. Lupton and Sukkarieh (2012) introduced it, and Forster et al. (2017) carried it onto the rotation manifold.
The sum depends on the bias estimate, and the optimiser changes the bias at every iteration. Integrating 200 readings again each time is the cost we wanted to avoid. Two tricks remove it. We write the sum in keyframe i's own frame, so it does not depend on where keyframe i is. And a bias change shifts the sum by a derivative computed once.
Take one axis with no rotation. Call the accelerometer reading at tick k a-k, the bias estimate b, and the tick length delta-t. Call the velocity change from keyframe i up to tick k delta-v-i-k.
Delta-v-i-j equals the sum over k of a-k minus b, times delta-t. Delta-p-i-j equals the sum over k of delta-v-i-k times delta-t, plus one half of a-k minus b, times delta-t squared.
Walk the ticks from keyframe i to keyframe j. At each tick, take the bias b off the reading a-k. Add it to the running velocity change, and add the distance that tick covers to the position change. The two totals are the factor.
With a made-up wobbling profile, 200 ticks over T = 1 s give delta-v = 0.0033 m/s and delta-p = 0.0490 m. If b rises by 0.02 m/s², the derivatives say delta-v drops by T × 0.02 = 0.02 m/s and delta-p by ½ T² × 0.02 = 0.01 m. Integrating all 200 ticks again gives the same: −0.0167 m/s and 0.0390 m.
Top: every IMU reading between keyframe i and keyframe j. Bottom: the running position change. Move the bias change and compare the derivative shortcut (dashed) with a full re-integration (solid).
Illustrative 1-D toy: the readings follow 0.3 sin(2πt) plus seeded noise of 0.05 m/s², keyframes 1 s apart. In 1-D the shortcut is exact; with rotation it is first order.
In 1-D the shortcut is exact, because the sums are linear in b. With rotation, the gyro bias enters through products of rotations, so the correction is first order and is redone only when the bias moves far. The factor ties pose, velocity and bias at both keyframes. So LiDAR factors teach the graph the IMU bias; LIO-SAM optimises the bias jointly with the poses.
Chapter 7
Compare LIO-SAM's factor graph with FAST-LIO's filter, and count what each Kalman gain costs
Two designs lead the field. LIO-SAM (Shan et al. 2020) builds a factor graph with four factor types: IMU preintegration, LiDAR odometry, GNSS and loop closure, solved by iSAM2. It adds a keyframe when the robot moves 1 m or turns 10°. Its title says tightly coupled: the IMU deskews and seeds scan matching, and the LiDAR factors correct the IMU bias. But the LiDAR enters the graph as a finished relative pose from scan matching, not as point residuals.
FAST-LIO (Xu and Zhang 2021) is an iterated error-state Kalman filter. It fuses each feature point's residual directly and re-linearises the update until it converges. It deskews by running the IMU backward through the sweep. FAST-LIO2 (2022) drops feature extraction, registers raw points, and keeps the map in an incremental k-d tree, the ikd-Tree.
A scan gives over a thousand residuals, and the usual gain inverts a matrix as big as the measurement count. FAST-LIO rewrites the gain so the inverse is state-sized. Call the state covariance P (n by n), the measurement Jacobian H (m rows), the measurement noise R (m by m) and the gain K.
K equals the inverse of, H-transpose R-inverse H plus P-inverse, times H-transpose R-inverse.
Weight each residual row of H by R-inverse. Add the weighted rows into one n by n information matrix, add P-inverse, and invert that small matrix. Multiply by H-transpose R-inverse. The result equals the usual gain P H-transpose times the inverse of H P H-transpose plus R.
FAST-LIO's state has 18 numbers. With 1,802 points, the usual form inverts an 1,802 by 1,802 matrix: of order 1,802³ = 5.9 billion operations. The new form is of order 1,802 × 18² + 18³ = 0.59 million. The paper measured 1,621 ms against 1.16 ms at that size (its Table II).
Log axes. Red is the usual gain, which inverts an m by m matrix. Teal is FAST-LIO's form. The dots are the paper's measured times. Choose the state size and move the point count.
Operation counts are orders of magnitude (m³ against m n² + n³), not timings. The dots are the paper's measured milliseconds, from FAST-LIO Table II (307 to 1,802 feature points), on their own scale.
| Design | Engine | What enters | Loop closure |
|---|---|---|---|
| LIO-SAM | factor graph, iSAM2 | scan-match poses, IMU preintegration, GNSS, loops | built in: one more factor |
| FAST-LIO2 | iterated error-state Kalman filter | every raw point's residual, the IMU | none: odometry only |
Chapter 8
Read the scan's information matrix, find the direction with no information, and let the IMU carry it
In a straight corridor, sliding the scan along the walls changes no point-to-plane residual. The registration has a flat direction, and the solver can return any answer along it. This is degeneracy, and we can detect it from the scan itself.
For a pure shift, each planar point's residual changes by its normal dotted with the shift (Chapter 2). So each point adds its normal to the scan's information matrix, the same H as Chapter 5 for one scan match. Call the normal of point k n-k.
H equals the sum over k of n-k times n-k-transpose.
For each planar point, multiply its normal n-k by itself into a 2 by 2 block, and add all the blocks. Each eigenvalue of H says how stiffly the scan pins motion along its eigenvector. A tiny smallest eigenvalue means a blind direction.
A corridor along x: 120 points on each wall have n = (0, ±1), so each adds 1 to the y-y entry, 240 in all. Three points on an end wall have n = (1, 0) and add 3 to the x-x entry. The eigenvalues are 3 and 240, and the condition number is 240 / 3 = 80. With a threshold of 100, x is blind.
Left: the scene with its scan points and normals, and the information ellipse. Right: the two eigenvalues against the threshold. Choose a scene, then add end-wall points.
Illustrative toy matching the Code Lab: 120 points per long wall, unit normals, shift only (no rotation), threshold 100. The ellipse axes grow with the square root of each eigenvalue.
Zhang, Kaess and Singh (2016) defined a degeneracy factor from the smallest eigenvalue. Their solution remapping keeps the scan's correction in the well-constrained directions and takes the prediction in the blind ones. They set the threshold from data, between degenerate and healthy scenes. In a factor graph the same idea means: give the scan factor almost no weight along the blind eigenvector, and let the IMU factor carry it.
Chapter 9
Switch the IMU, the degeneracy check and the loop closure on and off, and read the error along the loop
Here is the hero's building again, with every switch exposed. The pose graph has one keyframe per metre. Where walls have features, LiDAR odometry factors link the keyframes; in the corridor, IMU-carried factors do. One loop closure links the last pose to the first. Gauss-Newton solves it, as in Chapter 5.
Top: the map, truth dashed. Bottom: the error at every pose, corridor shaded; dashed is the odometry before the solve. Flip the switches and watch what the loop closure can and cannot fix.
Illustrative keyframe toy, seeded, the same engine as the hero. Phone and tactical differ only in the bias left over in the corridor: 0.004 against 0.0005 m/s². The check gives blind corridor factors a 5 m sigma along the corridor; without it they claim 2 cm.
Start with the defaults: IMU on, loop on, a 30 m corridor. The worst error is 0.69 m and the corridor comes out 30.34 m long. A tactical IMU brings the worst error to 0.28 m.
Now turn the IMU off. The corridor factors claim 2 cm, so the solver believes the squeezed corridor. The loop still closes to 0.41 m, but the map folds: worst error 26.5 m, corridor 2.0 m long. Turn the degeneracy check on. The blind factors now carry 5 m of doubt along the corridor, and the loop closure stretches the corridor back to 29.4 m, worst error 0.79 m.
Chapter 10
Keep the equations, the numbers, the checklist, the failures and the code in one place
The IMU deskews each sweep and carries the robot between scans. Point-to-plane registration on edge and planar features turns each scan into a motion estimate. A factor graph adds those estimates, preintegrated IMU factors, GNSS fixes and loop closures, and solves them as one sparse least-squares problem. The scan's eigenvalues say where the walls cannot see, and there the IMU takes over.
| Quantity | Formula | Numbers from this page |
|---|---|---|
| IMU drift | e = ½ b t² | 0.25 mm per 0.1 s gap; 90 m in 60 s |
| Deskew | pref = Tref−1 T(τ) p | 3.00 m read at τ = 0.08 s becomes 3.12 m |
| Point-to-plane | r = n · (p′ − q) | 0.1 m, where point-to-point says 0.316 m |
| Smoothness | c = ‖Σ(pi − pj)‖ / (|S| ‖pi‖) | 0 on a wall, 0.021 at a corner |
| MAP | x̂ = argmin Σ ri² / σi² | x0 = 0.167, x1 = 10.333 |
| Information | H = Σ JiT Ωi Ji | 101 poses: 303 of 10,201 entries |
| Preintegration | Δp shifts by −½ T² δb | δb = 0.02: 1.0 cm, no re-integration |
| FAST-LIO gain | K = (HTR−1H + P−1)−1HTR−1 | 1,802 points: 1,621 ms to 1.16 ms (paper) |
| Degeneracy | H = Σ nk nkT | eigenvalues 3 and 240, condition 80 |
| Symptom | Cause | Test |
|---|---|---|
| Walls thicken with speed | no deskew, or a LiDAR-IMU time offset | drive one wall slow, then fast; turn in place |
| Corridor shrinks or stretches | degeneracy | log the smallest eigenvalue |
| Tilted from the first second | bad gravity or bias start | stand still; bias settles, gravity points down |
| Map tears after a loop event | a false loop closure | log each closure's match and ICP score |
| Blur that grows with turn rate | wrong LiDAR-IMU extrinsic | rotate in place; recalibrate (Lesson 15) |
| Crisp locally, bent globally | odometry drift with no loops | drive a known loop, measure the gap |
pythonimport numpy as np def point_to_plane_step(P, Q, NQ, thresh=100.0): # P: new scan (N,2) already moved by the IMU guess; Q, NQ: map points and unit normals idx = ((P[:, None] - Q[None]) ** 2).sum(2).argmin(1) # closest map point q, n = Q[idx], NQ[idx] r = np.sum(n * (P - q), axis=1) # r = n . (p' - q) R90p = np.stack([-P[:, 1], P[:, 0]], axis=1) # small-rotation column J = np.column_stack([np.sum(n * R90p, axis=1), n]) # rows [n.R90p, nx, ny] H = J.T @ J # information of this scan match w, V = np.linalg.eigh(H[1:, 1:]) # shift block: sum n n^T blind = V[:, w < thresh] # directions the walls cannot see dx = np.linalg.lstsq(J, -r, rcond=None)[0] # twist: (dtheta, dx, dy) dx[1:] -= blind @ (blind.T @ dx[1:]) # remap: keep the IMU guess there return dx, w
LiDAR-inertial odometry maps places where GNSS is weak or absent: warehouses, mines, tunnels and building interiors. Survey vans and handheld scanners use it to build maps, and drones and legged robots use it to move. The software is open: LOAM-style feature registration, LIO-SAM on the GTSAM library, and FAST-LIO2.
Now press Present or Teach and derive it from memory. Cover the drift law, deskew, point-to-plane, the smoothness score, MAP as least squares, the sparse H, preintegration, FAST-LIO's gain and the eigenvalue test. Then go back to the building and say why one loop factor fixes the fused map but not LiDAR alone.
Lesson 17 of 22 in Sensor Fusion: Classical to Modern on Engineermaxxing. Every toy number on this page carries the word illustrative; the paper numbers name their source.