Updated and Validated
[betaflight.git] / src / test / unit / baro_bmp085_unittest.cc
blob6b7c46b1df73d70bcee71b2a91f3b7a784738ba1
1 /*
2 * This file is part of Cleanflight.
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.
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.
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/>.
17 #include <stdint.h>
19 extern "C" {
21 #include "platform.h"
22 #include "target.h"
23 #include "drivers/barometer/barometer.h"
24 #include "drivers/bus.h"
26 void bmp085Calculate(int32_t *pressure, int32_t *temperature);
27 extern uint32_t bmp085_up;
28 extern uint16_t bmp085_ut;
30 typedef struct {
31 int16_t ac1;
32 int16_t ac2;
33 int16_t ac3;
34 uint16_t ac4;
35 uint16_t ac5;
36 uint16_t ac6;
37 int16_t b1;
38 int16_t b2;
39 int16_t mb;
40 int16_t mc;
41 int16_t md;
42 } bmp085_smd500_calibration_param_t;
44 typedef struct {
45 bmp085_smd500_calibration_param_t cal_param;
46 uint8_t mode;
47 uint8_t chip_id, ml_version, al_version;
48 uint8_t dev_addr;
49 int32_t param_b5;
50 int16_t oversampling_setting;
51 } bmp085_t;
53 bmp085_t bmp085;
58 #include "unittest_macros.h"
59 #include "gtest/gtest.h"
62 TEST(baroBmp085Test, TestBmp085CalculateOss0)
65 // given
66 int32_t pressure, temperature;
67 bmp085_up = 23843; // Digital pressure value
68 bmp085_ut = 27898; // Digital temperature value
70 // and
71 bmp085.cal_param.ac1 = 408;
72 bmp085.cal_param.ac2 = -72;
73 bmp085.cal_param.ac3 = -14383;
74 bmp085.cal_param.ac4 = 32741;
75 bmp085.cal_param.ac5 = 32757;
76 bmp085.cal_param.ac6 = 23153;
77 bmp085.cal_param.b1 = 6190;
78 bmp085.cal_param.b2 = 4;
79 bmp085.cal_param.mb = -32767;
80 bmp085.cal_param.mc = -8711;
81 bmp085.cal_param.md = 2868;
82 bmp085.oversampling_setting = 0;
84 // when
85 bmp085Calculate(&pressure, &temperature);
87 // then
88 EXPECT_EQ(69964, pressure); // Datasheet says 69965
89 EXPECT_EQ(1500, temperature);
93 TEST(baroBmp085Test, TestBmp085CalculateOss3)
96 // given
97 int32_t pressure, temperature;
98 bmp085_up = 271097; // Digital pressure value
99 bmp085_ut = 27898; // Digital temperature value
101 // and
102 bmp085.cal_param.ac1 = 408;
103 bmp085.cal_param.ac2 = -72;
104 bmp085.cal_param.ac3 = -14383;
105 bmp085.cal_param.ac4 = 32741;
106 bmp085.cal_param.ac5 = 32757;
107 bmp085.cal_param.ac6 = 23153;
108 bmp085.cal_param.b1 = 6190;
109 bmp085.cal_param.b2 = 4;
110 bmp085.cal_param.mb = -32767;
111 bmp085.cal_param.mc = -8711;
112 bmp085.cal_param.md = 2868;
113 bmp085.oversampling_setting = 3;
115 // when
116 bmp085Calculate(&pressure, &temperature);
118 // then
119 EXPECT_EQ(99998, pressure);
120 EXPECT_EQ(1500, temperature);
124 TEST(baroBmp085Test, TestBmp085CalculateOss3Cold)
127 // given
128 int32_t pressure, temperature;
129 bmp085_up = 271097; // Digital pressure value
130 bmp085_ut = 24342; // Digital temperature value 24342 = -20degC 27898 = 15degC
132 // and
133 bmp085.cal_param.ac1 = 408;
134 bmp085.cal_param.ac2 = -72;
135 bmp085.cal_param.ac3 = -14383;
136 bmp085.cal_param.ac4 = 32741;
137 bmp085.cal_param.ac5 = 32757;
138 bmp085.cal_param.ac6 = 23153;
139 bmp085.cal_param.b1 = 6190;
140 bmp085.cal_param.b2 = 4;
141 bmp085.cal_param.mb = -32767;
142 bmp085.cal_param.mc = -8711;
143 bmp085.cal_param.md = 2868;
144 bmp085.oversampling_setting = 3;
146 // when
147 bmp085Calculate(&pressure, &temperature);
149 // then
150 EXPECT_EQ(92251, pressure);
151 EXPECT_EQ(-2006, temperature); // -20.06 degC
155 TEST(baroBmp085Test, TestBmp085CalculateOss3Hot)
158 // given
159 int32_t pressure, temperature;
160 bmp085_up = 271097; // Digital pressure value
161 bmp085_ut = 33315; // Digital temperature value
163 // and
164 bmp085.cal_param.ac1 = 408;
165 bmp085.cal_param.ac2 = -72;
166 bmp085.cal_param.ac3 = -14383;
167 bmp085.cal_param.ac4 = 32741;
168 bmp085.cal_param.ac5 = 32757;
169 bmp085.cal_param.ac6 = 23153;
170 bmp085.cal_param.b1 = 6190;
171 bmp085.cal_param.b2 = 4;
172 bmp085.cal_param.mb = -32767;
173 bmp085.cal_param.mc = -8711;
174 bmp085.cal_param.md = 2868;
175 bmp085.oversampling_setting = 3;
177 // when
178 bmp085Calculate(&pressure, &temperature);
180 // then
181 EXPECT_EQ(108922, pressure);
182 EXPECT_EQ(5493, temperature); // 54.93 degC
186 // STUBS
188 extern "C" {
190 void gpioInit() {}
191 void RCC_APB2PeriphClockCmd() {}
192 void delay(uint32_t) {}
193 void delayMicroseconds(uint32_t) {}
194 bool busReadRegisterBuffer(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
195 bool busWriteRegister(const extDevice_t*, uint8_t, uint8_t) {return true;}
196 void IOConfigGPIO() {}
197 void IOHi() {}
198 void IOLo() {}
199 void IOInit() {}
200 void IOGetByTag() {}
201 bool busBusy(const extDevice_t*, bool*) {return false;}
202 void busDeviceRegister(const extDevice_t*) {}
203 bool busReadRegisterBufferStart(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
204 bool busWriteRegisterStart(const extDevice_t*, uint8_t, uint8_t) {return true;}