fome-fw/firmware/controllers/sensors/thermistors.cpp

247 lines
7.1 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file thermistors.cpp
*
* @date Feb 17, 2013
2017-01-03 03:05:22 -08:00
* @author Andrey Belomutskiy, (c) 2012-2017
2015-07-10 06:01:56 -07:00
*/
/**
* http://en.wikipedia.org/wiki/Thermistor
* http://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
*/
#include "main.h"
#include "thermistors.h"
#include "adc_inputs.h"
#include "engine_configuration.h"
#include "engine_math.h"
2016-12-21 15:01:56 -08:00
#define _5_VOLTS 5.0
2015-07-10 06:01:56 -07:00
// Celsius
2016-12-16 16:02:14 -08:00
#define NO_IAT_SENSOR_TEMPERATURE 32.0f
2015-07-10 06:01:56 -07:00
#define LIMPING_MODE_IAT_TEMPERATURE 30.0f
#define LIMPING_MODE_CLT_TEMPERATURE 70.0f
2016-12-16 16:02:14 -08:00
#define NO_CLT_SENSOR_TEMPERATURE 72.0f
2015-07-10 06:01:56 -07:00
EXTERN_ENGINE
;
static Logging *logger;
static bool initialized = false;
/**
* http://en.wikipedia.org/wiki/Voltage_divider
*/
float getR1InVoltageDividor(float Vout, float Vin, float r2) {
return r2 * Vin / Vout - r2;
}
float getR2InVoltageDividor(float Vout, float Vin, float r1) {
if (Vout == 0) {
return NAN;
}
return r1 / (Vin / Vout - 1);
}
float getVoutInVoltageDividor(float Vin, float r1, float r2) {
return r2 * Vin / (r1 + r2);
}
2016-12-27 08:01:26 -08:00
float ThermistorMath::getKelvinTemperatureByResistance(float resistance) {
2015-07-10 06:01:56 -07:00
if (resistance <= 0) {
//warning("Invalid resistance in getKelvinTemperature=", resistance);
return 0.0f;
}
float logR = logf(resistance);
2016-12-27 08:01:26 -08:00
return 1 / (s_h_a + s_h_b * logR + s_h_c * logR * logR * logR);
2015-07-10 06:01:56 -07:00
}
float convertCelsiustoF(float tempC) {
return tempC * 9 / 5 + 32;
}
float convertFtoCelsius(float tempF) {
return (tempF - 32) / 9 * 5;
}
float convertKelvinToFahrenheit(float kelvin) {
float tempC = convertKelvinToCelcius(kelvin);
return convertCelsiustoF(tempC);
}
2016-12-22 11:02:38 -08:00
float getResistance(ThermistorConf *config, float voltage) {
2015-07-10 06:01:56 -07:00
efiAssert(config != NULL, "thermistor config is null", NAN);
thermistor_conf_s *tc = &config->config;
float resistance = getR2InVoltageDividor(voltage, _5_VOLTS, tc->bias_resistor);
return resistance;
}
float getTemperatureC(ThermistorConf *config, ThermistorMath *tm) {
if (!initialized) {
2016-10-10 13:02:39 -07:00
firmwareError(CUSTOM_ERR_THERM, "thermstr not initialized");
2015-07-10 06:01:56 -07:00
return NAN;
}
2016-12-23 11:01:45 -08:00
tm->setConfig(&config->config); // implementation checks if configuration has changed or not
2016-12-22 11:02:38 -08:00
float voltage = getVoltageDivided("term", config->adcChannel);
float resistance = getResistance(config, voltage);
2015-07-10 06:01:56 -07:00
2016-12-27 08:01:26 -08:00
float kelvinTemperature = tm->getKelvinTemperatureByResistance(resistance);
2015-07-10 06:01:56 -07:00
return convertKelvinToCelcius(kelvinTemperature);
}
bool isValidCoolantTemperature(float temperature) {
// I hope magic constants are appropriate here
return !cisnan(temperature) && temperature > -50 && temperature < 250;
}
bool isValidIntakeAirTemperature(float temperature) {
// I hope magic constants are appropriate here
return !cisnan(temperature) && temperature > -50 && temperature < 100;
}
2016-12-17 08:01:40 -08:00
bool hasCltSensor(DECLARE_ENGINE_PARAMETER_F) {
return engineConfiguration->clt.adcChannel != EFI_ADC_NONE;
}
2015-07-10 06:01:56 -07:00
/**
* @return coolant temperature, in Celsius
*/
float getCoolantTemperature(DECLARE_ENGINE_PARAMETER_F) {
2016-12-17 08:01:40 -08:00
if (!hasCltSensor(PASS_ENGINE_PARAMETER_F)) {
2016-12-16 18:02:54 -08:00
return NO_CLT_SENSOR_TEMPERATURE;
}
float temperature = getTemperatureC(&engineConfiguration->clt, &engine->engineState.cltCurve);
2015-07-10 06:01:56 -07:00
if (!isValidCoolantTemperature(temperature)) {
efiAssert(engineConfiguration!=NULL, "NULL engineConfiguration", NAN);
2016-12-16 18:02:54 -08:00
warning(OBD_Engine_Coolant_Temperature_Circuit_Malfunction, "unrealistic CLT %f", temperature);
2015-07-10 06:01:56 -07:00
return LIMPING_MODE_CLT_TEMPERATURE;
}
return temperature;
}
void setThermistorConfiguration(ThermistorConf * thermistor, float tempC1, float r1, float tempC2, float r2, float tempC3,
float r3) {
thermistor_conf_s *tc = &thermistor->config;
tc->tempC_1 = tempC1;
tc->resistance_1 = r1;
tc->tempC_2 = tempC2;
tc->resistance_2 = r2;
tc->tempC_3 = tempC3;
tc->resistance_3 = r3;
}
2016-12-22 11:02:38 -08:00
void ThermistorMath::prepareThermistorCurve(thermistor_conf_s *tc) {
2015-07-10 06:01:56 -07:00
float T1 = tc->tempC_1 + KELV;
float T2 = tc->tempC_2 + KELV;
float T3 = tc->tempC_3 + KELV;
2016-10-31 17:02:09 -07:00
#if EXTREME_TERM_LOGGING || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "T1=%..100000f/T2=%..100000f/T3=%..100000f", T1, T2, T3);
2016-10-31 17:02:09 -07:00
#endif
2015-07-10 06:01:56 -07:00
float L1 = logf(tc->resistance_1);
if (L1 == tc->resistance_1) {
2016-10-10 13:02:39 -07:00
firmwareError(CUSTOM_ERR_LOG_ERROR, "log is broken?");
2015-07-10 06:01:56 -07:00
}
float L2 = logf(tc->resistance_2);
float L3 = logf(tc->resistance_3);
2016-10-31 17:02:09 -07:00
#if EXTREME_TERM_LOGGING || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "R1=%..100000f/R2=%..100000f/R3=%..100000f", tc->resistance_1, tc->resistance_2,
tc->resistance_3);
scheduleMsg(logger, "L1=%..100000f/L2=%..100000f/L3=%..100000f", L1, L2, L3);
2016-10-31 17:02:09 -07:00
#endif
2015-07-10 06:01:56 -07:00
float Y1 = 1 / T1;
float Y2 = 1 / T2;
float Y3 = 1 / T3;
float U2 = (Y2 - Y1) / (L2 - L1);
float U3 = (Y3 - Y1) / (L3 - L1);
2016-12-22 11:02:38 -08:00
s_h_c = (U3 - U2) / (L3 - L2) * pow(L1 + L2 + L3, -1);
s_h_b = U2 - s_h_c * (L1 * L1 + L1 * L2 + L2 * L2);
s_h_a = Y1 - (s_h_b + L1 * L1 * s_h_c) * L1;
2015-07-10 06:01:56 -07:00
2016-10-31 17:02:09 -07:00
#if EXTREME_TERM_LOGGING || defined(__DOXYGEN__)
scheduleMsg(logger, "Y1=%..100000f/Y2=%..100000f/Y3=%..100000f", Y1, Y2, Y3);
scheduleMsg(logger, "U2=%..100000f/U3=%..100000f", U2, U3);
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "s_h_c=%..100000f/s_h_b=%..100000f/s_h_a=%..100000f", curve->s_h_c, curve->s_h_b,
curve->s_h_a);
2016-10-31 17:02:09 -07:00
#endif
2015-07-10 06:01:56 -07:00
}
2016-12-17 08:01:40 -08:00
bool hasIatSensor(DECLARE_ENGINE_PARAMETER_F) {
return engineConfiguration->iat.adcChannel != EFI_ADC_NONE;
}
2015-07-10 06:01:56 -07:00
/**
* @return Celsius value
*/
float getIntakeAirTemperature(DECLARE_ENGINE_PARAMETER_F) {
2016-12-17 08:01:40 -08:00
if (!hasIatSensor(PASS_ENGINE_PARAMETER_F)) {
2016-12-16 16:02:14 -08:00
return NO_IAT_SENSOR_TEMPERATURE;
}
2015-07-10 06:01:56 -07:00
float temperature = getTemperatureC(&engineConfiguration->iat, &engine->engineState.iatCurve);
if (!isValidIntakeAirTemperature(temperature)) {
efiAssert(engineConfiguration!=NULL, "NULL engineConfiguration", NAN);
2016-12-17 06:02:59 -08:00
warning(OBD_Intake_Air_Temperature_Circuit_Malfunction, "unrealistic IAT %f", temperature);
2015-07-10 06:01:56 -07:00
return LIMPING_MODE_IAT_TEMPERATURE;
}
return temperature;
}
void setDodgeSensor(ThermistorConf *thermistorConf) {
setThermistorConfiguration(thermistorConf, -40, 336660, 30, 7550, 120, 390);
}
// todo: better method name?
void setCommonNTCSensor(ThermistorConf *thermistorConf) {
/**
* 18K Ohm @ -20C
* 2.1K Ohm @ 24C
* 294 Ohm @ 80C
* http://www.rexbo.eu/hella/coolant-temperature-sensor-6pt009107121?c=100334&at=3130
*/
setThermistorConfiguration(thermistorConf, -20, 18000, 23.8889, 2100, 120.0, 100.0);
}
#if EFI_PROD_CODE
static void testCltByR(float resistance) {
2016-12-23 11:01:45 -08:00
// we expect slowPeriodicCallback to already update configuration in the curve helper class see setConfig
2016-12-27 08:01:26 -08:00
float kTemp = engine->engineState.cltCurve.getKelvinTemperatureByResistance(resistance);
2015-07-10 06:01:56 -07:00
scheduleMsg(logger, "for R=%f we have %f", resistance, (kTemp - KELV));
}
#endif
void initThermistors(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_S) {
logger = sharedLogger;
efiAssertVoid(engine!=NULL, "e NULL initThermistors");
2016-12-21 15:01:56 -08:00
#if EFI_PROD_CODE || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
addConsoleActionF("test_clt_by_r", testCltByR);
#endif
initialized = true;
}
ThermistorMath::ThermistorMath() {
memset(&currentConfig, 0, sizeof(currentConfig));
2016-12-22 11:02:38 -08:00
s_h_a = s_h_b = s_h_c = 0;
2015-07-10 06:01:56 -07:00
}
2016-12-21 15:01:56 -08:00
void ThermistorMath::setConfig(thermistor_conf_s *config) {
2016-01-11 14:01:33 -08:00
bool isSameConfig = memcmp(config, &currentConfig, sizeof(currentConfig)) == 0;
2015-07-10 06:01:56 -07:00
if (isSameConfig) {
return;
}
memcpy(&currentConfig, config, sizeof(currentConfig));
2016-12-22 11:02:38 -08:00
prepareThermistorCurve(config);
2015-07-10 06:01:56 -07:00
}