bool sensorsAutodetect(void) { memset(&acc, 0, sizeof(acc)); memset(&gyro, 0, sizeof(gyro)); #if defined(USE_GYRO_MPU6050) || defined(USE_GYRO_MPU3050) || defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500) || defined(USE_GYRO_SPI_MPU6000) || defined(USE_ACC_MPU6050) const extiConfig_t *extiConfig = selectMPUIntExtiConfig(); mpuDetectionResult_t *mpuDetectionResult = detectMpu(extiConfig); UNUSED(mpuDetectionResult); #endif if (!detectGyro()) { return false; } detectAcc(sensorSelectionConfig()->acc_hardware); detectBaro(sensorSelectionConfig()->baro_hardware); // Now time to init things, acc first if (sensors(SENSOR_ACC)) acc.init(); // this is safe because either mpu6050 or mpu3050 or lg3d20 sets it, and in case of fail, we never get here. gyro.init(gyroConfig()->gyro_lpf); #ifdef MAG detectMag(sensorSelectionConfig()->mag_hardware); #endif reconfigureAlignment(sensorAlignmentConfig()); return true; }
void gyroUpdate(void) { int16_t gyroADCRaw[XYZ_AXIS_COUNT]; int axis; // range: +/- 8192; +/- 2000 deg/sec if (!gyro.read(gyroADCRaw)) { return; } for (axis = 0; axis < XYZ_AXIS_COUNT; axis++) { if (debugMode == DEBUG_GYRO) debug[axis] = gyroADC[axis]; gyroADC[axis] = gyroADCRaw[axis]; } alignSensors(gyroADC, gyroADC, gyroAlign); if (gyroLpfCutFreq) { if (!gyroFilterStateIsSet) initGyroFilterCoefficients(); /* initialise filter coefficients */ if (gyroFilterStateIsSet) { for (axis = 0; axis < XYZ_AXIS_COUNT; axis++) gyroADC[axis] = lrintf(applyBiQuadFilter((float) gyroADC[axis], &gyroFilterState[axis])); } } if (!isGyroCalibrationComplete()) { performAcclerationCalibration(gyroConfig->gyroMovementCalibrationThreshold); } applyGyroZero(); }
void sensorsInit(void) { zeroSensorAccumulators(); // TODO allow user to select hardware if there are multiple choices if(mpu6050Detect(&gyro, &accel, cfg.mpu6050Scale)) { sensorsSet(SENSOR_ACC); } else if(!mpu3050Detect(&gyro)) { failureMode(3); } if(adxl345Detect(&accel)) { sensorsSet(SENSOR_ACC); } // At the moment we will do this after mpu6050 and overrride mpu6050 accel if detected if(mma8452Detect(&accel)) { sensorsSet(SENSOR_ACC); } gyro.init(); if(sensorsGet(SENSOR_ACC)) accel.init(); if(hmc5883Detect(&mag)) { mag.init(); sensorsSet(SENSOR_MAG); } #ifdef SONAR if(feature(FEATURE_PPM);) {
bool getMpuDataStatus(gyro_t *gyro) { bool mpuDataStatus; if (!gyro->intStatus) return false; gyro->intStatus(&mpuDataStatus); return mpuDataStatus; }
void gyroSample(void) { uint8_t i; gyro.read(sensorData.gyro); for(i = 0; i < 3; ++i) sensorData.gyroAccum[i] += sensorData.gyro[i] - sensorParams.gyroRTBias[i]; sensorData.gyroSamples++; }
bool sensorsAutodetect(sensorAlignmentConfig_t *sensorAlignmentConfig, uint8_t gyroLpf, uint8_t accHardwareToUse, uint8_t magHardwareToUse, uint8_t baroHardwareToUse, int16_t magDeclinationFromConfig, uint32_t looptime, uint8_t gyroSync, uint8_t gyroSyncDenominator) { int16_t deg, min; #ifndef MAG UNUSED(magHardwareToUse); #endif memset(&acc, 0, sizeof(acc)); memset(&gyro, 0, sizeof(gyro)); #if defined(USE_GYRO_MPU6050) || defined(USE_GYRO_MPU3050) || defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500) || defined(USE_GYRO_SPI_MPU6000) || defined(USE_ACC_MPU6050) const extiConfig_t *extiConfig = selectMPUIntExtiConfig(); mpuDetectionResult_t *mpuDetectionResult = detectMpu(extiConfig); UNUSED(mpuDetectionResult); #endif if (!detectGyro()) { return false; } detectAcc(accHardwareToUse); detectBaro(baroHardwareToUse); // Now time to init things, acc first if (sensors(SENSOR_ACC)) acc.init(); // this is safe because either mpu6050 or mpu3050 or lg3d20 sets it, and in case of fail, we never get here. gyroUpdateSampleRate(looptime, gyroLpf, gyroSync, gyroSyncDenominator); // Set gyro sampling rate divider before initialization gyro.init(gyroLpf); #ifdef MAG detectMag(magHardwareToUse); #endif reconfigureAlignment(sensorAlignmentConfig); // FIXME extract to a method to reduce dependencies, maybe move to sensors_compass.c if (sensors(SENSOR_MAG)) { // calculate magnetic declination deg = magDeclinationFromConfig / 100; min = magDeclinationFromConfig % 100; magneticDeclination = (deg + ((float)min * (1.0f / 60.0f))) * 10; // heading is in 0.1deg units } else { magneticDeclination = 0.0f; // TODO investigate if this is actually needed if there is no mag sensor or if the value stored in the config should be used. } return true; }
void gyroGetADC(void) { // FIXME When gyro.read() fails due to i2c or other error gyroZero is continually re-applied to gyroADC resulting in a old reading that gets worse over time. // range: +/- 8192; +/- 2000 deg/sec gyro.read(gyroADC); alignSensors(gyroADC, gyroADC, gyroAlign); if (!isGyroCalibrationComplete()) { performAcclerationCalibration(gyroConfig->gyroMovementCalibrationThreshold); } applyGyroZero(); }
void gyroUpdate(void) { int16_t gyroADCRaw[XYZ_AXIS_COUNT]; // range: +/- 8192; +/- 2000 deg/sec if (!gyro.read(gyroADCRaw)) { return; } for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) { gyroADC[axis] = gyroADCRaw[axis]; } alignSensors(gyroADC, gyroADC, gyroAlign); if (!isGyroCalibrationComplete()) { performGyroCalibration(gyroConfig->gyroMovementCalibrationThreshold); } applyGyroZero(); if (gyroSoftLpfHz) { for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) { if (debugMode == DEBUG_GYRO) debug[axis] = gyroADC[axis]; if (gyroSoftLpfType == FILTER_BIQUAD) gyroADCf[axis] = biquadFilterApply(&gyroFilterLPF[axis], (float) gyroADC[axis]); else if (gyroSoftLpfType == FILTER_PT1) gyroADCf[axis] = pt1FilterApply4(&gyroFilterPt1[axis], (float) gyroADC[axis], gyroSoftLpfHz, gyroDt); else gyroADCf[axis] = firFilterDenoiseUpdate(&gyroDenoiseState[axis], (float) gyroADC[axis]); if (debugMode == DEBUG_NOTCH) debug[axis] = lrintf(gyroADCf[axis]); if (gyroSoftNotchHz1) gyroADCf[axis] = biquadFilterApply(&gyroFilterNotch_1[axis], gyroADCf[axis]); if (gyroSoftNotchHz2) gyroADCf[axis] = biquadFilterApply(&gyroFilterNotch_2[axis], gyroADCf[axis]); gyroADC[axis] = lrintf(gyroADCf[axis]); } } else { for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) gyroADCf[axis] = gyroADC[axis]; } }
bool sensorsAutodetect(sensorAlignmentConfig_t *sensorAlignmentConfig, uint8_t gyroLpf, uint8_t accHardwareToUse, uint8_t magHardwareToUse, uint8_t baroHardwareToUse, int16_t magDeclinationFromConfig) { int16_t deg, min; memset(&acc, 0, sizeof(acc)); memset(&gyro, 0, sizeof(gyro)); #if defined(USE_GYRO_MPU6050) || defined(USE_GYRO_MPU3050) || defined(USE_GYRO_MPU6500) || defined(USE_GYRO_SPI_MPU6500) || defined(USE_GYRO_SPI_MPU6000) || defined(USE_ACC_MPU6050) const extiConfig_t *extiConfig = selectMPUIntExtiConfig(); mpuDetectionResult_t *mpuDetectionResult = detectMpu(extiConfig); UNUSED(mpuDetectionResult); #endif if (!detectGyro()) { return false; } detectAcc(accHardwareToUse); detectBaro(baroHardwareToUse); // Now time to init things, acc first if (sensors(SENSOR_ACC)) acc.init(&acc); gyro.init(gyroLpf); detectMag(magHardwareToUse); reconfigureAlignment(sensorAlignmentConfig); // FIXME extract to a method to reduce dependencies, maybe move to sensors_compass.c if (sensors(SENSOR_MAG)) { // calculate magnetic declination deg = magDeclinationFromConfig / 100; min = magDeclinationFromConfig % 100; magneticDeclination = (deg + ((float)min * (1.0f / 60.0f))) * 10; // heading is in 0.1deg units } else { magneticDeclination = 0.0f; // TODO investigate if this is actually needed if there is no mag sensor or if the value stored in the config should be used. } return true; }
void gyroUpdate(void) { // range: +/- 8192; +/- 2000 deg/sec if (!gyro.read(gyroADCRaw)) { return; } gyroADC[X] = gyroADCRaw[X]; gyroADC[Y] = gyroADCRaw[Y]; gyroADC[Z] = gyroADCRaw[Z]; alignSensors(gyroADC, gyroADC, gyroAlign); if (!isGyroCalibrationComplete()) { performAcclerationCalibration(gyroConfig()->gyroMovementCalibrationThreshold); } gyroADC[X] -= gyroZero[X]; gyroADC[Y] -= gyroZero[Y]; gyroADC[Z] -= gyroZero[Z]; if (gyroConfig()->gyro_soft_lpf_hz) { for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) { if (debugMode == DEBUG_GYRO) debug[axis] = gyroADC[axis]; if (gyroConfig()->gyro_soft_type == FILTER_BIQUAD) gyroADCf[axis] = biquadFilterApply(&gyroFilterLPF[axis], (float) gyroADC[axis]); else gyroADCf[axis] = pt1FilterApply(&gyroFilterPt1[axis], (float) gyroADC[axis]); if (debugMode == DEBUG_NOTCH) debug[axis] = lrintf(gyroADCf[axis]); if (gyroConfig()->gyro_soft_notch_hz) gyroADCf[axis] = biquadFilterApply(&gyroFilterNotch[axis], gyroADCf[axis]); gyroADC[axis] = lrintf(gyroADCf[axis]); } } else { for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) { gyroADCf[axis] = gyroADC[axis]; } } }
bool sensorsAutodetect(sensorAlignmentConfig_t *sensorAlignmentConfig, uint16_t gyroLpf, uint8_t accHardwareToUse, int16_t magDeclinationFromConfig) { int16_t deg, min; memset(&acc, sizeof(acc), 0); memset(&gyro, sizeof(gyro), 0); if (!detectGyro(gyroLpf)) { return false; } detectAcc(accHardwareToUse); detectBaro(); reconfigureAlignment(sensorAlignmentConfig); // Now time to init things, acc first if (sensors(SENSOR_ACC)) acc.init(); // this is safe because either mpu6050 or mpu3050 or lg3d20 sets it, and in case of fail, we never get here. gyro.init(); #ifdef MAG if (hmc5883lDetect()) { magAlign = CW180_DEG; // default NAZE alignment } else { sensorsClear(SENSOR_MAG); } #endif // FIXME extract to a method to reduce dependencies, maybe move to sensors_compass.c if (sensors(SENSOR_MAG)) { // calculate magnetic declination deg = magDeclinationFromConfig / 100; min = magDeclinationFromConfig % 100; magneticDeclination = (deg + ((float)min * (1.0f / 60.0f))) * 10; // heading is in 0.1deg units } else { magneticDeclination = 0.0f; // TODO investigate if this is actually needed if there is no mag sensor or if the value stored in the config should be used. } return true; }
void gyroUpdate(void) { int8_t axis; // range: +/- 8192; +/- 2000 deg/sec if (!gyro.read(gyroADCRaw)) { return; } // Prepare a copy of int32_t gyroADC for mangling to prevent overflow for (axis = 0; axis < XYZ_AXIS_COUNT; axis++) gyroADC[axis] = gyroADCRaw[axis]; if (gyroLpfCutHz) { if (!gyroFilterInitialised) { if (targetLooptime) { /* Initialisation needs to happen once sample rate is known */ for (axis = 0; axis < 3; axis++) { filterInitBiQuad(gyroLpfCutHz, &gyroFilterState[axis], 0); } gyroFilterInitialised = true; } } if (gyroFilterInitialised) { for (axis = 0; axis < XYZ_AXIS_COUNT; axis++) { gyroADC[axis] = lrintf(filterApplyBiQuad((float) gyroADC[axis], &gyroFilterState[axis])); } } } alignSensors(gyroADC, gyroADC, gyroAlign); if (!isGyroCalibrationComplete()) { performAcclerationCalibration(gyroConfig->gyroMovementCalibrationThreshold); } applyGyroZero(); }
void gyroUpdate(void) { // range: +/- 8192; +/- 2000 deg/sec if (!gyro.read(gyroADC)) { return; } alignSensors(gyroADC, gyroADC, gyroAlign); if (gyroLpfCutFreq) { if (!gyroFilterStateIsSet) { initGyroFilterCoefficients(); } else { for (axis = 0; axis < XYZ_AXIS_COUNT; axis++) gyroADC[axis] = lrintf(applyBiQuadFilter((float) gyroADC[axis], &gyroBiQuadState[axis])); } } if (!isGyroCalibrationComplete()) { performAcclerationCalibration(gyroConfig->gyroMovementCalibrationThreshold); } applyGyroZero(); }
static void detectAcc(accelerationSensor_e accHardwareToUse) { accelerationSensor_e accHardware; #ifdef USE_ACC_ADXL345 drv_adxl345_config_t acc_params; #endif retry: accAlign = ALIGN_DEFAULT; switch (accHardwareToUse) { case ACC_DEFAULT: ; // fallthrough case ACC_ADXL345: // ADXL345 #ifdef USE_ACC_ADXL345 acc_params.useFifo = false; acc_params.dataRate = 800; // unused currently #ifdef NAZE if (hardwareRevision < NAZE32_REV5 && adxl345Detect(&acc_params, &acc)) { #else if (adxl345Detect(&acc_params, &acc)) { #endif #ifdef ACC_ADXL345_ALIGN accAlign = ACC_ADXL345_ALIGN; #endif accHardware = ACC_ADXL345; break; } #endif ; // fallthrough case ACC_LSM303DLHC: #ifdef USE_ACC_LSM303DLHC if (lsm303dlhcAccDetect(&acc)) { #ifdef ACC_LSM303DLHC_ALIGN accAlign = ACC_LSM303DLHC_ALIGN; #endif accHardware = ACC_LSM303DLHC; break; } #endif ; // fallthrough case ACC_MPU6050: // MPU6050 #ifdef USE_ACC_MPU6050 if (mpu6050AccDetect(selectMPU6050Config(), &acc)) { #ifdef ACC_MPU6050_ALIGN accAlign = ACC_MPU6050_ALIGN; #endif accHardware = ACC_MPU6050; break; } #endif ; // fallthrough case ACC_MMA8452: // MMA8452 #ifdef USE_ACC_MMA8452 #ifdef NAZE // Not supported with this frequency if (hardwareRevision < NAZE32_REV5 && mma8452Detect(&acc)) { #else if (mma8452Detect(&acc)) { #endif #ifdef ACC_MMA8452_ALIGN accAlign = ACC_MMA8452_ALIGN; #endif accHardware = ACC_MMA8452; break; } #endif ; // fallthrough case ACC_BMA280: // BMA280 #ifdef USE_ACC_BMA280 if (bma280Detect(&acc)) { #ifdef ACC_BMA280_ALIGN accAlign = ACC_BMA280_ALIGN; #endif accHardware = ACC_BMA280; break; } #endif ; // fallthrough case ACC_SPI_MPU6000: #ifdef USE_ACC_SPI_MPU6000 if (mpu6000SpiAccDetect(&acc)) { #ifdef ACC_SPI_MPU6000_ALIGN accAlign = ACC_SPI_MPU6000_ALIGN; #endif accHardware = ACC_SPI_MPU6000; break; } #endif ; // fallthrough case ACC_SPI_MPU6500: #ifdef USE_ACC_SPI_MPU6500 #ifdef NAZE if (hardwareRevision == NAZE32_SP && mpu6500SpiAccDetect(&acc)) { #else if (mpu6500SpiAccDetect(&acc)) { #endif #ifdef ACC_SPI_MPU6500_ALIGN accAlign = ACC_SPI_MPU6500_ALIGN; #endif accHardware = ACC_SPI_MPU6500; break; } #endif ; // fallthrough case ACC_FAKE: #ifdef USE_FAKE_ACC if (fakeAccDetect(&acc)) { accHardware = ACC_FAKE; break; } #endif ; // fallthrough case ACC_NONE: // disable ACC accHardware = ACC_NONE; break; } // Found anything? Check if error or ACC is really missing. if (accHardware == ACC_NONE && accHardwareToUse != ACC_DEFAULT && accHardwareToUse != ACC_NONE) { // Nothing was found and we have a forced sensor that isn't present. accHardwareToUse = ACC_DEFAULT; goto retry; } if (accHardware == ACC_NONE) { return; } detectedSensors[SENSOR_INDEX_ACC] = accHardware; sensorsSet(SENSOR_ACC); } static void detectBaro(baroSensor_e baroHardwareToUse) { #ifdef BARO // Detect what pressure sensors are available. baro->update() is set to sensor-specific update function baroSensor_e baroHardware = baroHardwareToUse; #ifdef USE_BARO_BMP085 const bmp085Config_t *bmp085Config = NULL; #if defined(BARO_XCLR_GPIO) && defined(BARO_EOC_GPIO) static const bmp085Config_t defaultBMP085Config = { .gpioAPB2Peripherals = BARO_APB2_PERIPHERALS, .xclrGpioPin = BARO_XCLR_PIN, .xclrGpioPort = BARO_XCLR_GPIO, .eocGpioPin = BARO_EOC_PIN, .eocGpioPort = BARO_EOC_GPIO }; bmp085Config = &defaultBMP085Config; #endif #ifdef NAZE if (hardwareRevision == NAZE32) { bmp085Disable(bmp085Config); } #endif #endif switch (baroHardware) { case BARO_DEFAULT: ; // fallthough case BARO_MS5611: #ifdef USE_BARO_MS5611 if (ms5611Detect(&baro)) { baroHardware = BARO_MS5611; break; } #endif ; // fallthough case BARO_BMP085: #ifdef USE_BARO_BMP085 if (bmp085Detect(bmp085Config, &baro)) { baroHardware = BARO_BMP085; break; } #endif case BARO_NONE: baroHardware = BARO_NONE; break; } if (baroHardware == BARO_NONE) { return; } detectedSensors[SENSOR_INDEX_BARO] = baroHardware; sensorsSet(SENSOR_BARO); #endif } static void detectMag(magSensor_e magHardwareToUse) { magSensor_e magHardware; #ifdef USE_MAG_HMC5883 const hmc5883Config_t *hmc5883Config = 0; #ifdef NAZE static const hmc5883Config_t nazeHmc5883Config_v1_v4 = { .gpioAPB2Peripherals = RCC_APB2Periph_GPIOB, .gpioPin = Pin_12, .gpioPort = GPIOB, /* Disabled for v4 needs more work. .exti_port_source = GPIO_PortSourceGPIOB, .exti_pin_source = GPIO_PinSource12, .exti_line = EXTI_Line12, .exti_irqn = EXTI15_10_IRQn */ }; static const hmc5883Config_t nazeHmc5883Config_v5 = { .gpioAPB2Peripherals = RCC_APB2Periph_GPIOC, .gpioPin = Pin_14, .gpioPort = GPIOC, .exti_port_source = GPIO_PortSourceGPIOC, .exti_line = EXTI_Line14, .exti_pin_source = GPIO_PinSource14, .exti_irqn = EXTI15_10_IRQn }; if (hardwareRevision < NAZE32_REV5) { hmc5883Config = &nazeHmc5883Config_v1_v4; } else { hmc5883Config = &nazeHmc5883Config_v5; } #endif #ifdef SPRACINGF3 static const hmc5883Config_t spRacingF3Hmc5883Config = { .gpioAHBPeripherals = RCC_AHBPeriph_GPIOC, .gpioPin = Pin_14, .gpioPort = GPIOC, .exti_port_source = EXTI_PortSourceGPIOC, .exti_pin_source = EXTI_PinSource14, .exti_line = EXTI_Line14, .exti_irqn = EXTI15_10_IRQn }; hmc5883Config = &spRacingF3Hmc5883Config; #endif #endif retry: magAlign = ALIGN_DEFAULT; switch(magHardwareToUse) { case MAG_DEFAULT: ; // fallthrough case MAG_HMC5883: #ifdef USE_MAG_HMC5883 if (hmc5883lDetect(&mag, hmc5883Config)) { #ifdef MAG_HMC5883_ALIGN magAlign = MAG_HMC5883_ALIGN; #endif magHardware = MAG_HMC5883; break; } #endif ; // fallthrough case MAG_AK8975: #ifdef USE_MAG_AK8975 if (ak8975detect(&mag)) { #ifdef MAG_AK8975_ALIGN magAlign = MAG_AK8975_ALIGN; #endif magHardware = MAG_AK8975; break; } #endif ; // fallthrough case MAG_NONE: magHardware = MAG_NONE; break; } if (magHardware == MAG_NONE && magHardwareToUse != MAG_DEFAULT && magHardwareToUse != MAG_NONE) { // Nothing was found and we have a forced sensor that isn't present. magHardwareToUse = MAG_DEFAULT; goto retry; } if (magHardware == MAG_NONE) { return; } detectedSensors[SENSOR_INDEX_MAG] = magHardware; sensorsSet(SENSOR_MAG); } void reconfigureAlignment(sensorAlignmentConfig_t *sensorAlignmentConfig) { if (sensorAlignmentConfig->gyro_align != ALIGN_DEFAULT) { gyroAlign = sensorAlignmentConfig->gyro_align; } if (sensorAlignmentConfig->acc_align != ALIGN_DEFAULT) { accAlign = sensorAlignmentConfig->acc_align; } if (sensorAlignmentConfig->mag_align != ALIGN_DEFAULT) { magAlign = sensorAlignmentConfig->mag_align; } } bool sensorsAutodetect(sensorAlignmentConfig_t *sensorAlignmentConfig, uint16_t gyroLpf, uint8_t accHardwareToUse, uint8_t magHardwareToUse, uint8_t baroHardwareToUse, int16_t magDeclinationFromConfig) { int16_t deg, min; memset(&acc, 0, sizeof(acc)); memset(&gyro, 0, sizeof(gyro)); if (!detectGyro(gyroLpf)) { return false; } detectAcc(accHardwareToUse); detectBaro(baroHardwareToUse); // Now time to init things, acc first if (sensors(SENSOR_ACC)) acc.init(); // this is safe because either mpu6050 or mpu3050 or lg3d20 sets it, and in case of fail, we never get here. gyro.init(); detectMag(magHardwareToUse); reconfigureAlignment(sensorAlignmentConfig); // FIXME extract to a method to reduce dependencies, maybe move to sensors_compass.c if (sensors(SENSOR_MAG)) { // calculate magnetic declination deg = magDeclinationFromConfig / 100; min = magDeclinationFromConfig % 100; magneticDeclination = (deg + ((float)min * (1.0f / 60.0f))) * 10; // heading is in 0.1deg units } else { magneticDeclination = 0.0f; // TODO investigate if this is actually needed if there is no mag sensor or if the value stored in the config should be used. } return true; }
bool gyroSyncCheckUpdate(void) { return gyro.isDataReady && gyro.isDataReady(); }