From f0dca86a4eb6b9f57ab1dbae2057013405fdef34 Mon Sep 17 00:00:00 2001 From: Raphael Hunziker Date: Thu, 10 Sep 2026 22:32:59 +0200 Subject: [PATCH 1/3] compass: add MAG debug mode for raw magnetometer logging Raw magnetometer samples were not obtainable from a blackbox log. The existing magADC fields are written after the magZero/magGain correction and after sensor and board alignment have been applied, so they cannot be used to solve for hard and soft iron offsets externally. On a board that already has a stored calibration the original sample cannot be recovered from them at all. Publish mag.dev.magADCRaw into debug[0..2] straight after the sensor read, before calibration and before alignment, mirroring how DEBUG_ACC already exposes acc.dev.ADCRaw in acceleration.c. Enable with: set debug_mode = MAG The three axes then appear as debug[0] (X), debug[1] (Y) and debug[2] (Z) in the blackbox log and in the CLI "debug" command. This reuses the existing debug channel, so the log format is unchanged. debug_mode defaults to NONE and the debug columns are only emitted when debug_mode is not NONE, so logs from users who do not opt in are unaffected. DEBUG_MAG is appended to the end of debugType_e so the numbering of the existing debug modes is preserved. Closes #10758 --- docs/Blackbox.md | 1 + docs/Settings.md | 1 + src/main/build/debug.h | 1 + src/main/fc/cli.c | 3 ++- src/main/fc/settings.yaml | 2 +- src/main/sensors/compass.c | 1 + 6 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/Blackbox.md b/docs/Blackbox.md index d770955497d..54c79d4afc1 100644 --- a/docs/Blackbox.md +++ b/docs/Blackbox.md @@ -185,6 +185,7 @@ Available debug modes include: - `POS_EST` - Position estimation debugging - `GPS` - GPS debugging - `ALTITUDE` - Altitude estimation debugging +- `MAG` - Raw, uncalibrated magnetometer samples (useful for external compass calibration) - And 20+ other modes for specific subsystems To use debug mode logging: diff --git a/docs/Settings.md b/docs/Settings.md index 0a4792476b7..ae188759691 100644 --- a/docs/Settings.md +++ b/docs/Settings.md @@ -799,6 +799,7 @@ Defines debug values exposed in debug variables (developer / debugging setting) | VTOL_TRANSITION | | | VTOL_MC_PROTECT | | | TERRAIN_NAV | | +| MAG | | --- diff --git a/src/main/build/debug.h b/src/main/build/debug.h index d5fd7f71baf..7b9b94e499c 100644 --- a/src/main/build/debug.h +++ b/src/main/build/debug.h @@ -83,6 +83,7 @@ typedef enum { DEBUG_VTOL_TRANSITION, DEBUG_VTOL_MC_PROTECT, DEBUG_TERRAIN_NAV, + DEBUG_MAG, DEBUG_COUNT // also update debugModeNames in cli.c } debugType_e; diff --git a/src/main/fc/cli.c b/src/main/fc/cli.c index 718dadca046..1fff6c52a93 100644 --- a/src/main/fc/cli.c +++ b/src/main/fc/cli.c @@ -233,7 +233,8 @@ static const char *debugModeNames[DEBUG_COUNT] = { "OSD_REFRESH", "VTOL_TRANSITION", "VTOL_MC_PROTECT", - "TERRAIN_NAV" + "TERRAIN_NAV", + "MAG" }; /* Sensor names (used in lookup tables for *_hardware settings and in status diff --git a/src/main/fc/settings.yaml b/src/main/fc/settings.yaml index 78d8f20ca90..b5b32f9f8a1 100644 --- a/src/main/fc/settings.yaml +++ b/src/main/fc/settings.yaml @@ -84,7 +84,7 @@ tables: "VIBE", "CRUISE", "REM_FLIGHT_TIME", "SMARTAUDIO", "ACC", "NAV_YAW", "PCF8574", "DYN_GYRO_LPF", "AUTOLEVEL", "ALTITUDE", "AUTOTRIM", "AUTOTUNE", "RATE_DYNAMICS", "LANDING", "POS_EST", - "ADAPTIVE_FILTER", "HEADTRACKER", "GPS", "LULU", "SBUS2", "OSD_REFRESH", "VTOL_TRANSITION", "VTOL_MC_PROTECT", "TERRAIN_NAV"] + "ADAPTIVE_FILTER", "HEADTRACKER", "GPS", "LULU", "SBUS2", "OSD_REFRESH", "VTOL_TRANSITION", "VTOL_MC_PROTECT", "TERRAIN_NAV", "MAG"] - name: vtol_mc_protection_mode values: ["OFF", "NAV", "NAV_AND_STABILIZED"] - name: aux_operator diff --git a/src/main/sensors/compass.c b/src/main/sensors/compass.c index bd61b2d8c38..d5fa5da00fe 100644 --- a/src/main/sensors/compass.c +++ b/src/main/sensors/compass.c @@ -423,6 +423,7 @@ void compassUpdate(timeUs_t currentTimeUs) for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) { mag.magADC[axis] = mag.dev.magADCRaw[axis]; // int32_t copy to work with + DEBUG_SET(DEBUG_MAG, axis, mag.dev.magADCRaw[axis]); } if (STATE(CALIBRATE_MAG)) { From 99f7a68717a8d1adf26efa490a7be11e84a2bb9b Mon Sep 17 00:00:00 2001 From: Raffi1202 Date: Fri, 11 Sep 2026 17:56:31 +0200 Subject: [PATCH 2/3] Reject failed MLX90393 bus reads --- src/main/drivers/compass/compass_mlx90393.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/drivers/compass/compass_mlx90393.c b/src/main/drivers/compass/compass_mlx90393.c index c9318761431..607d2bf6d35 100644 --- a/src/main/drivers/compass/compass_mlx90393.c +++ b/src/main/drivers/compass/compass_mlx90393.c @@ -105,7 +105,9 @@ static bool mlx90393Read(magDev_t * mag) uint8_t buf[7] = {0}; - busReadBuf(mag->busDev, MLX90393_READ_MEASUREMENT | MLX90393_MEASURE_3D, buf, 7); + if (!busReadBuf(mag->busDev, MLX90393_READ_MEASUREMENT | MLX90393_MEASURE_3D, buf, sizeof(buf))) { + return false; + } mag->magADCRaw[X] = ((short)(buf[1] << 8 | buf[2])); mag->magADCRaw[Y] = ((short)(buf[3] << 8 | buf[4])); From 8c50a4aeddfc4872da2281720f8b3608f6014d4d Mon Sep 17 00:00:00 2001 From: Raphael Hunziker Date: Fri, 18 Sep 2026 09:30:45 +0200 Subject: [PATCH 3/3] settings: add the missing MAG_CALIB entry to debug_modes debugType_e gained DEBUG_MAG_CALIB and cli.c gained its name, but the debug_modes table in settings.yaml was not updated, leaving one name fewer than DEBUG_COUNT. Since set debug_mode = stores the table index, every name from index 27 up selected the wrong mode - ESC selected DEBUG_TERRAIN_NAV - and DEBUG_ESC could not be selected at all. Without this the MAG entry added by this branch would land on DEBUG_ESC too. --- docs/Settings.md | 1 + src/main/fc/settings.yaml | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/Settings.md b/docs/Settings.md index 4e3035600d4..0ffaf63a6a7 100644 --- a/docs/Settings.md +++ b/docs/Settings.md @@ -796,6 +796,7 @@ Defines debug values exposed in debug variables (developer / debugging setting) | LULU | | | SBUS2 | | | OSD_REFRESH | | +| MAG_CALIB | | | VTOL_TRANSITION | | | VTOL_MC_PROTECT | | | TERRAIN_NAV | | diff --git a/src/main/fc/settings.yaml b/src/main/fc/settings.yaml index f73b4470b2a..28cd42ba2e7 100644 --- a/src/main/fc/settings.yaml +++ b/src/main/fc/settings.yaml @@ -84,7 +84,8 @@ tables: "VIBE", "CRUISE", "REM_FLIGHT_TIME", "SMARTAUDIO", "ACC", "NAV_YAW", "PCF8574", "DYN_GYRO_LPF", "AUTOLEVEL", "ALTITUDE", "AUTOTRIM", "AUTOTUNE", "RATE_DYNAMICS", "LANDING", "POS_EST", - "ADAPTIVE_FILTER", "HEADTRACKER", "GPS", "LULU", "SBUS2", "OSD_REFRESH", "VTOL_TRANSITION", "VTOL_MC_PROTECT", "TERRAIN_NAV", "ESC", "MAG"] + "ADAPTIVE_FILTER", "HEADTRACKER", "GPS", "LULU", "SBUS2", "OSD_REFRESH", + "MAG_CALIB", "VTOL_TRANSITION", "VTOL_MC_PROTECT", "TERRAIN_NAV", "ESC", "MAG"] - name: vtol_mc_protection_mode values: ["OFF", "NAV", "NAV_AND_STABILIZED"] - name: aux_operator