Closed Loop
You'll add an encoder to a motor and write a PID controller that holds a set speed under load, then use it to make a robot drive straight and turn exact angles.
About
Open loop is telling a motor to go; closed loop is checking whether it did. An encoder on the shaft (a two-dollar magnetic disc, or the slotted wheel in a cheap chassis kit) tells you the real speed, and a PID loop corrects the difference. This is the technique under every drone, 3D printer, cruise control and balancing robot, and tuning one by hand teaches you more than any formula. The first trap is a noisy speed measurement from counting pulses over too short a window, which makes the loop twitch, so filter the measurement before you tune. The second is integral windup when the wheel is held, so clamp the integral term.
At a glance
- Per requirement
- 50 XP
- Completion bonus
- 600 XP
- Requirements
- 6
- Takes
- A season
- Skill level
- Advanced (years of practice)
- Cost
- Inexpensive (under $20)
- Supplies
- You need some
- Where
- At home
- Other people
- On your own
Requirements
-
1
Read a quadrature encoder with interrupts and print the count and the direction. Two channels, 90 degrees apart; the order they change tells you the direction. Count on both edges of one channel at least. Turn the wheel by hand ten times and the count should land on ten times the pulses per revolution. Take a screenshot of the counts and direction. Photo
-
2
Measure the speed and plot the open-loop step response. Step the PWM from 0 to 150 and record the RPM every 20 ms for two seconds. The curve shows the motor's lag and its top speed, which sizes your gains. Post the graph. Photo
-
3
Write a P controller, then add I, then D, and show each on a graph. Hold a setpoint of half the top speed. P alone leaves a steady error, I removes it, and D damps the overshoot. Plot all three responses on one chart with the setpoint line. Post the chart. Photo
-
4
Hold the setpoint while the load changes. Press a finger or a brake pad on the wheel, then release it. The RPM should dip and recover within a second, and the PWM should visibly rise to compensate. Keep fingers clear of spokes and gears. Film the test with a live RPM readout in frame. Video
-
5
Drive a two-wheel robot in a straight line and turn exact 90-degree angles by encoder counts. Two loops, one per wheel, with the same setpoint, and the robot stops drifting. For the turn, work out the counts from the wheel base and wheel diameter, then correct the number from a test. Film a square course with the robot ending where it started, within 5 cm. Video
-
6
Build one thing that only works because of the loop. A two-wheel balancing robot (the loop holds the tilt angle from an IMU), a motorized camera slider that moves at a constant speed, or a reel that keeps a string at constant tension. Film it working, and say what gains you ended with. Video