software:firmware
MakAir Firmware
pressure_utl.cpp
Go to the documentation of this file.
1 
8 // INCLUDES ===================================================================
9 
10 #include <algorithm>
11 
12 #include "../includes/pressure_utl.h"
13 
14 // INITIALISATION =============================================================
15 
16 static int32_t filteredRawPressure = 0;
17 
18 static const int32_t RAW_PRESSURE_FILTER_DIVIDER = 5;
19 
20 // From the datasheet:
21 // Transfer Function (kPa): Vout = VS×(0.09×P + 0.04)
22 // VS is 5.05 volt typical on our boards
23 // Transfer function for P in mmH2O, with 1 kPa = 101.97162129779 mmH2O
24 // Vout = VS×(0.09×(P/101.9716) + 0.04)
25 // P = (101.9716/0.09)×(Vout/VS-0.04)
26 // P(mmH20) = 1133(Vout/VS) - 45
27 // The offset really depend on the sensor itself with a great variation
28 // There must be a calibration sequence to set the zero.
29 //
30 // VS is 5.05V.
31 //
32 // There is a voltage divider in between Vout and Vadc
33 // Vadc = 68/83 Vout
34 //
35 // With current ADC (12 bits), and mean Vref=3.348V:
36 // Vadc = 3.348 * RawAdc/4096 = 0,000817382 * RawAdc
37 //
38 // Vout = 83/68 * 0,000817382 * RawAdc
39 // Vout = 0,000997686 * RawAdc
40 //
41 // Put it together:
42 // P(mmH20) = 1133((0,000997686 * RawAdc)/5.05) - 45
43 // P(mmH20) = 0,223837466 * RawAdc - 45
44 
45 static const int16_t RAW_PRESSURE_TO_MMH20_CONSTANT = 45;
46 static const int32_t RAW_PRESSURE_TO_MMH20_NUM = 2238;
47 static const int32_t RAW_PRESSURE_TO_MMH20_DEN = 10000;
48 
49 // FUNCTIONS ==================================================================
50 
51 int16_t convertSensor2Pressure(uint32_t sensorValue) {
52  int32_t rawPressure = static_cast<int32_t>(sensorValue);
53  int32_t delta = rawPressure - filteredRawPressure;
54 
55  // Adjust delta so that the division result will be rounded away from zero.
56  // This is needed to guaranty that filteredRawPressure will reach
57  // rawPressure when it is constant.
58  int32_t rounding = RAW_PRESSURE_FILTER_DIVIDER - 1;
59  delta += (delta > 0) ? rounding : -rounding;
61 
62  int16_t scaledRawPressure =
64  return scaledRawPressure - RAW_PRESSURE_TO_MMH20_CONSTANT;
65 }
66 
67 // cppcheck-suppress unusedFunction
void resetFilteredRawPressure()
Reset the value of void filteredRawPressure to 0.
static int32_t filteredRawPressure
static const int32_t RAW_PRESSURE_FILTER_DIVIDER
static const int32_t RAW_PRESSURE_TO_MMH20_DEN
static const int32_t RAW_PRESSURE_TO_MMH20_NUM
int16_t convertSensor2Pressure(uint32_t sensorValue)
Convert the analog value from sensor to a pressure value.
static const int16_t RAW_PRESSURE_TO_MMH20_CONSTANT