Split thermistor functions into separate logical units and add TODO

This commit is contained in:
2024-09-19 19:54:28 +02:00
parent b3fda53036
commit 1f01f92291
5 changed files with 74 additions and 52 deletions

View File

@@ -2,29 +2,67 @@
#include <math.h>
// https://bmcnoldy.earth.miami.edu/Humidity.html
// https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
// https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18280890511
// https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.18441370202
// https://bmcnoldy.earth.miami.edu/Humidity.html
// TODO: Make sure all results for conversion functions are correct.
// TODO: Is resistance function semantically correct?
// TODO: Check results for humidity conversion functions.
// Conversion constants
static const float C1 = 100.0f;
static const float C2 = 243.04f;
static const float C3 = 17.625f;
// Thermistor coeficients
static const float SC1 = 1.009249522e-3f;
static const float SC2 = 2.378405444e-4f;
static const float SC3 = 2.019202697e-7f;
static const float R1 = 10000.0f;
// Humidity conversion constants
static const float TH1 = 100.0f;
static const float TH2 = 243.04f;
static const float TH3 = 17.625f;
float Resistance(int adc_raw)
{
float r2;
// TODO: Improve readability.
// TODO: Make sure output is correct.
// [GND] - [10K RESISTOR] - [ADC INPUT] - [THERMISTOR] - [VCC]
r2 = (float) adc_raw / 264.8f * 0.05f;
// XXX: Ohms Law solved for resistance?
// R = V / I
return R1 / (5.0f / r2 - 1.0f);
}
// Resistance to temperature
float SteinhartHart(float res)
{
float logr, t;
// 25C = 10000 Ohms
// 100C = 6744 Ohms
logr = log(res);
t = 1.0f / (SC1 + SC2 * logr + SC3 * pow(logr, 3));
return t - 273.15f; // Kelvin to celsius
}
// Dewpoint TD
float Dewp(float t, float rh)
{
float a, b;
a = log(rh / C1);
b = C3 * t / (C2 + t);
// 243.04*(LN(RH/100)+((17.625*T)/(243.04+T)))/
// (17.625-LN(RH/100)-((17.625*T)/(243.04+T)))
return C2 * (a + b) / (C3 - a - b);
a = log(rh / TH1);
b = TH3 * t / (TH2 + t);
return TH2 * (a + b) / (TH3 - a - b);
}
// Temperature T
@@ -32,13 +70,13 @@ float Temp(float td, float rh)
{
float a, b;
a = log(rh / C1);
b = C3 * td / (C2 + td);
// 243.04*(((17.625*TD)/(243.04+TD))-LN(RH/100))/
// (17.625+LN(RH/100)-((17.625*TD)/(243.04+TD)))
return C2 * (b - a) / (C3 + a - b);
a = log(rh / TH1);
b = TH3 * td / (TH2 + td);
return TH2 * (b - a) / (TH3 + a - b);
}
// Relative Humidity RH
@@ -46,11 +84,11 @@ float Rhum(float t, float td)
{
float a, b;
a = C3 * td / (C2 + td);
b = C3 * t / (C2 + t);
// 100*(EXP((17.625*TD)/(243.04+TD))/
// EXP((17.625*T)/(243.04+T)))
return C1 * (exp(a) / exp(b));
a = TH3 * td / (TH2 + td);
b = TH3 * t / (TH2 + t);
return TH1 * (exp(a) / exp(b));
}