Skip to content

Commit 872d759

Browse files
authored
fix: IDF v6.0 ESP-NOW callback compat (#944) + occupancy noise-floor anchor (#942) (#945)
* fix(firmware): on_send ESP-NOW callback compat for IDF v6.0 (closes #944) ESP-IDF v6.0 changed `esp_now_send_cb_t` from void (*)(const uint8_t *mac, esp_now_send_status_t status) to void (*)(const esp_now_send_info_t *tx_info, esp_now_send_status_t status) The C6 sync ESP-NOW path's `on_recv` was already version-guarded with `#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)` (lines 102-112) but the `on_send` sibling missed the equivalent guard. CI runs against IDF v5.4 so the regression slipped through; the reporter on IDF v6.0.1 with xtensa-esp-elf esp-15.2.0_20251204 hit: c6_sync_espnow.c:182:30: error: passing argument 1 of 'esp_now_register_send_cb' from incompatible pointer type [-Wincompatible-pointer-types] Fix: mirror the recv guard with `#if ESP_IDF_VERSION_MAJOR >= 6` since the send-callback signature change happened at IDF v6.0 (not v5.x like the recv-callback). Both branches ignore the address-side argument since `on_send` only inspects `status` to bump the TX-fail counter. Adds `#include "esp_idf_version.h"` so the macro is in scope. Closes #944 Co-Authored-By: claude-flow <ruv@ruv.net> * fix(signal): anchor estimate_occupancy noise floor to calibration (closes #942) `test_estimate_occupancy_noise_only` asserts that 20 noise-only frames fed through a 50-frame calibrated `FieldModel` yield 0 occupancy. Failure reported on the upstream Linux + BLAS build. Root cause Calibration and estimation each compute their own Marcenko-Pastur threshold: threshold = noise_var · (1 + sqrt(p / N))² with `noise_var` = median of the bottom half of positive eigenvalues from their own covariance. The MP ratio differs across the two phases: calibration (50 frames, p=8): ratio = 0.16, factor ≈ 1.96 estimation (20 frames, p=8): ratio = 0.40, factor ≈ 2.66 On a small estimation window the local `noise_var` estimate can also be smaller than the calibration's (fewer samples → bottom-half median hits lower-magnitude eigenvalues). The combination of a smaller noise_var on estimation and the larger MP factor can flip eigenvalues on/off the "significant" line in a sample-size-dependent way, so an identical-distribution test window scores `significant > baseline_eigenvalue_count` and reports phantom persons. Fix Persist the calibration `noise_var` on `FieldNormalMode` (new field `baseline_noise_var: f64`) and use `max(local_noise_var, baseline_noise_var)` as the noise floor inside `estimate_occupancy`. This anchors the threshold to the calibration scale and prevents the short-window collapse without changing behavior when the local window's own noise dominates (the real-motion case). `baseline_noise_var` defaults to 0.0 in the diagonal-fallback paths; the estimation code treats 0.0 as "no anchored floor available" and preserves the pre-#942 single-window behavior — so older `FieldNormalMode` instances deserialised from disk continue to work unchanged. Test results cargo test --workspace --no-default-features → 413 lib tests pass (signal crate), 0 fail, 1 ignored. The actual `eigenvalue`-gated test still requires BLAS (not buildable on Windows). Logic-trace via the four numerical anchors above shows the fix flips `noise_var` from the smaller local value back up to the calibration scale, dropping `significant` to or below `baseline_eigenvalue_count` so the saturating subtraction returns 0. Closes #942 Co-Authored-By: claude-flow <ruv@ruv.net>
1 parent 2c136ac commit 872d759

2 files changed

Lines changed: 57 additions & 7 deletions

File tree

firmware/esp32-csi-node/main/c6_sync_espnow.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "esp_wifi.h"
2222
#include "esp_mac.h"
2323
#include "esp_timer.h"
24+
#include "esp_idf_version.h"
2425
#include "freertos/FreeRTOS.h"
2526
#include "freertos/timers.h"
2627
#include <string.h>
@@ -144,11 +145,27 @@ static void on_recv(const uint8_t *src_mac, const uint8_t *data, int len)
144145
}
145146
}
146147

148+
/* Issue #944: ESP-IDF v6.0 changed `esp_now_send_cb_t` from
149+
* void (*)(const uint8_t *mac, esp_now_send_status_t status)
150+
* to
151+
* void (*)(const esp_now_send_info_t *tx_info, esp_now_send_status_t status)
152+
* Both signatures ignore the address-side argument here — we only inspect
153+
* `status` to bump the TX-fail counter — so the body is identical; only the
154+
* function-pointer type differs. ESP_IDF_VERSION_MAJOR is the canonical guard.
155+
*/
156+
#if ESP_IDF_VERSION_MAJOR >= 6
157+
static void on_send(const esp_now_send_info_t *tx_info, esp_now_send_status_t status)
158+
{
159+
(void)tx_info;
160+
if (status != ESP_NOW_SEND_SUCCESS) s_tx_fail++;
161+
}
162+
#else
147163
static void on_send(const uint8_t *mac, esp_now_send_status_t status)
148164
{
149165
(void)mac;
150166
if (status != ESP_NOW_SEND_SUCCESS) s_tx_fail++;
151167
}
168+
#endif
152169

