dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_mpxv5004.c
Go to the documentation of this file.
1 
18 /*
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27  * for more details.
28  *
29  * You should have received a copy of the GNU General Public License along
30  * with this program; if not, see <http://www.gnu.org/licenses/>
31  */
32 
33 /* Project Includes */
34 #include "pios.h"
35 #include "physical_constants.h"
36 
37 #define VCC 5.0f //Supply voltage in V
38 #define POWER (2.0f/7.0f)
39 
40 #if defined(PIOS_INCLUDE_MPXV5004)
41 
42 #include "pios_mpxv5004.h"
43 
44 static uint32_t calibrationSum = 0; //static?
45 static int16_t calibrationOffset; //static?
46 
47 
48 /*
49  * Reads ADC.
50  */
51 uint16_t PIOS_MPXV5004_Measure(uint8_t airspeedADCPin)
52 {
53  return PIOS_ADC_GetChannelRaw(airspeedADCPin);
54 }
55 
56 /*
57  *Returns zeroPoint so that the user can inspect the calibration vs. the sensor value
58  */
59 uint16_t PIOS_MPXV5004_Calibrate(uint8_t airspeedADCPin, uint16_t calibrationCount){
60  calibrationSum += PIOS_MPXV5004_Measure(airspeedADCPin);
61  uint16_t zeroPoint = (uint16_t)(((float)calibrationSum) / calibrationCount + 0.5f);
62 
63  calibrationOffset = zeroPoint - (int16_t)(1.0f/3.3f*4096.0f+0.5f); //The offset should set the zero point to 1.0V
64 
65  return zeroPoint;
66 }
67 
68 
69 /*
70  * Updates the calibration when zero point is manually set by user.
71  */
72 void PIOS_MPXV5004_UpdateCalibration(uint16_t zeroPoint){
73  calibrationOffset = zeroPoint - (int16_t)(1.0f/3.3f*4096.0f+0.5f); //The offset should set the zero point to 1.0V
74 }
75 
76 
77 /*
78  * Reads the airspeed and returns CAS (calibrated airspeed) in the case of success.
79  * In the case of a failed read, returns -1.
80  */
81 float PIOS_MPXV5004_ReadAirspeed(uint8_t airspeedADCPin)
82 {
83  float sensorVal = PIOS_MPXV5004_Measure(airspeedADCPin);
84 
85  //Calculate dynamic pressure, as per docs
86  float Qc = 5.0f*(((sensorVal - calibrationOffset)/4096.0f*3.3f)/VCC - 0.2f);
87 
88  //Saturate Qc on the lower bound, in order to make sure we don't have negative airspeeds. No need
89  // to saturate on the upper bound, we'll handle that later with calibratedAirspeed.
90  if (Qc < 0) {
91  Qc=0;
92  }
93 
94  //Compute calibraterd airspeed, as per http://en.wikipedia.org/wiki/Calibrated_airspeed
95  float calibratedAirspeed = STANDARD_AIR_MACH_SPEED*sqrtf(5.0f*(powf(Qc/(STANDARD_AIR_SEA_LEVEL_PRESSURE/1000)+1.0f,POWER)-1.0f));
96 
97  //Upper bound airspeed. No need to lower bound it, that comes from Qc
98  if (calibratedAirspeed > 80) { //in [m/s]
99  calibratedAirspeed=80;
100  }
101 
102 
103  return calibratedAirspeed;
104 }
105 
106 #endif /* PIOS_INCLUDE_MPXV5004 */
Main PiOS header to include all the compiled in PiOS options.
#define VCC
Definition: pios_mpxv5004.c:37
float PIOS_MPXV5004_ReadAirspeed(uint8_t airspeedADCPin)
void PIOS_MPXV5004_UpdateCalibration(uint16_t zeroPoint)
ETASV3 Airspeed Sensor Driver.
#define POWER
Definition: pios_mpxv5004.c:38
tuple f
Definition: px_mkfw.py:81
uint16_t PIOS_MPXV5004_Measure(uint8_t airspeedADCPin)
int32_t PIOS_ADC_GetChannelRaw(uint32_t channel)
uint16_t PIOS_MPXV5004_Calibrate(uint8_t airspeedADCPin, uint16_t calibrationCount)