1 |
/*
|
2 |
* main.c
|
3 |
*
|
4 |
* ATtiny85 / AVR-GCC
|
5 |
*
|
6 |
* CPU: 8 MHz
|
7 |
* Fan PWM: 25 kHz, OC0B / PB1
|
8 |
* Optical sensor: PB3 / PCINT3, active LOW
|
9 |
* Target: 90 RPM (1 pulse/revolution)
|
10 |
* Measurement: 10-second averaging window
|
11 |
* PID: Q8.8 fixed point
|
12 |
* Autotune: Relay / Ziegler-Nichols
|
13 |
* EEPROM: PID parameter storage
|
14 |
*
|
15 |
* Compile:
|
16 |
* avr-gcc -mmcu=attiny85 -DF_CPU=8000000UL \
|
17 |
* -Os -std=gnu11 -Wall main.c -o main.elf
|
18 |
*
|
19 |
* IMPORTANT:
|
20 |
* Disable CKDIV8 fuse and configure the internal
|
21 |
* oscillator for 8 MHz.
|
22 |
*/
|
23 |
|
24 |
#define F_CPU 8000000UL
|
25 |
|
26 |
#include <avr/io.h>
|
27 |
#include <avr/interrupt.h>
|
28 |
#include <avr/eeprom.h>
|
29 |
#include <util/atomic.h>
|
30 |
#include <stdint.h>
|
31 |
|
32 |
/* ---------------- Configuration ---------------- */
|
33 |
|
34 |
#define TARGET_RPM 90
|
35 |
#define PWM_TOP 39
|
36 |
#define PWM_MAX 39
|
37 |
#define PWM_MIN 0
|
38 |
|
39 |
#define STOP_TIMEOUT_MS 2000UL
|
40 |
#define WINDOW_MS 10000UL
|
41 |
|
42 |
#define AUTOTUNE_CENTER 20
|
43 |
#define AUTOTUNE_DELTA 4
|
44 |
#define AUTOTUNE_CYCLES 6
|
45 |
|
46 |
#define Q8 256L
|
47 |
|
48 |
/* ---------------- EEPROM ---------------- */
|
49 |
|
50 |
typedef struct {
|
51 |
uint16_t magic;
|
52 |
int16_t kp;
|
53 |
int16_t ki;
|
54 |
int16_t kd;
|
55 |
uint16_t checksum;
|
56 |
} PID_EEPROM;
|
57 |
|
58 |
PID_EEPROM EEMEM ee_pid;
|
59 |
|
60 |
#define EEPROM_MAGIC 0x85A5
|
61 |
|
62 |
/* ---------------- Global variables ---------------- */
|
63 |
|
64 |
volatile uint32_t ms_ticks = 0;
|
65 |
|
66 |
volatile uint32_t last_pulse_ms = 0;
|
67 |
volatile uint32_t pulse_period_ms = 0;
|
68 |
|
69 |
volatile uint8_t pulse_received = 0;
|
70 |
volatile uint8_t have_pulse = 0;
|
71 |
|
72 |
static uint8_t pwm_value = 20;
|
73 |
|
74 |
static int16_t kp = 26; // Q8.8
|
75 |
static int16_t ki = 2; // Q8.8
|
76 |
static int16_t kd = 0; // Q8.8
|
77 |
|
78 |
static int32_t integral = 20L * Q8;
|
79 |
static int16_t last_error = 0;
|
80 |
|
81 |
static uint32_t last_control_ms = 0;
|
82 |
static uint32_t last_window_ms = 0;
|
83 |
|
84 |
static uint32_t period_sum = 0;
|
85 |
static uint16_t period_count = 0;
|
86 |
|
87 |
static uint16_t average_rpm = 0;
|
88 |
static uint8_t stopped = 1;
|
89 |
|
90 |
/* ---------------- Autotune ---------------- */
|
91 |
|
92 |
static uint8_t autotune_active = 0;
|
93 |
static uint8_t autotune_done = 0;
|
94 |
|
95 |
static uint8_t relay_state = 0;
|
96 |
static uint8_t crossing_count = 0;
|
97 |
|
98 |
static uint16_t rpm_min = 65535;
|
99 |
static uint16_t rpm_max = 0;
|
100 |
|
101 |
static uint32_t last_crossing_ms = 0;
|
102 |
static uint32_t oscillation_period_ms = 0;
|
103 |
|
104 |
/* ---------------- Utility ---------------- */
|
105 |
|
106 |
static uint32_t get_ms(void)
|
107 |
{
|
108 |
uint32_t t;
|
109 |
|
110 |
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
111 |
t = ms_ticks;
|
112 |
}
|
113 |
|
114 |
return t;
|
115 |
}
|
116 |
|
117 |
static void set_pwm(uint8_t value)
|
118 |
{
|
119 |
if (value > PWM_MAX)
|
120 |
value = PWM_MAX;
|
121 |
|
122 |
if (value < PWM_MIN)
|
123 |
value = PWM_MIN;
|
124 |
|
125 |
pwm_value = value;
|
126 |
OCR0B = value;
|
127 |
}
|
128 |
|
129 |
/* ---------------- Timer0 / PWM ---------------- */
|
130 |
|
131 |
static void hardware_init(void)
|
132 |
{
|
133 |
/* PB1 = OC0B output */
|
134 |
DDRB |= (1 << PB1);
|
135 |
|
136 |
/* PB3 = sensor input */
|
137 |
DDRB &= ~(1 << PB3);
|
138 |
|
139 |
/* Internal pull-up */
|
140 |
PORTB |= (1 << PB3);
|
141 |
|
142 |
/*
|
143 |
* Fast PWM, OCR0A as TOP.
|
144 |
* Inverting OC0B output.
|
145 |
*
|
146 |
* 8 MHz / 8 / (39 + 1) = 25 kHz
|
147 |
*/
|
148 |
|
149 |
TCCR0A =
|
150 |
(1 << WGM00) |
|
151 |
(1 << WGM01) |
|
152 |
(1 << COM0B1) |
|
153 |
(1 << COM0B0);
|
154 |
|
155 |
TCCR0B =
|
156 |
(1 << WGM02) |
|
157 |
(1 << CS01);
|
158 |
|
159 |
OCR0A = PWM_TOP;
|
160 |
OCR0B = pwm_value;
|
161 |
|
162 |
/* Compare A interrupt: 25 kHz */
|
163 |
TIMSK |= (1 << OCIE0A);
|
164 |
|
165 |
/* Pin change interrupt PB3 */
|
166 |
GIMSK |= (1 << PCIE);
|
167 |
PCMSK |= (1 << PCINT3);
|
168 |
|
169 |
sei();
|
170 |
}
|
171 |
|
172 |
/* ---------------- Timer interrupt ---------------- */
|
173 |
|
174 |
ISR(TIMER0_COMPA_vect)
|
175 |
{
|
176 |
static uint8_t divider = 0;
|
177 |
|
178 |
if (++divider >= 25) {
|
179 |
divider = 0;
|
180 |
ms_ticks++;
|
181 |
}
|
182 |
}
|
183 |
|
184 |
/* ---------------- Sensor interrupt ---------------- */
|
185 |
|
186 |
ISR(PCINT0_vect)
|
187 |
{
|
188 |
/* Only count falling edges */
|
189 |
if (!(PINB & (1 << PB3))) {
|
190 |
|
191 |
uint32_t now = ms_ticks;
|
192 |
|
193 |
if (!have_pulse) {
|
194 |
|
195 |
last_pulse_ms = now;
|
196 |
have_pulse = 1;
|
197 |
pulse_received = 1;
|
198 |
|
199 |
} else {
|
200 |
|
201 |
uint32_t dt = now - last_pulse_ms;
|
202 |
|
203 |
/* Reject implausibly short pulses */
|
204 |
if (dt >= 50) {
|
205 |
|
206 |
pulse_period_ms = dt;
|
207 |
last_pulse_ms = now;
|
208 |
pulse_received = 1;
|
209 |
}
|
210 |
}
|
211 |
}
|
212 |
}
|
213 |
|
214 |
/* ---------------- EEPROM ---------------- */
|
215 |
|
216 |
static uint16_t checksum_pid(int16_t p,
|
217 |
int16_t i,
|
218 |
int16_t d)
|
219 |
{
|
220 |
return (uint16_t)(0x5A5A ^ p ^ i ^ d);
|
221 |
}
|
222 |
|
223 |
static void load_pid(void)
|
224 |
{
|
225 |
PID_EEPROM data;
|
226 |
|
227 |
eeprom_read_block(&data, &ee_pid, sizeof(data));
|
228 |
|
229 |
if (data.magic == EEPROM_MAGIC &&
|
230 |
data.checksum ==
|
231 |
checksum_pid(data.kp, data.ki, data.kd)) {
|
232 |
|
233 |
kp = data.kp;
|
234 |
ki = data.ki;
|
235 |
kd = data.kd;
|
236 |
|
237 |
autotune_done = 1;
|
238 |
}
|
239 |
}
|
240 |
|
241 |
static void save_pid(void)
|
242 |
{
|
243 |
PID_EEPROM data;
|
244 |
|
245 |
data.magic = EEPROM_MAGIC;
|
246 |
data.kp = kp;
|
247 |
data.ki = ki;
|
248 |
data.kd = kd;
|
249 |
|
250 |
data.checksum =
|
251 |
checksum_pid(kp, ki, kd);
|
252 |
|
253 |
eeprom_update_block(&data, &ee_pid, sizeof(data));
|
254 |
}
|
255 |
|
256 |
/* ---------------- Measurement ---------------- */
|
257 |
|
258 |
static void process_measurement(uint32_t now)
|
259 |
{
|
260 |
uint32_t period;
|
261 |
|
262 |
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
263 |
period = pulse_period_ms;
|
264 |
}
|
265 |
|
266 |
if (period == 0)
|
267 |
return;
|
268 |
|
269 |
/* Start a new averaging window */
|
270 |
if (now - last_window_ms >= WINDOW_MS) {
|
271 |
|
272 |
period_sum = 0;
|
273 |
period_count = 0;
|
274 |
last_window_ms = now;
|
275 |
}
|
276 |
|
277 |
period_sum += period;
|
278 |
period_count++;
|
279 |
|
280 |
/*
|
281 |
* Average RPM:
|
282 |
*
|
283 |
* RPM = 60000 * number_of_periods / sum_of_periods
|
284 |
*/
|
285 |
|
286 |
if (period_sum > 0) {
|
287 |
|
288 |
average_rpm =
|
289 |
(uint32_t)60000UL * period_count /
|
290 |
period_sum;
|
291 |
}
|
292 |
|
293 |
stopped = 0;
|
294 |
}
|
295 |
|
296 |
/* ---------------- PID ---------------- */
|
297 |
|
298 |
static void pid_update(uint16_t measured,
|
299 |
uint32_t dt_ms)
|
300 |
{
|
301 |
if (dt_ms == 0)
|
302 |
return;
|
303 |
|
304 |
int16_t error =
|
305 |
TARGET_RPM - (int16_t)measured;
|
306 |
|
307 |
/* Proportional */
|
308 |
int32_t p =
|
309 |
(int32_t)kp * error;
|
310 |
|
311 |
/* Integral */
|
312 |
integral +=
|
313 |
((int32_t)ki * error * dt_ms) / 1000L;
|
314 |
|
315 |
/* Anti-windup */
|
316 |
if (integral < 0)
|
317 |
integral = 0;
|
318 |
|
319 |
if (integral > PWM_MAX * Q8)
|
320 |
integral = PWM_MAX * Q8;
|
321 |
|
322 |
/* Derivative */
|
323 |
int32_t d =
|
324 |
(int32_t)kd *
|
325 |
(error - last_error) *
|
326 |
1000L / dt_ms;
|
327 |
|
328 |
last_error = error;
|
329 |
|
330 |
int32_t output =
|
331 |
(integral + p + d) / Q8;
|
332 |
|
333 |
if (output < PWM_MIN)
|
334 |
output = PWM_MIN;
|
335 |
|
336 |
if (output > PWM_MAX)
|
337 |
output = PWM_MAX;
|
338 |
|
339 |
set_pwm((uint8_t)output);
|
340 |
}
|
341 |
|
342 |
/* ---------------- Relay Autotune ---------------- */
|
343 |
|
344 |
static void start_autotune(uint32_t now)
|
345 |
{
|
346 |
autotune_active = 1;
|
347 |
|
348 |
relay_state = 0;
|
349 |
crossing_count = 0;
|
350 |
|
351 |
rpm_min = 65535;
|
352 |
rpm_max = 0;
|
353 |
|
354 |
last_crossing_ms = now;
|
355 |
|
356 |
oscillation_period_ms = 0;
|
357 |
|
358 |
integral = AUTOTUNE_CENTER * Q8;
|
359 |
|
360 |
set_pwm(AUTOTUNE_CENTER + AUTOTUNE_DELTA);
|
361 |
}
|
362 |
|
363 |
static void autotune_update(uint16_t rpm,
|
364 |
uint32_t now)
|
365 |
{
|
366 |
if (!autotune_active)
|
367 |
return;
|
368 |
|
369 |
if (rpm < rpm_min)
|
370 |
rpm_min = rpm;
|
371 |
|
372 |
if (rpm > rpm_max)
|
373 |
rpm_max = rpm;
|
374 |
|
375 |
/*
|
376 |
* Relay switching around target.
|
377 |
*/
|
378 |
|
379 |
uint8_t new_state = (rpm >= TARGET_RPM);
|
380 |
|
381 |
if (new_state != relay_state) {
|
382 |
|
383 |
relay_state = new_state;
|
384 |
|
385 |
if (crossing_count > 0) {
|
386 |
|
387 |
oscillation_period_ms =
|
388 |
now - last_crossing_ms;
|
389 |
}
|
390 |
|
391 |
last_crossing_ms = now;
|
392 |
crossing_count++;
|
393 |
|
394 |
if (relay_state) {
|
395 |
set_pwm(AUTOTUNE_CENTER -
|
396 |
AUTOTUNE_DELTA);
|
397 |
} else {
|
398 |
set_pwm(AUTOTUNE_CENTER +
|
399 |
AUTOTUNE_DELTA);
|
400 |
}
|
401 |
}
|
402 |
|
403 |
/*
|
404 |
* Calculate parameters after sufficient
|
405 |
* oscillations.
|
406 |
*/
|
407 |
|
408 |
if (crossing_count >= AUTOTUNE_CYCLES * 2 &&
|
409 |
oscillation_period_ms > 100) {
|
410 |
|
411 |
uint16_t amplitude =
|
412 |
(rpm_max - rpm_min) / 2;
|
413 |
|
414 |
if (amplitude >= 1) {
|
415 |
|
416 |
/*
|
417 |
* Ku Q8.8:
|
418 |
* Ku = 1.273 * relay_amplitude / rpm_amplitude
|
419 |
*/
|
420 |
|
421 |
int32_t ku =
|
422 |
(326L * AUTOTUNE_DELTA) /
|
423 |
amplitude;
|
424 |
|
425 |
/* Ziegler-Nichols PID */
|
426 |
kp = (int16_t)((ku * 3) / 5);
|
427 |
|
428 |
ki = (int16_t)(
|
429 |
(ku * 1200L) /
|
430 |
oscillation_period_ms
|
431 |
);
|
432 |
|
433 |
kd = (int16_t)(
|
434 |
(ku * 75L *
|
435 |
oscillation_period_ms) /
|
436 |
1000000L
|
437 |
);
|
438 |
|
439 |
/* Parameter limits */
|
440 |
if (kp < 1) kp = 1;
|
441 |
if (ki < 0) ki = 0;
|
442 |
if (kd < 0) kd = 0;
|
443 |
|
444 |
if (kp > 4096) kp = 4096;
|
445 |
if (ki > 1024) ki = 1024;
|
446 |
if (kd > 4096) kd = 4096;
|
447 |
|
448 |
integral = pwm_value * Q8;
|
449 |
|
450 |
autotune_active = 0;
|
451 |
autotune_done = 1;
|
452 |
|
453 |
save_pid();
|
454 |
}
|
455 |
}
|
456 |
|
457 |
/* Autotune timeout: 180 seconds */
|
458 |
if (now - last_crossing_ms > 180000UL) {
|
459 |
|
460 |
autotune_active = 0;
|
461 |
autotune_done = 0;
|
462 |
|
463 |
/* Conservative fallback */
|
464 |
kp = 26;
|
465 |
ki = 2;
|
466 |
kd = 0;
|
467 |
}
|
468 |
}
|
469 |
|
470 |
/* ---------------- Main ---------------- */
|
471 |
|
472 |
int main(void)
|
473 |
{
|
474 |
hardware_init();
|
475 |
|
476 |
load_pid();
|
477 |
|
478 |
uint32_t now = get_ms();
|
479 |
|
480 |
last_window_ms = now;
|
481 |
last_control_ms = now;
|
482 |
|
483 |
set_pwm(20);
|
484 |
|
485 |
while (1) {
|
486 |
|
487 |
now = get_ms();
|
488 |
|
489 |
uint8_t new_pulse = 0;
|
490 |
|
491 |
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
492 |
|
493 |
if (pulse_received) {
|
494 |
pulse_received = 0;
|
495 |
new_pulse = 1;
|
496 |
}
|
497 |
}
|
498 |
|
499 |
/*
|
500 |
* Process every new sensor pulse.
|
501 |
*/
|
502 |
|
503 |
if (new_pulse) {
|
504 |
|
505 |
process_measurement(now);
|
506 |
|
507 |
if (!autotune_done &&
|
508 |
!autotune_active) {
|
509 |
|
510 |
start_autotune(now);
|
511 |
}
|
512 |
|
513 |
if (autotune_active) {
|
514 |
|
515 |
autotune_update(average_rpm, now);
|
516 |
|
517 |
} else {
|
518 |
|
519 |
uint32_t dt =
|
520 |
now - last_control_ms;
|
521 |
|
522 |
if (dt > 0) {
|
523 |
pid_update(average_rpm, dt);
|
524 |
last_control_ms = now;
|
525 |
}
|
526 |
}
|
527 |
}
|
528 |
|
529 |
/*
|
530 |
* Stop detection.
|
531 |
*/
|
532 |
|
533 |
uint32_t last;
|
534 |
|
535 |
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
536 |
last = last_pulse_ms;
|
537 |
}
|
538 |
|
539 |
if (have_pulse &&
|
540 |
now - last > STOP_TIMEOUT_MS) {
|
541 |
|
542 |
stopped = 1;
|
543 |
average_rpm = 0;
|
544 |
}
|
545 |
|
546 |
/*
|
547 |
* Keep regulating at standstill.
|
548 |
* This allows the fan to restart without
|
549 |
* a separate startup boost.
|
550 |
*/
|
551 |
|
552 |
if (stopped &&
|
553 |
!autotune_active &&
|
554 |
now - last_control_ms >= 1000) {
|
555 |
|
556 |
pid_update(0,
|
557 |
now - last_control_ms);
|
558 |
|
559 |
last_control_ms = now;
|
560 |
}
|
561 |
|
562 |
/*
|
563 |
* Autotune must not continue without
|
564 |
* valid sensor feedback.
|
565 |
*/
|
566 |
|
567 |
if (autotune_active &&
|
568 |
have_pulse &&
|
569 |
now - last > STOP_TIMEOUT_MS) {
|
570 |
|
571 |
autotune_active = 0;
|
572 |
autotune_done = 0;
|
573 |
|
574 |
kp = 26;
|
575 |
ki = 2;
|
576 |
kd = 0;
|
577 |
}
|
578 |
}
|
579 |
}
|