coffee/barista/overheat.c
Rodrigo Arias Mallo 8502ee3c5c Implement PID thremostat controller
For now only the proportional (Kp) and derivative (Kd) components are
used, the integral term is 0.
2025-11-02 19:24:02 +01:00

62 lines
1.1 KiB
C

/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "overheat.h"
#include <string.h>
void
overheat_init(struct overheat *o)
{
memset(o, 0, sizeof(struct overheat));
}
void
overheat_input(struct overheat *o, unsigned long t_ms, float temp)
{
unsigned long delta = t_ms - o->last_time;
if (delta < OVH_INTERVAL)
return;
/* If already go n samples, recompute delta and speed */
if (o->n == OVH_NSAMPLES) {
float last_T = o->temp[o->next];
float last_t = o->t[o->next];
float dt = (float) (t_ms - last_t) * 1e-3;
o->delta = temp - last_T;
o->speed = o->delta / dt;
}
/* Add the new sample */
o->temp[o->next] = temp;
o->t[o->next] = t_ms;
o->next++;
if (o->next >= OVH_NSAMPLES)
o->next = 0;
if (o->n < OVH_NSAMPLES)
o->n++;
o->last_time = t_ms;
}
float
overheat_delta(struct overheat *o)
{
return o->delta;
}
float
overheat_speed(struct overheat *o)
{
return o->speed;
}
int
overheat_panic(struct overheat *o)
{
if (o->speed > OVH_THRESHOLD)
return 1;
else
return 0;
}