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 00:42:52 -04:00
|
|
|
|
/*
|
2012-08-17 03:09:23 -03:00
|
|
|
|
* APM_BMP085.cpp - Arduino Library for BMP085 absolute pressure sensor
|
|
|
|
|
* Code by Jordi Mu<EFBFBD>oz and Jose Julio. DIYDrones.com
|
|
|
|
|
* Sensor is conected to I2C port
|
|
|
|
|
* Sensor End of Conversion (EOC) pin is PC7 (30)
|
|
|
|
|
*
|
|
|
|
|
* Variables:
|
|
|
|
|
* RawTemp : Raw temperature data
|
|
|
|
|
* RawPress : Raw pressure data
|
|
|
|
|
*
|
|
|
|
|
* Temp : Calculated temperature (in 0.1<EFBFBD>C units)
|
|
|
|
|
* Press : Calculated pressure (in Pa units)
|
|
|
|
|
*
|
|
|
|
|
* Methods:
|
|
|
|
|
* Init() : Initialization of I2C and read sensor calibration data
|
|
|
|
|
* Read() : Read sensor data and calculate Temperature and Pressure
|
|
|
|
|
* This function is optimized so the main host don<EFBFBD>t need to wait
|
|
|
|
|
* You can call this function in your main loop
|
|
|
|
|
* It returns a 1 if there are new data.
|
|
|
|
|
*
|
|
|
|
|
* Internal functions:
|
|
|
|
|
* Command_ReadTemp(): Send commando to read temperature
|
|
|
|
|
* Command_ReadPress(): Send commando to read Pressure
|
|
|
|
|
* ReadTemp() : Read temp register
|
|
|
|
|
* ReadPress() : Read press register
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
|
// AVR LibC Includes
|
|
|
|
|
#include <inttypes.h>
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2011-12-28 05:32:21 -04:00
|
|
|
|
#include <AP_Common.h>
|
2012-08-17 03:09:23 -03:00
|
|
|
|
#include <AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
|
2012-10-11 14:53:21 -03:00
|
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
2011-11-27 01:34:26 -04:00
|
|
|
|
#include "AP_Baro_BMP085.h"
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2012-10-11 14:53:21 -03:00
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
2011-11-27 00:42:52 -04:00
|
|
|
|
#define BMP085_ADDRESS 0x77 //(0xEE >> 1)
|
2013-09-28 07:38:37 -03:00
|
|
|
|
#define BMP085_EOC 30 // End of conversion pin PC7 on APM1
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2011-11-25 19:11:14 -04:00
|
|
|
|
// the apm2 hardware needs to check the state of the
|
2011-11-12 22:45:23 -04:00
|
|
|
|
// chip using a direct IO port
|
2011-11-25 19:11:14 -04:00
|
|
|
|
// On APM2 prerelease hw, the data ready port is hooked up to PE7, which
|
2011-11-12 22:45:23 -04:00
|
|
|
|
// is not available to the arduino digitalRead function.
|
2013-09-28 07:38:37 -03:00
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_APM1
|
|
|
|
|
#define BMP_DATA_READY() hal.gpio->read(BMP085_EOC)
|
|
|
|
|
#else
|
|
|
|
|
// No EOC connection from Baro
|
2013-09-23 00:12:09 -03:00
|
|
|
|
// Use times instead.
|
|
|
|
|
// Temp conversion time is 4.5ms
|
|
|
|
|
// Pressure conversion time is 25.5ms (for OVERSAMPLING=3)
|
|
|
|
|
#define BMP_DATA_READY() (BMP085_State == 0 ? hal.scheduler->millis() > (_last_temp_read_command_time + 5) : hal.scheduler->millis() > (_last_press_read_command_time + 26))
|
2012-12-12 17:43:28 -04:00
|
|
|
|
#endif
|
2011-11-12 22:45:23 -04:00
|
|
|
|
|
2013-01-09 08:05:17 -04:00
|
|
|
|
// oversampling 3 gives 26ms conversion time. We then average
|
2012-01-14 04:50:45 -04:00
|
|
|
|
#define OVERSAMPLING 3
|
2011-11-12 22:45:23 -04:00
|
|
|
|
|
2011-11-27 00:42:52 -04:00
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2012-10-11 14:53:21 -03:00
|
|
|
|
bool AP_Baro_BMP085::init()
|
2011-11-27 00:42:52 -04:00
|
|
|
|
{
|
2012-10-11 14:53:21 -03:00
|
|
|
|
uint8_t buff[22];
|
2012-08-17 03:09:23 -03:00
|
|
|
|
|
2013-10-07 21:22:58 -03:00
|
|
|
|
// get pointer to i2c bus semaphore
|
|
|
|
|
AP_HAL::Semaphore* i2c_sem = hal.i2c->get_semaphore();
|
|
|
|
|
|
|
|
|
|
// take i2c bus sempahore
|
|
|
|
|
if (!i2c_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER))
|
|
|
|
|
return false;
|
|
|
|
|
|
2014-06-01 20:29:28 -03:00
|
|
|
|
hal.gpio->pinMode(BMP085_EOC, HAL_GPIO_INPUT);// End Of Conversion (PC7) input
|
2012-08-17 03:09:23 -03:00
|
|
|
|
|
|
|
|
|
// We read the calibration data registers
|
2012-10-11 14:53:21 -03:00
|
|
|
|
if (hal.i2c->readRegisters(BMP085_ADDRESS, 0xAA, 22, buff) != 0) {
|
2014-08-13 09:42:51 -03:00
|
|
|
|
_flags.healthy = false;
|
2013-10-07 21:22:58 -03:00
|
|
|
|
i2c_sem->give();
|
2012-08-17 03:09:23 -03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 08:44:26 -04:00
|
|
|
|
ac1 = ((int16_t)buff[0] << 8) | buff[1];
|
|
|
|
|
ac2 = ((int16_t)buff[2] << 8) | buff[3];
|
|
|
|
|
ac3 = ((int16_t)buff[4] << 8) | buff[5];
|
|
|
|
|
ac4 = ((int16_t)buff[6] << 8) | buff[7];
|
|
|
|
|
ac5 = ((int16_t)buff[8] << 8) | buff[9];
|
|
|
|
|
ac6 = ((int16_t)buff[10] << 8) | buff[11];
|
|
|
|
|
b1 = ((int16_t)buff[12] << 8) | buff[13];
|
|
|
|
|
b2 = ((int16_t)buff[14] << 8) | buff[15];
|
|
|
|
|
mb = ((int16_t)buff[16] << 8) | buff[17];
|
|
|
|
|
mc = ((int16_t)buff[18] << 8) | buff[19];
|
|
|
|
|
md = ((int16_t)buff[20] << 8) | buff[21];
|
2012-08-17 03:09:23 -03:00
|
|
|
|
|
2013-09-23 00:12:09 -03:00
|
|
|
|
_last_press_read_command_time = 0;
|
|
|
|
|
_last_temp_read_command_time = 0;
|
|
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
|
//Send a command to read Temp
|
|
|
|
|
Command_ReadTemp();
|
2012-10-11 14:53:21 -03:00
|
|
|
|
|
2013-01-09 08:05:17 -04:00
|
|
|
|
BMP085_State = 0;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
|
|
|
|
|
// init raw temo
|
|
|
|
|
RawTemp = 0;
|
|
|
|
|
|
2014-08-13 09:42:51 -03:00
|
|
|
|
_flags.healthy = true;
|
2013-10-07 21:22:58 -03:00
|
|
|
|
i2c_sem->give();
|
2012-08-17 03:09:23 -03:00
|
|
|
|
return true;
|
2011-11-27 00:42:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the sensor. This is a state machine
|
2013-01-09 08:05:17 -04:00
|
|
|
|
// acumulate a new sensor reading
|
|
|
|
|
void AP_Baro_BMP085::accumulate(void)
|
|
|
|
|
{
|
2013-10-07 21:22:58 -03:00
|
|
|
|
// get pointer to i2c bus semaphore
|
|
|
|
|
AP_HAL::Semaphore* i2c_sem = hal.i2c->get_semaphore();
|
|
|
|
|
|
2013-01-09 08:05:17 -04:00
|
|
|
|
if (!BMP_DATA_READY()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-10-07 21:22:58 -03:00
|
|
|
|
|
|
|
|
|
// take i2c bus sempahore
|
|
|
|
|
if (!i2c_sem->take(1))
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-09 08:05:17 -04:00
|
|
|
|
if (BMP085_State == 0) {
|
|
|
|
|
ReadTemp();
|
|
|
|
|
} else {
|
|
|
|
|
ReadPress();
|
|
|
|
|
Calculate();
|
2013-01-09 08:44:26 -04:00
|
|
|
|
}
|
|
|
|
|
BMP085_State++;
|
|
|
|
|
if (BMP085_State == 5) {
|
|
|
|
|
BMP085_State = 0;
|
2013-01-09 08:05:17 -04:00
|
|
|
|
Command_ReadTemp();
|
2013-01-09 08:44:26 -04:00
|
|
|
|
} else {
|
|
|
|
|
Command_ReadPress();
|
2013-01-09 08:05:17 -04:00
|
|
|
|
}
|
2013-10-07 21:22:58 -03:00
|
|
|
|
|
|
|
|
|
i2c_sem->give();
|
2013-01-09 08:05:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read the sensor using accumulated data
|
2011-11-30 00:33:53 -04:00
|
|
|
|
uint8_t AP_Baro_BMP085::read()
|
2011-11-27 00:42:52 -04:00
|
|
|
|
{
|
2013-01-09 08:44:26 -04:00
|
|
|
|
if (_count == 0 && BMP_DATA_READY()) {
|
2013-01-09 08:05:17 -04:00
|
|
|
|
accumulate();
|
2012-08-17 03:09:23 -03:00
|
|
|
|
}
|
2013-01-09 08:05:17 -04:00
|
|
|
|
if (_count == 0) {
|
|
|
|
|
return 0;
|
2012-06-19 23:25:19 -03:00
|
|
|
|
}
|
2013-01-09 08:05:17 -04:00
|
|
|
|
_last_update = hal.scheduler->millis();
|
|
|
|
|
|
2013-09-21 08:30:41 -03:00
|
|
|
|
Temp = 0.1f * _temp_sum / _count;
|
2013-01-09 08:05:17 -04:00
|
|
|
|
Press = _press_sum / _count;
|
|
|
|
|
|
2013-01-09 08:44:26 -04:00
|
|
|
|
_pressure_samples = _count;
|
2013-01-09 08:05:17 -04:00
|
|
|
|
_count = 0;
|
|
|
|
|
_temp_sum = 0;
|
|
|
|
|
_press_sum = 0;
|
|
|
|
|
|
|
|
|
|
return 1;
|
2011-11-27 00:42:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-06 02:11:22 -03:00
|
|
|
|
float AP_Baro_BMP085::get_pressure() {
|
2011-11-30 00:33:53 -04:00
|
|
|
|
return Press;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-06 02:11:22 -03:00
|
|
|
|
float AP_Baro_BMP085::get_temperature() {
|
2011-11-30 00:33:53 -04:00
|
|
|
|
return Temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private functions: /////////////////////////////////////////////////////////
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
|
|
|
|
// Send command to Read Pressure
|
2011-11-27 01:34:26 -04:00
|
|
|
|
void AP_Baro_BMP085::Command_ReadPress()
|
2011-11-27 00:42:52 -04:00
|
|
|
|
{
|
2013-09-23 00:12:09 -03:00
|
|
|
|
// Mode 0x34+(OVERSAMPLING << 6) is osrs=3 when OVERSAMPLING=3 => 25.5ms conversion time
|
2012-10-11 14:53:21 -03:00
|
|
|
|
uint8_t res = hal.i2c->writeRegister(BMP085_ADDRESS, 0xF4,
|
|
|
|
|
0x34+(OVERSAMPLING << 6));
|
2013-09-23 00:12:09 -03:00
|
|
|
|
_last_press_read_command_time = hal.scheduler->millis();
|
2012-10-11 14:53:21 -03:00
|
|
|
|
if (res != 0) {
|
2014-08-13 09:42:51 -03:00
|
|
|
|
_flags.healthy = false;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
}
|
2011-11-27 00:42:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read Raw Pressure values
|
2011-11-27 01:34:26 -04:00
|
|
|
|
void AP_Baro_BMP085::ReadPress()
|
2011-11-27 00:42:52 -04:00
|
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
|
uint8_t buf[3];
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2014-08-13 09:42:51 -03:00
|
|
|
|
if (!_flags.healthy && hal.scheduler->millis() < _retry_time) {
|
2012-01-04 01:35:16 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 14:53:21 -03:00
|
|
|
|
if (hal.i2c->readRegisters(BMP085_ADDRESS, 0xF6, 3, buf) != 0) {
|
|
|
|
|
_retry_time = hal.scheduler->millis() + 1000;
|
|
|
|
|
hal.i2c->setHighSpeed(false);
|
2014-08-13 09:42:51 -03:00
|
|
|
|
_flags.healthy = false;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2012-10-11 14:53:21 -03:00
|
|
|
|
RawPress = (((uint32_t)buf[0] << 16)
|
|
|
|
|
| ((uint32_t)buf[1] << 8)
|
|
|
|
|
| ((uint32_t)buf[2])) >> (8 - OVERSAMPLING);
|
2011-11-27 00:42:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send Command to Read Temperature
|
2011-11-27 01:34:26 -04:00
|
|
|
|
void AP_Baro_BMP085::Command_ReadTemp()
|
2011-11-27 00:42:52 -04:00
|
|
|
|
{
|
2012-10-11 14:53:21 -03:00
|
|
|
|
if (hal.i2c->writeRegister(BMP085_ADDRESS, 0xF4, 0x2E) != 0) {
|
2014-08-13 09:42:51 -03:00
|
|
|
|
_flags.healthy = false;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
}
|
2013-09-23 00:12:09 -03:00
|
|
|
|
_last_temp_read_command_time = hal.scheduler->millis();
|
2011-11-27 00:42:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read Raw Temperature values
|
2011-11-27 01:34:26 -04:00
|
|
|
|
void AP_Baro_BMP085::ReadTemp()
|
2011-11-27 00:42:52 -04:00
|
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
|
uint8_t buf[2];
|
|
|
|
|
int32_t _temp_sensor;
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2014-08-13 09:42:51 -03:00
|
|
|
|
if (!_flags.healthy && hal.scheduler->millis() < _retry_time) {
|
2012-01-04 01:35:16 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 14:53:21 -03:00
|
|
|
|
if (hal.i2c->readRegisters(BMP085_ADDRESS, 0xF6, 2, buf) != 0) {
|
|
|
|
|
_retry_time = hal.scheduler->millis() + 1000;
|
|
|
|
|
hal.i2c->setHighSpeed(false);
|
2014-08-13 09:42:51 -03:00
|
|
|
|
_flags.healthy = false;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_temp_sensor = buf[0];
|
|
|
|
|
_temp_sensor = (_temp_sensor << 8) | buf[1];
|
2011-11-27 00:42:52 -04:00
|
|
|
|
|
2012-08-17 03:09:23 -03:00
|
|
|
|
RawTemp = _temp_filter.apply(_temp_sensor);
|
2011-11-27 00:42:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-09 08:44:26 -04:00
|
|
|
|
|
2011-11-27 00:42:52 -04:00
|
|
|
|
// Calculate Temperature and Pressure in real units.
|
2011-11-27 01:34:26 -04:00
|
|
|
|
void AP_Baro_BMP085::Calculate()
|
2011-11-27 00:42:52 -04:00
|
|
|
|
{
|
2012-08-17 03:09:23 -03:00
|
|
|
|
int32_t x1, x2, x3, b3, b5, b6, p;
|
|
|
|
|
uint32_t b4, b7;
|
|
|
|
|
int32_t tmp;
|
|
|
|
|
|
|
|
|
|
// See Datasheet page 13 for this formulas
|
|
|
|
|
// Based also on Jee Labs BMP085 example code. Thanks for share.
|
|
|
|
|
// Temperature calculations
|
|
|
|
|
x1 = ((int32_t)RawTemp - ac6) * ac5 >> 15;
|
|
|
|
|
x2 = ((int32_t) mc << 11) / (x1 + md);
|
|
|
|
|
b5 = x1 + x2;
|
2013-01-09 08:05:17 -04:00
|
|
|
|
_temp_sum += (b5 + 8) >> 4;
|
2012-08-17 03:09:23 -03:00
|
|
|
|
|
|
|
|
|
// Pressure calculations
|
|
|
|
|
b6 = b5 - 4000;
|
|
|
|
|
x1 = (b2 * (b6 * b6 >> 12)) >> 11;
|
|
|
|
|
x2 = ac2 * b6 >> 11;
|
|
|
|
|
x3 = x1 + x2;
|
|
|
|
|
//b3 = (((int32_t) ac1 * 4 + x3)<<OVERSAMPLING + 2) >> 2; // BAD
|
|
|
|
|
//b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 2; //OK for OVERSAMPLING=0
|
|
|
|
|
tmp = ac1;
|
|
|
|
|
tmp = (tmp*4 + x3)<<OVERSAMPLING;
|
|
|
|
|
b3 = (tmp+2)/4;
|
|
|
|
|
x1 = ac3 * b6 >> 13;
|
|
|
|
|
x2 = (b1 * (b6 * b6 >> 12)) >> 16;
|
|
|
|
|
x3 = ((x1 + x2) + 2) >> 2;
|
|
|
|
|
b4 = (ac4 * (uint32_t) (x3 + 32768)) >> 15;
|
|
|
|
|
b7 = ((uint32_t) RawPress - b3) * (50000 >> OVERSAMPLING);
|
|
|
|
|
p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
|
|
|
|
|
|
|
|
|
|
x1 = (p >> 8) * (p >> 8);
|
|
|
|
|
x1 = (x1 * 3038) >> 16;
|
|
|
|
|
x2 = (-7357 * p) >> 16;
|
2013-01-09 08:05:17 -04:00
|
|
|
|
_press_sum += p + ((x1 + x2 + 3791) >> 4);
|
|
|
|
|
|
|
|
|
|
_count++;
|
|
|
|
|
if (_count == 254) {
|
2014-07-08 07:26:54 -03:00
|
|
|
|
_temp_sum *= 0.5f;
|
|
|
|
|
_press_sum *= 0.5f;
|
2013-01-09 08:05:17 -04:00
|
|
|
|
_count /= 2;
|
|
|
|
|
}
|
2011-11-12 22:45:23 -04:00
|
|
|
|
}
|
2013-09-28 07:38:37 -03:00
|
|
|
|
|