A commercial milking robot cannot assume the udder sits still — the cow shifts weight, and a laser or ultrasonic sensor's single reading is noisy. The controller fuses repeated scans with a scalar recursive (Kalman) filter, updating a position estimate x̂ and its variance P after every measurement z:
K = P / (P + R) gain from current uncertainty P and sensor noise R
x̂' = x̂ + K·(z − x̂) blend new measurement into the estimate
P' = (1 − K)·P uncertainty shrinks with every scan
This 2D version views the udder from directly above: the arm's 2-link reach already reduces to a planar law-of-cosines problem in the original 3D engine (a horizontal radius r and one more axis h), so a true top-down flatten needs no approximation — it is exactly that same plane, with the vertical reach axis removed instead of collapsed:
d = √(dx² + dy²) dx, dy = target offset from the arm base
elbow angle = acos((L1²+L2²−d²) / (2·L1·L2))
shoulder angle = atan2(dy, dx) + acos((L1²+d²−L2²) / (2·L1·d))
- Sensor noise — the standard deviation R of each raw scan; higher noise needs more scans to converge, or leaves the final lock less certain.
- Cow movement — the true teat position keeps drifting by a small sway while the estimate is being built and while the arm is en route, which is the main real-world source of attachment misses.
- Arm speed — how fast the end effector chases the current estimate once it commits to move.
- An attach only succeeds if the cup arrives within tolerance of the true, currently-swaying teat position — not just the last estimate — which is exactly the race real systems have to win.
Fix from the 3D source: the original engine derives one scalar Kalman gain from an isotropic noise model (R = σ²) but then applies that same gain to a vertical measurement stream fed with 0.6σ noise — a numerically confirmed ~2.8× mismatch between the assumed and actual vertical noise variance (checked with a standalone recursion script). This 2D version tracks teat position in a single horizontal plane with one true, isotropic noise σ on both axes, so the scalar filter equations shown above are exactly the ones being computed — no hidden anisotropy.
Drag inside the scene panel to pan, scroll to zoom — useful for lining up a close look at the attach tolerance ring around a swaying teat.
Real-world relevance: automatic milking systems (e.g. Lely, DeLaval, GEA) use this laser-scan-plus-filter-plus-analytic-IK pipeline per teat, several times a day, per cow, unsupervised.