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.
This commit is contained in:
Bruce Luckcuck 2019-12-14 19:53:23 -05:00
parent 4c58889915
commit d890c3afcc
1 changed files with 5 additions and 5 deletions

View File

@ -216,7 +216,7 @@ static float invSqrt(float x)
static void imuMahonyAHRSupdate(float dt, float gx, float gy, float gz, static void imuMahonyAHRSupdate(float dt, float gx, float gy, float gz,
bool useAcc, float ax, float ay, float az, 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) bool useCOG, float courseOverGround, const float dcmKpGain)
{ {
static float integralFBx = 0.0f, integralFBy = 0.0f, integralFBz = 0.0f; // integral error terms scaled by Ki 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 #ifdef USE_MAG
// Use measured magnetic field vector // 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); float recipMagNorm = sq(mx) + sq(my) + sq(mz);
if (useMag && recipMagNorm > 0.01f) { if (useMag && recipMagNorm > 0.01f) {
// Normalise magnetometer measurement // Normalise magnetometer measurement
@ -271,9 +274,6 @@ static void imuMahonyAHRSupdate(float dt, float gx, float gy, float gz,
} }
#else #else
UNUSED(useMag); UNUSED(useMag);
UNUSED(mx);
UNUSED(my);
UNUSED(mz);
#endif #endif
// Use measured acceleration vector // Use measured acceleration vector
@ -553,7 +553,7 @@ static void imuCalculateEstimatedAttitude(timeUs_t currentTimeUs)
imuMahonyAHRSupdate(deltaT * 1e-6f, imuMahonyAHRSupdate(deltaT * 1e-6f,
DEGREES_TO_RADIANS(gyroAverage[X]), DEGREES_TO_RADIANS(gyroAverage[Y]), DEGREES_TO_RADIANS(gyroAverage[Z]), DEGREES_TO_RADIANS(gyroAverage[X]), DEGREES_TO_RADIANS(gyroAverage[Y]), DEGREES_TO_RADIANS(gyroAverage[Z]),
useAcc, accAverage[X], accAverage[Y], accAverage[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)); useCOG, courseOverGround, imuCalcKpGain(currentTimeUs, useAcc, gyroAverage));
imuUpdateEulerAngles(); imuUpdateEulerAngles();