forked from iNavFlight/inav
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
157 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* This file is part of INAV. | ||
* | ||
* INAV is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* INAV is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with INAV. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "platform.h" | ||
|
||
#include "build/debug.h" | ||
|
||
#include "common/maths.h" | ||
|
||
#include "config/parameter_group.h" | ||
#include "config/parameter_group_ids.h" | ||
|
||
#include "drivers/time.h" | ||
|
||
#include "fc/runtime_config.h" | ||
|
||
#include "io/beeper.h" | ||
|
||
#include "sensors/acceleration.h" | ||
#include "sensors/barometer.h" | ||
#include "sensors/sensors.h" | ||
|
||
sensor_compensation_t sensor_comp_data[SENSOR_INDEX_COUNT]; | ||
|
||
float applySensorTempCompensation(int16_t sensorTemp, float sensorMeasurement, sensorIndex_e sensorType) | ||
{ | ||
float setting = 0.0f; | ||
if (sensorType == SENSOR_INDEX_ACC) { | ||
setting = accelerometerConfig()->acc_temp_correction; | ||
} else if (sensorType == SENSOR_INDEX_BARO) { | ||
setting = barometerConfig()->baro_temp_correction; | ||
} | ||
|
||
if (!setting) { | ||
return 0.0f; | ||
} | ||
|
||
if (sensor_comp_data[sensorType].calibrationState == SENSOR_TEMP_CAL_COMPLETE) { | ||
return sensor_comp_data[sensorType].correctionFactor * CENTIDEGREES_TO_DEGREES(sensor_comp_data[sensorType].referenceTemp - sensorTemp); | ||
} | ||
|
||
static timeMs_t startTimeMs = 0; | ||
|
||
if (!ARMING_FLAG(WAS_EVER_ARMED)) { | ||
if (sensor_comp_data[sensorType].calibrationState == SENSOR_TEMP_CAL_INITIALISE) { | ||
sensor_comp_data[sensorType].referenceTemp = sensorTemp; | ||
sensor_comp_data[sensorType].calibrationState = SENSOR_TEMP_CAL_IN_PROGRESS; | ||
} | ||
|
||
if (setting == 51.0f) { // initiate auto calibration | ||
static float referenceMeasurement = 0.0f; | ||
static int16_t lastTemp = 0.0f; | ||
|
||
if (sensor_comp_data[sensorType].referenceTemp == sensorTemp) { | ||
referenceMeasurement = sensorMeasurement; | ||
lastTemp = sensorTemp; | ||
startTimeMs = millis(); | ||
} | ||
|
||
float referenceDeltaTemp = ABS(sensorTemp - sensor_comp_data[sensorType].referenceTemp); // centidegrees | ||
if (referenceDeltaTemp > 300 && referenceDeltaTemp > ABS(lastTemp - sensor_comp_data[sensorType].referenceTemp)) { | ||
/* Min 3 deg reference temperature difference required for valid calibration. | ||
* Correction adjusted only if temperature difference to reference temperature increasing | ||
* Calibration assumes a simple linear relationship */ | ||
lastTemp = sensorTemp; | ||
sensor_comp_data[sensorType].correctionFactor = 0.9f * sensor_comp_data[sensorType].correctionFactor + 0.1f * (sensorMeasurement - referenceMeasurement) / CENTIDEGREES_TO_DEGREES(lastTemp - sensor_comp_data[sensorType].referenceTemp); | ||
sensor_comp_data[sensorType].correctionFactor = constrainf(sensor_comp_data[sensorType].correctionFactor, -50.0f, 50.0f); | ||
} | ||
} else { | ||
sensor_comp_data[sensorType].correctionFactor = setting; | ||
sensor_comp_data[sensorType].calibrationState = SENSOR_TEMP_CAL_COMPLETE; | ||
} | ||
} | ||
|
||
// Calibration ends on first Arm or after 5 min timeout | ||
if (sensor_comp_data[sensorType].calibrationState == SENSOR_TEMP_CAL_IN_PROGRESS && (ARMING_FLAG(WAS_EVER_ARMED) || millis() > startTimeMs + 300000)) { | ||
if (!ARMING_FLAG(WAS_EVER_ARMED)) { | ||
beeper(sensor_comp_data[sensorType].correctionFactor ? BEEPER_ACTION_SUCCESS : BEEPER_ACTION_FAIL); | ||
} | ||
|
||
if (sensorType == SENSOR_INDEX_ACC) { | ||
accelerometerConfigMutable()->acc_temp_correction = sensor_comp_data[sensorType].correctionFactor; | ||
} else if (sensorType == SENSOR_INDEX_BARO) { | ||
barometerConfigMutable()->baro_temp_correction = sensor_comp_data[sensorType].correctionFactor; | ||
} | ||
sensor_comp_data[sensorType].calibrationState = SENSOR_TEMP_CAL_COMPLETE; | ||
startTimeMs = 0; | ||
} | ||
|
||
return 0.0f; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters