47 lines
1.8 KiB
C++
47 lines
1.8 KiB
C++
#ifndef PRESSUREMONITOR_H
|
|
#define PRESSUREMONITOR_H
|
|
|
|
class PressureMonitor
|
|
{
|
|
public:
|
|
PressureMonitor();
|
|
|
|
// 每个100ms tick 调用一次(外部调用频率不变)
|
|
// realtimePressure: 实时压力 (sysPara->status.PRESSURE)
|
|
// calibratedPressure: 标定压力 (sysPara->para.pressure)
|
|
//
|
|
// 内部逻辑:每500ms采样一次,维护最近12个采样的环形缓冲区。
|
|
// 缓冲区满12个后,先检查最旧的4个采样是否各自在标定值 ±10 范围内,
|
|
// 满足后去掉全部12个中的最大值和最小值取平均,判断均值是否在 ±10 内。
|
|
// 每次新采样入队、最旧采样出队后立即重新判定。
|
|
// 返回 true 表示:
|
|
// 1) 前4入界且去极值均值在 ±10 范围内,或
|
|
// 2) 超过 2 分钟仍未稳定,兜底超时结束
|
|
bool tick(float realtimePressure, float calibratedPressure);
|
|
|
|
void reset();
|
|
|
|
bool isCurrentlyInRange() const;
|
|
int stableTickCount() const;
|
|
bool wasTimeout() const;
|
|
|
|
// 返回环形缓冲区(12个压力值)
|
|
const float* buffer() const;
|
|
|
|
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
|