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 HwSettingsOptionalModulesData optionalModules
;
98 HwSettingsOptionalModulesGet(&optionalModules
);
100 #ifdef MODULE_AIRSPEED_BUILTIN
101 airspeedEnabled
= true;
102 optionalModules
.Airspeed
= HWSETTINGS_OPTIONALMODULES_ENABLED
;
103 HwSettingsOptionalModulesSet(&optionalModules
);
105 if (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();
125 AirspeedSettingsConnectCallback(AirspeedSettingsUpdatedCb
);
129 MODULE_INITCALL(AirspeedInitialize
, AirspeedStart
);
133 * Module thread, should not return.
135 static void airspeedTask(__attribute__((unused
)) void *parameters
)
137 AirspeedSettingsUpdatedCb(AirspeedSettingsHandle());
138 bool imuAirspeedInitialized
= false;
139 AirspeedSensorData airspeedData
;
140 AirspeedSensorGet(&airspeedData
);
142 AirspeedSettingsUpdatedCb(NULL
);
144 airspeedData
.SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_FALSE
;
147 portTickType lastSysTime
= xTaskGetTickCount();
149 vTaskDelayUntil(&lastSysTime
, airspeedSettings
.SamplePeriod
/ portTICK_RATE_MS
);
151 // Update the airspeed object
152 AirspeedSensorGet(&airspeedData
);
154 // if sensor type changed reset Airspeed alarm
155 if (airspeedSettings
.AirspeedSensorType
!= lastAirspeedSensorType
) {
156 AirspeedAlarm(SYSTEMALARMS_ALARM_DEFAULT
);
157 lastAirspeedSensorType
= airspeedSettings
.AirspeedSensorType
;
158 switch (airspeedSettings
.AirspeedSensorType
) {
159 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_NONE
:
160 // AirspeedSensor will not be updated until a different sensor is selected
161 // set the disconencted satus now
162 airspeedData
.SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_FALSE
;
163 AirspeedSensorSet(&airspeedData
);
165 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_GROUNDSPEEDBASEDWINDESTIMATION
:
166 if (!imuAirspeedInitialized
) {
167 imuAirspeedInitialized
= true;
168 imu_airspeedInitialize(&airspeedSettings
);
175 switch (airspeedSettings
.AirspeedSensorType
) {
176 #if defined(PIOS_INCLUDE_MPXV)
177 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV7002
:
178 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV5004
:
179 // MPXV5004 and MPXV7002 sensors
180 baro_airspeedGetMPXV(&airspeedData
, &airspeedSettings
, airspeedADCPin
);
183 #if defined(PIOS_INCLUDE_ETASV3)
184 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_EAGLETREEAIRSPEEDV3
:
185 // Eagletree Airspeed v3
186 baro_airspeedGetETASV3(&airspeedData
, &airspeedSettings
);
189 #if defined(PIOS_INCLUDE_MS4525DO)
190 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_PIXHAWKAIRSPEEDMS4525DO
:
191 // PixHawk Airpeed based on MS4525DO
192 baro_airspeedGetMS4525DO(&airspeedData
, &airspeedSettings
);
195 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_GROUNDSPEEDBASEDWINDESTIMATION
:
196 imu_airspeedGet(&airspeedData
, &airspeedSettings
);
198 case AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_NONE
:
199 // no need to check so often until a sensor is enabled
200 AlarmsSet(SYSTEMALARMS_ALARM_AIRSPEED
, SYSTEMALARMS_ALARM_DEFAULT
);
201 vTaskDelay(2000 / portTICK_RATE_MS
);
204 airspeedData
.SensorConnected
= AIRSPEEDSENSOR_SENSORCONNECTED_FALSE
;
209 AirspeedSensorSet(&airspeedData
);
214 static void AirspeedSettingsUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
216 AirspeedSettingsGet(&airspeedSettings
);