rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
thermistor_func.cpp
Go to the documentation of this file.
1/**
2 * @author Matthew Kennedy, (c) 2019
3 */
4
5#include "pch.h"
6
7#include "thermistor_func.h"
8
9#include <cmath>
10
12 // This resistance should have already been validated - only
13 // thing we can check is that it's non-negative
14 if (ohms <= 0) {
15 return UnexpectedCode::Low;
16 }
17
18 float lnR = logf(ohms);
19
20 float lnR3 = lnR * lnR * lnR;
21
22 float recip = m_a + m_b * lnR + m_c * lnR3;
23
24 float kelvin = 1 / recip;
25
26 float celsius = convertKelvinToCelcius(kelvin);
27
28 // bounds check result - please don't try to run rusEfi when colder than -50C
29 // high end limit is required as this could be an oil temp sensor on an
30 // air cooled engine
31 if (celsius < -50) {
32 return UnexpectedCode::Low;
33 }
34
35 if (celsius > 250) {
36 return UnexpectedCode::High;
37 }
38
39 return celsius;
40}
41
43 // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
44 float l1 = logf(cfg.resistance_1);
45 float l2 = logf(cfg.resistance_2);
46 float l3 = logf(cfg.resistance_3);
47
48 float y1 = 1 / convertCelsiusToKelvin(cfg.tempC_1);
49 float y2 = 1 / convertCelsiusToKelvin(cfg.tempC_2);
50 float y3 = 1 / convertCelsiusToKelvin(cfg.tempC_3);
51
52 float u2 = (y2 - y1) / (l2 - l1);
53 float u3 = (y3 - y1) / (l3 - l1);
54
55 m_c = ((u3 - u2) / (l3 - l2)) / (l1 + l2 + l3);
56 m_b = u2 - m_c * (l1 * l1 + l1 * l2 + l2 * l2);
57 m_a = y1 - (m_b + l1 * l1 * m_c) * l1;
58
59 float resistance10percent = cfg.resistance_1 + 0.1 * (cfg.resistance_2 - cfg.resistance_1);
60 float tempAt10percentPoint = convert(resistance10percent).Value;
61
62 if (tempAt10percentPoint < cfg.tempC_1) {
63#if EFI_UNIT_TEST
64 throw std::logic_error("Bad thermistor configuration at the left");
65#endif
66 criticalError("Thermistor configuration has failed 10% test");
67 }
68
69 float resistance90percent = cfg.resistance_2 + 0.9 * (cfg.resistance_3 - cfg.resistance_2);
70 float tempAt90percentPoint = convert(resistance90percent).Value;
71 if (tempAt90percentPoint > cfg.tempC_3) {
72#if EFI_UNIT_TEST
73 throw std::logic_error("Bad thermistor configuration at the right");
74#endif
75 criticalError("Thermistor configuration has failed 90% test");
76 }
77}
void configure(thermistor_conf_s &cfg)
SensorResult convert(float ohms) const override
expected< float > SensorResult
Definition sensor.h:46