Improved sensing algo, to only generate one 100 ms pulse every sense.

Also some more conditions to make probing more reliable.
This commit is contained in:
Hakan Bastedt
2025-10-13 18:15:04 +02:00
parent b3bb88cf08
commit 93c48940b8

View File

@@ -49,12 +49,21 @@ class OhmicSensing {
float voltageDropLimit, uint32_t setupTime, uint8_t enabled, float voltageDropLimit, uint32_t setupTime, uint8_t enabled,
uint8_t &sensed); uint8_t &sensed);
private: // private:
enum OhmicStates { OHMIC_IDLE, OHMIC_SETUP, OHMIC_PROBE }; enum OhmicStates {
OHMIC_IDLE,
OHMIC_SETUP,
OHMIC_PROBE,
OHMIC_PROBED_ON,
OHMIC_PROBE_DONE
};
OhmicStates ohmicState = OHMIC_IDLE; OhmicStates ohmicState = OHMIC_IDLE;
uint32_t setupTimeSoFar = 0; uint32_t setupTimeSoFar = 0;
uint32_t probingTime = 0;
uint16_t senseOnTime = 0;
float_t oldVoltage = 0.0; float_t oldVoltage = 0.0;
std::queue<float> voltages; std::queue<float> voltages;
float_t refVoltage;
}; };
OhmicSensing Ohm1; OhmicSensing Ohm1;
OhmicSensing Ohm2; OhmicSensing Ohm2;
@@ -429,6 +438,7 @@ void lowpassFilter(float &oldLowPassGain,
oldLowPassFilteredVoltage = outFilteredVoltage; oldLowPassFilteredVoltage = outFilteredVoltage;
} }
#define N_VOLTAGES 3
void OhmicSensing::handle(uint8_t voltageState, float inVoltage, void OhmicSensing::handle(uint8_t voltageState, float inVoltage,
float limitVoltage, float voltageDropLimit, float limitVoltage, float voltageDropLimit,
uint32_t setupTime, uint8_t enabled, uint32_t setupTime, uint8_t enabled,
@@ -438,26 +448,52 @@ void OhmicSensing::handle(uint8_t voltageState, float inVoltage,
if (ohmicState == OHMIC_IDLE && inVoltage > limitVoltage) { if (ohmicState == OHMIC_IDLE && inVoltage > limitVoltage) {
ohmicState = OHMIC_SETUP; ohmicState = OHMIC_SETUP;
setupTimeSoFar = 0; setupTimeSoFar = 0;
while (!voltages.empty()) voltages.pop(); // Remove history
return;
} }
if (ohmicState == OHMIC_SETUP) { if (ohmicState == OHMIC_SETUP) {
if (setupTimeSoFar++ > setupTime) { if (setupTimeSoFar++ > setupTime) {
ohmicState = OHMIC_PROBE; ohmicState = OHMIC_PROBE;
probingTime = 0;
oldVoltage = 0.0; oldVoltage = 0.0;
while (!voltages.empty()) voltages.pop(); // Remove history refVoltage = inVoltage; // RefVoltage = voltage at end of setup
return;
} }
} }
if (ohmicState == OHMIC_PROBE) { if (ohmicState == OHMIC_PROBE) {
voltages.push(inVoltage); voltages.push(inVoltage);
#define N_VOLTAGES 3
while (voltages.size() > N_VOLTAGES) voltages.pop(); // Only N_VOLTAGES while (voltages.size() > N_VOLTAGES) voltages.pop(); // Only N_VOLTAGES
if (inVoltage <= limitVoltage || // Below threshold if (probingTime++ > 30000) { // Go to IDLE after 30 seconds
(abs(voltageDropLimit) > 1e-3 && // Immediate drop ohmicState = OHMIC_IDLE;
return;
}
if ((inVoltage <= limitVoltage) || // Below starting threshold
(fabs(voltageDropLimit) > 1e-3 &&
refVoltage - inVoltage >=
voltageDropLimit) || // Delta below refVoltage
(fabs(voltageDropLimit) > 1e-3 && // Immediate drop
oldVoltage - inVoltage >= voltageDropLimit) || oldVoltage - inVoltage >= voltageDropLimit) ||
(abs(voltageDropLimit) > 1e-3 && // Drop over 3 cycles (fabs(voltageDropLimit) > 1e-3 && // Drop over 3 cycles
voltages.front() - voltages.back() > voltageDropLimit)) { voltages.front() - voltages.back() > voltageDropLimit)) {
sensed = 1; sensed = 1;
senseOnTime = 0;
ohmicState = OHMIC_PROBED_ON;
} }
oldVoltage = inVoltage; oldVoltage = inVoltage;
return;
}
if (ohmicState == OHMIC_PROBED_ON) {
sensed = 1;
if (senseOnTime++ >= 100) {
sensed = 0;
ohmicState = OHMIC_PROBE_DONE;
}
return;
}
if (ohmicState == OHMIC_PROBE_DONE) {
sensed = 0;
if (!enabled) ohmicState = OHMIC_IDLE;
return;
} }
} else { } else {
ohmicState = OHMIC_IDLE; ohmicState = OHMIC_IDLE;