dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
baro_airspeed_etasv3.c
Go to the documentation of this file.
1 
16 /*
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.
21  *
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.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
38 #include "openpilot.h"
39 #include "airspeedsettings.h"
40 #include "baroairspeed.h" // object that will be updated by the module
41 #include "pios_thread.h"
42 
43 #if defined(PIOS_INCLUDE_ETASV3)
44 
45 #define SAMPLING_DELAY_MS_ETASV3 50 //Update at 20Hz
46 #define ETS_AIRSPEED_SCALE 1.0f //???
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 CALIBRATION_COUNT_IDLE (CALIBRATION_IDLE_MS/SAMPLING_DELAY_MS_ETASV3) //Ticks to wait before calibrating
50 #define CALIBRATION_COUNT (CALIBRATION_COUNT_MS/SAMPLING_DELAY_MS_ETASV3) //Ticks to wait while calibrating
51 
52 // Private types
53 
54 // Private variables
55 static uint16_t calibrationCount = 0;
56 static uint32_t calibrationSum = 0;
57 
58 void baro_airspeedGetETASV3(BaroAirspeedData *baroAirspeedData, uint32_t *lastSysTime, uint8_t airspeedSensorType, int8_t airspeedADCPin)
59 {
60  //Wait until our turn.
61  PIOS_Thread_Sleep_Until(lastSysTime, SAMPLING_DELAY_MS_ETASV3);
62 
63  AirspeedSettingsData airspeedSettingsData;
64  AirspeedSettingsGet(&airspeedSettingsData);
65 
66  //Check to see if airspeed sensor is returning baroAirspeedData
67  baroAirspeedData->SensorValue = PIOS_ETASV3_ReadAirspeed();
68  if (baroAirspeedData->SensorValue == -1) {
69  baroAirspeedData->BaroConnected = BAROAIRSPEED_BAROCONNECTED_FALSE;
70  baroAirspeedData->CalibratedAirspeed = 0;
71  return;
72  }
73 
74  //Calibrate sensor by averaging zero point value //THIS SHOULD NOT BE DONE IF THERE IS AN IN-AIR RESET. HOW TO DETECT THIS?
75  if (calibrationCount < CALIBRATION_COUNT_IDLE) {
76  calibrationCount++;
77  calibrationSum = 0;
78  return;
79  } else if (calibrationCount < CALIBRATION_COUNT_IDLE + CALIBRATION_COUNT) {
80  calibrationCount++;
81  calibrationSum += baroAirspeedData->SensorValue;
82 
83  if (calibrationCount == CALIBRATION_COUNT_IDLE + CALIBRATION_COUNT) {
84  airspeedSettingsData.ZeroPoint = (uint16_t) roundf(((float)calibrationSum) / CALIBRATION_COUNT);
85  AirspeedSettingsZeroPointSet( &airspeedSettingsData.ZeroPoint );
86  } else {
87  return;
88  }
89  }
90 
91  //Compute airspeed
92  float calibratedAirspeed;
93  if (baroAirspeedData->SensorValue > airspeedSettingsData.ZeroPoint)
94  calibratedAirspeed = ETS_AIRSPEED_SCALE * sqrtf(baroAirspeedData->SensorValue - airspeedSettingsData.ZeroPoint);
95  else
96  calibratedAirspeed = 0;
97 
98  baroAirspeedData->BaroConnected = BAROAIRSPEED_BAROCONNECTED_TRUE;
99  baroAirspeedData->CalibratedAirspeed = calibratedAirspeed;
100 }
101 
102 #else
103 
104 void baro_airspeedGetETASV3(BaroAirspeedData *baroAirspeedData, uint32_t *lastSysTime, uint8_t airspeedSensorType, int8_t airspeedADCPin)
105 {
106  /* Do nothing when driver support not compiled. */
107  PIOS_Thread_Sleep_Until(lastSysTime, 1000);
108 }
109 #endif
110 
static uint8_t airspeedSensorType
Definition: airspeed.c:95
void baro_airspeedGetETASV3(BaroAirspeedData *baroAirspeedData, uint32_t *lastSysTime, uint8_t airspeedSensorType, int8_t airspeedADCPin)
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
Definition: pios_thread.c:255
Includes PiOS and core architecture components.
static uint32_t lastSysTime
int16_t PIOS_ETASV3_ReadAirspeed(void)