diff --git a/flight/modules/Sensors/sensors.c b/flight/modules/Sensors/sensors.c index b1d8288d6..623480dd5 100644 --- a/flight/modules/Sensors/sensors.c +++ b/flight/modules/Sensors/sensors.c @@ -411,9 +411,9 @@ static void handleAccel(float *samples, float temperature) AccelSensorData accelSensorData; updateAccelTempBias(temperature); - float accels_out[3] = { samples[0] * agcal.accel_scale.X - agcal.accel_bias.X - accel_temp_bias[0], - samples[1] * agcal.accel_scale.Y - agcal.accel_bias.Y - accel_temp_bias[1], - samples[2] * agcal.accel_scale.Z - agcal.accel_bias.Z - accel_temp_bias[2] }; + float accels_out[3] = { (samples[0] - agcal.accel_bias.X) * agcal.accel_scale.X - accel_temp_bias[0], + (samples[1] - agcal.accel_bias.Y) * agcal.accel_scale.Y - accel_temp_bias[1], + (samples[2] - agcal.accel_bias.Z) * agcal.accel_scale.Z - accel_temp_bias[2] }; rot_mult(R, accels_out, samples); accelSensorData.x = samples[0]; @@ -485,7 +485,10 @@ static void updateAccelTempBias(float temperature) if ((accel_temp_calibrated) && !accel_temp_calibration_count) { accel_temp_calibration_count = TEMP_CALIB_INTERVAL; if (accel_temp_calibrated) { - float ctemp = boundf(accel_temperature, agcal.temp_calibrated_extent.max, agcal.temp_calibrated_extent.min); + float ctemp = boundf(accel_temperature, + agcal.temp_calibrated_extent.max, + agcal.temp_calibrated_extent.min); + accel_temp_bias[0] = agcal.accel_temp_coeff.X * ctemp; accel_temp_bias[1] = agcal.accel_temp_coeff.Y * ctemp; accel_temp_bias[2] = agcal.accel_temp_coeff.Z * ctemp; @@ -588,8 +591,12 @@ static void settingsUpdatedCb(__attribute__((unused)) UAVObjEvent *objEv) RevoSettingsBaroTempCorrectionPolynomialGet(&baroCorrection); RevoSettingsBaroTempCorrectionExtentGet(&baroCorrectionExtent); - baro_temp_correction_enabled = !(baroCorrectionExtent.max - baroCorrectionExtent.min < 0.1f || - (baroCorrection.a < 1e-9f && baroCorrection.b < 1e-9f && baroCorrection.c < 1e-9f && baroCorrection.d < 1e-9f)); + baro_temp_correction_enabled = + (baroCorrectionExtent.max - baroCorrectionExtent.min > 0.1f && + (fabsf(baroCorrection.a) > 1e-9f || + fabsf(baroCorrection.b) > 1e-9f || + fabsf(baroCorrection.c) > 1e-9f || + fabsf(baroCorrection.d) > 1e-9f)); } /** * @} diff --git a/flight/pios/common/pios_ms5611.c b/flight/pios/common/pios_ms5611.c index f10632415..6a9c70879 100644 --- a/flight/pios/common/pios_ms5611.c +++ b/flight/pios/common/pios_ms5611.c @@ -42,7 +42,9 @@ #ifndef PIOS_MS5611_SLOW_TEMP_RATE #define PIOS_MS5611_SLOW_TEMP_RATE 1 #endif - +// Running moving average smoothing factor +#define PIOS_MS5611_TEMP_SMOOTHING 10 +// /* Local Types */ typedef struct { uint16_t C[6]; @@ -72,6 +74,7 @@ static uint32_t RawTemperature; static uint32_t RawPressure; static int64_t Pressure; static int64_t Temperature; +static int64_t FilteredTemperature = INT32_MIN; static int32_t lastConversionStart; static uint32_t conversionDelayMs; @@ -247,6 +250,12 @@ int32_t PIOS_MS5611_ReadADC(void) // Actual temperature (-40…85°C with 0.01°C resolution) // TEMP = 20°C + dT * TEMPSENS = 2000 + dT * C6 / 2^23 Temperature = 2000l + ((deltaTemp * CalibData.C[5]) / POW2(23)); + if (FilteredTemperature != INT32_MIN) { + FilteredTemperature = (FilteredTemperature * (PIOS_MS5611_TEMP_SMOOTHING - 1) + + Temperature) / PIOS_MS5611_TEMP_SMOOTHING; + } else { + FilteredTemperature = Temperature; + } } else { int64_t Offset; int64_t Sens; @@ -259,21 +268,21 @@ int32_t PIOS_MS5611_ReadADC(void) return -2; } // check if temperature is less than 20°C - if (Temperature < 2000) { + if (FilteredTemperature < 2000) { // Apply compensation // T2 = dT^2 / 2^31 // OFF2 = 5 ⋅ (TEMP – 2000)^2/2 // SENS2 = 5 ⋅ (TEMP – 2000)^2/2^2 - int64_t tcorr = (Temperature - 2000) * (Temperature - 2000); + int64_t tcorr = (FilteredTemperature - 2000) * (FilteredTemperature - 2000); Offset2 = (5 * tcorr) / 2; Sens2 = (5 * tcorr) / 4; compensation_t2 = (deltaTemp * deltaTemp) >> 31; // Apply the "Very low temperature compensation" when temp is less than -15°C - if (Temperature < -1500) { + if (FilteredTemperature < -1500) { // OFF2 = OFF2 + 7 ⋅ (TEMP + 1500)^2 // SENS2 = SENS2 + 11 ⋅ (TEMP + 1500)^2 / 2 - int64_t tcorr2 = (Temperature + 1500) * (Temperature + 1500); + int64_t tcorr2 = (FilteredTemperature + 1500) * (FilteredTemperature + 1500); Offset2 += 7 * tcorr2; Sens2 += (11 * tcorr2) / 2; } @@ -302,7 +311,7 @@ int32_t PIOS_MS5611_ReadADC(void) static float PIOS_MS5611_GetTemperature(void) { // Apply the second order low and very low temperature compensation offset - return ((float)(Temperature - compensation_t2)) / 100.0f; + return ((float)(FilteredTemperature - compensation_t2)) / 100.0f; } /**