1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-15 20:35:33 +03:00

Fix crash when first or second gyro is not detected.

Firmware now silently uses the one it does find.

User can detect this by looking at the `diff` output.
This commit is contained in:
Dominic Clifton 2019-03-19 16:24:49 +01:00
parent f4ce78f939
commit b7417a6d3d

View file

@ -516,24 +516,49 @@ bool gyroInit(void)
} }
firstArmingCalibrationWasStarted = false; firstArmingCalibrationWasStarted = false;
bool ret = false; enum {
NO_GYROS_DETECTED = 0,
DETECTED_GYRO_1 = (1 << 0),
DETECTED_GYRO_2 = (1 << 1),
};
uint8_t detectionFlags = NO_GYROS_DETECTED;
gyroToUse = gyroConfig()->gyro_to_use; gyroToUse = gyroConfig()->gyro_to_use;
if (gyroToUse == GYRO_CONFIG_USE_GYRO_1 || gyroToUse == GYRO_CONFIG_USE_GYRO_BOTH) { if (gyroToUse == GYRO_CONFIG_USE_GYRO_1 || gyroToUse == GYRO_CONFIG_USE_GYRO_BOTH) {
ret = gyroInitSensor(&gyroSensor1, gyroDeviceConfig(0)); if (gyroInitSensor(&gyroSensor1, gyroDeviceConfig(0))) {
if (!ret) {
return false; // TODO handle failure of first gyro detection better. - Perhaps update the config to use second gyro then indicate a new failure mode and reboot.
}
gyroHasOverflowProtection = gyroHasOverflowProtection && gyroSensor1.gyroDev.gyroHasOverflowProtection; gyroHasOverflowProtection = gyroHasOverflowProtection && gyroSensor1.gyroDev.gyroHasOverflowProtection;
detectionFlags |= DETECTED_GYRO_1;
}
} }
#ifdef USE_MULTI_GYRO #ifdef USE_MULTI_GYRO
if (gyroToUse == GYRO_CONFIG_USE_GYRO_2 || gyroToUse == GYRO_CONFIG_USE_GYRO_BOTH) { if (gyroToUse == GYRO_CONFIG_USE_GYRO_2 || gyroToUse == GYRO_CONFIG_USE_GYRO_BOTH) {
ret = gyroInitSensor(&gyroSensor2, gyroDeviceConfig(1)); if (gyroInitSensor(&gyroSensor2, gyroDeviceConfig(1))) {
if (!ret) {
return false; // TODO handle failure of second gyro detection better. - Perhaps update the config to use first gyro then indicate a new failure mode and reboot.
}
gyroHasOverflowProtection = gyroHasOverflowProtection && gyroSensor2.gyroDev.gyroHasOverflowProtection; gyroHasOverflowProtection = gyroHasOverflowProtection && gyroSensor2.gyroDev.gyroHasOverflowProtection;
detectionFlags |= DETECTED_GYRO_2;
}
}
if (gyroToUse == GYRO_CONFIG_USE_GYRO_BOTH && detectionFlags != (DETECTED_GYRO_1 | DETECTED_GYRO_2)) {
// at least one gyro is missing.
if (detectionFlags & DETECTED_GYRO_1) {
gyroToUse = GYRO_CONFIG_USE_GYRO_1;
gyroConfigMutable()->gyro_to_use = GYRO_CONFIG_USE_GYRO_1;
detectedSensors[SENSOR_INDEX_GYRO] = gyroSensor1.gyroDev.gyroHardware;
sensorsSet(SENSOR_GYRO);
return true;
}
if (detectionFlags & DETECTED_GYRO_2) {
gyroToUse = GYRO_CONFIG_USE_GYRO_2;
gyroConfigMutable()->gyro_to_use = GYRO_CONFIG_USE_GYRO_2;
detectedSensors[SENSOR_INDEX_GYRO] = gyroSensor2.gyroDev.gyroHardware;
sensorsSet(SENSOR_GYRO);
return true;
}
} }
// Only allow using both gyros simultaneously if they are the same hardware type. // Only allow using both gyros simultaneously if they are the same hardware type.
@ -544,12 +569,11 @@ bool gyroInit(void)
gyroConfigMutable()->gyro_to_use = GYRO_CONFIG_USE_GYRO_1; gyroConfigMutable()->gyro_to_use = GYRO_CONFIG_USE_GYRO_1;
detectedSensors[SENSOR_INDEX_GYRO] = gyroSensor1.gyroDev.gyroHardware; detectedSensors[SENSOR_INDEX_GYRO] = gyroSensor1.gyroDev.gyroHardware;
sensorsSet(SENSOR_GYRO); sensorsSet(SENSOR_GYRO);
} }
} }
#endif #endif
return ret; return detectionFlags != NO_GYROS_DETECTED;
} }
#ifdef USE_DYN_LPF #ifdef USE_DYN_LPF