Merge pull request #10285 from flywoo/master
[inav.git] / src / test / unit / baro_bmp085_unittest.cc.txt
blob655a9e451407ca8df048442c0759ec9ee72b4e0b
1 /*
2  * This file is part of Cleanflight.
3  *
4  * Cleanflight is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Cleanflight is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Cleanflight.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <stdint.h>
19 extern "C" {
21     void bmp085_calculate(int32_t *pressure, int32_t *temperature);
22     extern uint32_t bmp085_up;
23     extern uint16_t bmp085_ut;
25 typedef struct {
26     int16_t ac1;
27     int16_t ac2;
28     int16_t ac3;
29     uint16_t ac4;
30     uint16_t ac5;
31     uint16_t ac6;
32     int16_t b1;
33     int16_t b2;
34     int16_t mb;
35     int16_t mc;
36     int16_t md;
37 } bmp085_smd500_calibration_param_t;
39 typedef struct {
40     bmp085_smd500_calibration_param_t cal_param;
41     uint8_t mode;
42     uint8_t chip_id, ml_version, al_version;
43     uint8_t dev_addr;
44     int32_t param_b5;
45     int16_t oversampling_setting;
46 } bmp085_t;
48     bmp085_t bmp085;
53 #include "unittest_macros.h"
54 #include "gtest/gtest.h"
57 TEST(baroBmp085Test, TestBmp085CalculateOss0)
60     // given
61     int32_t pressure, temperature;
62     bmp085_up = 23843; // Digital pressure value
63     bmp085_ut = 27898; // Digital temperature value
65     // and
66     bmp085.cal_param.ac1 = 408;
67     bmp085.cal_param.ac2 = -72;
68     bmp085.cal_param.ac3 = -14383;
69     bmp085.cal_param.ac4 = 32741;
70     bmp085.cal_param.ac5 = 32757;
71     bmp085.cal_param.ac6 = 23153;
72     bmp085.cal_param.b1 = 6190;
73     bmp085.cal_param.b2 = 4;
74     bmp085.cal_param.mb = -32767;
75     bmp085.cal_param.mc = -8711;
76     bmp085.cal_param.md = 2868;
77     bmp085.oversampling_setting = 0;
79     // when
80     bmp085_calculate(&pressure, &temperature);
82     // then
83     EXPECT_EQ(69964, pressure); // Datasheet says 69965
84     EXPECT_EQ(1500, temperature);
88 TEST(baroBmp085Test, TestBmp085CalculateOss3)
91     // given
92     int32_t pressure, temperature;
93     bmp085_up = 271097; // Digital pressure value
94     bmp085_ut = 27898; // Digital temperature value
96     // and
97     bmp085.cal_param.ac1 = 408;
98     bmp085.cal_param.ac2 = -72;
99     bmp085.cal_param.ac3 = -14383;
100     bmp085.cal_param.ac4 = 32741;
101     bmp085.cal_param.ac5 = 32757;
102     bmp085.cal_param.ac6 = 23153;
103     bmp085.cal_param.b1 = 6190;
104     bmp085.cal_param.b2 = 4;
105     bmp085.cal_param.mb = -32767;
106     bmp085.cal_param.mc = -8711;
107     bmp085.cal_param.md = 2868;
108     bmp085.oversampling_setting = 3;
110     // when
111     bmp085_calculate(&pressure, &temperature);
113     // then
114     EXPECT_EQ(99998, pressure);
115     EXPECT_EQ(1500, temperature);
119 TEST(baroBmp085Test, TestBmp085CalculateOss3Cold)
122     // given
123     int32_t pressure, temperature;
124     bmp085_up = 271097; // Digital pressure value
125     bmp085_ut = 24342; // Digital temperature value  24342 = -20degC    27898 = 15degC
127     // and
128     bmp085.cal_param.ac1 = 408;
129     bmp085.cal_param.ac2 = -72;
130     bmp085.cal_param.ac3 = -14383;
131     bmp085.cal_param.ac4 = 32741;
132     bmp085.cal_param.ac5 = 32757;
133     bmp085.cal_param.ac6 = 23153;
134     bmp085.cal_param.b1 = 6190;
135     bmp085.cal_param.b2 = 4;
136     bmp085.cal_param.mb = -32767;
137     bmp085.cal_param.mc = -8711;
138     bmp085.cal_param.md = 2868;
139     bmp085.oversampling_setting = 3;
141     // when
142     bmp085_calculate(&pressure, &temperature);
144     // then
145     EXPECT_EQ(92251, pressure);
146     EXPECT_EQ(-2006, temperature); // -20.06 degC
150 TEST(baroBmp085Test, TestBmp085CalculateOss3Hot)
153     // given
154     int32_t pressure, temperature;
155     bmp085_up = 271097; // Digital pressure value
156     bmp085_ut = 33315; // Digital temperature value
158     // and
159     bmp085.cal_param.ac1 = 408;
160     bmp085.cal_param.ac2 = -72;
161     bmp085.cal_param.ac3 = -14383;
162     bmp085.cal_param.ac4 = 32741;
163     bmp085.cal_param.ac5 = 32757;
164     bmp085.cal_param.ac6 = 23153;
165     bmp085.cal_param.b1 = 6190;
166     bmp085.cal_param.b2 = 4;
167     bmp085.cal_param.mb = -32767;
168     bmp085.cal_param.mc = -8711;
169     bmp085.cal_param.md = 2868;
170     bmp085.oversampling_setting = 3;
172     // when
173     bmp085_calculate(&pressure, &temperature);
175     // then
176     EXPECT_EQ(108922, pressure);
177     EXPECT_EQ(5493, temperature); // 54.93 degC
181 // STUBS
183 extern "C" {
185     void RCC_APB2PeriphClockCmd() {}
186     void delay(uint32_t) {}
187     void delayMicroseconds(uint32_t) {}
188     bool i2cWrite(uint8_t, uint8_t, uint8_t) {
189         return 1;
190     }
191     bool i2cRead(uint8_t, uint8_t, uint8_t, uint8_t) {
192         return 1;
193     }