esp32 firmware for a toaster reflow oven WIP!!!!!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
reflower/main/analog.c

110 lines
7.5 KiB

#include <stdint.h>
#include <stdatomic.h>
#include "analog.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#include "firehazard.h"
static esp_adc_cal_characteristics_t *adc_chars;
// 32-39
static const adc1_channel_t channel = ADC1_CHANNEL_4; //GPIO34 if ADC1, GPIO14 if ADC2
static float measurement_celsius;
static const adc_atten_t atten = ADC_ATTEN_DB_0;
static const adc_unit_t unit = ADC_UNIT_1;
float reg_meas_history[REG_HISTORY_LEN] = {};
float reg_tset_history[REG_HISTORY_LEN] = {};
uint32_t history_counter = 0;
static void analog_service(void *arg);
static TaskHandle_t hAnalog;
#define DEFAULT_VREF 1100 // TODO try to find the exact value
#define NO_OF_SAMPLES 128
void analog_init() {
printf("Analog init\n");
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(channel, atten);
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
int rv = xTaskCreate(analog_service, "analog", 4096, NULL, 6, &hAnalog);
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) {
uint32_t adc_reading = 0;
//Multisampling
for (int i = 0; i < NO_OF_SAMPLES; i++) {
adc_reading += adc1_get_raw(channel);
}
adc_reading /= NO_OF_SAMPLES;
//Convert adc_reading to voltage in mV
uint32_t mv = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
#define CORRECT -10;
mv += CORRECT;
printf("Raw: %d ... Voltage: %dmV ...", adc_reading, mv);
float volts = mv * 0.001f;
float celsius = v_to_c(volts);
printf("Celsius: %.1f°C\n", celsius);
measurement_celsius = celsius;
for (int i = 0; i < REG_HISTORY_LEN-1; i++) {
reg_meas_history[i] = reg_meas_history[i+1];
reg_tset_history[i] = reg_tset_history[i+1];
}
reg_meas_history[REG_HISTORY_LEN-1] = celsius;
reg_tset_history[REG_HISTORY_LEN-1] = fire_get_setpoint(true);
history_counter = (history_counter + 1) % 20;
fire_regulate(celsius);
vTaskDelay(pdMS_TO_TICKS(ANALOG_SAMPLE_TIME_MS));
}
}
float analog_read() {
return measurement_celsius;
}