fix:resident 字段修复(误伤了,重新放开)
feat:增加防抖功能。
This commit is contained in:
+37
-3
@@ -24,6 +24,8 @@ RadarManager::RadarManager()
|
||||
, _stablePresence(false)
|
||||
, _presenceCnt(0)
|
||||
, _lastRawPresence(false)
|
||||
, _motionCnt(0)
|
||||
, _lastRawMotion(false)
|
||||
{
|
||||
memset(&_currentFrame, 0, sizeof(_currentFrame));
|
||||
memset(&_data, 0, sizeof(_data));
|
||||
@@ -241,7 +243,13 @@ void RadarManager::_processCompleteFrame() {
|
||||
_extractHeartRate(_currentFrame);
|
||||
break;
|
||||
case CTRL_FALL:
|
||||
#if RADAR_TYPE == RADAR_TYPE_AFD1
|
||||
_extractFall(_currentFrame); // AFD1 专用跌倒
|
||||
#else
|
||||
// ABD1 不会发 0x83 帧, 若收到可能是干扰, 直接丢弃
|
||||
(void)_currentFrame;
|
||||
#endif
|
||||
break;
|
||||
break;
|
||||
case CTRL_SLEEP:
|
||||
_extractSleep(_currentFrame);
|
||||
@@ -282,7 +290,8 @@ void RadarManager::_extractPresence(const RadarFrame& frame) {
|
||||
// cmd 0x01 (len=1): 运动状态/体动 (值越大越活跃)
|
||||
else if (frame.command == 0x01 && frame.data_length >= 1) {
|
||||
_data.body = raw;
|
||||
_data.motion = (raw >= 1);
|
||||
bool rawMotion = (raw >= 1);
|
||||
_filterMotion(rawMotion); // 3 帧防抖
|
||||
_data.radarValid = true;
|
||||
_data.lastUpdate = millis();
|
||||
_notifyUpdate();
|
||||
@@ -297,14 +306,16 @@ void RadarManager::_extractPresence(const RadarFrame& frame) {
|
||||
// cmd 0x04 (len=2): [0]=presence [1]=motion
|
||||
else if (frame.command == 0x04 && frame.data_length >= 2) {
|
||||
rawPresence = (frame.data[0] != 0);
|
||||
_data.motion = (frame.data[1] == 0);
|
||||
bool rawMotion = (frame.data[1] == 0);
|
||||
_filterMotion(rawMotion); // 3 帧防抖
|
||||
_data.radarValid = true;
|
||||
_data.lastUpdate = millis();
|
||||
}
|
||||
// cmd 0x05 (len=6): 完整包
|
||||
else if (frame.command == 0x05 && frame.data_length >= 6) {
|
||||
rawPresence = (frame.data[0] != 0);
|
||||
_data.motion = (frame.data[1] == 0);
|
||||
bool rawMotion = (frame.data[1] == 0);
|
||||
_filterMotion(rawMotion); // 3 帧防抖
|
||||
_data.distance = ((uint16_t)frame.data[2] << 8) | frame.data[3];
|
||||
_data.body = frame.data[4];
|
||||
_data.radarValid = true;
|
||||
@@ -386,10 +397,33 @@ void RadarManager::_extractSleep(const RadarFrame& frame) {
|
||||
}
|
||||
|
||||
if (frame.data_length >= 4) {
|
||||
#if RADAR_TYPE == RADAR_TYPE_AFD1
|
||||
// AFD1 协议: data[3] 低 2 位 = 跌倒/驻留
|
||||
_data.fall = (frame.data[3] & 0x01) != 0;
|
||||
_data.resident = (frame.data[3] & 0x02) != 0;
|
||||
_data.lastUpdate = millis();
|
||||
_notifyUpdate();
|
||||
#else
|
||||
// ABD1 协议: data[3] bit0 是睡眠状态标志 (不是跌倒), bit1 才是驻留
|
||||
// 之前误把 bit0 当 fall 触发误报, 现只取 resident
|
||||
_data.resident = (frame.data[3] & 0x02) != 0;
|
||||
_data.lastUpdate = millis();
|
||||
_notifyUpdate();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// 运动状态防抖: 连续 3 帧一致才更新 _data.motion
|
||||
// 雷达体动信号容易因小幅抖动/呼吸胸腔起伏/电磁干扰在 0/1 之间跳变
|
||||
void RadarManager::_filterMotion(bool rawMotion) {
|
||||
if (rawMotion == _lastRawMotion) {
|
||||
if (_motionCnt < 3) _motionCnt++;
|
||||
} else {
|
||||
_motionCnt = 0;
|
||||
_lastRawMotion = rawMotion;
|
||||
}
|
||||
if (_motionCnt >= 3 && rawMotion != _data.motion) {
|
||||
_data.motion = rawMotion;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user