update credits
[librepilot.git] / flight / modules / Airspeed / baro_airspeed_mpxv.c
blob5c7ead7779e46d9f1a51c46c7c97964f0c51cff4
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
4 * @{
5 * @addtogroup AirspeedModule Airspeed Module
6 * @brief Communicate with airspeed sensors and return values
7 * @{
9 * @file baro_airspeed.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
11 * @brief Airspeed module, handles temperature and pressure readings from BMP085
13 * @see The GNU Public License (GPL) Version 3
15 *****************************************************************************/
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 /**
33 * Output object: BaroAirspeed
35 * This module will periodically update the value of the BaroAirspeed object.
39 #include "openpilot.h"
40 #include "hwsettings.h"
41 #include "airspeedsettings.h"
42 #include "airspeedsensor.h" // object that will be updated by the module
43 #include "airspeedalarm.h"
45 #if defined(PIOS_INCLUDE_MPXV)
47 #define CALIBRATION_IDLE_MS 2000 // Time to wait before calibrating, in [ms]
48 #define CALIBRATION_COUNT_MS 2000 // Time to spend calibrating, in [ms]
49 #define ANALOG_BARO_AIRSPEED_TIME_CONSTANT_MS 256 // low pass filter
51 // Private types
53 // Private variables
55 // Private functions
57 static uint16_t calibrationCount = 0;
59 PIOS_MPXV_descriptor sensor = { .type = PIOS_MPXV_UNKNOWN };
62 void baro_airspeedGetMPXV(AirspeedSensorData *airspeedSensor, AirspeedSettingsData *airspeedSettings, int8_t airspeedADCPin)
64 // Ensure that the ADC pin is properly configured
65 if (airspeedADCPin < 0) {
66 airspeedSensor->SensorConnected = AIRSPEEDSENSOR_SENSORCONNECTED_FALSE;
67 AirspeedAlarm(SYSTEMALARMS_ALARM_ERROR);
68 return;
70 if (sensor.type == PIOS_MPXV_UNKNOWN) {
71 switch (airspeedSettings->AirspeedSensorType) {
72 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV7002:
73 sensor = PIOS_MPXV_7002_DESC(airspeedADCPin);
74 break;
75 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV5004:
76 sensor = PIOS_MPXV_5004_DESC(airspeedADCPin);
77 break;
78 default:
79 airspeedSensor->SensorConnected = AIRSPEEDSENSOR_SENSORCONNECTED_FALSE;
80 AirspeedAlarm(SYSTEMALARMS_ALARM_ERROR);
81 return;
85 airspeedSensor->SensorValue = PIOS_MPXV_Measure(&sensor);
87 if (!airspeedSettings->ZeroPoint) {
88 AirspeedAlarm(SYSTEMALARMS_ALARM_WARNING);
89 // Calibrate sensor by averaging zero point value
90 if (calibrationCount < CALIBRATION_IDLE_MS / airspeedSettings->SamplePeriod) { // First let sensor warm up and stabilize.
91 calibrationCount++;
92 return;
93 } else if (calibrationCount <= (CALIBRATION_IDLE_MS + CALIBRATION_COUNT_MS) / airspeedSettings->SamplePeriod) { // Then compute the average.
94 calibrationCount++; /*DO NOT MOVE FROM BEFORE sensorCalibration=... LINE, OR ELSE WILL HAVE DIVIDE BY ZERO */
96 PIOS_MPXV_Calibrate(&sensor, airspeedSensor->SensorValue);
98 // Set settings UAVO. The airspeed UAVO is set elsewhere in the function.
99 if (calibrationCount > (CALIBRATION_IDLE_MS + CALIBRATION_COUNT_MS) / airspeedSettings->SamplePeriod) {
100 airspeedSettings->ZeroPoint = sensor.zeroPoint;
101 AirspeedSettingsZeroPointSet(&airspeedSettings->ZeroPoint);
103 return;
106 sensor.zeroPoint = airspeedSettings->ZeroPoint;
107 sensor.scale = airspeedSettings->Scale;
109 // Filter CAS : airspeedSettings->SamplePeriod value is 0 - 255 range
110 float alpha = 1 - (airspeedSettings->SamplePeriod / ANALOG_BARO_AIRSPEED_TIME_CONSTANT_MS); // Low pass filter.
112 airspeedSensor->CalibratedAirspeed = PIOS_MPXV_CalcAirspeed(&sensor, airspeedSensor->SensorValue) * (alpha) + airspeedSensor->CalibratedAirspeed * (1.0f - alpha);
113 airspeedSensor->SensorConnected = AIRSPEEDSENSOR_SENSORCONNECTED_TRUE;
114 AirspeedAlarm(SYSTEMALARMS_ALARM_OK);
117 #endif /* if defined(PIOS_INCLUDE_MPXV) */
120 * @}
121 * @}