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 03
Damp it with D
Checkpoints · 0 of 2 (waiting for Run)
- Reach the end of the line (waiting for Run)
- Glide it home with D (low wobble) (waiting for Run)
A controller that remembers
Your snappy Kp from lesson 2b is still in charge, and you can see the result on the right: it reacts hard, overshoots, and rings back and forth across the line. Derivative control adds a second term that watches how fast the error is changing and pushes back against it: steer = Kp·error + Kd·Δerror. A shock absorber on the correction.
To know how fast the error is changing, the controller has to remember last tick’s error. A plain function forgets everything the moment it returns, so this lesson hands you a class.
Read the skeleton in the editor. class PIDController: is a blueprint. When you press Run, the sim builds one instance from it, and __init__ runs once to give that instance its starting state. self is the instance itself: the same object on every tick, so anything stored on it, like self.prev_error, is still there when step() is called again a sixtieth of a second later.
Compute the derivative
Damp the correction
Feel the wobble fall
▸API reference
- PIDController(kp=6, kd=0.0, ki=0)
- Built once when you press Run, from the defaults in your __init__ signature. This class is the piece you're earning: lesson 04 imports it from your library.
- step(error, dt)
- Called every tick with the line offset (px, sensor-delayed) and dt = 1/60 s. Return the steer CORRECTION (one number); the sim applies (38 − steer, 38 + steer) to the wheels.
- kp / kd defaults (tuned in code)
- Your gains live in the signature. Edit a default, press Run, and a fresh instance drives with the new value; Reset also rebuilds from your source.
space pause · r reset