From 4707f2b454bd50509c70b02126a37a47f3b863a5 Mon Sep 17 00:00:00 2001 From: stijncarelsbergh Date: Thu, 8 Oct 2026 00:04:15 +0200 Subject: [PATCH] fix(current sense): swap the measured currents, not a variable with itself The alignment routine relabels the pins when the highest current was measured on another channel, but the corresponding sample swap was written as `_swap(c_a.b, c_a.b)` - a no-op. The polarity check that follows (`_sign(c_a.a) < 0`) therefore inspects the sample of the wrong channel and can invert the gain of the phase it just repaired, which turns the current feedback into positive feedback at low currents. Also fixed the same pattern in the 'A-(C)NC' branch, which swapped c_a.b with c_a.c while the pins swapped were A and C. Reported in the alignment audit; trigger is exactly the miswiring this feature exists to correct. --- src/common/base_classes/CurrentSense.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/base_classes/CurrentSense.cpp b/src/common/base_classes/CurrentSense.cpp index ce96f85b5..b1596973c 100644 --- a/src/common/base_classes/CurrentSense.cpp +++ b/src/common/base_classes/CurrentSense.cpp @@ -267,7 +267,7 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver, bool m _swap(pinA, pinB); _swap(offset_ia, offset_ib); _swap(gain_a, gain_b); - _swap(c_a.b, c_a.b); + _swap(c_a.a, c_a.b); phases_switched = true; // signal that pins have been switched break; case 2: // phase C is the max current @@ -297,14 +297,14 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver, bool m _swap(pinA, pinB); _swap(offset_ia, offset_ib); _swap(gain_a, gain_b); - _swap(c_a.b, c_a.b); + _swap(c_a.a, c_a.b); phases_switched = true; // signal that pins have been switched }else if(_isset(pinA) && !_isset(pinC)){ SIMPLEFOC_DEBUG("CS: Switch A-(C)NC"); _swap(pinA, pinC); _swap(offset_ia, offset_ic); _swap(gain_a, gain_c); - _swap(c_a.b, c_a.c); + _swap(c_a.a, c_a.c); phases_switched = true; // signal that pins have been switched } }