software:firmware
MakAir Firmware
test_pressure.cpp
Go to the documentation of this file.
1 
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
10 
11 #include <iostream>
12 #include <vector>
13 
14 #include "../includes/pressure_utl.h"
15 
16 #define KPA_MMH2O 101.97162129779
17 #define RATIO_VOLTAGE_DIVIDER 0.8192
18 #define V_SUPPLY 5.08
19 
21 int16_t convertSensor2PressureFloat(uint16_t sensorValue) {
22  static float filteredVout = 0;
23  double rawVout = sensorValue * 3.3 / 1024.0;
24  filteredVout = filteredVout + (rawVout - filteredVout) * 0.2;
25 
26  // Voltage divider ratio
27  double vOut = filteredVout / RATIO_VOLTAGE_DIVIDER;
28 
29  // Pressure converted to kPA
30  double pressure = ((vOut / V_SUPPLY) - 0.04) / 0.09;
31 
32  if (pressure <= 0.0) {
33  pressure = 0;
34  }
35 
36  return pressure * KPA_MMH2O;
37 }
38 
43 class PressionTest : public ::testing::Test {
44  protected:
46 
47  virtual void SetUp() { resetFilteredRawPressure(); }
48 
49  std::vector<uint16_t> input = {0, 100, 1000, 5000, 8000, 16000, 24000, 65535};
50 };
51 
52 #define NEAR_EQUAL_DIFF 2
57 TEST_F(PressionTest, testConvertSensor2PressureCompWithFloat) {
58  std::vector<uint16_t> outputTruth;
59  std::vector<uint16_t> output;
60 
61  for (size_t i = 0; i < input.size(); i++) {
62  outputTruth.push_back(convertSensor2PressureFloat(input[i]));
63  output.push_back(convertSensor2Pressure(input[i]));
64 
65  std::cout << outputTruth.back() << " " << output.back() << std::endl;
66  }
67 
68  for (int i = 0; i < output.size(); i++) {
69  ASSERT_LE(std::abs(outputTruth[i] - output[i]), 2);
70  }
71 }
72 
77 TEST_F(PressionTest, testConvertSensor2Pressure) {
78  std::vector<uint16_t> outputTruth = {0, 0, 144, 984, 2182, 4545, 7839, 17762};
79  std::vector<uint16_t> output;
80 
81  for (size_t i = 0; i < input.size(); i++) {
82  output.push_back(convertSensor2Pressure(input[i]));
83 
84  std::cout << output.back() << std::endl;
85  }
86 
87  ASSERT_THAT(output, testing::ElementsAreArray(outputTruth));
88 }
Test Fixture to test the pression file.
std::vector< uint16_t > input
virtual void SetUp()
uint16_t i
void resetFilteredRawPressure()
Reset the value of void filteredRawPressure to 0.
int16_t convertSensor2Pressure(uint32_t sensorValue)
Convert the analog value from sensor to a pressure value.
int16_t convertSensor2PressureFloat(uint16_t sensorValue)
Old converting function using floats.
TEST_F(PressionTest, testConvertSensor2PressureCompWithFloat)
Test convertSensor2Pressure with comparison with old floating point function.
#define KPA_MMH2O
#define RATIO_VOLTAGE_DIVIDER
#define V_SUPPLY