2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
5 * @addtogroup AirspeedModule Airspeed Module
6 * @brief Communicate with airspeed sensors and return values
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
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
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_ETASV3)
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]
56 static uint16_t calibrationCount
= 0;
57 static uint16_t calibrationCount2
= 0;
58 static uint32_t calibrationSum
= 0;
61 void baro_airspeedGetETASV3(AirspeedSensorData
*airspeedSensor
, AirspeedSettingsData
*airspeedSettings
)
63 // Check to see if airspeed sensor is returning airspeedSensor
64 airspeedSensor
->SensorValue
= PIOS_ETASV3_ReadAirspeed();
65 if (airspeedSensor
->SensorValue
== (uint16_t)-1) {
66 airspeedSensor
->SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_FALSE
;
67 airspeedSensor
->CalibratedAirspeed
= 0;
68 AirspeedAlarm(SYSTEMALARMS_ALARM_ERROR
);
72 // only calibrate if no stored calibration is available
73 if (!airspeedSettings
->ZeroPoint
) {
74 AirspeedAlarm(SYSTEMALARMS_ALARM_WARNING
);
75 // Calibrate sensor by averaging zero point value
76 if (calibrationCount
<= CALIBRATION_IDLE_MS
/ airspeedSettings
->SamplePeriod
) {
79 calibrationCount2
= 0;
81 } else if (calibrationCount
<= (CALIBRATION_IDLE_MS
+ CALIBRATION_COUNT_MS
) / airspeedSettings
->SamplePeriod
) {
84 calibrationSum
+= airspeedSensor
->SensorValue
;
85 if (calibrationCount
> (CALIBRATION_IDLE_MS
+ CALIBRATION_COUNT_MS
) / airspeedSettings
->SamplePeriod
) {
86 airspeedSettings
->ZeroPoint
= (int16_t)(((float)calibrationSum
) / calibrationCount2
);
87 AirspeedSettingsZeroPointSet(&airspeedSettings
->ZeroPoint
);
90 calibrationCount2
= 0;
97 airspeedSensor
->CalibratedAirspeed
= airspeedSettings
->Scale
* sqrtf((float)abs(airspeedSensor
->SensorValue
- airspeedSettings
->ZeroPoint
));
98 airspeedSensor
->SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_TRUE
;
99 AirspeedAlarm(SYSTEMALARMS_ALARM_OK
);
103 #endif /* if defined(PIOS_INCLUDE_ETASV3) */