logging pid-parameters too

This commit is contained in:
Patrick Haßel 2024-04-15 14:09:16 +02:00
parent 3ca74503d9
commit be49cd9123
5 changed files with 67 additions and 87 deletions

View File

@ -31,7 +31,7 @@ private:
Entry *bufferWrite = buffer;
size_t dataCount = 0;
size_t usage = 0;
public:
@ -41,18 +41,18 @@ public:
}
bool add(const time_t timestamp, const T &data) {
if (dataCount >= size) {
if (usage >= size) {
return false;
}
bufferWrite->timestamp = timestamp;
bufferWrite->data = data;
bufferWrite = (bufferWrite - buffer + 1) % size + buffer;
dataCount++;
usage++;
return true;
}
void loop() {
if (dataCount == 0 || !isTimeSet()) {
if (usage == 0 || !isTimeSet()) {
return;
}
JsonDocument json;
@ -61,10 +61,18 @@ public:
bufferRead->data.toJson(data);
if (mqttPublishData(name, json)) {
bufferRead = (bufferRead - buffer + 1) % size + buffer;
dataCount--;
usage--;
}
}
size_t getUsage() const {
return usage;
}
size_t getSize() const {
return size;
}
};
#endif

View File

@ -15,8 +15,6 @@ private:
uint64_t address;
const char *name;
const double valueThreshold;
const time_t timeoutSec;
@ -37,10 +35,9 @@ private:
public:
DallasSensor(Dallas &sensors, const uint64_t address, const char *name, const double valueThreshold, const time_t timeoutSec, const time_t minIntervalMillis) :
DallasSensor(Dallas &sensors, const uint64_t address, const double valueThreshold, const time_t timeoutSec, const time_t minIntervalMillis) :
sensors(sensors),
address(address),
name(name),
valueThreshold(valueThreshold),
timeoutSec(timeoutSec),
minIntervalMillis(minIntervalMillis) {
@ -73,10 +70,6 @@ public:
return false;
}
[[nodiscard]] const char *getName() const {
return name;
}
[[nodiscard]] double getLastValue() const {
return lastValue;
}

View File

@ -1,4 +1,4 @@
#if defined(FERMENTER)
#if defined(FERMENTER) || defined(TEST8266)
#include <Patrix.h>
#include "sensors/Dallas.h"
@ -8,26 +8,27 @@
#include "console.h"
#include <Arduino.h>
#include "LedControl.h"
#include "FermenterData.h"
#define SENSOR_GPIO D4
#define CONTROL_GPIO D2
#define CONTROL_PWM_BITS 10
#define CONTROL_PWM_MAX (pow(2, CONTROL_PWM_BITS) - 1)
#define PID_DEFAULT_P 100
#define PID_DEFAULT_I 10
#define PID_DEFAULT_P 1500
#define PID_DEFAULT_I 0
#define PID_DEFAULT_D 0
#define PID_DEFAULT_TARGET 31
#define PID_DEFAULT_OVER 5
Dallas dallas(SENSOR_GPIO);
DallasSensor sensor(dallas, 0x3D0417C1D740FF28, "temperatur.ist", 0.1, 5, 60 * 1000);
ArduPID pid;
DallasSensor sensor(dallas, 0x3D0417C1D740FF28, 0.1, 5, 60 * 1000);
LedControl lc = LedControl(13, 14, 15, 1);
ArduPID pid;
double proportional = PID_DEFAULT_P;
double integral = PID_DEFAULT_I;
@ -42,41 +43,14 @@ double temperatureCurrent = NAN;
double heaterPWM = 0;
struct Data : IData {
double temperature;
double target;
double p;
double i;
double d;
Data() {
temperature = NAN;
target = NAN;
p = NAN;
i = NAN;
d = NAN;
}
Data(double temperature, double target, double p, double i, double d) : temperature(temperature), target(target), p(p), i(i), d(d) {
// -
}
void toJson(JsonObject &json) const override {
json["temperature"] = temperature;
json["target"] = target;
json["p"] = p;
json["i"] = i;
json["d"] = d;
}
};
Cache<Data, 500> cache("data");
Cache<FermenterData, 500> cache("data");
void writeDecimal(int *digit, double value);
void displayLoop();
void pidLoop();
void patrixSetup() {
dallas.begin();
analogWriteResolution(CONTROL_PWM_BITS);
@ -90,21 +64,16 @@ void patrixSetup() {
void patrixLoop() {
dallas.loop();
if (sensor.loop()) {
Data data = {
temperatureCurrent,
temperatureTarget,
proportional,
integral,
derivative,
};
const FermenterData data = {sensor.getLastValue(), temperatureTarget, proportional, integral, derivative};
cache.add(sensor.getLastTimestamp(), data);
}
cache.loop();
displayLoop();
pidLoop();
}
void pidLoop() {
temperatureCurrent = sensor.getLastValue();
if (!isnan(temperatureCurrent)) {
pid.compute();
@ -126,7 +95,10 @@ void patrixLoop() {
lastDebug = now;
int heaterPercent = (int) round(100.0 * heaterPWM / CONTROL_PWM_MAX);
debug(
"p: %f | i: %f | d: %f | t: %4.1f | %3d%% | %5.2f %s",
"cache: %3d/%d | p: %f | i: %f | d: %f | target: %4.1f | heater: %3d%% | temperature: %5.2f %s",
cache.getUsage(),
cache.getSize(),
proportional == 0 ? NAN : proportional,
integral == 0 ? NAN : integral,
derivative == 0 ? NAN : derivative,
@ -140,7 +112,6 @@ void patrixLoop() {
}
}
void displayLoop() {
static unsigned long lastDisplayInit = 0;
if (lastDisplayInit == 0 || millis() - lastDisplayInit > 60 * 60 * 1000) {

View File

@ -0,0 +1,35 @@
#ifndef SENSOR3_FERMENTERDATA_H
#define SENSOR3_FERMENTERDATA_H
#include "data.h"
struct FermenterData : IData {
double temperature;
double target;
double p;
double i;
double d;
FermenterData() {
temperature = NAN;
target = NAN;
p = NAN;
i = NAN;
d = NAN;
}
FermenterData(double temperature, double target, double p, double i, double d) : temperature(temperature), target(target), p(p), i(i), d(d) {
// -
}
void toJson(JsonObject &json) const override {
json["temperature"] = temperature;
json["target"] = target;
json["p"] = p;
json["i"] = i;
json["d"] = d;
}
};
#endif

View File

@ -1,27 +0,0 @@
#if defined(TEST8266) || defined(TEST32)
#include <Patrix.h>
#include "config.h"
void patrixSetup() {
}
void patrixLoop() {
}
void configLoaded() {
}
bool patrix_command(char *first) {
if (strcmp(first, "test") == 0 || strcmp(first, "t") == 0) {
configPutDouble("test", millis());
return true;
}
return false;
}
#endif