hil: segmented capture with tuner-fault recovery, in-window IQ accounting

This commit is contained in:
Lars Nolden
2026-09-01 10:06:32 +02:00
parent 9a65fa2040
commit 3a2e23e204
4 changed files with 85 additions and 32 deletions
+68 -26
View File
@@ -1,40 +1,82 @@
#!/usr/bin/env bash
# Capture rtl_wmbus decodes AND raw IQ from a SINGLE rtl_sdr stream.
# Capture rtl_wmbus decodes AND raw IQ from a single rtl_sdr stream, in
# bounded segments with recovery between them.
#
# Why one stream: this dongle's sensitivity drifts by ~17 dB over minutes,
# so measurements taken in separate captures at "the same gain" are not
# comparable. Decoding meter originals and measuring our own transmissions
# in the IQ must therefore share one stream, one gain, one instant.
# Two hardware facts drive this design (docs/FINDINGS.md):
# 1. This dongle's R820T tuner dies after a few minutes of continuous
# streaming ("r82xx_write: i2c wr failed"). rtl_sdr does NOT exit -- it
# keeps producing nothing, so a naive long capture silently goes deaf.
# Bounded segments with -n make it exit cleanly, and we usbreset between
# segments instead of relying on failure detection.
# 2. Sensitivity drifts ~17 dB over minutes, so meter decodes and our own
# TX measurements must come from the same stream to be comparable.
#
# Usage: capture_both.sh DURATION_S DECODE_LOG IQ_FILE [GAIN]
# Usage: capture_both.sh DURATION_S DECODE_LOG IQ_FILE [GAIN] [IQ_SEGMENTS]
set -euo pipefail
cd "$(dirname "$0")/.."
DUR=$1; OUT=$2; IQ=$3; GAIN=${4:-42.1}
DUR=$1; OUT=$2; IQ=$3; GAIN=${4:-42.1}; IQ_SEGMENTS=${5:-1}
SEG=${SEG_SECONDS:-140} # below the observed tuner death point
RATE=1600000
: > "$OUT"
: > "$IQ"
ERR="${OUT%.log}.err"
STATS="${OUT%.log}.stats"
: > "$ERR"
health_ok=0
for i in 1 2 3; do
if ./result-py/bin/python3 hil/sdr_health.py >> "$ERR" 2>&1; then
health_ok=1; break
health() {
./result-py/bin/python3 hil/sdr_health.py >> "$ERR" 2>&1
}
usable=0
segments=0
recovered=0
iq_seconds=0
deadline=$(( $(date +%s) + DUR ))
while :; do
remaining=$(( deadline - $(date +%s) ))
[ "$remaining" -le 10 ] && break
this=$(( remaining < SEG ? remaining : SEG ))
segments=$((segments + 1))
if ! health; then
echo "--- segment $segments: SDR unhealthy, usbreset" >> "$ERR"
usbreset 0bda:2838 >> "$ERR" 2>&1 || true
recovered=$((recovered + 1))
sleep 5
health || { echo "--- still unhealthy, skipping segment" >> "$ERR"; sleep 2; continue; }
fi
echo "--- segment $segments: ${this}s (gain $GAIN)" >> "$ERR"
t0=$(date +%s)
if [ "$segments" -le "$IQ_SEGMENTS" ]; then
# keep IQ for the first segments only; bounded so disk use is known
timeout $((this + 20)) rtl_sdr -f 868950000 -s "$RATE" -g "$GAIN" \
-n $((this * RATE)) - 2>>"$ERR" \
| tee -a "$IQ" \
| rtl_wmbus -f >> "$OUT" 2>/dev/null || true
iq_seconds=$((iq_seconds + this))
else
timeout $((this + 20)) rtl_sdr -f 868950000 -s "$RATE" -g "$GAIN" \
-n $((this * RATE)) - 2>>"$ERR" \
| rtl_wmbus -f >> "$OUT" 2>/dev/null || true
fi
t1=$(date +%s)
usable=$(( usable + t1 - t0 ))
# tuner commonly dies during a segment; reset before the next one
if grep -q "i2c wr failed" "$ERR"; then
echo "--- tuner fault seen; usbreset" >> "$ERR"
usbreset 0bda:2838 >> "$ERR" 2>&1 || true
recovered=$((recovered + 1))
: > "$ERR.tmp"; tail -c 20000 "$ERR" > "$ERR.tmp"; mv "$ERR.tmp" "$ERR"
sleep 5
fi
echo "--- SDR unhealthy; usbreset (try $i)" >> "$ERR"
usbreset 0bda:2838 >> "$ERR" 2>&1 || true
sleep $((i * 5))
done
echo "sdr_health_ok=$health_ok" > "${OUT%.log}.health"
t0=$(date +%s)
# tee duplicates the sample stream: one copy to disk, one to the decoder
timeout "$DUR" rtl_sdr -f 868950000 -s 1600000 -g "$GAIN" - 2>>"$ERR" \
| tee "$IQ" \
| rtl_wmbus -f >> "$OUT" 2>/dev/null || true
t1=$(date +%s)
printf 'streamed_seconds=%s\nrequested_seconds=%s\ngain=%s\niq_bytes=%s\n' \
"$((t1 - t0))" "$DUR" "$GAIN" "$(stat -c%s "$IQ" 2>/dev/null || echo 0)" \
> "$STATS"
echo "--- streamed $((t1 - t0))s of ${DUR}s" >> "$ERR"
printf 'usable_seconds=%s\nrequested_seconds=%s\nsegments=%s\nrecoveries=%s\ngain=%s\niq_seconds=%s\niq_bytes=%s\n' \
"$usable" "$DUR" "$segments" "$recovered" "$GAIN" "$iq_seconds" \
"$(stat -c%s "$IQ" 2>/dev/null || echo 0)" > "$STATS"
echo "--- usable ${usable}s of ${DUR}s in $segments segment(s), $recovered recovery(ies)" >> "$ERR"