2013-09-28 05:40:29 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
backend driver for airspeed from a I2C MS4525D0 sensor
|
|
|
|
*/
|
2016-01-29 07:38:33 -04:00
|
|
|
#include "AP_Airspeed_I2C.h"
|
2013-09-28 05:40:29 -03:00
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2016-06-08 09:13:53 -03:00
|
|
|
#include <AP_HAL/I2CDevice.h>
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2016-06-08 09:13:53 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <utility>
|
2013-09-28 05:40:29 -03:00
|
|
|
|
2016-01-29 07:38:33 -04:00
|
|
|
extern const AP_HAL::HAL &hal;
|
2013-09-28 05:40:29 -03:00
|
|
|
|
2016-07-12 02:56:25 -03:00
|
|
|
#define MS4525D0_I2C_ADDR 0x28
|
2013-09-28 05:40:29 -03:00
|
|
|
|
2016-06-08 09:13:53 -03:00
|
|
|
#ifdef HAL_AIRSPEED_MS4515DO_I2C_BUS
|
|
|
|
#define MS4525D0_I2C_BUS HAL_AIRSPEED_MS4515DO_I2C_BUS
|
|
|
|
#else
|
|
|
|
#define MS4525D0_I2C_BUS 1
|
|
|
|
#endif
|
|
|
|
|
2016-06-08 21:10:54 -03:00
|
|
|
AP_Airspeed_I2C::AP_Airspeed_I2C(const AP_Float &psi_range) :
|
|
|
|
_psi_range(psi_range)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-28 05:40:29 -03:00
|
|
|
// probe and initialise the sensor
|
2016-01-29 07:38:33 -04:00
|
|
|
bool AP_Airspeed_I2C::init()
|
2013-09-28 05:40:29 -03:00
|
|
|
{
|
2016-07-12 02:56:25 -03:00
|
|
|
_dev = hal.i2c_mgr->get_device(MS4525D0_I2C_BUS, MS4525D0_I2C_ADDR);
|
2013-10-29 00:55:23 -03:00
|
|
|
|
|
|
|
// take i2c bus sempahore
|
2016-07-12 02:56:25 -03:00
|
|
|
if (!_dev || !_dev->get_semaphore()->take(200)) {
|
2013-10-29 00:55:23 -03:00
|
|
|
return false;
|
2016-01-29 07:38:33 -04:00
|
|
|
}
|
2013-10-29 00:55:23 -03:00
|
|
|
|
2013-09-28 05:40:29 -03:00
|
|
|
_measure();
|
|
|
|
hal.scheduler->delay(10);
|
|
|
|
_collect();
|
2016-04-19 14:16:19 -03:00
|
|
|
_dev->get_semaphore()->give();
|
2016-07-12 02:56:25 -03:00
|
|
|
|
2013-09-28 05:40:29 -03:00
|
|
|
if (_last_sample_time_ms != 0) {
|
2015-05-24 20:24:11 -03:00
|
|
|
hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&AP_Airspeed_I2C::_timer, void));
|
2013-09-28 05:40:29 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start a measurement
|
2016-01-29 07:38:33 -04:00
|
|
|
void AP_Airspeed_I2C::_measure()
|
2013-09-28 05:40:29 -03:00
|
|
|
{
|
|
|
|
_measurement_started_ms = 0;
|
2016-07-12 02:56:25 -03:00
|
|
|
uint8_t cmd = 0;
|
|
|
|
if (_dev->transfer(&cmd, 1, nullptr, 0)) {
|
2015-11-19 23:07:00 -04:00
|
|
|
_measurement_started_ms = AP_HAL::millis();
|
2013-09-28 05:40:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the values from the sensor
|
2016-01-29 07:38:33 -04:00
|
|
|
void AP_Airspeed_I2C::_collect()
|
2013-09-28 05:40:29 -03:00
|
|
|
{
|
|
|
|
uint8_t data[4];
|
2013-10-29 00:55:23 -03:00
|
|
|
|
2013-09-28 05:40:29 -03:00
|
|
|
_measurement_started_ms = 0;
|
|
|
|
|
2016-07-12 02:56:25 -03:00
|
|
|
if (!_dev->transfer(nullptr, 0, data, sizeof(data))) {
|
2013-09-28 05:40:29 -03:00
|
|
|
return;
|
|
|
|
}
|
2016-01-29 07:38:33 -04:00
|
|
|
|
|
|
|
uint8_t status = data[0] & 0xC0;
|
|
|
|
if (status == 2 || status == 3) {
|
2013-09-28 05:40:29 -03:00
|
|
|
return;
|
2016-01-29 07:38:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t dp_raw, dT_raw;
|
|
|
|
dp_raw = (data[0] << 8) + data[1];
|
|
|
|
dp_raw = 0x3FFF & dp_raw;
|
|
|
|
dT_raw = (data[2] << 8) + data[3];
|
|
|
|
dT_raw = (0xFFE0 & dT_raw) >> 5;
|
|
|
|
|
2016-06-08 21:10:54 -03:00
|
|
|
const float P_max = _psi_range.get();
|
|
|
|
const float P_min = - P_max;
|
2016-01-29 07:38:33 -04:00
|
|
|
const float PSI_to_Pa = 6894.757f;
|
|
|
|
/*
|
|
|
|
this equation is an inversion of the equation in the
|
|
|
|
pressure transfer function figure on page 4 of the datasheet
|
|
|
|
|
|
|
|
We negate the result so that positive differential pressures
|
|
|
|
are generated when the bottom port is used as the static
|
|
|
|
port on the pitot and top port is used as the dynamic port
|
|
|
|
*/
|
|
|
|
float diff_press_PSI = -((dp_raw - 0.1f*16383) * (P_max-P_min)/(0.8f*16383) + P_min);
|
|
|
|
|
|
|
|
_pressure = diff_press_PSI * PSI_to_Pa;
|
|
|
|
_temperature = ((200.0f * dT_raw) / 2047) - 50;
|
2014-02-13 02:21:06 -04:00
|
|
|
|
2015-11-19 23:07:00 -04:00
|
|
|
_last_sample_time_ms = AP_HAL::millis();
|
2013-09-28 05:40:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1kHz timer
|
2016-01-29 07:38:33 -04:00
|
|
|
void AP_Airspeed_I2C::_timer()
|
2013-09-28 05:40:29 -03:00
|
|
|
{
|
2016-04-19 14:16:19 -03:00
|
|
|
if (!_dev->get_semaphore()->take_nonblocking()) {
|
2013-10-29 00:55:23 -03:00
|
|
|
return;
|
2016-01-29 07:38:33 -04:00
|
|
|
}
|
2013-10-29 00:55:23 -03:00
|
|
|
|
2013-09-28 05:40:29 -03:00
|
|
|
if (_measurement_started_ms == 0) {
|
|
|
|
_measure();
|
2016-04-19 14:16:19 -03:00
|
|
|
_dev->get_semaphore()->give();
|
2013-09-28 05:40:29 -03:00
|
|
|
return;
|
|
|
|
}
|
2015-11-19 23:07:00 -04:00
|
|
|
if ((AP_HAL::millis() - _measurement_started_ms) > 10) {
|
2013-09-28 05:40:29 -03:00
|
|
|
_collect();
|
|
|
|
// start a new measurement
|
|
|
|
_measure();
|
|
|
|
}
|
2016-04-19 14:16:19 -03:00
|
|
|
_dev->get_semaphore()->give();
|
2013-09-28 05:40:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the current differential_pressure in Pascal
|
|
|
|
bool AP_Airspeed_I2C::get_differential_pressure(float &pressure)
|
|
|
|
{
|
2015-11-19 23:07:00 -04:00
|
|
|
if ((AP_HAL::millis() - _last_sample_time_ms) > 100) {
|
2013-09-28 05:40:29 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pressure = _pressure;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the current temperature in degrees C, if available
|
|
|
|
bool AP_Airspeed_I2C::get_temperature(float &temperature)
|
|
|
|
{
|
2015-11-19 23:07:00 -04:00
|
|
|
if ((AP_HAL::millis() - _last_sample_time_ms) > 100) {
|
2013-09-28 05:40:29 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
temperature = _temperature;
|
|
|
|
return true;
|
|
|
|
}
|