dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
baro_airspeed_analog.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_MPXV7002) || defined (PIOS_INCLUDE_MPXV5004) || defined (PIOS_INCLUDE_ADC)
44 
45 #define SAMPLING_DELAY_MS_MPXV 10 //Update at 100Hz
46 #define CALIBRATION_IDLE_MS 2000 //Time to wait before calibrating, in [ms]
47 #define CALIBRATION_COUNT_MS 2000 //Time to spend calibrating, in [ms]
48 #define ANALOG_BARO_AIRSPEED_TIME_CONSTANT_MS 100.0f //Needs to be settable in a UAVO
49 
50 // Private types
51 
52 // Private variables
53 
54 // Private functions
55 
56 static uint16_t calibrationCount=0;
57 
58 
59 void baro_airspeedGetAnalog(BaroAirspeedData *baroAirspeedData, uint32_t *lastSysTime, uint8_t airspeedSensorType, int8_t airspeedADCPin)
60 {
61  //Wait until our turn
62  PIOS_Thread_Sleep_Until(lastSysTime, SAMPLING_DELAY_MS_MPXV);
63 
64  //Ensure that the ADC pin is properly configured
65  if(airspeedADCPin <0){ //It's not, so revert to former sensor type
66  baroAirspeedData->BaroConnected = BAROAIRSPEED_BAROCONNECTED_FALSE;
67 
68  return;
69  }
70 
71  //Calibrate sensor by averaging zero point value //THIS SHOULD NOT BE DONE IF THERE IS AN IN-AIR RESET. HOW TO DETECT THIS?
72  if (calibrationCount < CALIBRATION_IDLE_MS/SAMPLING_DELAY_MS_MPXV) { //First let sensor warm up and stabilize.
73  calibrationCount++;
74  return;
75  } else if (calibrationCount <= (CALIBRATION_IDLE_MS + CALIBRATION_COUNT_MS)/SAMPLING_DELAY_MS_MPXV) { //Then compute the average.
76  calibrationCount++; /*DO NOT MOVE FROM BEFORE sensorCalibration=... LINE, OR ELSE WILL HAVE DIVIDE BY ZERO */
77 
78  uint16_t sensorCalibration;
79 #if defined(PIOS_INCLUDE_MPXV7002) || defined (PIOS_INCLUDE_MPXV5004)
80  if(airspeedSensorType==AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV7002){
81  sensorCalibration=PIOS_MPXV7002_Calibrate(airspeedADCPin, calibrationCount-CALIBRATION_IDLE_MS/SAMPLING_DELAY_MS_MPXV);
82  PIOS_MPXV7002_UpdateCalibration(sensorCalibration); //This makes sense for the user if the initial calibration was not good and the user does not wish to reboot.
83  }
84  else if(airspeedSensorType==AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV5004){
85  sensorCalibration=PIOS_MPXV5004_Calibrate(airspeedADCPin, calibrationCount-CALIBRATION_IDLE_MS/SAMPLING_DELAY_MS_MPXV);
86  PIOS_MPXV5004_UpdateCalibration(sensorCalibration); //This makes sense for the user if the initial calibration was not good and the user does not wish to reboot.
87  }
88 #endif
89 
90 
91  baroAirspeedData->BaroConnected = BAROAIRSPEED_BAROCONNECTED_TRUE;
92 
93  //Set settings UAVO. The airspeed UAVO is set elsewhere in the function.
94  if (calibrationCount == (CALIBRATION_IDLE_MS + CALIBRATION_COUNT_MS)/SAMPLING_DELAY_MS_MPXV)
95  AirspeedSettingsZeroPointSet(&sensorCalibration);
96 
97  return;
98  }
99 
100  //Get CAS
101  float calibratedAirspeed=0;
102 #if defined(PIOS_INCLUDE_MPXV7002) || defined (PIOS_INCLUDE_MPXV5004)
103  if(airspeedSensorType==AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV7002){
104  calibratedAirspeed = PIOS_MPXV7002_ReadAirspeed(airspeedADCPin);
105  if (calibratedAirspeed < 0) //This only occurs when there's a bad ADC reading.
106  return;
107 
108  //Get sensor value, just for telemetry purposes.
109  //This is a silly waste of resources, and should probably be removed at some point in the future.
110  //At this time, PIOS_MPXV7002_Measure() should become a static function and be removed from the header file.
111  //
112  //Moreover, due to the way the ADC driver is currently written, this code will return 0 more often than
113  //not. This is something that will have to change on the ADC side of things.
114  baroAirspeedData->SensorValue=PIOS_MPXV7002_Measure(airspeedADCPin);
115 
116  }
117  else if(airspeedSensorType==AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_DIYDRONESMPXV5004){
118  calibratedAirspeed = PIOS_MPXV5004_ReadAirspeed(airspeedADCPin);
119  if (calibratedAirspeed < 0) //This only occurs when there's a bad ADC reading.
120  return;
121 
122  //Get sensor value, just for telemetry purposes.
123  //This is a silly waste of resources, and should probably be removed at some point in the future.
124  //At this time, PIOS_MPXV7002_Measure() should become a static function and be removed from the header file.
125  //
126  //Moreover, due to the way the ADC driver is currently written, this code will return 0 more often than
127  //not. This is something that will have to change on the ADC side of things.
128  baroAirspeedData->SensorValue=PIOS_MPXV5004_Measure(airspeedADCPin);
129 
130 #else
131  if (false) {
132 #endif
133  } else if (airspeedSensorType == AIRSPEEDSETTINGS_AIRSPEEDSENSORTYPE_RAWANALOG) {
134  // will return raw ADC reading in mV (typically in range [0, 3300])
135  calibratedAirspeed = PIOS_ADC_GetChannelRaw(airspeedADCPin);
136  baroAirspeedData->SensorValue = calibratedAirspeed;
137  }
138  //Filter CAS
139  float alpha=SAMPLING_DELAY_MS_MPXV/(SAMPLING_DELAY_MS_MPXV + ANALOG_BARO_AIRSPEED_TIME_CONSTANT_MS); //Low pass filter.
140  float filteredAirspeed = calibratedAirspeed*(alpha) + baroAirspeedData->CalibratedAirspeed*(1.0f-alpha);
141 
142  //Set two values, one for the UAVO airspeed sensor reading, and the other for the GPS corrected one
143  baroAirspeedData->CalibratedAirspeed = filteredAirspeed;
144 
145 }
146 
147 #else
148 
149 void baro_airspeedGetAnalog(BaroAirspeedData *baroAirspeedData, uint32_t *lastSysTime, uint8_t airspeedSensorType, int8_t airspeedADCPin)
150 {
151  /* Do nothing when driver support not compiled. */
152  PIOS_Thread_Sleep_Until(lastSysTime, 1000);
153 }
154 #endif
155 
float PIOS_MPXV5004_ReadAirspeed(uint8_t airspeedADCPin)
void PIOS_MPXV5004_UpdateCalibration(uint16_t zeroPoint)
static uint8_t airspeedSensorType
Definition: airspeed.c:95
float PIOS_MPXV7002_ReadAirspeed(uint8_t airspeedADCPin)
void PIOS_MPXV7002_UpdateCalibration(uint16_t zeroPoint)
uint16_t PIOS_MPXV7002_Calibrate(uint8_t airspeedADCPin, uint16_t calibrationCount)
uint16_t PIOS_MPXV7002_Measure(uint8_t airspeedADCPin)
void baro_airspeedGetAnalog(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
uint16_t PIOS_MPXV5004_Measure(uint8_t airspeedADCPin)
int32_t PIOS_ADC_GetChannelRaw(uint32_t channel)
Includes PiOS and core architecture components.
static uint32_t lastSysTime
uint16_t PIOS_MPXV5004_Calibrate(uint8_t airspeedADCPin, uint16_t calibrationCount)