ms4525.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "ms4525.h"
33 
35 
36 static void cb(uint8_t result)
37 {
38  as_ptr->read_cb(result);
39 }
40 
42 
43 bool MS4525::init(I2C *_i2c)
44 {
45  as_ptr = this;
46  i2c_ = _i2c;
47  sensor_present_ = false;
48  uint8_t buf[1];
49  if (i2c_->read(ADDR, 0xFF, buf) == I2C::RESULT_SUCCESS)
50  sensor_present_ = true;
51  else
52  sensor_present_ = false;
53  next_update_ms_ = 0;
54  return sensor_present_;
55 }
56 
58 {
59  if (sensor_present_ && millis() > last_update_ms_ + 200)
60  sensor_present_ = false;
61  return sensor_present_;
62 }
63 
65 {
66  if (millis() > next_update_ms_)
67  {
68  if (i2c_->read(ADDR, 0xFF, 4, buf_, &cb) == I2C::RESULT_SUCCESS)
69  next_update_ms_ += 100;
70  }
71 }
72 
73 void MS4525::read_cb(uint8_t result)
74 {
75  if (result == I2C::RESULT_SUCCESS)
76  {
77  new_data_ = true;
78  sensor_present_ = true;
79  }
80  next_update_ms_ = millis() + 20;
82 }
83 
84 void MS4525::read(float* differential_pressure, float* temp)
85 {
86  if (new_data_)
87  {
88  uint8_t status = (buf_[0] & 0xC0) >> 6;
89  if(status == 0x00) // good data packet
90  {
91  int16_t raw_diff_pressure = 0x3FFF & ((buf_[0] << 8) + buf_[1]);
92  int16_t raw_temp = ( 0xFFE0 & ((buf_[2] << 8) + buf_[3])) >> 5;
93  // Convert to Pa and K
94  diff_press_ = -((static_cast<float>(raw_diff_pressure) - 1638.3f) / 6553.2f - 1.0f) * 6894.757f;
95  temp_ = ((200.0f * raw_temp) / 2047.0) - 50 ; // K
96  }
97  new_data_ = false;
98  }
99  (*differential_pressure) = diff_press_;
100  (*temp) = temp_;
101 }
102 
I2C * i2c_
Definition: ms4525.h:50
bool present()
Definition: ms4525.cpp:57
volatile uint32_t millis(void)
Definition: system.c:50
MS4525 * as_ptr
Definition: ms4525.cpp:34
bool sensor_present_
Definition: ms4525.h:57
static volatile uint8_t * status
Definition: drv_i2c.c:102
float temp_
Definition: ms4525.h:53
MS4525()
Definition: ms4525.cpp:41
static const uint8_t ADDR
Definition: ms4525.h:48
static void cb(uint8_t result)
Definition: ms4525.cpp:36
static volatile int16_t temp
Definition: drv_mpu6050.c:278
uint32_t last_update_ms_
Definition: ms4525.h:55
float diff_press_
Definition: ms4525.h:52
uint32_t next_update_ms_
Definition: ms4525.h:54
void read_cb(uint8_t result)
Definition: ms4525.cpp:73
void update()
Definition: ms4525.cpp:64
bool new_data_
Definition: ms4525.h:56
Definition: i2c.h:40
Definition: ms4525.h:35
int8_t read(uint8_t addr, uint8_t reg, uint8_t num_bytes, uint8_t *data, void(*callback)(uint8_t)=nullptr, bool blocking=false)
Definition: i2c.cpp:179
uint8_t buf_[4]
Definition: ms4525.h:51
bool init(I2C *_i2c)
Definition: ms4525.cpp:43
void read(float *differential_pressure, float *temp)
Definition: ms4525.cpp:84


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Oct 24 2019 03:17:19