2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
5 * @addtogroup AirspeedModule Airspeed Module
6 * @brief Calculate airspeed from diverse sources and update @ref Airspeed "Airspeed UAV Object"
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
11 * @brief Airspeed module
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: AirspeedSensor
35 * This module will periodically update the value of the AirspeedSensor object.
39 #include <openpilot.h>
41 #include "hwsettings.h"
42 #include "airspeedsettings.h"
43 #include "airspeedsensor.h" // object that will be updated by the module
44 #include "baro_airspeed_ms4525do.h"
45 #include "baro_airspeed_etasv3.h"
46 #include "baro_airspeed_mpxv.h"
47 #include "imu_airspeed.h"
48 #include "airspeedalarm.h"
53 #define STACK_SIZE_BYTES 650
56 #define TASK_PRIORITY (tskIDLE_PRIORITY + 1)
61 static xTaskHandle taskHandle
;
62 static bool airspeedEnabled
= false;
63 static AirspeedSettingsData airspeedSettings
;
64 static AirspeedSettingsAirspeedSensorTypeOptions lastAirspeedSensorType
= -1;
65 static int8_t airspeedADCPin
= -1;
69 static void airspeedTask(void *parameters
);
70 static void AirspeedSettingsUpdatedCb(UAVObjEvent
*ev
);
74 * Initialise the module, called on startup
75 * \returns 0 on success or -1 if initialisation failed
77 int32_t AirspeedStart()
79 // Check if module is enabled or not
80 if (airspeedEnabled
== false) {
85 xTaskCreate(airspeedTask
, "Airspeed", STACK_SIZE_BYTES
/ 4, NULL
, TASK_PRIORITY
, &taskHandle
);
86 PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_AIRSPEED
, taskHandle
);
91 * Initialise the module, called on startup
92 * \returns 0 on success or -1 if initialisation failed
94 int32_t AirspeedInitialize()
96 #ifdef MODULE_AIRSPEED_BUILTIN
97 airspeedEnabled
= true;
100 HwSettingsInitialize();
101 HwSettingsOptionalModulesOptions optionalModules
[HWSETTINGS_OPTIONALMODULES_NUMELEM
];
102 HwSettingsOptionalModulesArrayGet(optionalModules
);
105 if (optionalModules
[HWSETTINGS_OPTIONALMODULES_AIRSPEED
] == HWSETTINGS_OPTIONALMODULES_ENABLED
) {
106 airspeedEnabled
= true;
108 airspeedEnabled
= false;
113 HwSettingsADCRoutingOptions adcRouting
[HWSETTINGS_ADCROUTING_NUMELEM
];
114 HwSettingsADCRoutingArrayGet(adcRouting
);
116 // Determine if the barometric airspeed sensor is routed to an ADC pin
117 for (int i
= 0; i
< HWSETTINGS_ADCROUTING_NUMELEM
; i
++) {
118 if (adcRouting
[i
] == HWSETTINGS_ADCROUTING_ANALOGAIRSPEED
) {
123 AirspeedSensorInitialize();
124 AirspeedSettingsInitialize();
126 AirspeedSettingsConnectCallback(AirspeedSettingsUpdatedCb
);
130 MODULE_INITCALL(AirspeedInitialize
, AirspeedStart
);
134 * Module thread, should not return.
136 static void airspeedTask(__attribute__((unused
)) void *parameters
)
138 AirspeedSettingsUpdatedCb(AirspeedSettingsHandle());
139 bool imuAirspeedInitialized
= false;
140 AirspeedSensorData airspeedData
;
141 AirspeedSensorGet(&airspeedData
);
143 AirspeedSettingsUpdatedCb(NULL
);
145 airspeedData
.SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_FALSE
;
148 portTickType lastSysTime
= xTaskGetTickCount();
150 vTaskDelayUntil(&lastSysTime
, airspeedSettings
.SamplePeriod
/ portTICK_RATE_MS
);
152 // Update the airspeed object
153 AirspeedSensorGet(&airspeedData
);
155 // if sensor type changed reset Airspeed alarm
156 if (airspeedSettings
.AirspeedSensorType
!= lastAirspeedSensorType
) {
157 AirspeedAlarm(SYSTEMALARMS_ALARM_DEFAULT
);
158 lastAirspeedSensorType
= airspeedSettings
.AirspeedSensorType
;
159 switch (airspeedSettings
.AirspeedSensorType
) {
160 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_NONE
:
161 // AirspeedSensor will not be updated until a different sensor is selected
162 // set the disconencted satus now
163 airspeedData
.SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_FALSE
;
164 AirspeedSensorSet(&airspeedData
);
166 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_GROUNDSPEEDBASEDWINDESTIMATION
:
167 if (!imuAirspeedInitialized
) {
168 imuAirspeedInitialized
= true;
169 imu_airspeedInitialize(&airspeedSettings
);
176 switch (airspeedSettings
.AirspeedSensorType
) {
177 #if defined(PIOS_INCLUDE_MPXV)
178 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV7002
:
179 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV5004
:
180 // MPXV5004 and MPXV7002 sensors
181 baro_airspeedGetMPXV(&airspeedData
, &airspeedSettings
, airspeedADCPin
);
184 #if defined(PIOS_INCLUDE_ETASV3)
185 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_EAGLETREEAIRSPEEDV3
:
186 // Eagletree Airspeed v3
187 baro_airspeedGetETASV3(&airspeedData
, &airspeedSettings
);
190 #if defined(PIOS_INCLUDE_MS4525DO)
191 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_PIXHAWKAIRSPEEDMS4525DO
:
192 // PixHawk Airpeed based on MS4525DO
193 baro_airspeedGetMS4525DO(&airspeedData
, &airspeedSettings
);
196 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_GROUNDSPEEDBASEDWINDESTIMATION
:
197 imu_airspeedGet(&airspeedData
, &airspeedSettings
);
199 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_NONE
:
200 // no need to check so often until a sensor is enabled
201 AlarmsSet(SYSTEMALARMS_ALARM_AIRSPEED
, SYSTEMALARMS_ALARM_DEFAULT
);
202 vTaskDelay(2000 / portTICK_RATE_MS
);
205 airspeedData
.SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_FALSE
;
210 AirspeedSensorSet(&airspeedData
);
215 static void AirspeedSettingsUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
217 AirspeedSettingsGet(&airspeedSettings
);