更新压力稳定判定算法
This commit is contained in:
+73
-20
@@ -2,57 +2,110 @@
|
||||
#include <cmath>
|
||||
|
||||
PressureMonitor::PressureMonitor()
|
||||
: m_stableTickCount(0)
|
||||
: m_tickCounter(0)
|
||||
, m_sampleCount(0)
|
||||
, m_bufIdx(0)
|
||||
, m_totalTickCount(0)
|
||||
, m_timeout(false)
|
||||
{
|
||||
for (int i = 0; i < BUF_SIZE; i++) {
|
||||
m_buf[i] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
bool PressureMonitor::tick(float realtimePressure, float calibratedPressure)
|
||||
{
|
||||
m_totalTickCount++;
|
||||
|
||||
if (calibratedPressure <= 0.0f) {
|
||||
m_stableTickCount = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
float upperBound = calibratedPressure + 10.0f;
|
||||
float lowerBound = calibratedPressure - 10.0f;
|
||||
|
||||
if (realtimePressure >= lowerBound && realtimePressure <= upperBound) {
|
||||
m_stableTickCount++;
|
||||
} else {
|
||||
m_stableTickCount = 0;
|
||||
}
|
||||
|
||||
// 5分钟超时兜底:无论压力是否稳定,都触发结束
|
||||
if (m_totalTickCount >= TIMEOUT_TICKS) {
|
||||
m_timeout = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return m_stableTickCount >= REQUIRED_STABLE_TICKS;
|
||||
if (m_tickCounter != 0) {
|
||||
m_tickCounter = (m_tickCounter + 1) % SAMPLE_INTERVAL_TICKS;
|
||||
return false;
|
||||
}
|
||||
m_tickCounter = (m_tickCounter + 1) % SAMPLE_INTERVAL_TICKS;
|
||||
|
||||
if (calibratedPressure <= 0.0f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_buf[m_bufIdx] = realtimePressure;
|
||||
m_bufIdx = (m_bufIdx + 1) % BUF_SIZE;
|
||||
m_sampleCount++;
|
||||
|
||||
if (m_sampleCount < BUF_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
float rangeMin = calibratedPressure - 10.0f;
|
||||
float rangeMax = calibratedPressure + 10.0f;
|
||||
|
||||
if (!checkFirst4InRange(rangeMin, rangeMax)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
float avg = computeTrimmedMean();
|
||||
return (avg >= rangeMin && avg <= rangeMax);
|
||||
}
|
||||
|
||||
void PressureMonitor::reset()
|
||||
{
|
||||
m_stableTickCount = 0;
|
||||
m_tickCounter = 0;
|
||||
m_sampleCount = 0;
|
||||
m_bufIdx = 0;
|
||||
m_totalTickCount = 0;
|
||||
m_timeout = false;
|
||||
for (int i = 0; i < BUF_SIZE; i++) {
|
||||
m_buf[i] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
bool PressureMonitor::isCurrentlyInRange() const
|
||||
{
|
||||
return m_stableTickCount > 0;
|
||||
return (m_sampleCount >= BUF_SIZE);
|
||||
}
|
||||
|
||||
int PressureMonitor::stableTickCount() const
|
||||
{
|
||||
return m_stableTickCount;
|
||||
return m_sampleCount;
|
||||
}
|
||||
|
||||
bool PressureMonitor::wasTimeout() const
|
||||
{
|
||||
return m_timeout;
|
||||
}
|
||||
|
||||
const float* PressureMonitor::buffer() const
|
||||
{
|
||||
return m_buf;
|
||||
}
|
||||
|
||||
bool PressureMonitor::checkFirst4InRange(float rangeMin, float rangeMax) const
|
||||
{
|
||||
for (int i = 0; i < 4; i++) {
|
||||
float val = m_buf[(m_bufIdx + i) % BUF_SIZE];
|
||||
if (val < rangeMin || val > rangeMax) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
float PressureMonitor::computeTrimmedMean() const
|
||||
{
|
||||
float minVal = m_buf[0];
|
||||
float maxVal = m_buf[0];
|
||||
float sum = 0.0f;
|
||||
|
||||
for (int i = 0; i < BUF_SIZE; i++) {
|
||||
float val = m_buf[i];
|
||||
sum += val;
|
||||
if (val < minVal) minVal = val;
|
||||
if (val > maxVal) maxVal = val;
|
||||
}
|
||||
|
||||
return (sum - minVal - maxVal) / (float)(BUF_SIZE - 2);
|
||||
}
|
||||
Reference in New Issue
Block a user