fix: 触发
This commit is contained in:
@@ -245,8 +245,8 @@ def cmd_str():
|
||||
# 4. 初始化设备ID(network_manager 内部会自动设置 device_id 和 password)
|
||||
network_manager.read_device_id()
|
||||
|
||||
# 5. 创建照片存储目录(如果启用图像保存)
|
||||
if config.SAVE_IMAGE_ENABLED:
|
||||
# 5. 创建照片存储目录(如果启用图像保存或检测失败时强制保存)
|
||||
if config.SAVE_IMAGE_ENABLED or getattr(config, "SAVE_IMAGE_ON_FAILURE", False):
|
||||
photo_dir = config.PHOTO_DIR
|
||||
if photo_dir not in os.listdir("/root"):
|
||||
try:
|
||||
@@ -278,6 +278,12 @@ def cmd_str():
|
||||
logger.info("系统准备完成...")
|
||||
|
||||
last_adc_trigger = 0
|
||||
# 读取一次ADC初始值,防止开机时传感器已有压力导致误触发
|
||||
try:
|
||||
last_adc_val = hardware_manager.adc_obj.read()
|
||||
except Exception:
|
||||
last_adc_val = 0
|
||||
peak_adc_val = 0 # 当前周期内的压力峰值
|
||||
# 气压采样:减少日志频率(每 N 个点输出一条),避免 logger.debug 拖慢采样
|
||||
PRESSURE_BATCH_SIZE = 100
|
||||
|
||||
@@ -367,15 +373,21 @@ def cmd_str():
|
||||
pressure_max = adc_val
|
||||
if len(pressure_buf) >= PRESSURE_BATCH_SIZE:
|
||||
_flush_pressure_buf("batch")
|
||||
# if adc_val >= 2000:
|
||||
# print(f"adc :{adc_val}")
|
||||
if adc_val >= config.ADC_TRIGGER_THRESHOLD:
|
||||
# 峰值检测:压力从峰值下降时触发,确保捕获到最大冲击时刻
|
||||
if adc_val > peak_adc_val:
|
||||
peak_adc_val = adc_val # 更新峰值
|
||||
if (peak_adc_val >= config.ADC_TRIGGER_THRESHOLD
|
||||
and adc_val < peak_adc_val
|
||||
and last_adc_val >= peak_adc_val):
|
||||
# 封顶后下降沿触发:peak是最大值,当前值开始下降,且上次值还在peak位置
|
||||
hardware_manager.start_idle_timer() # 重新计时
|
||||
diff_ms = current_time - last_adc_trigger
|
||||
if diff_ms < 3000:
|
||||
logger.info(f"[MAIN] 扳机触发过于频繁, {diff_ms}ms")
|
||||
peak_adc_val = 0 # 去抖期间重置峰值
|
||||
time.sleep_ms(5)
|
||||
continue
|
||||
last_adc_trigger = current_time
|
||||
peak_adc_val = 0 # 触发后重置峰值
|
||||
# 触发前先把缓存刷出来,避免波形被长耗时处理截断
|
||||
_flush_pressure_buf("before_trigger")
|
||||
|
||||
@@ -396,6 +408,7 @@ def cmd_str():
|
||||
except Exception as e:
|
||||
pass
|
||||
time.sleep_ms(5)
|
||||
last_adc_val = adc_val
|
||||
|
||||
except Exception as e:
|
||||
# 主循环的顶层异常捕获,防止程序静默退出
|
||||
|
||||
Reference in New Issue
Block a user