Temperature (#63)

* integer id interpolation

* Calculate sensor temperature based on Ri

* Sensor Ri around 25C

Co-authored-by: Andrey Gusakov <dron0gus@gmail.com>
This commit is contained in:
rusefillc 2022-05-07 14:12:20 -04:00 committed by GitHub
parent 87763c55a4
commit b9b94fcadc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 0 deletions

View File

@ -144,6 +144,7 @@ CPPSRC = $(ALLCPPSRC) \
pid.cpp \ pid.cpp \
pump_control.cpp \ pump_control.cpp \
uart.cpp \ uart.cpp \
interpolation.cpp \
main.cpp main.cpp
# List ASM source files here. # List ASM source files here.

View File

@ -0,0 +1,37 @@
#include "interpolation.h"
int interpolateInt(int x1, int y1, int x2, int y2, int x)
{
if (x1 == x2)
return y1;
return (y1 + (y2 - y1) * (x - x1) / (x2 - x1));
}
int interpolateIntClamped(int x1, int y1, int x2, int y2, int x)
{
if (x <= x1)
return y1;
if (x >= x2)
return y2;
return interpolateInt(x1, y1, x2, y2, x);
}
int interpolate_1d_int(const struct inter_point *p, int size, int x)
{
int i;
/* no exterpolation */
if (x < p[0].x)
return p[0].y;
for (i = 0; i < size - 1; i++) {
if ((x >= p[i].x) && (x < p[i + 1].x)) {
return interpolateInt(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y, x);
}
}
/* no exterpolation */
return p[size - 1].y;
}

10
firmware/interpolation.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
struct inter_point {
int x;
int y;
};
int interpolateInt(int x1, int y1, int x2, int y2, int x);
int interpolateIntClamped(int x1, int y1, int x2, int y2, int x);
int interpolate_1d_int(const struct inter_point *p, int size, int x);

View File

@ -1,4 +1,5 @@
#include "sampling.h" #include "sampling.h"
#include "interpolation.h"
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
@ -14,6 +15,27 @@ static float nernstDc = 0;
static float pumpCurrentSenseVoltage = 0; static float pumpCurrentSenseVoltage = 0;
static float internalBatteryVoltage = 0; static float internalBatteryVoltage = 0;
static const struct inter_point lsu49_r_to_temp[] =
{
{ 80, 1030},
{ 150, 890},
{ 200, 840},
{ 250, 805},
{ 300, 780},
{ 350, 760},
{ 400, 745},
{ 450, 730},
{ 550, 705},
{ 650, 685},
{ 800, 665},
{ 1000, 640},
{ 1200, 630},
{ 2500, 565},
{ 6000, 25} /* measured while cold */
};
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
constexpr float f_abs(float x) constexpr float f_abs(float x)
{ {
return x > 0 ? x : -x; return x > 0 ? x : -x;
@ -86,6 +108,11 @@ float GetSensorInternalResistance()
return totalEsr - VM_RESISTOR_VALUE; return totalEsr - VM_RESISTOR_VALUE;
} }
float GetSensorTemperature()
{
return interpolate_1d_int(lsu49_r_to_temp, ARRAY_SIZE(lsu49_r_to_temp), GetSensorInternalResistance());
}
float GetNernstDc() float GetNernstDc()
{ {
return nernstDc; return nernstDc;

View File

@ -4,6 +4,7 @@ void StartSampling();
float GetNernstAc(); float GetNernstAc();
float GetSensorInternalResistance(); float GetSensorInternalResistance();
float GetSensorTemperature();
float GetNernstDc(); float GetNernstDc();
float GetPumpNominalCurrent(); float GetPumpNominalCurrent();