Skip to content

Commit

Permalink
fix spikes in MagYaw debug by re-calc only on new data
Browse files Browse the repository at this point in the history
  • Loading branch information
ctzsnooze authored and pichim committed Sep 25, 2023
1 parent 65890cb commit 0c68430
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/flight/imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ STATIC_UNIT_TESTED void imuMahonyAHRSupdate(float dt, float gx, float gy, float
matrixVectorMul(&mag_ef, (const fpMat33_t*)&rMat, &mag_bf); // BF->EF true north

// Encapsulate additional operations in a block so that it is only executed when the according debug mode is used
if (debugMode == DEBUG_GPS_RESCUE_HEADING) {
// Only re-calculate magYaw when there is a new Mag data reading, to avoid spikes
if (debugMode == DEBUG_GPS_RESCUE_HEADING && mag.isNewMagADCFlag) {
fpMat33_t rMatZTrans;
yawToRotationMatrixZ(&rMatZTrans, -atan2_approx(rMat[1][0], rMat[0][0]));
fpVector3_t mag_ef_yawed;
Expand All @@ -274,6 +275,9 @@ STATIC_UNIT_TESTED void imuMahonyAHRSupdate(float dt, float gx, float gy, float
magYaw += 3600;
}
DEBUG_SET(DEBUG_GPS_RESCUE_HEADING, 4, magYaw); // mag heading in degrees * 10
// reset new mag data flag to false to initiate monitoring for new Mag data.
// note that if the debug doesn't run, this reset will not occur, and we won't waste cycles on the comparison
mag.isNewMagADCFlag = false;
}

if (useMag && magNormSquared > 0.01f) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/sensors/compass.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ void pgResetFn_compassConfig(compassConfig_t *compassConfig)
}

static int16_t magADCRaw[XYZ_AXIS_COUNT];
static int16_t magADCRawPrevious[XYZ_AXIS_COUNT];
static uint8_t magInit = 0;

void compassPreInit(void)
Expand Down Expand Up @@ -379,8 +380,13 @@ uint32_t compassUpdate(timeUs_t currentTimeUs)
}

for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
if (magADCRaw[axis] != magADCRawPrevious[axis]) {
// this test, and the isNewMagADCFlag itself, is only needed if we calculate magYaw in imu.c
mag.isNewMagADCFlag = true;
}
mag.magADC[axis] = magADCRaw[axis];
}

if (magDev.magAlignment == ALIGN_CUSTOM) {
alignSensorViaMatrix(mag.magADC, &magDev.rotationMatrix);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/sensors/compass.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef enum {
} magSensor_e;

typedef struct mag_s {
bool isNewMagADCFlag;
float magADC[XYZ_AXIS_COUNT];
} mag_t;

Expand Down

0 comments on commit 0c68430

Please sign in to comment.