更新压力稳定判定算法

This commit is contained in:
2026-09-15 17:07:46 +08:00
parent d48826b028
commit 5942564f5e
3 changed files with 115 additions and 36 deletions
+23 -14
View File
@@ -6,33 +6,42 @@ class PressureMonitor
public:
PressureMonitor();
// 每个100ms tick 调用一次
// 每个100ms tick 调用一次(外部调用频率不变)
// realtimePressure: 实时压力 (sysPara->status.PRESSURE)
// calibratedPressure: 标定压力 (sysPara->para.pressure)
//
// 内部逻辑:每500ms采样一次,维护最近12个采样的环形缓冲区。
// 缓冲区满12个后,先检查最旧的4个采样是否各自在标定值 ±10 范围内,
// 满足后去掉全部12个中的最大值和最小值取平均,判断均值是否在 ±10 内。
// 每次新采样入队、最旧采样出队后立即重新判定。
// 返回 true 表示:
// 1) 压力已持续稳定 3 秒(±10 范围内),或
// 1) 前4入界且去极值均值在 ±10 范围内,或
// 2) 超过 2 分钟仍未稳定,兜底超时结束
bool tick(float realtimePressure, float calibratedPressure);
// 重置监测器状态
void reset();
// 当前压力是否在 ±10 范围内
bool isCurrentlyInRange() const;
// 当前连续稳定的 tick 计数
int stableTickCount() const;
// 是否因超时而触发结束(兜底)
bool wasTimeout() const;
private:
int m_stableTickCount;
int m_totalTickCount;
bool m_timeout;
// 返回环形缓冲区(12个压力值)
const float* buffer() const;
static const int REQUIRED_STABLE_TICKS = 30; // 3秒 / 100ms = 30 ticks
static const int TIMEOUT_TICKS = 1200; // 2分钟 / 100ms = 1200 ticks
private:
float computeTrimmedMean() const;
bool checkFirst4InRange(float rangeMin, float rangeMax) const;
static const int SAMPLE_INTERVAL_TICKS = 5; // 500ms / 100ms
static const int BUF_SIZE = 12; // 缓冲区大小
static const int TIMEOUT_TICKS = 1200; // 2分钟 / 100ms
int m_tickCounter; // 100ms tick 计数,==0 时采样
int m_sampleCount; // 已采集样本总数
int m_bufIdx; // 环形缓冲区下一次写入位置
float m_buf[12]; // 环形缓冲区
int m_totalTickCount; // 超时用总 tick 计数
bool m_timeout; // 是否已超时
};
#endif // PRESSUREMONITOR_H