Part 3 complete

This commit is contained in:
Thorsteinngretar
2026-09-18 20:25:16 +00:00
parent 728568402a
commit ee3e08fd12
@@ -0,0 +1,76 @@
# Project 2 – Part 3
## Measured values
From Part 2 with the wheel attached:
- Steady-state speed: approximately 77.4 RPM
- Time constant: τ ≈ 41 ms = 0.041 s
The controller update rate must be at least:
f_update >= 10 / τ
f_update >= 10 / 0.041 ≈ 244 Hz
A controller update frequency of 250 Hz is selected:
T_update = 1 / 250 = 4 ms
This satisfies the requirement because 4 ms < τ / 10 = 4.1 ms.
## Timing, methods and resources
| Task | Requirement | Method / Resource |
|---|---|---|
| Count encoder pulses | Capture all encoder edges | Pin-change interrupt on D8 |
| Determine direction | At each encoder edge | Read C2 on D9 |
| Calculate speed | Fresh value every 4 ms | Encoder count difference + Timer1 |
| Update controller | At least 244 Hz | Timer1 interrupt at 250 Hz |
| Generate PWM | Much faster than controller update | Timer2 hardware PWM |
| Send data to laptop | Not time-critical | Serial in main loop |
At the measured speed of 77.4 RPM and approximately 2100 counts/revolution:
f_encoder = (77.4 / 60) × 2100 ≈ 2709 counts/s
Therefore the time between encoder counts is approximately:
T_encoder = 1 / 2709 ≈ 369 us
The encoder interrupt must therefore execute in much less than 369 us.
## Timer and PWM choice
Timer1 will provide the 250 Hz controller time base.
With a 16 MHz clock and prescaler of 64:
OCR1A = (16000000 / (64 × 250)) - 1 = 999
Therefore Timer1 can generate an interrupt every 4 ms.
Timer2 will generate hardware PWM. Using Fast PWM with a prescaler of 8:
f_pwm = 16000000 / (8 × 256) ≈ 7.8 kHz
T_pwm ≈ 128 us
Since:
128 us << 4 ms
the PWM frequency is sufficiently faster than the controller update rate.
## Validation
| Requirement | Validation |
|---|---|
| Encoder counting | Compare measured count/RPM while rotating motor |
| 250 Hz controller update | Toggle debug pin and measure 4 ms period with oscilloscope |
| PWM frequency | Measure PWM output with oscilloscope; expect about 7.8 kHz |
| Controller execution time | Measure debug pulse around update routine; must be < 4 ms |
| Serial transmission | Keep Serial outside time-critical interrupt routines |