2011-12-28 05:32:21 -04:00
|
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-27 01:43:34 -04:00
|
|
|
|
/*
|
2012-08-17 03:09:23 -03:00
|
|
|
|
* APM_MS5611.cpp - Arduino Library for MS5611-01BA01 absolute pressure sensor
|
|
|
|
|
* Code by Jose Julio, Pat Hickey and Jordi Muñoz. DIYDrones.com
|
|
|
|
|
*
|
|
|
|
|
* Sensor is conected to standard SPI port
|
|
|
|
|
* Chip Select pin: Analog2 (provisional until Jordi defines the pin)!!
|
|
|
|
|
*
|
|
|
|
|
* Variables:
|
2013-09-21 08:30:41 -03:00
|
|
|
|
* Temp : Calculated temperature (in Celsius degrees)
|
2012-08-17 03:09:23 -03:00
|
|
|
|
* Press : Calculated pressure (in mbar units * 100)
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Methods:
|
|
|
|
|
* init() : Initialization and sensor reset
|
|
|
|
|
* read() : Read sensor data and _calculate Temperature, Pressure
|
|
|
|
|
* This function is optimized so the main host don´t need to wait
|
|
|
|
|
* You can call this function in your main loop
|
|
|
|
|
* Maximum data output frequency 100Hz - this allows maximum oversampling in the chip ADC
|
|
|
|
|
* It returns a 1 if there are new data.
|
|
|
|
|
* get_pressure() : return pressure in mbar*100 units
|
|
|
|
|
* get_temperature() : return temperature in celsius degrees*100 units
|
|
|
|
|
*
|
|
|
|
|
* Internal functions:
|
|
|
|
|
* _calculate() : Calculate Temperature and Pressure (temperature compensated) in real units
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2011-11-27 01:43:34 -04:00
|
|
|
|
|
2012-10-11 14:53:21 -03:00
|
|
|
|
#include <AP_HAL.h>
|
2011-11-27 01:49:40 -04:00
|
|
|
|
#include "AP_Baro_MS5611.h"
|
2011-11-27 01:43:34 -04:00
|
|
|
|
|
2012-10-11 14:53:21 -03:00
|
|
|
|
extern const AP_HAL::HAL& hal;
|
2011-11-27 01:49:17 -04:00
|
|
|
|
|
|
|
|
|
#define CMD_MS5611_RESET 0x1E
|
|
|
|
|
#define CMD_MS5611_PROM_Setup 0xA0
|
|
|
|
|
#define CMD_MS5611_PROM_C1 0xA2
|
|
|
|
|
#define CMD_MS5611_PROM_C2 0xA4
|
|
|
|
|
#define CMD_MS5611_PROM_C3 0xA6
|
|
|
|
|
#define CMD_MS5611_PROM_C4 0xA8
|
|
|
|
|
#define CMD_MS5611_PROM_C5 0xAA
|
|
|
|
|
#define CMD_MS5611_PROM_C6 0xAC
|
|
|
|
|
#define CMD_MS5611_PROM_CRC 0xAE
|
2012-02-12 20:00:06 -04:00
|
|
|
|
#define CMD_CONVERT_D1_OSR4096 0x48 // Maximum resolution (oversampling)
|
|
|
|
|
#define CMD_CONVERT_D2_OSR4096 0x58 // Maximum resolution (oversampling)
|
2011-11-27 01:49:17 -04:00
|
|
|
|
|
2012-07-02 00:44:02 -03:00
|
|
|
|
uint32_t volatile AP_Baro_MS5611::_s_D1;
|
|
|
|
|
uint32_t volatile AP_Baro_MS5611::_s_D2;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
uint8_t volatile AP_Baro_MS5611::_d1_count;
|
|
|
|
|
uint8_t volatile AP_Baro_MS5611::_d2_count;
|
|
|
|
|
uint8_t AP_Baro_MS5611::_state;
|
2012-02-13 06:39:18 -04:00
|
|
|
|
uint32_t AP_Baro_MS5611::_timer;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
bool volatile AP_Baro_MS5611::_updated;
|
2011-11-27 01:49:17 -04:00
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
|
AP_Baro_MS5611_Serial* AP_Baro_MS5611::_serial = NULL;
|
|
|
|
|
AP_Baro_MS5611_SPI AP_Baro_MS5611::spi;
|
2014-07-08 00:27:06 -03:00
|
|
|
|
#if MS5611_WITH_I2C
|
2013-01-03 14:06:22 -04:00
|
|
|
|
AP_Baro_MS5611_I2C AP_Baro_MS5611::i2c;
|
2014-07-08 00:27:06 -03:00
|
|
|
|
#endif
|
2012-11-19 21:23:26 -04:00
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
|
// SPI Device //////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void AP_Baro_MS5611_SPI::init()
|
2011-11-05 22:11:25 -03:00
|
|
|
|
{
|
2013-01-03 14:06:22 -04:00
|
|
|
|
_spi = hal.spi->device(AP_HAL::SPIDevice_MS5611);
|
|
|
|
|
if (_spi == NULL) {
|
2013-01-03 15:05:00 -04:00
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: AP_Baro_MS5611 did not get "
|
2013-01-03 14:06:22 -04:00
|
|
|
|
"valid SPI device driver!"));
|
2013-01-03 15:05:00 -04:00
|
|
|
|
return; /* never reached */
|
2013-01-03 14:06:22 -04:00
|
|
|
|
}
|
|
|
|
|
_spi_sem = _spi->get_semaphore();
|
2013-01-03 15:05:00 -04:00
|
|
|
|
if (_spi_sem == NULL) {
|
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: AP_Baro_MS5611 did not get "
|
|
|
|
|
"valid SPI semaphroe!"));
|
|
|
|
|
return; /* never reached */
|
2014-07-23 09:45:58 -03:00
|
|
|
|
|
2013-01-03 15:05:00 -04:00
|
|
|
|
}
|
2013-10-29 19:00:51 -03:00
|
|
|
|
|
|
|
|
|
// now that we have initialised, we set the SPI bus speed to high
|
|
|
|
|
// (8MHz on APM2)
|
|
|
|
|
_spi->set_bus_speed(AP_HAL::SPIDeviceDriver::SPI_SPEED_HIGH);
|
2011-11-05 22:11:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
|
uint16_t AP_Baro_MS5611_SPI::read_16bits(uint8_t reg)
|
2011-11-05 22:11:25 -03:00
|
|
|
|
{
|
2012-12-17 22:11:57 -04:00
|
|
|
|
uint8_t tx[3];
|
|
|
|
|
uint8_t rx[3];
|
|
|
|
|
tx[0] = reg; tx[1] = 0; tx[2] = 0;
|
|
|
|
|
_spi->transaction(tx, rx, 3);
|
|
|
|
|
return ((uint16_t) rx[1] << 8 ) | ( rx[2] );
|
2011-11-27 01:43:34 -04:00
|
|
|
|
}
|
2011-11-05 22:11:25 -03:00
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
|
uint32_t AP_Baro_MS5611_SPI::read_adc()
|
2011-11-27 01:43:34 -04:00
|
|
|
|
{
|
2012-12-17 22:11:57 -04:00
|
|
|
|
uint8_t tx[4];
|
|
|
|
|
uint8_t rx[4];
|
|
|
|
|
memset(tx, 0, 4); /* first byte is addr = 0 */
|
|
|
|
|
_spi->transaction(tx, rx, 4);
|
|
|
|
|
return (((uint32_t)rx[1])<<16) | (((uint32_t)rx[2])<<8) | ((uint32_t)rx[3]);
|
2011-11-27 01:43:34 -04:00
|
|
|
|
}
|
2011-11-05 22:11:25 -03:00
|
|
|
|
|
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
|
void AP_Baro_MS5611_SPI::write(uint8_t reg)
|
2011-11-27 01:43:34 -04:00
|
|
|
|
{
|
2012-12-17 22:11:57 -04:00
|
|
|
|
uint8_t tx[1];
|
|
|
|
|
tx[0] = reg;
|
|
|
|
|
_spi->transaction(tx, NULL, 1);
|
2011-11-05 22:11:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 15:05:00 -04:00
|
|
|
|
bool AP_Baro_MS5611_SPI::sem_take_blocking() {
|
|
|
|
|
return _spi_sem->take(10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AP_Baro_MS5611_SPI::sem_take_nonblocking()
|
2013-01-03 14:06:22 -04:00
|
|
|
|
{
|
2013-01-03 15:05:00 -04:00
|
|
|
|
/**
|
|
|
|
|
* Take nonblocking from a TimerProcess context &
|
|
|
|
|
* monitor for bad failures
|
|
|
|
|
*/
|
2013-01-03 14:06:22 -04:00
|
|
|
|
static int semfail_ctr = 0;
|
2013-01-03 15:05:00 -04:00
|
|
|
|
bool got = _spi_sem->take_nonblocking();
|
|
|
|
|
if (!got) {
|
2013-01-10 18:22:41 -04:00
|
|
|
|
if (!hal.scheduler->system_initializing()) {
|
|
|
|
|
semfail_ctr++;
|
|
|
|
|
if (semfail_ctr > 100) {
|
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: failed to take _spi_sem "
|
|
|
|
|
"100 times in a row, in "
|
|
|
|
|
"AP_Baro_MS5611::_update"));
|
|
|
|
|
}
|
2013-01-03 14:06:22 -04:00
|
|
|
|
}
|
2013-01-03 15:05:00 -04:00
|
|
|
|
return false; /* never reached */
|
|
|
|
|
} else {
|
|
|
|
|
semfail_ctr = 0;
|
2013-01-03 14:06:22 -04:00
|
|
|
|
}
|
2013-01-03 15:05:00 -04:00
|
|
|
|
return got;
|
2013-01-03 14:06:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 15:05:00 -04:00
|
|
|
|
void AP_Baro_MS5611_SPI::sem_give()
|
2013-01-03 14:06:22 -04:00
|
|
|
|
{
|
2013-01-03 15:05:00 -04:00
|
|
|
|
_spi_sem->give();
|
2013-01-03 14:06:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// I2C Device //////////////////////////////////////////////////////////////////
|
2014-07-08 00:27:06 -03:00
|
|
|
|
#if MS5611_WITH_I2C
|
2014-07-23 09:45:58 -03:00
|
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO
|
|
|
|
|
#define MS5611_ADDR 0x77
|
|
|
|
|
#else
|
|
|
|
|
#define MS5611_ADDR 0x76 /** I2C address of the MS5611 on the PX4 board. */
|
|
|
|
|
#endif
|
2013-01-03 14:06:22 -04:00
|
|
|
|
|
|
|
|
|
void AP_Baro_MS5611_I2C::init()
|
|
|
|
|
{
|
2013-01-04 18:26:26 -04:00
|
|
|
|
_i2c_sem = hal.i2c->get_semaphore();
|
|
|
|
|
if (_i2c_sem == NULL) {
|
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: AP_Baro_MS5611 did not get "
|
|
|
|
|
"valid I2C semaphroe!"));
|
|
|
|
|
return; /* never reached */
|
|
|
|
|
}
|
2013-01-03 14:06:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t AP_Baro_MS5611_I2C::read_16bits(uint8_t reg)
|
|
|
|
|
{
|
|
|
|
|
uint8_t buf[2];
|
|
|
|
|
|
|
|
|
|
if (hal.i2c->readRegisters(MS5611_ADDR, reg, sizeof(buf), buf) == 0)
|
|
|
|
|
return (((uint16_t)(buf[0]) << 8) | buf[1]);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t AP_Baro_MS5611_I2C::read_adc()
|
|
|
|
|
{
|
|
|
|
|
uint8_t buf[3];
|
|
|
|
|
|
|
|
|
|
if (hal.i2c->readRegisters(MS5611_ADDR, 0x00, sizeof(buf), buf) == 0)
|
|
|
|
|
return (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2];
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AP_Baro_MS5611_I2C::write(uint8_t reg)
|
|
|
|
|
{
|
|
|
|
|
hal.i2c->write(MS5611_ADDR, 1, ®);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-04 18:26:26 -04:00
|
|
|
|
bool AP_Baro_MS5611_I2C::sem_take_blocking() {
|
|
|
|
|
return _i2c_sem->take(10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AP_Baro_MS5611_I2C::sem_take_nonblocking()
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Take nonblocking from a TimerProcess context &
|
|
|
|
|
* monitor for bad failures
|
|
|
|
|
*/
|
|
|
|
|
static int semfail_ctr = 0;
|
|
|
|
|
bool got = _i2c_sem->take_nonblocking();
|
|
|
|
|
if (!got) {
|
2013-01-10 18:22:41 -04:00
|
|
|
|
if (!hal.scheduler->system_initializing()) {
|
|
|
|
|
semfail_ctr++;
|
|
|
|
|
if (semfail_ctr > 100) {
|
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: failed to take _i2c_sem "
|
|
|
|
|
"100 times in a row, in "
|
|
|
|
|
"AP_Baro_MS5611::_update"));
|
|
|
|
|
}
|
2013-01-04 18:26:26 -04:00
|
|
|
|
}
|
|
|
|
|
return false; /* never reached */
|
|
|
|
|
} else {
|
|
|
|
|
semfail_ctr = 0;
|
|
|
|
|
}
|
|
|
|
|
return got;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AP_Baro_MS5611_I2C::sem_give()
|
|
|
|
|
{
|
|
|
|
|
_i2c_sem->give();
|
|
|
|
|
}
|
2014-07-08 00:27:06 -03:00
|
|
|
|
#endif // MS5611_WITH_I2C
|
2013-01-04 18:26:26 -04:00
|
|
|
|
|
2011-11-27 01:43:34 -04:00
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2013-01-03 14:06:22 -04:00
|
|
|
|
|
2014-07-07 00:11:41 -03:00
|
|
|
|
#if CONFIG_HAL_BOARD != HAL_BOARD_APM2
|
|
|
|
|
/**
|
|
|
|
|
* MS5611 crc4 method based on PX4Firmware code
|
|
|
|
|
*/
|
|
|
|
|
bool AP_Baro_MS5611::check_crc(void)
|
|
|
|
|
{
|
|
|
|
|
int16_t cnt;
|
|
|
|
|
uint16_t n_rem;
|
|
|
|
|
uint16_t crc_read;
|
|
|
|
|
uint8_t n_bit;
|
|
|
|
|
uint16_t n_prom[8] = { _serial->read_16bits(CMD_MS5611_PROM_Setup),
|
|
|
|
|
C1, C2, C3, C4, C5, C6,
|
|
|
|
|
_serial->read_16bits(CMD_MS5611_PROM_CRC) };
|
|
|
|
|
n_rem = 0x00;
|
|
|
|
|
|
|
|
|
|
/* save the read crc */
|
|
|
|
|
crc_read = n_prom[7];
|
|
|
|
|
|
|
|
|
|
/* remove CRC byte */
|
|
|
|
|
n_prom[7] = (0xFF00 & (n_prom[7]));
|
|
|
|
|
|
|
|
|
|
for (cnt = 0; cnt < 16; cnt++) {
|
|
|
|
|
/* uneven bytes */
|
|
|
|
|
if (cnt & 1) {
|
|
|
|
|
n_rem ^= (uint8_t)((n_prom[cnt >> 1]) & 0x00FF);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
n_rem ^= (uint8_t)(n_prom[cnt >> 1] >> 8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (n_bit = 8; n_bit > 0; n_bit--) {
|
|
|
|
|
if (n_rem & 0x8000) {
|
|
|
|
|
n_rem = (n_rem << 1) ^ 0x3000;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
n_rem = (n_rem << 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* final 4 bit remainder is CRC value */
|
|
|
|
|
n_rem = (0x000F & (n_rem >> 12));
|
|
|
|
|
n_prom[7] = crc_read;
|
|
|
|
|
|
|
|
|
|
/* return true if CRCs match */
|
|
|
|
|
return (0x000F & crc_read) == (n_rem ^ 0x00);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-11-27 01:43:34 -04:00
|
|
|
|
// SPI should be initialized externally
|
2012-10-11 14:53:21 -03:00
|
|
|
|
bool AP_Baro_MS5611::init()
|
2011-11-05 22:11:25 -03:00
|
|
|
|
{
|
2013-01-03 14:06:22 -04:00
|
|
|
|
if (_serial == NULL) {
|
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: AP_Baro_MS5611: NULL serial driver"));
|
2013-01-03 15:05:00 -04:00
|
|
|
|
return false; /* never reached */
|
2012-12-05 22:22:03 -04:00
|
|
|
|
}
|
2012-02-14 12:55:32 -04:00
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
|
_serial->init();
|
2013-01-03 15:05:00 -04:00
|
|
|
|
if (!_serial->sem_take_blocking()){
|
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: AP_Baro_MS5611: failed to take "
|
|
|
|
|
"serial semaphore for init"));
|
|
|
|
|
return false; /* never reached */
|
|
|
|
|
}
|
2012-12-05 22:22:03 -04:00
|
|
|
|
|
2013-01-03 14:06:22 -04:00
|
|
|
|
_serial->write(CMD_MS5611_RESET);
|
2012-10-11 14:53:21 -03:00
|
|
|
|
hal.scheduler->delay(4);
|
2011-11-05 22:11:25 -03:00
|
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
|
// We read the factory calibration
|
|
|
|
|
// The on-chip CRC is not used
|
2013-01-03 14:06:22 -04:00
|
|
|
|
C1 = _serial->read_16bits(CMD_MS5611_PROM_C1);
|
|
|
|
|
C2 = _serial->read_16bits(CMD_MS5611_PROM_C2);
|
|
|
|
|
C3 = _serial->read_16bits(CMD_MS5611_PROM_C3);
|
|
|
|
|
C4 = _serial->read_16bits(CMD_MS5611_PROM_C4);
|
|
|
|
|
C5 = _serial->read_16bits(CMD_MS5611_PROM_C5);
|
|
|
|
|
C6 = _serial->read_16bits(CMD_MS5611_PROM_C6);
|
2011-11-27 01:43:34 -04:00
|
|
|
|
|
2014-07-07 00:11:41 -03:00
|
|
|
|
// if not on APM2 then check CRC
|
2014-07-13 20:50:17 -03:00
|
|
|
|
#if HAL_CPU_CLASS >= HAL_CPU_CLASS_75
|
2014-07-07 00:11:41 -03:00
|
|
|
|
if (!check_crc()) {
|
|
|
|
|
hal.scheduler->panic("Bad CRC on MS5611");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2011-11-27 01:43:34 -04:00
|
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
|
//Send a command to read Temp first
|
2013-01-03 14:06:22 -04:00
|
|
|
|
_serial->write(CMD_CONVERT_D2_OSR4096);
|
2012-10-11 14:53:21 -03:00
|
|
|
|
_timer = hal.scheduler->micros();
|
2012-08-17 03:09:23 -03:00
|
|
|
|
_state = 0;
|
|
|
|
|
Temp=0;
|
|
|
|
|
Press=0;
|
2012-02-14 12:55:32 -04:00
|
|
|
|
|
2012-07-02 00:44:02 -03:00
|
|
|
|
_s_D1 = 0;
|
|
|
|
|
_s_D2 = 0;
|
|
|
|
|
_d1_count = 0;
|
|
|
|
|
_d2_count = 0;
|
|
|
|
|
|
2013-09-30 04:01:20 -03:00
|
|
|
|
hal.scheduler->register_timer_process( AP_HAL_MEMBERPROC(&AP_Baro_MS5611::_update));
|
2013-01-03 15:05:00 -04:00
|
|
|
|
_serial->sem_give();
|
2011-12-09 02:35:40 -04:00
|
|
|
|
|
2012-07-02 00:44:02 -03:00
|
|
|
|
// wait for at least one value to be read
|
2012-12-05 21:16:50 -04:00
|
|
|
|
uint32_t tstart = hal.scheduler->millis();
|
2012-12-04 23:10:27 -04:00
|
|
|
|
while (!_updated) {
|
|
|
|
|
hal.scheduler->delay(10);
|
2012-12-05 21:16:50 -04:00
|
|
|
|
if (hal.scheduler->millis() - tstart > 1000) {
|
2012-12-17 20:28:55 -04:00
|
|
|
|
hal.scheduler->panic(PSTR("PANIC: AP_Baro_MS5611 took more than "
|
2012-12-05 21:16:50 -04:00
|
|
|
|
"1000ms to initialize"));
|
2014-08-13 09:43:31 -03:00
|
|
|
|
_flags.healthy = false;
|
2012-12-05 21:16:50 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-12-04 23:10:27 -04:00
|
|
|
|
}
|
2012-07-02 00:44:02 -03:00
|
|
|
|
|
2014-08-13 09:43:31 -03:00
|
|
|
|
_flags.healthy = true;
|
2011-12-28 05:32:21 -04:00
|
|
|
|
return true;
|
2011-11-05 22:11:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-27 01:43:34 -04:00
|
|
|
|
|
|
|
|
|
// Read the sensor. This is a state machine
|
|
|
|
|
// We read one time Temperature (state=1) and then 4 times Pressure (states 2-5)
|
|
|
|
|
// temperature does not change so quickly...
|
2013-09-28 03:30:26 -03:00
|
|
|
|
void AP_Baro_MS5611::_update(void)
|
2011-11-05 22:11:25 -03:00
|
|
|
|
{
|
2013-09-28 03:30:26 -03:00
|
|
|
|
uint32_t tnow = hal.scheduler->micros();
|
2012-02-12 20:00:06 -04:00
|
|
|
|
// Throttle read rate to 100hz maximum.
|
2013-10-10 05:50:09 -03:00
|
|
|
|
if (tnow - _timer < 10000) {
|
2012-08-17 03:09:23 -03:00
|
|
|
|
return;
|
2011-12-21 08:22:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 05:27:48 -04:00
|
|
|
|
if (!_serial->sem_take_nonblocking()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-12-21 08:22:37 -04:00
|
|
|
|
_timer = tnow;
|
2012-02-14 12:55:32 -04:00
|
|
|
|
|
2012-07-02 00:44:02 -03:00
|
|
|
|
if (_state == 0) {
|
2014-07-07 08:20:05 -03:00
|
|
|
|
// On state 0 we read temp
|
|
|
|
|
uint32_t d2 = _serial->read_adc();
|
|
|
|
|
if (d2 != 0) {
|
|
|
|
|
_s_D2 += d2;
|
|
|
|
|
_d2_count++;
|
|
|
|
|
if (_d2_count == 32) {
|
|
|
|
|
// we have summed 32 values. This only happens
|
|
|
|
|
// when we stop reading the barometer for a long time
|
|
|
|
|
// (more than 1.2 seconds)
|
|
|
|
|
_s_D2 >>= 1;
|
|
|
|
|
_d2_count = 16;
|
|
|
|
|
}
|
2012-07-02 00:44:02 -03:00
|
|
|
|
}
|
2012-08-17 03:09:23 -03:00
|
|
|
|
_state++;
|
2013-01-03 14:06:22 -04:00
|
|
|
|
_serial->write(CMD_CONVERT_D1_OSR4096); // Command to read pressure
|
2011-12-21 08:22:37 -04:00
|
|
|
|
} else {
|
2014-07-07 08:20:05 -03:00
|
|
|
|
uint32_t d1 = _serial->read_adc();;
|
|
|
|
|
if (d1 != 0) {
|
|
|
|
|
// occasional zero values have been seen on the PXF
|
|
|
|
|
// board. These may be SPI errors, but safest to ignore
|
|
|
|
|
_s_D1 += d1;
|
|
|
|
|
_d1_count++;
|
|
|
|
|
if (_d1_count == 128) {
|
|
|
|
|
// we have summed 128 values. This only happens
|
|
|
|
|
// when we stop reading the barometer for a long time
|
|
|
|
|
// (more than 1.2 seconds)
|
|
|
|
|
_s_D1 >>= 1;
|
|
|
|
|
_d1_count = 64;
|
|
|
|
|
}
|
|
|
|
|
// Now a new reading exists
|
|
|
|
|
_updated = true;
|
2012-07-02 00:44:02 -03:00
|
|
|
|
}
|
2012-08-17 03:09:23 -03:00
|
|
|
|
_state++;
|
2012-07-02 00:44:02 -03:00
|
|
|
|
if (_state == 5) {
|
2013-01-03 14:06:22 -04:00
|
|
|
|
_serial->write(CMD_CONVERT_D2_OSR4096); // Command to read temperature
|
2012-07-02 00:44:02 -03:00
|
|
|
|
_state = 0;
|
|
|
|
|
} else {
|
2013-01-03 14:06:22 -04:00
|
|
|
|
_serial->write(CMD_CONVERT_D1_OSR4096); // Command to read pressure
|
2012-07-02 00:44:02 -03:00
|
|
|
|
}
|
2011-12-21 08:22:37 -04:00
|
|
|
|
}
|
2012-11-19 21:23:26 -04:00
|
|
|
|
|
2013-01-03 15:05:00 -04:00
|
|
|
|
_serial->sem_give();
|
2011-12-09 02:35:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t AP_Baro_MS5611::read()
|
|
|
|
|
{
|
|
|
|
|
bool updated = _updated;
|
2012-07-02 00:44:02 -03:00
|
|
|
|
if (updated) {
|
|
|
|
|
uint32_t sD1, sD2;
|
|
|
|
|
uint8_t d1count, d2count;
|
2013-01-03 19:42:22 -04:00
|
|
|
|
|
|
|
|
|
// Suspend timer procs because these variables are written to
|
|
|
|
|
// in "_update".
|
|
|
|
|
hal.scheduler->suspend_timer_procs();
|
2012-07-02 00:44:02 -03:00
|
|
|
|
sD1 = _s_D1; _s_D1 = 0;
|
|
|
|
|
sD2 = _s_D2; _s_D2 = 0;
|
|
|
|
|
d1count = _d1_count; _d1_count = 0;
|
|
|
|
|
d2count = _d2_count; _d2_count = 0;
|
|
|
|
|
_updated = false;
|
2013-01-03 19:42:22 -04:00
|
|
|
|
hal.scheduler->resume_timer_procs();
|
|
|
|
|
|
2012-07-02 00:44:02 -03:00
|
|
|
|
if (d1count != 0) {
|
2012-07-06 02:11:22 -03:00
|
|
|
|
D1 = ((float)sD1) / d1count;
|
2012-07-02 00:44:02 -03:00
|
|
|
|
}
|
|
|
|
|
if (d2count != 0) {
|
2012-07-06 02:11:22 -03:00
|
|
|
|
D2 = ((float)sD2) / d2count;
|
2012-07-02 00:44:02 -03:00
|
|
|
|
}
|
2012-07-05 03:26:56 -03:00
|
|
|
|
_pressure_samples = d1count;
|
2011-12-09 02:35:40 -04:00
|
|
|
|
_raw_press = D1;
|
|
|
|
|
_raw_temp = D2;
|
|
|
|
|
}
|
|
|
|
|
_calculate();
|
2012-06-19 23:25:19 -03:00
|
|
|
|
if (updated) {
|
2012-10-11 14:53:21 -03:00
|
|
|
|
_last_update = hal.scheduler->millis();
|
2012-06-19 23:25:19 -03:00
|
|
|
|
}
|
2011-12-09 02:35:40 -04:00
|
|
|
|
return updated ? 1 : 0;
|
2011-11-05 22:11:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-27 01:43:34 -04:00
|
|
|
|
// Calculate Temperature and compensated Pressure in real units (Celsius degrees*100, mbar*100).
|
2011-12-09 02:35:40 -04:00
|
|
|
|
void AP_Baro_MS5611::_calculate()
|
2011-11-05 22:11:25 -03:00
|
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
|
float dT;
|
|
|
|
|
float TEMP;
|
|
|
|
|
float OFF;
|
|
|
|
|
float SENS;
|
|
|
|
|
float P;
|
2011-11-27 01:43:34 -04:00
|
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
|
// Formulas from manufacturer datasheet
|
2012-07-06 02:11:22 -03:00
|
|
|
|
// sub -20c temperature compensation is not included
|
|
|
|
|
|
|
|
|
|
// we do the calculations using floating point
|
|
|
|
|
// as this is much faster on an AVR2560, and also allows
|
|
|
|
|
// us to take advantage of the averaging of D1 and D1 over
|
|
|
|
|
// multiple samples, giving us more precision
|
2012-08-17 03:09:23 -03:00
|
|
|
|
dT = D2-(((uint32_t)C5)<<8);
|
|
|
|
|
TEMP = (dT * C6)/8388608;
|
2013-01-10 14:42:24 -04:00
|
|
|
|
OFF = C2 * 65536.0f + (C4 * dT) / 128;
|
|
|
|
|
SENS = C1 * 32768.0f + (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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
P = (D1*SENS/2097152 - OFF)/32768;
|
2013-09-21 08:30:41 -03:00
|
|
|
|
Temp = (TEMP + 2000) * 0.01f;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
Press = P;
|
2011-11-05 22:11:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-06 02:11:22 -03:00
|
|
|
|
float AP_Baro_MS5611::get_pressure()
|
2011-11-05 22:11:25 -03:00
|
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
|
return Press;
|
2011-11-27 01:43:34 -04:00
|
|
|
|
}
|
2011-11-05 22:11:25 -03:00
|
|
|
|
|
2012-07-06 02:11:22 -03:00
|
|
|
|
float AP_Baro_MS5611::get_temperature()
|
2011-11-27 01:43:34 -04:00
|
|
|
|
{
|
2013-09-21 08:30:41 -03:00
|
|
|
|
// temperature in degrees C units
|
|
|
|
|
return Temp;
|
2011-11-05 22:11:25 -03:00
|
|
|
|
}
|