GCC Code Coverage Report


Directory: ./
File: firmware/controllers/sensors/converters/thermistor_func.cpp
Date: 2025-10-03 00:57:22
Coverage Exec Excl Total
Lines: 88.2% 30 0 34
Functions: 100.0% 2 0 2
Branches: 58.3% 7 0 12
Decisions: 60.0% 6 - 10

Line Branch Decision Exec Source
1 /**
2 * @author Matthew Kennedy, (c) 2019
3 */
4
5 #include "pch.h"
6
7 #include "thermistor_func.h"
8
9 #include <cmath>
10
11 31 SensorResult ThermistorFunc::convert(float ohms) const {
12 // This resistance should have already been validated - only
13 // thing we can check is that it's non-negative
14
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 31 times.
31 if (ohms <= 0) {
15 return UnexpectedCode::Low;
16 }
17
18 31 float lnR = logf(ohms);
19
20 31 float lnR3 = lnR * lnR * lnR;
21
22 31 float recip = m_a + m_b * lnR + m_c * lnR3;
23
24 31 float kelvin = 1 / recip;
25
26 31 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 31 times.
31 if (celsius < -50) {
32 return UnexpectedCode::Low;
33 }
34
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 31 times.
31 if (celsius > 250) {
36 return UnexpectedCode::High;
37 }
38
39 31 return celsius;
40 }
41
42 7 void ThermistorFunc::configure(thermistor_conf_s &cfg) {
43 // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
44 7 float l1 = logf(cfg.resistance_1);
45 7 float l2 = logf(cfg.resistance_2);
46 7 float l3 = logf(cfg.resistance_3);
47
48 7 float y1 = 1 / convertCelsiusToKelvin(cfg.tempC_1);
49 7 float y2 = 1 / convertCelsiusToKelvin(cfg.tempC_2);
50 7 float y3 = 1 / convertCelsiusToKelvin(cfg.tempC_3);
51
52 7 float u2 = (y2 - y1) / (l2 - l1);
53 7 float u3 = (y3 - y1) / (l3 - l1);
54
55 7 m_c = ((u3 - u2) / (l3 - l2)) / (l1 + l2 + l3);
56 7 m_b = u2 - m_c * (l1 * l1 + l1 * l2 + l2 * l2);
57 7 m_a = y1 - (m_b + l1 * l1 * m_c) * l1;
58
59 7 float resistance10percent = cfg.resistance_1 + 0.1 * (cfg.resistance_2 - cfg.resistance_1);
60 7 float tempAt10percentPoint = convert(resistance10percent).Value;
61
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 7 times.
7 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 7 float resistance90percent = cfg.resistance_2 + 0.9 * (cfg.resistance_3 - cfg.resistance_2);
70 7 float tempAt90percentPoint = convert(resistance90percent).Value;
71
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
2/2
✓ Decision 'true' taken 1 time.
✓ Decision 'false' taken 6 times.
7 if (tempAt90percentPoint > cfg.tempC_3) {
72 #if EFI_UNIT_TEST
73
1/1
✓ Branch 2 taken 1 time.
1 throw std::logic_error("Bad thermistor configuration at the right");
74 #endif
75 criticalError("Thermistor configuration has failed 90% test");
76 }
77 6 }
78