From d890c3afcc0d7894a407eac9b7e4c291c10d685b Mon Sep 17 00:00:00 2001 From: Bruce Luckcuck Date: Sat, 14 Dec 2019 19:53:23 -0500 Subject: [PATCH] Fix undefined reference to mag when USE_MAG isn't defined The `mag` variable is defined as an `extern` in compass.h but the implementation in compass.c is bounded by `#ifdef USE_MAG`. So if `USE_MAG` is not debined then the `mag` variable is an undefined reference. In imu.c the `imuMahonyAHRSupdate()` function was being passed the elements of `mag` unconditionally like `mag.magADC[X]` so in the case that `USE_MAG` wasn't defined these were invalid null references. Luckily the logic in `imuMahonyAHRSupdate()` was properly bounded so that it never tried to access these variables. But in the case of a debug build the linker is unable to build a reference to these variables since they're never defined. --- src/main/flight/imu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/flight/imu.c b/src/main/flight/imu.c index b530fa9c28..8cd60e73fd 100644 --- a/src/main/flight/imu.c +++ b/src/main/flight/imu.c @@ -216,7 +216,7 @@ static float invSqrt(float x) static void imuMahonyAHRSupdate(float dt, float gx, float gy, float gz, bool useAcc, float ax, float ay, float az, - bool useMag, float mx, float my, float mz, + bool useMag, bool useCOG, float courseOverGround, const float dcmKpGain) { static float integralFBx = 0.0f, integralFBy = 0.0f, integralFBz = 0.0f; // integral error terms scaled by Ki @@ -244,6 +244,9 @@ static void imuMahonyAHRSupdate(float dt, float gx, float gy, float gz, #ifdef USE_MAG // Use measured magnetic field vector + float mx = mag.magADC[X]; + float my = mag.magADC[Y]; + float mz = mag.magADC[Z]; float recipMagNorm = sq(mx) + sq(my) + sq(mz); if (useMag && recipMagNorm > 0.01f) { // Normalise magnetometer measurement @@ -271,9 +274,6 @@ static void imuMahonyAHRSupdate(float dt, float gx, float gy, float gz, } #else UNUSED(useMag); - UNUSED(mx); - UNUSED(my); - UNUSED(mz); #endif // Use measured acceleration vector @@ -553,7 +553,7 @@ static void imuCalculateEstimatedAttitude(timeUs_t currentTimeUs) imuMahonyAHRSupdate(deltaT * 1e-6f, DEGREES_TO_RADIANS(gyroAverage[X]), DEGREES_TO_RADIANS(gyroAverage[Y]), DEGREES_TO_RADIANS(gyroAverage[Z]), useAcc, accAverage[X], accAverage[Y], accAverage[Z], - useMag, mag.magADC[X], mag.magADC[Y], mag.magADC[Z], + useMag, useCOG, courseOverGround, imuCalcKpGain(currentTimeUs, useAcc, gyroAverage)); imuUpdateEulerAngles();