rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hella_oil_level.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "hella_oil_level.h"
4
5#if EFI_HELLA_OIL
6
8
9static void hellaSensorExtiCallback(void* arg, efitick_t nowNt) {
10 reinterpret_cast<HellaOilLevelSensor*>(arg)->onEdge(nowNt);
11}
12
14 if (!isBrainPinValid(pin)) {
15 return;
16 }
17
18#if EFI_PROD_CODE
19 if (efiExtiEnablePin(getSensorName(), pin, PAL_EVENT_MODE_BOTH_EDGES,
20 hellaSensorExtiCallback, reinterpret_cast<void*>(this)) < 0) {
21 return;
22 }
23#endif // EFI_PROD_CODE
24
25 m_pin = pin;
26
27 Register();
28}
29
31 if (!isBrainPinValid(m_pin)) {
32 return;
33 }
34
35#if EFI_PROD_CODE
37#endif
38
40}
41
42void HellaOilLevelSensor::onEdge(efitick_t nowNt) {
43 if (efiReadPin(m_pin)) {
44 // Start pulse width timing at the rising edge
45 m_pulseTimer.reset(nowNt);
46
47 float timeBetweenPulses = m_betweenPulseTimer.getElapsedSecondsAndReset(nowNt);
48
49 if (timeBetweenPulses > 0.89 * 0.780 && timeBetweenPulses < 1.11 * 0.780) {
50 // 780ms nominal between Diag and next Temp pulse start, +-10%
51
52 // This was the "long gap" break, next pulse is temperature.
54 } else if (timeBetweenPulses > 0.89 * 0.110 && timeBetweenPulses < 1.11 * 0.110) {
55 // 110ms nominal between each pulse (other than break)
56
57 // Advance the state machine to decode the next pulse in the sequence
58 switch (m_nextPulse) {
59 case NextPulse::Temp:
61 break;
64 break;
65 default:
66 // We don't know how we got here, reset to safe state
68 break;
69 }
70 } else {
71 // The break was too long, ignore it for now.
73 }
74 } else {
75 // Stop timing at the falling edge
76 float lastPulseMs = 1000 * m_pulseTimer.getElapsedSeconds(nowNt);
77
78 if (lastPulseMs > 100 || lastPulseMs < 20) {
79 // Impossibly short or long pulse, something went wrong
81 return;
82 }
83
85 // TODO: decode diag pulse?
86 return;
87 } else if (m_nextPulse == NextPulse::Temp) {
88 // 22ms = Short circuit temp sensor
89 // 23ms = -40C
90 // 87ms = 160C
91 // 88ms = Temp sensor defective
92
93 if (lastPulseMs < 22.8) {
94 // Short circuit
95 // invalidate(UnexpectedCode::Low);
96 } else if (lastPulseMs > 87.2) {
97 // Defective
98 // invalidate(UnexpectedCode::High);
99 } else {
100 float tempC = interpolateClamped(23, -40, 87, 160, lastPulseMs);
101 // setValidValue(tempC, nowNt);
102 }
103 } else if (m_nextPulse == NextPulse::Level) {
104 // 22ms = Unreliable signal
105 // 23ms = level 0mm
106 // 87.86ms = level 150mm
107
108 if (lastPulseMs < 22.8f) {
109 // Unreliable
110 invalidate(UnexpectedCode::Low);
111 } else {
112 float levelMm = interpolateClamped(23, 0, 87.86, 150, lastPulseMs);
113 setValidValue(levelMm, nowNt);
114 }
115 }
116 }
117}
118
119#endif // EFI_HELLA_OIL
void init(brain_pin_e pin)
void onEdge(efitick_t nowNt)
bool Register()
Definition sensor.cpp:131
const char * getSensorName() const
Definition sensor.h:130
void setValidValue(float value, efitick_t timestamp)
@ Unassigned
void efiExtiDisablePin(brain_pin_e brainPin)
int efiExtiEnablePin(const char *msg, brain_pin_e brainPin, uint32_t mode, ExtiCallback cb, void *cb_data)
float interpolateClamped(float x1, float y1, float x2, float y2, float x)
static void hellaSensorExtiCallback(void *arg, efitick_t nowNt)
bool efiReadPin(brain_pin_e pin)
Definition io_pins.cpp:89
bool isBrainPinValid(brain_pin_e brainPin)
tempC("WBO: Temperature", SensorCategory.SENSOR_INPUTS, FieldType.INT16, 1936, 1.0, 500.0, 1000.0, "C")
brain_pin_e pin
Definition stm32_adc.cpp:15