153170
static void beacon_timer_cb(TimerHandle_t t)
154171
{

v2/crates/wifi-densepose-signal/src/ruvsense/field_model.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ pub struct FieldNormalMode {
276276
pub geometry_hash: u64,
277277
/// Baseline eigenvalue count above Marcenko-Pastur threshold (empty-room).
278278
pub baseline_eigenvalue_count: usize,
279+
/// Baseline noise variance estimate (median of bottom-half positive
280+
/// eigenvalues from the calibration covariance). Persisted so that
281+
/// `estimate_occupancy` can anchor its Marcenko-Pastur threshold to the
282+
/// calibration noise floor instead of letting it drift with the
283+
/// per-window sample size. Defaults to 0.0 in the diagonal-fallback path.
284+
/// Issue #942.
285+
pub baseline_noise_var: f64,
279286
}
280287

281288
/// Body perturbation extracted from a CSI observation.
@@ -504,7 +511,11 @@ impl FieldModel {
504511
let baseline: Vec<Vec<f64>> = self.link_stats.iter().map(|ls| ls.mean_vector()).collect();
505512

506513
// --- True eigenvalue decomposition (with diagonal fallback) ---
507-
let (mode_energies, environmental_modes, baseline_eig_count) =
514+
// Returns: (energies, modes, baseline_count, baseline_noise_var).
515+
// The noise_var slot is 0.0 in the diagonal-fallback paths; the
516+
// estimation hot path treats 0.0 as "no anchored noise floor" and
517+
// falls back to per-window noise_var, preserving pre-#942 behavior.
518+
let (mode_energies, environmental_modes, baseline_eig_count, baseline_noise_var) =
508519
if let Some(ref cov_sum) = self.covariance_sum {
509520
if self.covariance_count > 1 {
510521
// Compute sample covariance from raw outer products:
@@ -588,23 +599,28 @@ impl FieldModel {
588599
let baseline_count =
589600
eigenvalues.iter().filter(|&&ev| ev > mp_threshold).count();
590601

591-
(energies, modes, baseline_count)
602+
(energies, modes, baseline_count, noise_var)
592603
}
593604
Err(_) => {
594605
// Fallback to diagonal approximation on SVD failure
595-
diagonal_fallback(&self.link_stats, n_sc, n_modes)
606+
let (e, m, b) =
607+
diagonal_fallback(&self.link_stats, n_sc, n_modes);
608+
(e, m, b, 0.0_f64)
596609
}
597610
}
598611
// When eigenvalue feature is disabled, use diagonal fallback
599612
#[cfg(not(feature = "eigenvalue"))]
600613
{
601-
diagonal_fallback(&self.link_stats, n_sc, n_modes)
614+
let (e, m, b) = diagonal_fallback(&self.link_stats, n_sc, n_modes);
615+
(e, m, b, 0.0_f64)
602616
}
603617
} else {
604-
diagonal_fallback(&self.link_stats, n_sc, n_modes)
618+
let (e, m, b) = diagonal_fallback(&self.link_stats, n_sc, n_modes);
619+
(e, m, b, 0.0_f64)
605620
}
606621
} else {
607-
diagonal_fallback(&self.link_stats, n_sc, n_modes)
622+
let (e, m, b) = diagonal_fallback(&self.link_stats, n_sc, n_modes);
623+
(e, m, b, 0.0_f64)
608624
};
609625

610626
// Compute variance explained using the same centered covariance as modes.
@@ -648,6 +664,7 @@ impl FieldModel {
648664
calibrated_at_us: timestamp_us,
649665
geometry_hash,
650666
baseline_eigenvalue_count: baseline_eig_count,
667+
baseline_noise_var,
651668
};
652669

653670
self.modes = Some(field_mode);
@@ -794,7 +811,7 @@ impl FieldModel {
794811
// Marcenko-Pastur noise estimate: median of POSITIVE eigenvalues
795812
// in the bottom half. Excludes zeros from rank-deficient matrices
796813
// (common when n_subcarriers > n_frames, e.g. 56 subcarriers / 50 frames).
797-
let noise_var = {
814+
let local_noise_var = {
798815
let mut positive: Vec<f64> =
799816
eigenvalues.iter().copied().filter(|&e| e > 1e-10).collect();
800817
positive.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
@@ -807,6 +824,22 @@ impl FieldModel {
807824
return Ok(0); // All zero eigenvalues — can't estimate
808825
}
809826
};
827+
828+
// Issue #942: anchor the noise floor to the calibration's noise_var
829+
// when it's available. Per-window noise_var drifts with sample size —
830+
// a short estimation window can produce a small local_noise_var that
831+
// inflates `significant` and breaks the test_estimate_occupancy_noise_only
832+
// invariant. The max of (calibration noise, local noise) keeps the
833+
// threshold from collapsing on small windows while still letting the
834+
// per-window noise dominate when it's the larger estimate. Falls back
835+
// to local_noise_var when baseline_noise_var == 0 (diagonal-fallback
836+
// calibration path, or pre-#942 stored modes).
837+
let noise_var = if modes.baseline_noise_var > 0.0 {
838+
local_noise_var.max(modes.baseline_noise_var)
839+
} else {
840+
local_noise_var
841+
};
842+
810843
let ratio = n as f64 / count as f64;
811844
let mp_threshold = noise_var * (1.0 + ratio.sqrt()).powi(2);
812845

0 commit comments

Comments
 (0)