add firehazard.c
This commit is contained in:
@@ -19,6 +19,8 @@ set(COMPONENT_SRCS
|
||||
"web/websrv.c"
|
||||
"files/files_enum.c"
|
||||
"utils.c"
|
||||
"arduinopid.c"
|
||||
"firehazard.c"
|
||||
)
|
||||
set(COMPONENT_ADD_INCLUDEDIRS "." "liquid" "graphics")
|
||||
|
||||
|
||||
+39
-16
@@ -6,6 +6,7 @@
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/adc.h"
|
||||
#include "esp_adc_cal.h"
|
||||
#include "firehazard.h"
|
||||
|
||||
static esp_adc_cal_characteristics_t *adc_chars;
|
||||
|
||||
@@ -20,15 +21,12 @@ float reg_meas_history[REG_HISTORY_LEN] = {};
|
||||
float reg_tset_history[REG_HISTORY_LEN] = {};
|
||||
uint32_t history_counter = 0;
|
||||
|
||||
// TODO move to regulator module (make extern)
|
||||
float reg_setpoint = 125;
|
||||
|
||||
static void analog_service(void *arg);
|
||||
|
||||
static TaskHandle_t hAnalog;
|
||||
|
||||
#define DEFAULT_VREF 1100
|
||||
#define NO_OF_SAMPLES 64
|
||||
#define DEFAULT_VREF 1100 // TODO try to find the exact value
|
||||
#define NO_OF_SAMPLES 128
|
||||
|
||||
void analog_init() {
|
||||
printf("Analog init\n");
|
||||
@@ -42,6 +40,32 @@ void analog_init() {
|
||||
assert (rv == pdPASS);
|
||||
}
|
||||
|
||||
#define TSENSE_LOOKUP_LEN 81
|
||||
#define TSENSE_T_STEP 5
|
||||
#define TSENSE_T_MIN 0
|
||||
#define TSENSE_T_MAX 400
|
||||
static const float TSENSE_LOOKUP[TSENSE_LOOKUP_LEN] = {
|
||||
// 4k7
|
||||
//0.067859346082665f, 0.069156572911158f, 0.070450833857595f, 0.07174213479836f, 0.073030481589859f, 0.074315880068592f, 0.075598336051229f, 0.076877855334685f, 0.078154443696192f, 0.079428106893372f, 0.080698850664312f, 0.081966680727637f, 0.083231602782579f, 0.084493622509052f, 0.085752745567722f, 0.087008977600079f, 0.088262324228509f, 0.089512791056363f, 0.090760383668026f, 0.092005107628991f, 0.093246968485926f, 0.094485971766743f, 0.095722122980667f, 0.096955427618307f, 0.098185891151722f, 0.099413519034488f, 0.10063831670177f, 0.101860289570385f, 0.10307944303887f, 0.10429578248755f, 0.105509313278605f, 0.106720040756132f, 0.107927970246218f, 0.109133107056997f, 0.110335456478721f, 0.111535023783824f, 0.112731814226983f, 0.113925833045189f, 0.115117085457804f, 0.116305576666627f, 0.117491311855962f, 0.118674296192672f, 0.119854534826251f, 0.12103203288888f, 0.122206795495492f, 0.123378827743833f, 0.124548134714525f, 0.125714721471126f, 0.12687859306019f, 0.128039754511331f, 0.129198210837281f, 0.13035396703395f, 0.131507028080486f, 0.132657398939339f, 0.133805084556313f, 0.134950089860629f, 0.136092419764986f, 0.137232079165615f, 0.138369072942339f, 0.139503405958634f, 0.14063508306168f, 0.141764109082427f, 0.142890488835645f, 0.144014227119983f, 0.145135328718029f, 0.146253798396361f, 0.147369640905609f, 0.148482860980504f, 0.14959346333994f, 0.150701452687026f, 0.151806833709142f, 0.152909611077994f, 0.154009789449667f, 0.155107373464683f, 0.156202367748052f, 0.157294776909324f, 0.15838460554265f, 0.159471858226827f, 0.160556539525357f, 0.161638653986497f, 0.162718206143312f
|
||||
// 2k7
|
||||
//0.118961788031723f,0.121199278149888f,0.123430305551104f,0.125654890818048f,0.12787305443543f,0.130084816790549f,0.132290198173841f,0.134489218779432f,0.13668189870568f,0.138868257955715f,0.141048316437981f,0.143222093966762f,0.145389610262718f,0.147550884953408f,0.149705937573814f,0.151854787566858f,0.153997454283918f,0.156133956985341f,0.158264314840951f,0.160388546930552f,0.162506672244433f,0.164618709683859f,0.166724678061576f,0.168824596102292f,0.170918482443171f,0.173006355634316f,0.17508823413925f,0.177164136335394f,0.179234080514542f,0.181298084883333f,0.183356167563718f,0.185408346593427f,0.187454639926429f,0.189495065433395f,0.191529640902147f,0.19355838403812f,0.195581312464802f,0.197598443724189f,0.199609795277224f,0.20161538450424f,0.203615228705396f,0.205609345101115f,0.207597750832512f,0.209580462961826f,0.211557498472848f,0.213528874271338f,0.215494607185455f,0.217454713966165f,0.219409211287665f,0.221358115747788f,0.223301443868418f,0.225239212095895f,0.227171436801418f,0.229098134281449f,0.231019320758111f,0.232935012379582f,0.234845225220494f,0.236749975282318f,0.238649278493758f,0.240543150711133f,0.242431607718764f,0.24431466522935f,0.246192338884353f,0.248064644254368f,0.249931596839502f,0.25179321206974f,0.253649505305317f,0.255500491837083f,0.257346186886869f,0.259186605607846f,0.261021763084885f,0.262851674334914f,0.264676354307276f,0.266495817884073f,0.268310079880525f,0.270119155045314f,0.271923058060928f,0.273721803544007f,0.275515406045682f,0.277303880051916f,0.27908723998384f
|
||||
0.118709444844989f,0.120942188771995f,0.123168483690844f,0.125388350140555f,0.127601808562385f,0.129808879300387f,0.132009582601957f,0.134203938618385f,0.136391967405395f,0.138573688923688f,0.140749123039476f,0.142918289525014f,0.145081208059131f,0.14723789822775f,0.149388379524415f,0.151532671350807f,0.153670793017255f,0.155802763743251f,0.157928602657955f,0.1600483288007f,0.16216196112149f,0.1642695184815f,0.166371019653566f,0.168466483322681f,0.170555928086473f,0.172639372455698f,0.174716834854712f,0.176788333621955f,0.178853887010421f,0.180913513188126f,0.182967230238583f,0.185015056161259f,0.18705700887204f,0.189093106203687f,0.191123365906294f,0.193147805647736f,0.195166443014119f,0.197179295510229f,0.19918638055997f,0.201187715506807f,0.203183317614203f,0.205173204066052f,0.207157391967109f,0.209135898343422f,0.211108740142754f,0.213075934235005f,0.215037497412637f,0.216993446391085f,0.218943797809176f,0.220888568229535f,0.222827774139f,0.224761431949025f,0.226689557996082f,0.228612168542064f,0.230529279774684f,0.232440907807868f,0.234347068682147f,0.236247778365053f,0.238143052751499f,0.24003290766417f,0.241917358853906f,0.243796422000076f,0.245670112710962f,0.247538446524132f,0.249401438906812f,0.251259105256259f,0.253111460900123f,0.254958521096822f,0.256800301035897f,0.258636815838374f,0.260468080557129f,0.262294110177234f,0.264114919616321f,0.265930523724925f,0.267740937286839f,0.269546175019461f,0.271346251574132f,0.273141181536489f,0.274930979426797f,0.276715659700291f,0.278495236747511f
|
||||
};
|
||||
|
||||
static float v_to_c(float v){
|
||||
// TODO use binary search.. lol
|
||||
for (int i = 1; i < TSENSE_LOOKUP_LEN; i++) {
|
||||
float cur = TSENSE_LOOKUP[i];
|
||||
if (cur >= v) {
|
||||
float prev = TSENSE_LOOKUP[i-1];
|
||||
|
||||
float ratio = (v - prev) / (cur - prev);
|
||||
return TSENSE_T_MIN + ((float) i + ratio) * TSENSE_T_STEP;
|
||||
}
|
||||
}
|
||||
|
||||
return TSENSE_T_MAX;
|
||||
}
|
||||
|
||||
static void __attribute__((noreturn)) analog_service(void *arg) {
|
||||
while (1) {
|
||||
@@ -53,20 +77,17 @@ static void __attribute__((noreturn)) analog_service(void *arg) {
|
||||
adc_reading /= NO_OF_SAMPLES;
|
||||
|
||||
//Convert adc_reading to voltage in mV
|
||||
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
|
||||
uint32_t mv = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
|
||||
|
||||
#define CORRECT -10;
|
||||
voltage += CORRECT;
|
||||
mv += CORRECT;
|
||||
|
||||
// printf("Raw: %d ... Voltage: %dmV ...", adc_reading, voltage);
|
||||
printf("Raw: %d ... Voltage: %dmV ...", adc_reading, mv);
|
||||
|
||||
float volts = voltage * 0.001f;
|
||||
float volts = mv * 0.001f;
|
||||
|
||||
#define R1 4750
|
||||
#define V1 3.3f
|
||||
float r_pt100 = (volts * R1)/(V1 - volts);
|
||||
float celsius = (r_pt100/100.0f - 1.0f) / 3.9083E-3f;
|
||||
// printf("Rpt %.3f, Celsius: %.1f\n", r_pt100, celsius);
|
||||
float celsius = v_to_c(volts);
|
||||
printf("Celsius: %.1f°C\n", celsius);
|
||||
|
||||
measurement_celsius = celsius;
|
||||
|
||||
@@ -75,10 +96,12 @@ static void __attribute__((noreturn)) analog_service(void *arg) {
|
||||
reg_tset_history[i] = reg_tset_history[i+1];
|
||||
}
|
||||
reg_meas_history[REG_HISTORY_LEN-1] = celsius;
|
||||
reg_tset_history[REG_HISTORY_LEN-1] = reg_setpoint;
|
||||
reg_tset_history[REG_HISTORY_LEN-1] = fire_get_setpoint(true);
|
||||
history_counter = (history_counter + 1) % 20;
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
fire_regulate(celsius);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(ANALOG_SAMPLE_TIME_MS));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -7,7 +7,9 @@
|
||||
#ifndef REFLOWER_ANALOG_H
|
||||
#define REFLOWER_ANALOG_H
|
||||
|
||||
#define REG_HISTORY_LEN 121
|
||||
#define ANALOG_SAMPLE_TIME_MS 1000
|
||||
|
||||
#define REG_HISTORY_LEN 241
|
||||
extern float reg_meas_history[REG_HISTORY_LEN];
|
||||
extern float reg_tset_history[REG_HISTORY_LEN];
|
||||
extern uint32_t history_counter;
|
||||
|
||||
+32
-1
@@ -9,6 +9,7 @@
|
||||
#include <stdio.h>
|
||||
#include <web/websrv.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_log.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
@@ -20,6 +21,7 @@
|
||||
#include "utils.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "firehazard.h"
|
||||
|
||||
/**
|
||||
* Application event handler
|
||||
@@ -75,7 +77,17 @@ static void initialise_wifi(void)
|
||||
|
||||
void __attribute__((noreturn)) app_main()
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
// Initialize NVS
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
// NVS partition was truncated and needs to be erased
|
||||
// Retry nvs_flash_init
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK( err );
|
||||
|
||||
|
||||
//ESP_ERROR_CHECK(esp_register_shutdown_handler(cspemu_run_shutdown_handlers));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
@@ -108,6 +120,25 @@ void __attribute__((noreturn)) app_main()
|
||||
|
||||
try_reconn_if_have_wifi_creds();
|
||||
websrv_init();
|
||||
fire_init();
|
||||
|
||||
nvs_handle nvs;
|
||||
|
||||
ESP_ERROR_CHECK(nvs_open("config", NVS_READWRITE, &nvs)); // must use RW to allow opening nonexistent
|
||||
|
||||
union uf32 kp, ki, kd;
|
||||
|
||||
// TODO set good defaults
|
||||
kp.f = 0.3f;
|
||||
ki.f = 0.5f;
|
||||
kd.f = 0.0f;
|
||||
|
||||
nvs_get_u32(nvs, "kp", &kp.u);
|
||||
nvs_get_u32(nvs, "ki", &ki.u);
|
||||
nvs_get_u32(nvs, "kd", &kd.u);
|
||||
|
||||
fire_set_tuning(kp.f, ki.f, kd.f);
|
||||
nvs_close(nvs);
|
||||
|
||||
bool level = 0;
|
||||
while (1) {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
#include "arduinopid.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
static void clampOutput(struct PID *self) {
|
||||
if (self->Output > self->outMax) self->Output = self->outMax;
|
||||
else if (self->Output < self->outMin) self->Output = self->outMin;
|
||||
}
|
||||
|
||||
static void clampIterm(struct PID *self) {
|
||||
if (self->ITerm > self->outMax) self->ITerm = self->outMax;
|
||||
else if (self->ITerm < self->outMin) self->ITerm = self->outMin;
|
||||
}
|
||||
|
||||
void PID_Compute(struct PID *self, float Input)
|
||||
{
|
||||
if (!self->ctlMode) return;
|
||||
self->Input = Input;
|
||||
|
||||
uint32_t now = xTaskGetTickCount();
|
||||
int32_t timeChange = (now - self->lastTime);
|
||||
if (timeChange >= self->SampleTime) {
|
||||
/*Compute all the working error variables*/
|
||||
float error = self->Setpoint - Input;
|
||||
self->ITerm += (self->ki * error);
|
||||
|
||||
clampIterm(self);
|
||||
|
||||
float dInput = (Input - self->lastInput);
|
||||
|
||||
/*Compute PID Output*/
|
||||
self->Output = self->kp * error + self->ITerm - self->kd * dInput;
|
||||
|
||||
clampOutput(self);
|
||||
|
||||
/*Remember some variables for next time*/
|
||||
self->lastInput = Input;
|
||||
self->lastTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
void PID_SetSetpoint(struct PID *self, float Setpoint)
|
||||
{
|
||||
self->Setpoint = Setpoint;
|
||||
}
|
||||
|
||||
void PID_SetTunings(struct PID *self, float Kp, float Ki, float Kd)
|
||||
{
|
||||
if (Kp < 0 || Ki < 0 || Kd < 0) return;
|
||||
|
||||
float SampleTimeInSec = ((float) self->SampleTime) / 1000;
|
||||
self->kp = Kp;
|
||||
self->ki = Ki * SampleTimeInSec;
|
||||
self->kd = Kd / SampleTimeInSec;
|
||||
|
||||
if (self->controllerDirection == PID_REVERSE) {
|
||||
self->kp = -self->kp;
|
||||
self->ki = -self->ki;
|
||||
self->kd = -self->kd;
|
||||
}
|
||||
}
|
||||
|
||||
void PID_SetSampleTime(struct PID *self, uint32_t NewSampleTime)
|
||||
{
|
||||
if (NewSampleTime > 0) {
|
||||
float ratio = (float) NewSampleTime
|
||||
/ (float) self->SampleTime;
|
||||
self->ki *= ratio;
|
||||
self->kd /= ratio;
|
||||
self->SampleTime = (uint32_t) NewSampleTime;
|
||||
}
|
||||
}
|
||||
|
||||
void PID_SetOutputLimits(struct PID *self, float Min, float Max)
|
||||
{
|
||||
if (Min > Max) return;
|
||||
self->outMin = Min;
|
||||
self->outMax = Max;
|
||||
|
||||
clampOutput(self);
|
||||
clampIterm(self);
|
||||
}
|
||||
|
||||
void PID_SetCtlMode(struct PID *self, enum PIDCtlMode Mode)
|
||||
{
|
||||
bool newAuto = (Mode == PID_AUTOMATIC);
|
||||
if (newAuto == !self->ctlMode) { /*we just went from manual to auto*/
|
||||
PID_Initialize(self);
|
||||
}
|
||||
self->ctlMode = newAuto;
|
||||
}
|
||||
|
||||
void PID_Initialize(struct PID *self)
|
||||
{
|
||||
self->lastInput = self->Input;
|
||||
self->ITerm = self->Output;
|
||||
|
||||
clampIterm(self);
|
||||
}
|
||||
|
||||
void PID_SetControllerDirection(struct PID *self, enum PIDDirection Direction)
|
||||
{
|
||||
self->controllerDirection = Direction;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* adapted from the Arduino PID library
|
||||
*
|
||||
* Created on 2020/01/08.
|
||||
*/
|
||||
|
||||
#ifndef ARDUINOPID_H
|
||||
#define ARDUINOPID_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
enum PIDCtlMode {
|
||||
PID_MANUAL = 0,
|
||||
PID_AUTOMATIC = 1,
|
||||
};
|
||||
|
||||
enum PIDDirection {
|
||||
PID_DIRECT = 0,
|
||||
PID_REVERSE = 1,
|
||||
};
|
||||
|
||||
struct PID {
|
||||
/*working variables*/
|
||||
uint32_t lastTime;
|
||||
float Input, Output, Setpoint;
|
||||
float ITerm, lastInput;
|
||||
float kp, ki, kd;
|
||||
uint32_t SampleTime; //1 sec = 1000
|
||||
float outMin, outMax;
|
||||
enum PIDCtlMode ctlMode; // false
|
||||
enum PIDDirection controllerDirection;
|
||||
};
|
||||
|
||||
#define PID_DEFAULT() { .SampleTime = 1000, .ctlMode=PID_MANUAL, .controllerDirection=PID_DIRECT }
|
||||
|
||||
void PID_Compute(struct PID *self, float Input);
|
||||
|
||||
void PID_SetTunings(struct PID *self, float Kp, float Ki, float Kd);
|
||||
|
||||
void PID_SetSampleTime(struct PID *self, uint32_t NewSampleTime);
|
||||
|
||||
void PID_SetOutputLimits(struct PID *self, float Min, float Max);
|
||||
|
||||
void PID_SetCtlMode(struct PID *self, enum PIDCtlMode Mode);
|
||||
|
||||
void PID_Initialize(struct PID *self);
|
||||
|
||||
void PID_SetSetpoint(struct PID *self, float Setpoint);
|
||||
|
||||
void PID_SetControllerDirection(struct PID *self, enum PIDDirection Direction);
|
||||
|
||||
#endif //ARDUINOPID_H
|
||||
+44
-15
@@ -42,13 +42,9 @@
|
||||
stroke: black;
|
||||
}
|
||||
|
||||
path.major {
|
||||
stroke-width: 2px;
|
||||
}
|
||||
|
||||
.ylabels text {
|
||||
font-size: 10px;
|
||||
text-anchor: end;
|
||||
text-anchor: start;
|
||||
font-family: Droid Sans, sans-serif;
|
||||
vertical-align: middle;
|
||||
}
|
||||
@@ -63,13 +59,13 @@
|
||||
fill: none;
|
||||
}
|
||||
</style>
|
||||
<g transform="translate(50,15)">
|
||||
<g transform="translate(10,15)">
|
||||
<path d="M100,0 v400m100,-400 v400m100,-400 v400m100,-400 v400m100,-400 v400m100,-400 v400"
|
||||
class="grid" transform="translate(0,0)" id="grid-v" />
|
||||
<path d="M0,100 h600m-600,100 h600m-600,100 h600m-600,100"
|
||||
class="grid" stroke-dashoffset="0" id="grid-h" />
|
||||
<path d="M-10,0 h10m-10,100 h10m-10,100 h10m-10,100 h10m-10,100 h10" class="ticks" />
|
||||
<path d="M-5,10
|
||||
<path d="M600,0 h10m-10,100 h10m-10,100 h10m-10,100 h10m-10,100 h10" class="ticks" />
|
||||
<path d="M600,10
|
||||
h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,20
|
||||
h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,20
|
||||
h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,10h5m-5,20
|
||||
@@ -79,7 +75,7 @@
|
||||
<path d="{ser-act}" stroke="red" id="ser-act" /><!--M0,0L300,100L600,400-->
|
||||
</g>
|
||||
<path d="M0,0h600v400h-600Z" class="frame" />
|
||||
<g class="ylabels" transform="translate(0,3)">
|
||||
<g class="ylabels" transform="translate(630,3)">
|
||||
<text x="-15" y="0">400 °C</text>
|
||||
<text x="-15" y="100">300 °C</text>
|
||||
<text x="-15" y="200">200 °C</text>
|
||||
@@ -90,13 +86,42 @@
|
||||
</svg>
|
||||
</td>
|
||||
<td id="td-side">
|
||||
Sidebar
|
||||
t<sub>sens</sub> = <span id="temp">{temp}</span>°C<br>
|
||||
|
||||
<form action="/set" method="POST">
|
||||
<h3>Oven Control</h3>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Heater:</th>
|
||||
<td><select name="fire">
|
||||
<option value="0" {fire_dis_ck}>Disable</option>
|
||||
<option value="1" {fire_en_ck}>Enable</option>
|
||||
</select></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>t<sub>set</sub> =</th>
|
||||
<td><input type="number" step="1" name="tset" value="{tset}"></td>
|
||||
</tr>
|
||||
<tr><td></td><td><input type="submit" value="Set"></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
|
||||
<form action="/set" method="POST">
|
||||
<h3>PID tuning</h3>
|
||||
<table>
|
||||
<tr><th>Kp = </th><td><input type="number" step="any" name="kp" value="{kp}"></td></tr>
|
||||
<tr><th>Ki = </th><td><input type="number" step="any" name="ki" value="{ki}"></td></tr>
|
||||
<tr><th>Kd = </th><td><input type="number" step="any" name="kd" value="{kd}"></td></tr>
|
||||
<tr><td></td><td><input type="submit" value="Set"></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<script>
|
||||
var Qi = function (x) { return document.getElementById(x) };
|
||||
let Qi = function (x) { return document.getElementById(x) };
|
||||
const REFR_TIME = 1000;
|
||||
function update(data) {
|
||||
if (data) {
|
||||
let rows = data.split('\x1e');
|
||||
@@ -113,25 +138,29 @@ function update(data) {
|
||||
Qi('grid-v').setAttribute('transform', 'translate(-'+(va*5)+',0)');
|
||||
Qi('grid-h').setAttribute('stroke-dashoffset', -va * 5);
|
||||
break;
|
||||
case 'temp':
|
||||
Qi('temp').innerHTML = va;
|
||||
break;
|
||||
// form fields are not live updated.
|
||||
}
|
||||
});
|
||||
} else {
|
||||
var xhr=new XMLHttpRequest();
|
||||
let xhr=new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState===4){
|
||||
if (xhr.status===200) {
|
||||
update(xhr.responseText);
|
||||
}
|
||||
setTimeout(update, 500);
|
||||
setTimeout(update, REFR_TIME);
|
||||
}
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
setTimeout(update, 500);
|
||||
setTimeout(update, REFR_TIME);
|
||||
};
|
||||
xhr.open('GET', '/data');
|
||||
xhr.send();
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(update, 500);
|
||||
setTimeout(update, REFR_TIME);
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
#include "firehazard.h"
|
||||
#include "arduinopid.h"
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <esp_log.h>
|
||||
#include <nvs.h>
|
||||
#include "driver/ledc.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
static const char *TAG = "fire";
|
||||
|
||||
static struct PID pid = PID_DEFAULT();
|
||||
|
||||
static void pwm_init(void);
|
||||
static void pwm_set(float duty);
|
||||
|
||||
void fire_get_tuning(float *kp, float *ki, float *kd) {
|
||||
*kp = pid.kp;
|
||||
*ki = pid.ki;
|
||||
*kd = pid.kd;
|
||||
}
|
||||
|
||||
void fire_init() {
|
||||
printf("Regulator init");
|
||||
|
||||
PID_Initialize(&pid);
|
||||
PID_SetOutputLimits(&pid, 0, 1);
|
||||
PID_SetCtlMode(&pid, PID_MANUAL);
|
||||
|
||||
PID_SetTunings(&pid, 0.3, 0.01, 0.1); // TODO load from nvs
|
||||
pwm_init();
|
||||
|
||||
fire_setlevel(20);
|
||||
// fire_enable(true);
|
||||
}
|
||||
|
||||
void fire_set_tuning(float kp, float ki, float kd) {
|
||||
ESP_LOGI(TAG, "PID set tuning Kp=%.3f, Ki=%.3f, Kd=%.3f", kp, ki, kd);
|
||||
PID_SetTunings(&pid, kp, ki, kd);
|
||||
}
|
||||
|
||||
float fire_get_setpoint(bool off_is_zero) {
|
||||
if (off_is_zero) {
|
||||
return pid.ctlMode == PID_MANUAL ? 0 : pid.Setpoint;
|
||||
} else {
|
||||
return pid.Setpoint;
|
||||
}
|
||||
}
|
||||
|
||||
void fire_setlevel(float cels) {
|
||||
ESP_LOGI(TAG, "PID set target %.3f°C", cels);
|
||||
|
||||
if (cels < 0) cels = 0;
|
||||
if (cels > MAX_SETPOINT) cels = MAX_SETPOINT;
|
||||
PID_SetSetpoint(&pid, cels);
|
||||
}
|
||||
|
||||
void fire_enable(bool enable) {
|
||||
ESP_LOGI(TAG, "Heater %s", enable ? "enable" : "disable");
|
||||
|
||||
PID_SetCtlMode(&pid, enable ? PID_AUTOMATIC : PID_MANUAL);
|
||||
}
|
||||
|
||||
bool fire_enabled() {
|
||||
return pid.ctlMode == PID_AUTOMATIC;
|
||||
}
|
||||
|
||||
void fire_regulate(float cels) {
|
||||
PID_Compute(&pid, cels);
|
||||
|
||||
if (cels > MAX_TSENSE || cels < MIN_TSENSE) {
|
||||
ESP_LOGE(TAG, "Tsense out of bounds! Stopping.");
|
||||
fire_enable(false);
|
||||
}
|
||||
|
||||
if (pid.ctlMode == PID_MANUAL) {
|
||||
pwm_set(0);
|
||||
} else {
|
||||
printf("PID in %.2f°C, out %.3f, I %.3f\n", cels, pid.Output, pid.ITerm);
|
||||
pwm_set(pid.Output);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define PWM_CHANNEL LEDC_CHANNEL_1
|
||||
#define PWM_TIMER LEDC_TIMER_1
|
||||
#define PWM_BIT_NUM LEDC_TIMER_12_BIT
|
||||
|
||||
#define PWM_PIN GPIO_NUM_14
|
||||
|
||||
static void pwm_init(void)
|
||||
{
|
||||
ledc_channel_config_t ledc_channel_left = {0};
|
||||
ledc_channel_left.gpio_num = PWM_PIN;
|
||||
ledc_channel_left.speed_mode = LEDC_HIGH_SPEED_MODE;
|
||||
ledc_channel_left.channel = PWM_CHANNEL;
|
||||
ledc_channel_left.intr_type = LEDC_INTR_DISABLE;
|
||||
ledc_channel_left.timer_sel = PWM_TIMER;
|
||||
ledc_channel_left.duty = 0;
|
||||
|
||||
ledc_timer_config_t ledc_timer = {0};
|
||||
ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
|
||||
ledc_timer.duty_resolution = PWM_BIT_NUM;
|
||||
ledc_timer.timer_num = PWM_TIMER;
|
||||
ledc_timer.freq_hz = 1; // TODO ??
|
||||
|
||||
ESP_ERROR_CHECK( ledc_channel_config(&ledc_channel_left) );
|
||||
ESP_ERROR_CHECK( ledc_timer_config(&ledc_timer) );
|
||||
}
|
||||
|
||||
static void pwm_set(float duty)
|
||||
{
|
||||
uint32_t max_duty = (1 << PWM_BIT_NUM);// - 1
|
||||
uint32_t dutycycle = lroundf((duty) * (float)max_duty);
|
||||
printf("Dutycycle %d\n", dutycycle);
|
||||
|
||||
ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, PWM_CHANNEL, dutycycle) );
|
||||
ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, PWM_CHANNEL) );
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* TODO file description
|
||||
*
|
||||
* Created on 2020/01/08.
|
||||
*/
|
||||
|
||||
#ifndef REFLOWER_FIREHAZARD_H
|
||||
#define REFLOWER_FIREHAZARD_H
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_SETPOINT 350
|
||||
#define MAX_TSENSE 400
|
||||
#define MIN_TSENSE 0
|
||||
|
||||
void fire_regulate(float cels);
|
||||
void fire_init();
|
||||
void fire_setlevel(float cels);
|
||||
void fire_enable(bool enable);
|
||||
|
||||
float fire_get_setpoint(bool off_is_zero);
|
||||
void fire_get_tuning(float *kp, float *ki, float *kd);
|
||||
bool fire_enabled();
|
||||
void fire_set_tuning(float kp, float ki, float kd);
|
||||
|
||||
#endif //REFLOWER_FIREHAZARD_H
|
||||
@@ -73,4 +73,9 @@ void feed_all_dogs(void);
|
||||
/** Get embedded file size (must be declared with efile() first) */
|
||||
#define efsize(varname) (varname##_end - varname)
|
||||
|
||||
union uf32 {
|
||||
uint32_t u;
|
||||
float f;
|
||||
};
|
||||
|
||||
#endif //CSPEMU_UTILS_H
|
||||
|
||||
+92
-11
@@ -3,7 +3,9 @@
|
||||
|
||||
#include <fileserver/token_subs.h>
|
||||
#include <httpd_utils/captive.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "firehazard.h"
|
||||
#include "websrv.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "utils.h"
|
||||
@@ -40,14 +42,14 @@ static struct tpl_kv_list build_index_replacements_kv(void)
|
||||
bool last_empty = true; // first is move
|
||||
bool suc;
|
||||
for (int i = 0; i < REG_HISTORY_LEN; i++) {
|
||||
int x = i*5;
|
||||
float x = i*2.5f;
|
||||
if (reg_meas_history[i] == 0) {
|
||||
snprintf(scratch1, SCRATCH_SIZE, "M%d,0", x);
|
||||
snprintf(scratch2, SCRATCH_SIZE, "M%d,0", x);
|
||||
snprintf(scratch1, SCRATCH_SIZE, "M%.1f,0", x);
|
||||
snprintf(scratch2, SCRATCH_SIZE, "M%.1f,0", x);
|
||||
last_empty = true;
|
||||
} else {
|
||||
snprintf(scratch1, SCRATCH_SIZE, "%c%d,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_meas_history[i]));
|
||||
snprintf(scratch2, SCRATCH_SIZE, "%c%d,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_tset_history[i]));
|
||||
snprintf(scratch1, SCRATCH_SIZE, "%c%.1f,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_meas_history[i]));
|
||||
snprintf(scratch2, SCRATCH_SIZE, "%c%.1f,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_tset_history[i]));
|
||||
last_empty = false;
|
||||
}
|
||||
|
||||
@@ -60,8 +62,30 @@ static struct tpl_kv_list build_index_replacements_kv(void)
|
||||
tpl_kv_add_heapstr(&kv, "ser-act", path1);
|
||||
tpl_kv_add_heapstr(&kv, "ser-set", path2);
|
||||
|
||||
bool ena = fire_enabled();
|
||||
tpl_kv_add(&kv, "fire_dis_ck", ena ? "" : "selected");
|
||||
tpl_kv_add(&kv, "fire_en_ck", ena ? "selected" : "");
|
||||
|
||||
tpl_kv_add_int(&kv, "timeshift", history_counter);
|
||||
|
||||
snprintf(scratch1, SCRATCH_SIZE, "%.2f", analog_read());
|
||||
tpl_kv_add(&kv, "temp", scratch1);
|
||||
|
||||
snprintf(scratch1, SCRATCH_SIZE, "%.0f", fire_get_setpoint(false));
|
||||
tpl_kv_add(&kv, "tset", scratch1);
|
||||
|
||||
float kp, ki, kd;
|
||||
fire_get_tuning(&kp, &ki, &kd);
|
||||
|
||||
snprintf(scratch1, SCRATCH_SIZE, "%.3f", kp);
|
||||
tpl_kv_add(&kv, "kp", scratch1);
|
||||
|
||||
snprintf(scratch1, SCRATCH_SIZE, "%.3f", ki);
|
||||
tpl_kv_add(&kv, "ki", scratch1);
|
||||
|
||||
snprintf(scratch1, SCRATCH_SIZE, "%.3f", kd);
|
||||
tpl_kv_add(&kv, "kd", scratch1);
|
||||
|
||||
#undef SCRATCH_SIZE
|
||||
|
||||
return kv;
|
||||
@@ -90,7 +114,7 @@ static esp_err_t handler_update(httpd_req_t *req)
|
||||
/* Set a param */
|
||||
static esp_err_t handler_set(httpd_req_t *req)
|
||||
{
|
||||
char buf[64];
|
||||
char buf[200];
|
||||
int n = httpd_req_recv(req, buf, 63);
|
||||
if (n < 0) {
|
||||
ESP_LOGW(TAG, "rx er");
|
||||
@@ -98,13 +122,70 @@ static esp_err_t handler_set(httpd_req_t *req)
|
||||
}
|
||||
buf[n]=0;
|
||||
|
||||
char keybuf[20];
|
||||
char valbuf[20];
|
||||
if (ESP_OK != httpd_query_key_value(buf, "key", keybuf, 20)) goto err;
|
||||
if (ESP_OK != httpd_query_key_value(buf, "value", valbuf, 20)) goto err;
|
||||
char val[20];
|
||||
|
||||
// TODO handle
|
||||
// select box 0/1
|
||||
esp_err_t rv = httpd_query_key_value(buf, "fire", val, 20);
|
||||
if (rv == ESP_OK) {
|
||||
fire_enable(val[0] == '1');
|
||||
}
|
||||
|
||||
rv = httpd_query_key_value(buf, "tset", val, 20);
|
||||
if (rv == ESP_OK) {
|
||||
errno = 0;
|
||||
float f = atoff(val);
|
||||
if (!errno) {
|
||||
fire_setlevel(f);
|
||||
}
|
||||
}
|
||||
|
||||
float kp, ki, kd;
|
||||
|
||||
// sorry
|
||||
|
||||
// Kp
|
||||
rv = httpd_query_key_value(buf, "kp", val, 20);
|
||||
if (rv == ESP_OK) {
|
||||
errno = 0;
|
||||
kp = atoff(val);
|
||||
if (!errno) {
|
||||
// Ki
|
||||
rv = httpd_query_key_value(buf, "ki", val, 20);
|
||||
if (rv == ESP_OK) {
|
||||
errno = 0;
|
||||
ki = atoff(val);
|
||||
if (!errno) {
|
||||
// Kd
|
||||
rv = httpd_query_key_value(buf, "kd", val, 20);
|
||||
if (rv == ESP_OK) {
|
||||
errno = 0;
|
||||
kd = atoff(val);
|
||||
if (!errno) {
|
||||
fire_set_tuning(kp, ki, kd);
|
||||
|
||||
// save to NVS
|
||||
nvs_handle nvs;
|
||||
ESP_ERROR_CHECK(nvs_open("config", NVS_READWRITE, &nvs));
|
||||
union uf32 ukp, uki, ukd;
|
||||
ukp.f = kp;
|
||||
uki.f = ki;
|
||||
ukd.f = kd;
|
||||
nvs_set_u32(nvs, "kp", ukp.u);
|
||||
nvs_set_u32(nvs, "ki", uki.u);
|
||||
nvs_set_u32(nvs, "kd", ukd.u);
|
||||
nvs_commit(nvs);
|
||||
nvs_close(nvs);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
httpd_resp_set_hdr(req, "Location", "/");
|
||||
httpd_resp_set_status(req, "302 Found");
|
||||
return httpd_resp_send(req, NULL, 0);
|
||||
|
||||
err:
|
||||
|
||||
Reference in New Issue
Block a user