PX4 Firmware
PX4 Autopilot Software http://px4.io
BMP280.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "BMP280.hpp"
35 
37  ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(interface->get_device_id())),
38  _px4_baro(interface->get_device_id()),
39  _interface(interface),
40  _sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": sample")),
41  _measure_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": measure")),
42  _comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": comms errors"))
43 {
44 }
45 
47 {
48  // make sure we are truly inactive
49  Stop();
50 
51  // free perf counters
55 
56  delete _interface;
57 }
58 
59 int
61 {
62  // reset sensor
64  usleep(10000);
65 
66  // check id
68  PX4_WARN("id of your baro is not: 0x%02x", BMP280_VALUE_ID);
69  return -EIO;
70  }
71 
72  // set config, recommended settings
75 
76  // get calibration and pre process them
78 
79  _fcal.t1 = _cal->t1 * powf(2, 4);
80  _fcal.t2 = _cal->t2 * powf(2, -14);
81  _fcal.t3 = _cal->t3 * powf(2, -34);
82 
83  _fcal.p1 = _cal->p1 * (powf(2, 4) / -100000.0f);
84  _fcal.p2 = _cal->p1 * _cal->p2 * (powf(2, -31) / -100000.0f);
85  _fcal.p3 = _cal->p1 * _cal->p3 * (powf(2, -51) / -100000.0f);
86 
87  _fcal.p4 = _cal->p4 * powf(2, 4) - powf(2, 20);
88  _fcal.p5 = _cal->p5 * powf(2, -14);
89  _fcal.p6 = _cal->p6 * powf(2, -31);
90 
91  _fcal.p7 = _cal->p7 * powf(2, -4);
92  _fcal.p8 = _cal->p8 * powf(2, -19) + 1.0f;
93  _fcal.p9 = _cal->p9 * powf(2, -35);
94 
95  Start();
96 
97  return OK;
98 }
99 
100 void
102 {
103  // reset the report ring and state machine
104  _collect_phase = false;
105 
106  // schedule a cycle to start things
107  ScheduleNow();
108 }
109 
110 void
112 {
113  ScheduleClear();
114 }
115 
116 void
118 {
119  if (_collect_phase) {
120  collect();
121 
122  } else {
123  measure();
124  }
125 
126  ScheduleDelayed(_measure_interval);
127 }
128 
129 int
131 {
133 
134  _collect_phase = true;
135 
136  // start measure
138 
139  if (ret != OK) {
142  return -EIO;
143  }
144 
146 
147  return OK;
148 }
149 
150 int
152 {
154 
155  _collect_phase = false;
156 
157  // this should be fairly close to the end of the conversion, so the best approximation of the time
158  const hrt_abstime timestamp_sample = hrt_absolute_time();
160 
161  if (data == nullptr) {
164  return -EIO;
165  }
166 
167  // convert data to number 20 bit
168  uint32_t p_raw = data->p_msb << 12 | data->p_lsb << 4 | data->p_xlsb >> 4;
169  uint32_t t_raw = data->t_msb << 12 | data->t_lsb << 4 | data->t_xlsb >> 4;
170 
171  // Temperature
172  float ofs = (float) t_raw - _fcal.t1;
173  float t_fine = (ofs * _fcal.t3 + _fcal.t2) * ofs;
174  const float T = t_fine * (1.0f / 5120.0f);
175 
176  // Pressure
177  float tf = t_fine - 128000.0f;
178  float x1 = (tf * _fcal.p6 + _fcal.p5) * tf + _fcal.p4;
179  float x2 = (tf * _fcal.p3 + _fcal.p2) * tf + _fcal.p1;
180 
181  float pf = ((float) p_raw + x1) / x2;
182  const float P = (pf * _fcal.p9 + _fcal.p8) * pf + _fcal.p7;
183 
186 
187  float pressure = P / 100.0f; // to mbar
188  _px4_baro.update(timestamp_sample, pressure);
189 
191 
192  return OK;
193 }
194 
195 void
197 {
201 
203 }
void print_status()
PX4Barometer _px4_baro
Definition: BMP280.hpp:61
bmp280::fcalibration_s _fcal
Definition: BMP280.hpp:76
void print_info()
Definition: BMP280.cpp:196
void set_temperature(float temperature)
virtual int set_reg(uint8_t value, uint8_t addr)=0
bmp280::calibration_s * _cal
Definition: BMP280.hpp:75
uint8_t p_msb
Definition: bmp280.h:105
measure the time elapsed performing an event
Definition: perf_counter.h:56
bmp280::IBMP280 * _interface
Definition: BMP280.hpp:63
int collect()
Definition: BMP280.cpp:151
void set_error_count(uint64_t error_count)
static constexpr uint8_t _curr_ctrl
Definition: BMP280.hpp:66
perf_counter_t _sample_perf
Definition: BMP280.hpp:71
#define BMP280_ADDR_CAL
Definition: bmp280.h:41
virtual bmp280::data_s * get_data(uint8_t addr)=0
#define BMP280_VALUE_ID
Definition: bmp280.h:50
count the number of times an event occurs
Definition: perf_counter.h:55
virtual bmp280::calibration_s * get_calibration(uint8_t addr)=0
uint8_t t_msb
Definition: bmp280.h:109
int init()
Definition: BMP280.cpp:60
void perf_count(perf_counter_t handle)
Count a performance event.
void Stop()
Definition: BMP280.cpp:111
virtual uint8_t get_reg(uint8_t addr)=0
void perf_free(perf_counter_t handle)
Free a counter.
#define BMP280_ADDR_CTRL
Definition: bmp280.h:45
#define BMP280_CTRL_MODE_FORCE
Definition: bmp280.h:78
uint8_t p_lsb
Definition: bmp280.h:106
#define perf_alloc(a, b)
Definition: px4io.h:59
void update(hrt_abstime timestamp, float pressure)
uint8_t * data
Definition: dataman.cpp:149
void perf_end(perf_counter_t handle)
End a performance event.
#define BMP280_ADDR_ID
Definition: bmp280.h:48
uint8_t t_lsb
Definition: bmp280.h:110
#define BMP280_ADDR_CONFIG
Definition: bmp280.h:44
void Start()
Definition: BMP280.cpp:101
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
perf_counter_t _measure_perf
Definition: BMP280.hpp:72
void Run() override
Definition: BMP280.cpp:117
BMP280(bmp280::IBMP280 *interface)
Definition: BMP280.cpp:36
#define BMP280_CONFIG_F16
Definition: bmp280.h:74
void perf_print_counter(perf_counter_t handle)
Print one performance counter to stdout.
uint64_t perf_event_count(perf_counter_t handle)
Return current event_count.
Definition: bst.cpp:62
void perf_cancel(perf_counter_t handle)
Cancel a performance event.
#define BMP280_VALUE_RESET
Definition: bmp280.h:51
virtual ~BMP280()
Definition: BMP280.cpp:46
#define OK
Definition: uavcan_main.cpp:71
#define BMP280_ADDR_RESET
Definition: bmp280.h:47
P[0][0]
Definition: quatCovMat.c:44
perf_counter_t _comms_errors
Definition: BMP280.hpp:73
int measure()
Definition: BMP280.cpp:130
static constexpr uint32_t _measure_interval
Definition: BMP280.hpp:67
uint8_t t_xlsb
Definition: bmp280.h:111
uint8_t p_xlsb
Definition: bmp280.h:107
void perf_begin(perf_counter_t handle)
Begin a performance event.
#define BMP280_ADDR_DATA
Definition: bmp280.h:42
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
bool _collect_phase
Definition: BMP280.hpp:69