2016-11-30 02:21:48 -04: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
backend driver for airspeed from a I2C MS5525D0 sensor
|
|
|
|
*/
|
|
|
|
#include "AP_Airspeed_MS5525.h"
|
|
|
|
|
2022-05-04 05:13:29 -03:00
|
|
|
#if AP_AIRSPEED_MS5525_ENABLED
|
|
|
|
|
2017-01-13 13:28:06 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <utility>
|
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_HAL/I2CDevice.h>
|
2017-01-13 13:28:06 -04:00
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
2016-11-30 02:21:48 -04:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2019-04-24 03:52:34 -03:00
|
|
|
#include <AP_Math/crc.h>
|
2021-06-29 23:00:29 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2016-11-30 02:21:48 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
|
|
|
#define MS5525D0_I2C_ADDR_1 0x76
|
|
|
|
#define MS5525D0_I2C_ADDR_2 0x77
|
|
|
|
|
|
|
|
#define REG_RESET 0x1E
|
|
|
|
#define REG_CONVERT_D1_OSR_256 0x40
|
|
|
|
#define REG_CONVERT_D1_OSR_512 0x42
|
|
|
|
#define REG_CONVERT_D1_OSR_1024 0x44
|
|
|
|
#define REG_CONVERT_D1_OSR_2048 0x46
|
|
|
|
#define REG_CONVERT_D1_OSR_4096 0x48
|
|
|
|
#define REG_CONVERT_D2_OSR_256 0x50
|
|
|
|
#define REG_CONVERT_D2_OSR_512 0x52
|
|
|
|
#define REG_CONVERT_D2_OSR_1024 0x54
|
|
|
|
#define REG_CONVERT_D2_OSR_2048 0x56
|
|
|
|
#define REG_CONVERT_D2_OSR_4096 0x58
|
|
|
|
#define REG_ADC_READ 0x00
|
|
|
|
#define REG_PROM_BASE 0xA0
|
|
|
|
|
|
|
|
// go for 1024 oversampling. This should be fast enough to reduce
|
|
|
|
// noise but low enough to keep self-heating small
|
|
|
|
#define REG_CONVERT_PRESSURE REG_CONVERT_D1_OSR_1024
|
|
|
|
#define REG_CONVERT_TEMPERATURE REG_CONVERT_D2_OSR_1024
|
|
|
|
|
2017-11-03 03:17:34 -03:00
|
|
|
AP_Airspeed_MS5525::AP_Airspeed_MS5525(AP_Airspeed &_frontend, uint8_t _instance, MS5525_ADDR address) :
|
|
|
|
AP_Airspeed_Backend(_frontend, _instance)
|
2016-11-30 02:21:48 -04:00
|
|
|
{
|
2017-11-28 15:25:19 -04:00
|
|
|
_address = address;
|
2016-11-30 02:21:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// probe and initialise the sensor
|
|
|
|
bool AP_Airspeed_MS5525::init()
|
|
|
|
{
|
|
|
|
const uint8_t addresses[] = { MS5525D0_I2C_ADDR_1, MS5525D0_I2C_ADDR_2 };
|
|
|
|
bool found = false;
|
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(addresses); i++) {
|
2017-11-28 15:25:19 -04:00
|
|
|
if (_address != MS5525_ADDR_AUTO && i != (uint8_t)_address) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-30 02:21:48 -04:00
|
|
|
dev = hal.i2c_mgr->get_device(get_bus(), addresses[i]);
|
|
|
|
if (!dev) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-29 23:35:45 -03:00
|
|
|
WITH_SEMAPHORE(dev->get_semaphore());
|
2016-11-30 02:21:48 -04:00
|
|
|
|
|
|
|
// lots of retries during probe
|
|
|
|
dev->set_retries(5);
|
|
|
|
|
|
|
|
found = read_prom();
|
|
|
|
|
|
|
|
if (found) {
|
2021-06-29 23:00:29 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "MS5525[%u]: Found on bus %u addr 0x%02x", get_instance(), get_bus(), addresses[i]);
|
2016-11-30 02:21:48 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2021-11-03 17:19:47 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "MS5525[%u]: no sensor found", get_instance());
|
2016-11-30 02:21:48 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a command to read temperature first
|
2021-06-29 23:35:45 -03:00
|
|
|
WITH_SEMAPHORE(dev->get_semaphore());
|
2016-11-30 02:21:48 -04:00
|
|
|
uint8_t reg = REG_CONVERT_TEMPERATURE;
|
|
|
|
dev->transfer(®, 1, nullptr, 0);
|
|
|
|
state = 0;
|
2017-12-21 00:04:13 -04:00
|
|
|
command_send_us = AP_HAL::micros();
|
2016-11-30 02:21:48 -04:00
|
|
|
|
2021-06-29 23:35:45 -03:00
|
|
|
dev->set_device_type(uint8_t(DevType::MS5525));
|
|
|
|
set_bus_id(dev->get_bus_id());
|
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
// drop to 2 retries for runtime
|
|
|
|
dev->set_retries(2);
|
|
|
|
|
|
|
|
// read at 80Hz
|
|
|
|
dev->register_periodic_callback(1000000UL/80U,
|
2017-01-13 15:26:14 -04:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_Airspeed_MS5525::timer, void));
|
2016-11-30 02:21:48 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CRC used by MS pressure devices
|
|
|
|
*/
|
|
|
|
uint16_t AP_Airspeed_MS5525::crc4_prom(void)
|
|
|
|
{
|
2019-04-24 03:52:34 -03:00
|
|
|
return crc_crc4(prom);
|
2016-11-30 02:21:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Airspeed_MS5525::read_prom(void)
|
|
|
|
{
|
|
|
|
// reset the chip to ensure it has correct prom values loaded
|
|
|
|
uint8_t reg = REG_RESET;
|
|
|
|
if (!dev->transfer(®, 1, nullptr, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hal.scheduler->delay(5);
|
|
|
|
|
|
|
|
bool all_zero = true;
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
2017-01-13 13:28:06 -04:00
|
|
|
be16_t val;
|
|
|
|
if (!dev->read_registers(REG_PROM_BASE+i*2, (uint8_t *) &val,
|
|
|
|
sizeof(uint16_t))) {
|
2016-11-30 02:21:48 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-13 13:28:06 -04:00
|
|
|
prom[i] = be16toh(val);
|
2016-11-30 02:21:48 -04:00
|
|
|
if (prom[i] != 0) {
|
|
|
|
all_zero = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_zero) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the read crc */
|
|
|
|
const uint16_t crc_read = prom[7] & 0xf;
|
|
|
|
|
|
|
|
/* remove CRC byte */
|
|
|
|
prom[7] &= 0xff00;
|
|
|
|
|
|
|
|
uint16_t crc_calc = crc4_prom();
|
|
|
|
if (crc_read != crc_calc) {
|
|
|
|
printf("MS5525: CRC mismatch 0x%04x 0x%04x\n", crc_read, crc_calc);
|
|
|
|
}
|
|
|
|
return crc_read == crc_calc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
read from the ADC
|
|
|
|
*/
|
|
|
|
int32_t AP_Airspeed_MS5525::read_adc()
|
|
|
|
{
|
|
|
|
uint8_t val[3];
|
|
|
|
if (!dev->read_registers(REG_ADC_READ, val, 3)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (val[0] << 16) | (val[1] << 8) | val[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
calculate pressure and temperature
|
|
|
|
*/
|
|
|
|
void AP_Airspeed_MS5525::calculate(void)
|
|
|
|
{
|
|
|
|
// table for the 001DS part, 1PSI range
|
|
|
|
const uint8_t Q1 = 15;
|
|
|
|
const uint8_t Q2 = 17;
|
|
|
|
const uint8_t Q3 = 7;
|
|
|
|
const uint8_t Q4 = 5;
|
|
|
|
const uint8_t Q5 = 7;
|
|
|
|
const uint8_t Q6 = 21;
|
|
|
|
|
2021-08-16 23:16:15 -03:00
|
|
|
float dT = float(D2) - int64_t(prom[5]) * (1L<<Q5);
|
|
|
|
float TEMP = 2000 + (dT*int64_t(prom[6]))/(1L<<Q6);
|
|
|
|
float OFF = int64_t(prom[2])*(1L<<Q2) + (int64_t(prom[4])*dT)/(1L<<Q4);
|
|
|
|
float SENS = int64_t(prom[1])*(1L<<Q1) + (int64_t(prom[3])*dT)/(1L<<Q3);
|
|
|
|
float P = (float(D1)*SENS/(1L<<21)-OFF)/(1L<<15);
|
2016-11-30 02:21:48 -04:00
|
|
|
const float PSI_to_Pa = 6894.757f;
|
|
|
|
float P_Pa = PSI_to_Pa * 1.0e-4 * P;
|
|
|
|
float Temp_C = TEMP * 0.01;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static uint16_t counter;
|
|
|
|
if (counter++ == 100) {
|
|
|
|
printf("P=%.6f T=%.2f D1=%d D2=%d\n", P_Pa, Temp_C, D1, D2);
|
|
|
|
counter=0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-11 20:35:03 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
|
|
|
|
pressure_sum += P_Pa;
|
|
|
|
temperature_sum += Temp_C;
|
|
|
|
press_count++;
|
|
|
|
temp_count++;
|
|
|
|
last_sample_time_ms = AP_HAL::millis();
|
2016-11-30 02:21:48 -04:00
|
|
|
}
|
|
|
|
|
2018-07-11 23:05:12 -03:00
|
|
|
// 80Hz timer
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_Airspeed_MS5525::timer()
|
2016-11-30 02:21:48 -04:00
|
|
|
{
|
2017-12-21 00:04:13 -04:00
|
|
|
if (AP_HAL::micros() - command_send_us < 10000) {
|
|
|
|
// we should avoid trying to read the ADC too soon after
|
|
|
|
// sending the command
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
uint32_t adc_val = read_adc();
|
|
|
|
|
2017-12-21 00:04:13 -04:00
|
|
|
if (adc_val == 0) {
|
|
|
|
// we have either done a read too soon after sending the
|
|
|
|
// request or we have tried to read the same sample twice. We
|
|
|
|
// re-send the command now as we don't know what state the
|
|
|
|
// sensor is in, and we do want to trigger it sending a value,
|
|
|
|
// which we will discard
|
|
|
|
if (dev->transfer(&cmd_sent, 1, nullptr, 0)) {
|
|
|
|
command_send_us = AP_HAL::micros();
|
|
|
|
}
|
|
|
|
// when we get adc_val == 0 then then both the current value and
|
|
|
|
// the next value we read from the sensor are invalid
|
|
|
|
ignore_next = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
/*
|
|
|
|
* If read fails, re-initiate a read command for current state or we are
|
|
|
|
* stuck
|
|
|
|
*/
|
2017-12-21 00:04:13 -04:00
|
|
|
if (!ignore_next) {
|
|
|
|
if (cmd_sent == REG_CONVERT_TEMPERATURE) {
|
2016-11-30 02:21:48 -04:00
|
|
|
D2 = adc_val;
|
2017-12-21 00:04:13 -04:00
|
|
|
} else if (cmd_sent == REG_CONVERT_PRESSURE) {
|
2016-11-30 02:21:48 -04:00
|
|
|
D1 = adc_val;
|
|
|
|
calculate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 00:04:13 -04:00
|
|
|
ignore_next = false;
|
|
|
|
|
|
|
|
cmd_sent = (state == 0) ? REG_CONVERT_TEMPERATURE : REG_CONVERT_PRESSURE;
|
|
|
|
if (!dev->transfer(&cmd_sent, 1, nullptr, 0)) {
|
|
|
|
// we don't know for sure what state the sensor is in when we
|
|
|
|
// fail to send the command, so ignore the next response
|
|
|
|
ignore_next = true;
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-11-30 02:21:48 -04:00
|
|
|
}
|
2017-12-21 00:04:13 -04:00
|
|
|
command_send_us = AP_HAL::micros();
|
2016-11-30 02:21:48 -04:00
|
|
|
|
2017-12-21 00:04:13 -04:00
|
|
|
state = (state + 1) % 5;
|
2016-11-30 02:21:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the current differential_pressure in Pascal
|
|
|
|
bool AP_Airspeed_MS5525::get_differential_pressure(float &_pressure)
|
|
|
|
{
|
2019-10-12 03:19:33 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
if ((AP_HAL::millis() - last_sample_time_ms) > 100) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
|
|
|
if (press_count > 0) {
|
|
|
|
pressure = pressure_sum / press_count;
|
|
|
|
press_count = 0;
|
|
|
|
pressure_sum = 0;
|
2016-11-30 02:21:48 -04:00
|
|
|
}
|
|
|
|
_pressure = pressure;
|
2018-10-11 20:35:03 -03:00
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the current temperature in degrees C, if available
|
|
|
|
bool AP_Airspeed_MS5525::get_temperature(float &_temperature)
|
|
|
|
{
|
2019-10-12 03:19:33 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
if ((AP_HAL::millis() - last_sample_time_ms) > 100) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
|
|
|
if (temp_count > 0) {
|
|
|
|
temperature = temperature_sum / temp_count;
|
|
|
|
temp_count = 0;
|
|
|
|
temperature_sum = 0;
|
2016-11-30 02:21:48 -04:00
|
|
|
}
|
2018-10-11 20:35:03 -03:00
|
|
|
|
2016-11-30 02:21:48 -04:00
|
|
|
_temperature = temperature;
|
|
|
|
return true;
|
|
|
|
}
|
2022-05-04 05:13:29 -03:00
|
|
|
|
|
|
|
#endif // AP_AIRSPEED_MS5525_ENABLED
|