coffee/barista/test/test_thermostat.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

50 lines
1.1 KiB
C

/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stdio.h>
#include "overheat.h"
#include "thermostat.h"
#include "heater.h"
/* Read a CSV from the stdin in the format
* <time_in_seconds> <state> <temp_in_C>
* skipping the first row (header).
*
* Outputs overheat state. */
#define MAX_LINE 1024
int main(void)
{
char buf[MAX_LINE];
fgets(buf, MAX_LINE, stdin);
float t, temp;
int st;
struct overheat ovh = { 0 };
struct thermostat th = { 0 };
struct heater heater = { 0 };
overheat_init(&ovh);
for (int i = 0; scanf("%f %d %f", &t, &st, &temp) == 3; i++) {
if (i == 0)
thermostat_set(&th, 60.0);
unsigned long t_ms = t * 1000;
overheat_input(&ovh, t_ms, temp);
float delta = overheat_delta(&ovh);
float speed = overheat_speed(&ovh);
float u = thermostat_state(&th, temp, speed);
heater_on(&heater, t_ms, u);
int h = heater_state(&heater, t_ms);
int panic = overheat_panic(&ovh);
printf("%8.3f %2d %6.2f %6.1f %8.3f %8.3f %d %s\n", t, st, temp, delta, speed, u, h, panic ? "PANIC" : "OK");
}
return 0;
}