Click & Whirr needs a wider screen
Open it on a laptop or desktop (1280px or wider recommended). The Learn · Code · Watch workspace needs the room to breathe.
Lesson 12
Two liars, one truth
Your integrated heading is smooth but it drifts: an unseen bias turns the robot a little more than the wheels report, and the error quietly piles up. The absolute reading is honest but jittery: right on average, noisy every tick. Neither source is trustworthy alone. The fix is the classic complementary filter: mostly trust your smooth prediction, and let a small fraction of the absolute leak in so the drift can never accumulate. One blend knob, alpha, sets how much you trust yourself.
Checkpoints
- Fuse them: peak error beats the raw noise (waiting for Run)
- Hold steady: stay locked on near the end (waiting for Run)
Press Run ▸ to start checking your code.
Write the blend, press Run, then tune alpha down from the top. At 0.999 you barely listen to the absolute and the drift wins; leak a little more in and the belief locks on. Watch both checkpoints: beat the raw noise and hold steady.
Steps
- 1
Predict from the wheels
Short-term truth comes from the wheel difference:
omega = (v_right - v_left) * 1.6 / 55, thenpredicted = self.theta + omega * dt. - 2
Blend the two sources
- 3
Beat both liars
▸API reference
- ComplementaryFilter(alpha, theta=0.0)
- Built once on Run with the slider value. alpha near 1 trusts your smooth-but-drifting integration; the small (1 - alpha) leak pulls you toward the noisy absolute reading so drift can't accumulate.
- update(absolute_heading, v_left, v_right, dt)
- Called every tick. absolute_heading is the noisy compass-style reading (radians); v_left/v_right are the commanded wheel speeds (slider units) for your short-term integration. Return your fused heading belief.
- robot.sensors.noisy_heading()
- The same raw absolute reading via the facade.
- how you're graded
- Your belief is compared against the true heading. Checkpoint 1 (fused) needs your peak error to beat the raw noise; checkpoint 2 (steady) needs your error to settle and stay settled. Pure noise fails the first, pure integration drifts and fails both. The blend beats both parents.
space pause · r reset