2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-01-04 09:59:19 -04:00
|
|
|
#include "AP_Baro_MS5611.h"
|
2013-08-29 02:34:34 -03:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
#include <utility>
|
2016-11-09 05:42:29 -04:00
|
|
|
#include <stdio.h>
|
2014-10-19 16:22:51 -03:00
|
|
|
|
2016-07-25 10:33:28 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2019-04-24 03:51:54 -03:00
|
|
|
#include <AP_Math/crc.h>
|
2016-07-25 10:33:28 -03:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
extern const AP_HAL::HAL &hal;
|
2011-11-27 01:43:34 -04:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
static const uint8_t CMD_MS56XX_RESET = 0x1E;
|
|
|
|
static const uint8_t CMD_MS56XX_READ_ADC = 0x00;
|
2011-11-27 01:43:34 -04:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
/* PROM start address */
|
|
|
|
static const uint8_t CMD_MS56XX_PROM = 0xA0;
|
2011-11-27 01:49:17 -04:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
/* write to one of these addresses to start pressure conversion */
|
|
|
|
#define ADDR_CMD_CONVERT_D1_OSR256 0x40
|
|
|
|
#define ADDR_CMD_CONVERT_D1_OSR512 0x42
|
|
|
|
#define ADDR_CMD_CONVERT_D1_OSR1024 0x44
|
|
|
|
#define ADDR_CMD_CONVERT_D1_OSR2048 0x46
|
|
|
|
#define ADDR_CMD_CONVERT_D1_OSR4096 0x48
|
2015-11-09 17:25:42 -04:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
/* write to one of these addresses to start temperature conversion */
|
|
|
|
#define ADDR_CMD_CONVERT_D2_OSR256 0x50
|
|
|
|
#define ADDR_CMD_CONVERT_D2_OSR512 0x52
|
|
|
|
#define ADDR_CMD_CONVERT_D2_OSR1024 0x54
|
|
|
|
#define ADDR_CMD_CONVERT_D2_OSR2048 0x56
|
|
|
|
#define ADDR_CMD_CONVERT_D2_OSR4096 0x58
|
2015-11-09 17:25:42 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
use an OSR of 1024 to reduce the self-heating effect of the
|
|
|
|
sensor. Information from MS tells us that some individual sensors
|
|
|
|
are quite sensitive to this effect and that reducing the OSR can
|
|
|
|
make a big difference
|
|
|
|
*/
|
2016-01-04 09:59:19 -04:00
|
|
|
static const uint8_t ADDR_CMD_CONVERT_PRESSURE = ADDR_CMD_CONVERT_D1_OSR1024;
|
|
|
|
static const uint8_t ADDR_CMD_CONVERT_TEMPERATURE = ADDR_CMD_CONVERT_D2_OSR1024;
|
2016-11-04 01:36:03 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
2016-11-24 20:59:14 -04:00
|
|
|
AP_Baro_MS56XX::AP_Baro_MS56XX(AP_Baro &baro, AP_HAL::OwnPtr<AP_HAL::Device> dev, enum MS56XX_TYPE ms56xx_type)
|
2016-01-02 10:01:42 -04:00
|
|
|
: AP_Baro_Backend(baro)
|
2016-01-04 09:59:19 -04:00
|
|
|
, _dev(std::move(dev))
|
2016-11-24 20:59:14 -04:00
|
|
|
, _ms56xx_type(ms56xx_type)
|
2015-11-04 18:17:38 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-24 20:59:14 -04:00
|
|
|
AP_Baro_Backend *AP_Baro_MS56XX::probe(AP_Baro &baro,
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> dev,
|
|
|
|
enum MS56XX_TYPE ms56xx_type)
|
|
|
|
{
|
|
|
|
if (!dev) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
AP_Baro_MS56XX *sensor = new AP_Baro_MS56XX(baro, std::move(dev), ms56xx_type);
|
|
|
|
if (!sensor || !sensor->_init()) {
|
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Baro_MS56XX::_init()
|
2014-10-19 16:22:51 -03:00
|
|
|
{
|
2016-01-04 09:59:19 -04:00
|
|
|
if (!_dev) {
|
2016-11-24 20:59:14 -04:00
|
|
|
return false;
|
2016-01-04 09:59:19 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 17:42:32 -04:00
|
|
|
_dev->get_semaphore()->take_blocking();
|
2014-10-19 16:22:51 -03:00
|
|
|
|
2016-12-01 00:00:49 -04:00
|
|
|
// high retries for init
|
|
|
|
_dev->set_retries(10);
|
|
|
|
|
2015-11-26 10:56:10 -04:00
|
|
|
uint16_t prom[8];
|
2016-11-24 20:59:14 -04:00
|
|
|
bool prom_read_ok = false;
|
2016-12-01 05:59:50 -04:00
|
|
|
|
2016-12-12 20:10:35 -04:00
|
|
|
_dev->transfer(&CMD_MS56XX_RESET, 1, nullptr, 0);
|
|
|
|
hal.scheduler->delay(4);
|
|
|
|
|
2016-12-01 05:59:50 -04:00
|
|
|
const char *name = "MS5611";
|
2016-11-24 20:59:14 -04:00
|
|
|
switch (_ms56xx_type) {
|
|
|
|
case BARO_MS5607:
|
2016-12-01 05:59:50 -04:00
|
|
|
name = "MS5607";
|
2017-08-22 14:28:10 -03:00
|
|
|
FALLTHROUGH;
|
2016-11-24 20:59:14 -04:00
|
|
|
case BARO_MS5611:
|
|
|
|
prom_read_ok = _read_prom_5611(prom);
|
|
|
|
break;
|
2017-02-03 00:18:25 -04:00
|
|
|
case BARO_MS5837:
|
|
|
|
name = "MS5837";
|
|
|
|
prom_read_ok = _read_prom_5637(prom);
|
|
|
|
break;
|
2016-11-24 20:59:14 -04:00
|
|
|
case BARO_MS5637:
|
2016-12-01 05:59:50 -04:00
|
|
|
name = "MS5637";
|
2016-11-24 20:59:14 -04:00
|
|
|
prom_read_ok = _read_prom_5637(prom);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prom_read_ok) {
|
2016-11-09 05:42:29 -04:00
|
|
|
_dev->get_semaphore()->give();
|
2016-11-24 20:59:14 -04:00
|
|
|
return false;
|
2014-10-19 16:22:51 -03:00
|
|
|
}
|
|
|
|
|
2016-12-01 05:59:50 -04:00
|
|
|
printf("%s found on bus %u address 0x%02x\n", name, _dev->bus_num(), _dev->get_bus_address());
|
|
|
|
|
2015-11-26 10:56:10 -04:00
|
|
|
// Save factory calibration coefficients
|
2016-07-25 13:52:33 -03:00
|
|
|
_cal_reg.c1 = prom[1];
|
|
|
|
_cal_reg.c2 = prom[2];
|
|
|
|
_cal_reg.c3 = prom[3];
|
|
|
|
_cal_reg.c4 = prom[4];
|
|
|
|
_cal_reg.c5 = prom[5];
|
|
|
|
_cal_reg.c6 = prom[6];
|
2015-11-26 10:56:10 -04:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
// Send a command to read temperature first
|
|
|
|
_dev->transfer(&ADDR_CMD_CONVERT_TEMPERATURE, 1, nullptr, 0);
|
2014-10-19 16:22:51 -03:00
|
|
|
_state = 0;
|
|
|
|
|
2016-07-25 10:33:28 -03:00
|
|
|
memset(&_accum, 0, sizeof(_accum));
|
2014-10-19 16:22:51 -03:00
|
|
|
|
2016-11-09 05:42:29 -04:00
|
|
|
_instance = _frontend.register_sensor();
|
|
|
|
|
2020-07-18 04:12:41 -03:00
|
|
|
_dev->set_device_type(DEVTYPE_BARO_MS5611);
|
|
|
|
set_bus_id(_instance, _dev->get_bus_id());
|
|
|
|
|
2017-02-03 00:18:25 -04:00
|
|
|
if (_ms56xx_type == BARO_MS5837) {
|
|
|
|
_frontend.set_type(_instance, AP_Baro::BARO_TYPE_WATER);
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:00:49 -04:00
|
|
|
// lower retries for run
|
|
|
|
_dev->set_retries(3);
|
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
_dev->get_semaphore()->give();
|
2014-10-19 16:22:51 -03:00
|
|
|
|
2016-07-25 10:33:28 -03:00
|
|
|
/* Request 100Hz update */
|
2017-05-05 00:13:14 -03:00
|
|
|
_dev->register_periodic_callback(10 * AP_USEC_PER_MSEC,
|
2017-01-13 15:26:14 -04:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_Baro_MS56XX::_timer, void));
|
2016-11-24 20:59:14 -04:00
|
|
|
return true;
|
2014-10-19 16:22:51 -03:00
|
|
|
}
|
2013-01-03 14:06:22 -04:00
|
|
|
|
2016-01-04 09:59:19 -04:00
|
|
|
uint16_t AP_Baro_MS56XX::_read_prom_word(uint8_t word)
|
|
|
|
{
|
|
|
|
const uint8_t reg = CMD_MS56XX_PROM + (word << 1);
|
|
|
|
uint8_t val[2];
|
2017-01-12 08:35:11 -04:00
|
|
|
if (!_dev->transfer(®, 1, val, sizeof(val))) {
|
2016-01-04 09:59:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (val[0] << 8) | val[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AP_Baro_MS56XX::_read_adc()
|
|
|
|
{
|
|
|
|
uint8_t val[3];
|
2017-01-12 08:35:11 -04:00
|
|
|
if (!_dev->transfer(&CMD_MS56XX_READ_ADC, 1, val, sizeof(val))) {
|
2016-01-04 09:59:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (val[0] << 16) | (val[1] << 8) | val[2];
|
|
|
|
}
|
|
|
|
|
2016-11-24 20:59:14 -04:00
|
|
|
bool AP_Baro_MS56XX::_read_prom_5611(uint16_t prom[8])
|
2015-11-26 10:56:10 -04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* MS5611-01BA datasheet, CYCLIC REDUNDANCY CHECK (CRC): "MS5611-01BA
|
|
|
|
* contains a PROM memory with 128-Bit. A 4-bit CRC has been implemented
|
|
|
|
* to check the data validity in memory."
|
|
|
|
*
|
|
|
|
* CRC field must me removed for CRC-4 calculation.
|
|
|
|
*/
|
2016-11-24 20:59:14 -04:00
|
|
|
bool all_zero = true;
|
2015-11-26 10:56:10 -04:00
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
2016-01-04 09:59:19 -04:00
|
|
|
prom[i] = _read_prom_word(i);
|
2016-11-24 20:59:14 -04:00
|
|
|
if (prom[i] != 0) {
|
|
|
|
all_zero = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_zero) {
|
|
|
|
return false;
|
2015-11-26 10:56:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* save the read crc */
|
|
|
|
const uint16_t crc_read = prom[7] & 0xf;
|
|
|
|
|
|
|
|
/* remove CRC byte */
|
|
|
|
prom[7] &= 0xff00;
|
|
|
|
|
2019-04-24 03:51:54 -03:00
|
|
|
return crc_read == crc_crc4(prom);
|
2015-11-26 10:56:10 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 20:59:14 -04:00
|
|
|
bool AP_Baro_MS56XX::_read_prom_5637(uint16_t prom[8])
|
2015-11-11 19:00:57 -04:00
|
|
|
{
|
2015-11-26 10:56:10 -04:00
|
|
|
/*
|
|
|
|
* MS5637-02BA03 datasheet, CYCLIC REDUNDANCY CHECK (CRC): "MS5637
|
|
|
|
* contains a PROM memory with 112-Bit. A 4-bit CRC has been implemented
|
|
|
|
* to check the data validity in memory."
|
|
|
|
*
|
|
|
|
* 8th PROM word must be zeroed and CRC field removed for CRC-4
|
|
|
|
* calculation.
|
|
|
|
*/
|
2016-11-24 20:59:14 -04:00
|
|
|
bool all_zero = true;
|
2015-11-26 10:56:10 -04:00
|
|
|
for (uint8_t i = 0; i < 7; i++) {
|
2016-01-04 09:59:19 -04:00
|
|
|
prom[i] = _read_prom_word(i);
|
2016-11-24 20:59:14 -04:00
|
|
|
if (prom[i] != 0) {
|
|
|
|
all_zero = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_zero) {
|
|
|
|
return false;
|
2015-11-26 10:56:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
prom[7] = 0;
|
2015-11-11 19:00:57 -04:00
|
|
|
|
|
|
|
/* save the read crc */
|
2015-11-26 10:56:10 -04:00
|
|
|
const uint16_t crc_read = (prom[0] & 0xf000) >> 12;
|
2015-11-11 19:00:57 -04:00
|
|
|
|
|
|
|
/* remove CRC byte */
|
2015-11-26 10:56:10 -04:00
|
|
|
prom[0] &= ~0xf000;
|
2014-07-07 00:11:41 -03:00
|
|
|
|
2019-04-24 03:51:54 -03:00
|
|
|
return crc_read == crc_crc4(prom);
|
2014-07-07 00:11:41 -03:00
|
|
|
}
|
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
/*
|
2016-07-25 10:33:28 -03:00
|
|
|
* Read the sensor with a state machine
|
|
|
|
* We read one time temperature (state=0) and then 4 times pressure (states 1-4)
|
|
|
|
*
|
|
|
|
* Temperature is used to calculate the compensated pressure and doesn't vary
|
|
|
|
* as fast as pressure. Hence we reuse the same temperature for 4 samples of
|
|
|
|
* pressure.
|
2014-10-19 16:22:51 -03:00
|
|
|
*/
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_Baro_MS56XX::_timer(void)
|
2011-11-05 22:11:25 -03:00
|
|
|
{
|
2016-07-25 10:33:28 -03:00
|
|
|
uint8_t next_cmd;
|
|
|
|
uint8_t next_state;
|
|
|
|
uint32_t adc_val = _read_adc();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If read fails, re-initiate a read command for current state or we are
|
|
|
|
* stuck
|
|
|
|
*/
|
|
|
|
if (adc_val == 0) {
|
|
|
|
next_state = _state;
|
|
|
|
} else {
|
|
|
|
next_state = (_state + 1) % 5;
|
2013-01-09 05:27:48 -04:00
|
|
|
}
|
2012-02-14 12:55:32 -04:00
|
|
|
|
2016-07-25 10:33:28 -03:00
|
|
|
next_cmd = next_state == 0 ? ADDR_CMD_CONVERT_TEMPERATURE
|
|
|
|
: ADDR_CMD_CONVERT_PRESSURE;
|
2016-11-13 02:08:06 -04:00
|
|
|
if (!_dev->transfer(&next_cmd, 1, nullptr, 0)) {
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-11-13 02:08:06 -04:00
|
|
|
}
|
2015-08-17 13:44:46 -03:00
|
|
|
|
2016-07-25 10:33:28 -03:00
|
|
|
/* if we had a failed read we are all done */
|
2019-07-30 21:30:50 -03:00
|
|
|
if (adc_val == 0 || adc_val == 0xFFFFFF) {
|
2016-11-24 20:59:14 -04:00
|
|
|
// a failed read can mean the next returned value will be
|
2019-07-30 21:30:50 -03:00
|
|
|
// corrupt, we must discard it. This copes with MISO being
|
|
|
|
// pulled either high or low
|
2016-11-13 02:08:06 -04:00
|
|
|
_discard_next = true;
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-11-13 02:08:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_discard_next) {
|
|
|
|
_discard_next = false;
|
|
|
|
_state = next_state;
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-07-25 10:33:28 -03:00
|
|
|
}
|
|
|
|
|
2018-08-15 23:53:17 -03:00
|
|
|
WITH_SEMAPHORE(_sem);
|
|
|
|
|
|
|
|
if (_state == 0) {
|
|
|
|
_update_and_wrap_accumulator(&_accum.s_D2, adc_val,
|
|
|
|
&_accum.d2_count, 32);
|
|
|
|
} else if (pressure_ok(adc_val)) {
|
|
|
|
_update_and_wrap_accumulator(&_accum.s_D1, adc_val,
|
|
|
|
&_accum.d1_count, 128);
|
2011-12-21 08:22:37 -04:00
|
|
|
}
|
2018-08-15 23:53:17 -03:00
|
|
|
|
|
|
|
_state = next_state;
|
2011-12-09 02:35:40 -04:00
|
|
|
}
|
|
|
|
|
2016-07-25 10:33:28 -03:00
|
|
|
void AP_Baro_MS56XX::_update_and_wrap_accumulator(uint32_t *accum, uint32_t val,
|
|
|
|
uint8_t *count, uint8_t max_count)
|
2011-12-09 02:35:40 -04:00
|
|
|
{
|
2016-07-25 10:33:28 -03:00
|
|
|
*accum += val;
|
|
|
|
*count += 1;
|
|
|
|
if (*count == max_count) {
|
|
|
|
*count = max_count / 2;
|
|
|
|
*accum = *accum / 2;
|
2011-12-09 02:35:40 -04:00
|
|
|
}
|
2016-07-25 10:33:28 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Baro_MS56XX::update()
|
|
|
|
{
|
2014-10-19 16:22:51 -03:00
|
|
|
uint32_t sD1, sD2;
|
|
|
|
uint8_t d1count, d2count;
|
|
|
|
|
2018-08-15 23:53:17 -03:00
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(_sem);
|
2016-07-25 10:33:28 -03:00
|
|
|
|
2018-08-15 23:53:17 -03:00
|
|
|
if (_accum.d1_count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-25 10:33:28 -03:00
|
|
|
|
2018-08-15 23:53:17 -03:00
|
|
|
sD1 = _accum.s_D1;
|
|
|
|
sD2 = _accum.s_D2;
|
|
|
|
d1count = _accum.d1_count;
|
|
|
|
d2count = _accum.d2_count;
|
|
|
|
memset(&_accum, 0, sizeof(_accum));
|
|
|
|
}
|
2015-09-28 15:43:04 -03:00
|
|
|
|
2014-10-19 16:22:51 -03:00
|
|
|
if (d1count != 0) {
|
2015-07-10 00:56:06 -03:00
|
|
|
_D1 = ((float)sD1) / d1count;
|
2014-10-19 16:22:51 -03:00
|
|
|
}
|
|
|
|
if (d2count != 0) {
|
2015-07-10 00:56:06 -03:00
|
|
|
_D2 = ((float)sD2) / d2count;
|
2012-06-19 23:25:19 -03:00
|
|
|
}
|
2011-11-05 22:11:25 -03:00
|
|
|
|
2016-11-24 20:59:14 -04:00
|
|
|
switch (_ms56xx_type) {
|
|
|
|
case BARO_MS5607:
|
|
|
|
_calculate_5607();
|
|
|
|
break;
|
|
|
|
case BARO_MS5611:
|
|
|
|
_calculate_5611();
|
|
|
|
break;
|
|
|
|
case BARO_MS5637:
|
|
|
|
_calculate_5637();
|
|
|
|
break;
|
2017-02-03 00:18:25 -04:00
|
|
|
case BARO_MS5837:
|
|
|
|
_calculate_5837();
|
2016-11-24 20:59:14 -04:00
|
|
|
}
|
2015-11-04 18:17:38 -04:00
|
|
|
}
|
2015-07-10 00:56:06 -03:00
|
|
|
|
2011-11-27 01:43:34 -04:00
|
|
|
// Calculate Temperature and compensated Pressure in real units (Celsius degrees*100, mbar*100).
|
2016-11-24 20:59:14 -04:00
|
|
|
void AP_Baro_MS56XX::_calculate_5611()
|
2011-11-05 22:11:25 -03:00
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
float dT;
|
|
|
|
float TEMP;
|
|
|
|
float OFF;
|
|
|
|
float SENS;
|
2011-11-27 01:43:34 -04:00
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
// Formulas from manufacturer datasheet
|
2015-09-28 15:43:04 -03:00
|
|
|
// sub -15c temperature compensation is not included
|
2012-07-06 02:11:22 -03:00
|
|
|
|
2015-11-03 09:46:29 -04:00
|
|
|
// we do the calculations using floating point allows us to take advantage
|
|
|
|
// of the averaging of D1 and D1 over multiple samples, giving us more
|
|
|
|
// precision
|
2016-07-25 13:52:33 -03:00
|
|
|
dT = _D2-(((uint32_t)_cal_reg.c5)<<8);
|
|
|
|
TEMP = (dT * _cal_reg.c6)/8388608;
|
|
|
|
OFF = _cal_reg.c2 * 65536.0f + (_cal_reg.c4 * dT) / 128;
|
|
|
|
SENS = _cal_reg.c1 * 32768.0f + (_cal_reg.c3 * dT) / 256;
|
2012-07-06 02:11:22 -03:00
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
if (TEMP < 0) {
|
2012-07-06 02:11:22 -03:00
|
|
|
// second order temperature compensation when under 20 degrees C
|
2012-08-17 03:09:23 -03:00
|
|
|
float T2 = (dT*dT) / 0x80000000;
|
|
|
|
float Aux = TEMP*TEMP;
|
2013-01-10 14:42:24 -04:00
|
|
|
float OFF2 = 2.5f*Aux;
|
|
|
|
float SENS2 = 1.25f*Aux;
|
2012-08-17 03:09:23 -03:00
|
|
|
TEMP = TEMP - T2;
|
|
|
|
OFF = OFF - OFF2;
|
|
|
|
SENS = SENS - SENS2;
|
|
|
|
}
|
|
|
|
|
2015-07-10 00:56:06 -03:00
|
|
|
float pressure = (_D1*SENS/2097152 - OFF)/32768;
|
|
|
|
float temperature = (TEMP + 2000) * 0.01f;
|
|
|
|
_copy_to_frontend(_instance, pressure, temperature);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate Temperature and compensated Pressure in real units (Celsius degrees*100, mbar*100).
|
2016-11-24 20:59:14 -04:00
|
|
|
void AP_Baro_MS56XX::_calculate_5607()
|
2015-07-10 00:56:06 -03:00
|
|
|
{
|
|
|
|
float dT;
|
|
|
|
float TEMP;
|
|
|
|
float OFF;
|
|
|
|
float SENS;
|
|
|
|
|
|
|
|
// Formulas from manufacturer datasheet
|
2015-09-28 15:43:04 -03:00
|
|
|
// sub -15c temperature compensation is not included
|
2015-07-10 00:56:06 -03:00
|
|
|
|
2015-11-03 09:46:29 -04:00
|
|
|
// we do the calculations using floating point allows us to take advantage
|
|
|
|
// of the averaging of D1 and D1 over multiple samples, giving us more
|
|
|
|
// precision
|
2016-07-25 13:52:33 -03:00
|
|
|
dT = _D2-(((uint32_t)_cal_reg.c5)<<8);
|
|
|
|
TEMP = (dT * _cal_reg.c6)/8388608;
|
|
|
|
OFF = _cal_reg.c2 * 131072.0f + (_cal_reg.c4 * dT) / 64;
|
|
|
|
SENS = _cal_reg.c1 * 65536.0f + (_cal_reg.c3 * dT) / 128;
|
2015-07-10 00:56:06 -03:00
|
|
|
|
|
|
|
if (TEMP < 0) {
|
|
|
|
// second order temperature compensation when under 20 degrees C
|
|
|
|
float T2 = (dT*dT) / 0x80000000;
|
|
|
|
float Aux = TEMP*TEMP;
|
|
|
|
float OFF2 = 61.0f*Aux/16.0f;
|
|
|
|
float SENS2 = 2.0f*Aux;
|
|
|
|
TEMP = TEMP - T2;
|
|
|
|
OFF = OFF - OFF2;
|
|
|
|
SENS = SENS - SENS2;
|
|
|
|
}
|
|
|
|
|
|
|
|
float pressure = (_D1*SENS/2097152 - OFF)/32768;
|
2014-10-19 16:22:51 -03:00
|
|
|
float temperature = (TEMP + 2000) * 0.01f;
|
|
|
|
_copy_to_frontend(_instance, pressure, temperature);
|
2011-11-05 22:11:25 -03:00
|
|
|
}
|
2015-01-06 01:28:11 -04:00
|
|
|
|
2015-09-28 15:31:12 -03:00
|
|
|
// Calculate Temperature and compensated Pressure in real units (Celsius degrees*100, mbar*100).
|
2016-11-24 20:59:14 -04:00
|
|
|
void AP_Baro_MS56XX::_calculate_5637()
|
2015-09-28 15:31:12 -03:00
|
|
|
{
|
|
|
|
int32_t dT, TEMP;
|
|
|
|
int64_t OFF, SENS;
|
|
|
|
int32_t raw_pressure = _D1;
|
|
|
|
int32_t raw_temperature = _D2;
|
|
|
|
|
|
|
|
// Formulas from manufacturer datasheet
|
|
|
|
// sub -15c temperature compensation is not included
|
|
|
|
|
2016-07-25 13:52:33 -03:00
|
|
|
dT = raw_temperature - (((uint32_t)_cal_reg.c5) << 8);
|
|
|
|
TEMP = 2000 + ((int64_t)dT * (int64_t)_cal_reg.c6) / 8388608;
|
|
|
|
OFF = (int64_t)_cal_reg.c2 * (int64_t)131072 + ((int64_t)_cal_reg.c4 * (int64_t)dT) / (int64_t)64;
|
|
|
|
SENS = (int64_t)_cal_reg.c1 * (int64_t)65536 + ((int64_t)_cal_reg.c3 * (int64_t)dT) / (int64_t)128;
|
2015-09-28 15:31:12 -03:00
|
|
|
|
|
|
|
if (TEMP < 2000) {
|
|
|
|
// second order temperature compensation when under 20 degrees C
|
|
|
|
int32_t T2 = ((int64_t)3 * ((int64_t)dT * (int64_t)dT) / (int64_t)8589934592);
|
|
|
|
int64_t aux = (TEMP - 2000) * (TEMP - 2000);
|
|
|
|
int64_t OFF2 = 61 * aux / 16;
|
|
|
|
int64_t SENS2 = 29 * aux / 16;
|
|
|
|
|
|
|
|
TEMP = TEMP - T2;
|
|
|
|
OFF = OFF - OFF2;
|
|
|
|
SENS = SENS - SENS2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t pressure = ((int64_t)raw_pressure * SENS / (int64_t)2097152 - OFF) / (int64_t)32768;
|
|
|
|
float temperature = TEMP * 0.01f;
|
|
|
|
_copy_to_frontend(_instance, (float)pressure, temperature);
|
|
|
|
}
|
2017-02-03 00:18:25 -04:00
|
|
|
|
|
|
|
// Calculate Temperature and compensated Pressure in real units (Celsius degrees*100, mbar*100).
|
|
|
|
void AP_Baro_MS56XX::_calculate_5837()
|
|
|
|
{
|
|
|
|
int32_t dT, TEMP;
|
|
|
|
int64_t OFF, SENS;
|
|
|
|
int32_t raw_pressure = _D1;
|
|
|
|
int32_t raw_temperature = _D2;
|
|
|
|
|
|
|
|
// Formulas from manufacturer datasheet
|
|
|
|
// sub -15c temperature compensation is not included
|
|
|
|
|
|
|
|
dT = raw_temperature - (((uint32_t)_cal_reg.c5) << 8);
|
|
|
|
TEMP = 2000 + ((int64_t)dT * (int64_t)_cal_reg.c6) / 8388608;
|
|
|
|
OFF = (int64_t)_cal_reg.c2 * (int64_t)65536 + ((int64_t)_cal_reg.c4 * (int64_t)dT) / (int64_t)128;
|
|
|
|
SENS = (int64_t)_cal_reg.c1 * (int64_t)32768 + ((int64_t)_cal_reg.c3 * (int64_t)dT) / (int64_t)256;
|
|
|
|
|
|
|
|
if (TEMP < 2000) {
|
|
|
|
// second order temperature compensation when under 20 degrees C
|
|
|
|
int32_t T2 = ((int64_t)3 * ((int64_t)dT * (int64_t)dT) / (int64_t)8589934592);
|
|
|
|
int64_t aux = (TEMP - 2000) * (TEMP - 2000);
|
|
|
|
int64_t OFF2 = 3 * aux / 2;
|
|
|
|
int64_t SENS2 = 5 * aux / 8;
|
|
|
|
|
|
|
|
TEMP = TEMP - T2;
|
|
|
|
OFF = OFF - OFF2;
|
|
|
|
SENS = SENS - SENS2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t pressure = ((int64_t)raw_pressure * SENS / (int64_t)2097152 - OFF) / (int64_t)8192;
|
|
|
|
pressure = pressure * 10; // MS5837 only reports to 0.1 mbar
|
|
|
|
float temperature = TEMP * 0.01f;
|
2018-03-12 04:58:10 -03:00
|
|
|
|
2017-02-03 00:18:25 -04:00
|
|
|
_copy_to_frontend(_instance, (float)pressure, temperature);
|
|
|
|
}
|