Adding a new differential pressure sensor ASP5033 (#21568)

* added a new driver ASP5033 for measuring the differential pressure and airspeed

---------

Co-authored-by: nano <nanobotzhe@abv.bg>
Co-authored-by: Denislav Petrov <you@example.com>
This commit is contained in:
Denislav Petrov 2023-09-05 01:21:05 +03:00 committed by GitHub
parent fb30b4d288
commit ece338ceef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 591 additions and 0 deletions

View File

@ -172,6 +172,12 @@ then
ms5525dso start -X
fi
# TE ASP5033 differential pressure sensor external I2C
if param compare -s SENS_EN_ASP5033 1
then
asp5033 start -X
fi
# SHT3x temperature and hygrometer sensor, external I2C
if param compare -s SENS_EN_SHT3X 1
then

View File

@ -0,0 +1,258 @@
/****************************************************************************
*
*
* Copyright (c) 2023 PX4 Development Team. All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file ASP5033.cpp
*
*@author Denislav Petrov <denislavamitoba@gmail.com>
*/
#include "ASP5033.hpp"
ASP5033::ASP5033(const I2CSPIDriverConfig &config) :
I2C(config),
I2CSPIDriver(config)
{
}
ASP5033::~ASP5033()
{
perf_free(_sample_perf);
perf_free(_comms_errors);
perf_free(_fault_perf);
}
int ASP5033::probe()
{
int ret = sensor_id_check();
return ret;
}
int ASP5033::sensor_id_check()
{
uint8_t id[1];
uint8_t cmd_1 = REG_ID_SET_ASP5033;
uint8_t cmd_2 = REG_WHOAMI_RECHECK_ID_ASP5033;
uint8_t cmd_3 = REG_ID_ASP5033;
uint8_t cmd_1_2[2];
cmd_1_2[0] = static_cast<uint8_t>(cmd_1);
cmd_1_2[1] = static_cast<uint8_t>(cmd_2);
if ((transfer(&cmd_1, 1, &id[0], sizeof(id)) != PX4_OK) || (*id != REG_WHOAMI_DEFAULT_ID_ASP5033)) { return 0; }
if (transfer(&cmd_1_2[0], 2, nullptr, 0) != PX4_OK) { return 0; }
if ((transfer(&cmd_3, 1, &id[0], sizeof(id)) != PX4_OK) || (*id != REG_WHOAMI_RECHECK_ID_ASP5033)) { return 0; }
return 1;
}
int ASP5033::init()
{
int ret = I2C::init();
if (ret != PX4_OK) {
DEVICE_DEBUG("I2C::init failed (%i)", ret);
return ret;
}
ScheduleNow();
return ret;
}
/**
* @brief calculation of the differential pressure in this way:
* it collect all measured pressure and store it into press_sum,
* count the value of collected times-press_count, then divide both
* and get the actual value of differential pressure - _pressure
*
* @return true if pressure is valid and no errors, false if not
*/
bool ASP5033::get_differential_pressure()
{
if (hrt_elapsed_time(&last_sample_time) > 200_ms) {
return false;
}
if (press_count == 0) {
return false;
}
//calculation differential pressure
_pressure = press_sum / press_count;
press_sum = 0.;
press_count = 0;
return true;
}
void ASP5033::print_status()
{
I2CSPIDriverBase::print_status();
perf_print_counter(_sample_perf);
perf_print_counter(_comms_errors);
perf_print_counter(_fault_perf);
}
void ASP5033::RunImpl()
{
int ret = PX4_ERROR;
// collection phase
if (_collect_phase) {
// perform collection
ret = collect();
if (OK != ret) {
perf_count(_comms_errors);
/* restart the measurement state machine */
_collect_phase = false;
_sensor_ok = false;
ScheduleNow();
return;
}
// next phase is measurement
_collect_phase = false;
// is there a collect->measure gap?
if (_measure_interval > CONVERSION_INTERVAL) {
// schedule a fresh cycle call when we are ready to measure again
ScheduleDelayed(_measure_interval - CONVERSION_INTERVAL);
return;
}
}
/* measurement phase */
ret = measure();
if (OK != ret) {
DEVICE_DEBUG("measure error");
}
_sensor_ok = (ret == OK);
// next phase is collection
_collect_phase = true;
// schedule a fresh cycle call when the measurement is done
ScheduleDelayed(CONVERSION_INTERVAL);
}
int ASP5033::measure()
{
// Send the command to begin a measurement.
uint8_t cmd_1 = CMD_MEASURE_ASP5033;
uint8_t cmd_2 = REG_CMD_ASP5033;;
//write to driver to start
uint8_t cmd[2];
cmd[0] = static_cast<uint8_t>(cmd_2);
cmd[1] = static_cast<uint8_t>(cmd_1);
int ret = transfer(&cmd[0], 2, nullptr, 0);
if (OK != ret) {
perf_count(_comms_errors);
}
return ret;
}
int ASP5033::collect()
{
perf_begin(_sample_perf);
const hrt_abstime timestamp_sample = hrt_absolute_time();
// Read pressure and temperature as one block
uint8_t val[5] {0, 0, 0, 0, 0};
uint8_t cmd = REG_PRESS_DATA_ASP5033;
transfer(&cmd, 1, &val[0], sizeof(val));
//Pressure is a signed 24-bit value
int32_t press = (val[0] << 24) | (val[1] << 16) | (val[2] << 8);
// convert back to 24 bit
press >>= 8;
// k is a shift based on the pressure range of the device. See
// table in the datasheet
constexpr uint8_t k = 8;
constexpr float press_scale = 1.0f / (1U << k); //= 1.0f / (1U << k);
press_sum += press * press_scale;
press_count++;
// temperature is 16 bit signed in units of 1/256 C
const int16_t temp = (val[3] << 8) | val[4];
constexpr float temp_scale = 1.0f / 256;
_temperature = temp * temp_scale;
last_sample_time = hrt_absolute_time();
bool status = get_differential_pressure();
if (status == true && (int)_temperature != 0) {
// publish values
differential_pressure_s differential_pressure{};
differential_pressure.timestamp_sample = timestamp_sample;
differential_pressure.device_id = get_device_id();
differential_pressure.differential_pressure_pa = _pressure;
differential_pressure.temperature = _temperature ;
differential_pressure.error_count = perf_event_count(_comms_errors);
differential_pressure.timestamp = timestamp_sample;
_differential_pressure_pub.publish(differential_pressure);
}
perf_end(_sample_perf);
return PX4_OK;
}

View File

@ -0,0 +1,137 @@
/****************************************************************************
*
* Copyright (c) 2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file ASP5033.hpp
*
* Driver for ASP5033 connected via I2C.
*
* Supported sensors:
*
* - ASP5033
*
* Interface application notes:
*
*
*@author Denislav Petrov <denislavamitoba@gmail.com>
*/
#pragma once
#include <drivers/device/i2c.h>
#include <drivers/drv_hrt.h>
#include <lib/perf/perf_counter.h>
#include <px4_platform_common/i2c_spi_buses.h>
#include <uORB/PublicationMulti.hpp>
#include <uORB/topics/differential_pressure.h>
#include <px4_platform_common/module.h>
#include <lib/systemlib/mavlink_log.h>
/* Measurement rate is 100Hz */
#define MEAS_RATE 100
#define CONVERSION_INTERVAL (1000000 / MEAS_RATE) /* microseconds */
/* Configuration Constants */
static constexpr uint8_t I2C_ADDRESS_DEFAULT = 0x6D; /* 0x6D 0xE4 */
static constexpr uint32_t I2C_SPEED = 100 * 1000; // 100 kHz I2C serial interface
#define REG_CMD_ASP5033 0x30
#define REG_PRESS_DATA_ASP5033 0X06
#define REG_TEMP_DATA_ASP5033 0X09
#define CMD_MEASURE_ASP5033 0X0A
#define REG_WHOAMI_DEFAULT_ID_ASP5033 0X00
#define REG_WHOAMI_RECHECK_ID_ASP5033 0X66
#define REG_ID_ASP5033 0x01
#define REG_ID_SET_ASP5033 0xa4
using namespace time_literals;
class ASP5033 : public device::I2C, public I2CSPIDriver<ASP5033>
{
public:
ASP5033(const I2CSPIDriverConfig &config);
~ASP5033() override;
static void print_usage();
void print_status() override;
void RunImpl();
int init() override;
float press_sum;
uint32_t press_count;
private:
float _pressure = 0.f;
float _temperature = 0.f;
float _pressure_prev = 0.f;
float _temperaute_prev = 0.f;
int probe() override;
int measure();
int collect();
int sensor_id_check();
bool get_differential_pressure();
hrt_abstime last_sample_time = hrt_absolute_time();
orb_advert_t _mavlink_log_pub {nullptr}; //log send to
uint32_t _measure_interval{CONVERSION_INTERVAL};
uint32_t _conversion_interval{CONVERSION_INTERVAL};
bool _sensor_ok{false};
bool _collect_phase{false};
uORB::PublicationMulti<differential_pressure_s> _differential_pressure_pub{ORB_ID(differential_pressure)};
perf_counter_t _sample_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": read")};
perf_counter_t _comms_errors{perf_alloc(PC_COUNT, MODULE_NAME": communication errors")};
perf_counter_t _fault_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": fault detected")};
};

View File

@ -0,0 +1,43 @@
############################################################################
#
# Copyright (c) 2023 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
px4_add_module(
MODULE drivers__differential_pressure__asp5033
MAIN asp5033
COMPILE_FLAGS
SRCS
asp5033_main.cpp
ASP5033.cpp
ASP5033.hpp
DEPENDS
px4_work_queue
)

View File

@ -0,0 +1,5 @@
menuconfig DRIVERS_DIFFERENTIAL_PRESSURE_ASP5033
bool "asp5033"
default n
---help---
Enable support for asp5033

View File

@ -0,0 +1,94 @@
/****************************************************************************
*
* Copyright (c) 2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "ASP5033.hpp"
#include <px4_platform_common/getopt.h>
#include <px4_platform_common/module.h>
void ASP5033::print_usage()
{
PRINT_MODULE_DESCRIPTION(
R"DESCR_STR(
### Description
Driver to enable an external [ASP5033]
(https://www.qio-tek.com/index.php/product/qiotek-asp5033-dronecan-airspeed-and-compass-module/)
TE connected via I2C.
This is not included by default in firmware. It can be included with terminal command: "make <your_board> boardconfig"
or in default.px4board with adding the line: "CONFIG_DRIVERS_DIFFERENTIAL_PRESSURE_ASP5033=y"
It can be enabled with the "SENS_EN_ASP5033" parameter set to 1.
)DESCR_STR");
PRINT_MODULE_USAGE_NAME("asp5033", "driver");
PRINT_MODULE_USAGE_SUBCATEGORY("airspeed_sensor");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x6D);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
extern "C" int asp5033_main(int argc, char *argv[])
{
using ThisDriver = ASP5033;
BusCLIArguments cli{true, false};
cli.i2c_address = I2C_ADDRESS_DEFAULT;
cli.default_i2c_frequency = I2C_SPEED;
const char *verb = cli.parseDefaultArguments(argc, argv);
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_DIFF_PRESS_DEVTYPE_ASP5033);
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
} else if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
} else if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
}

View File

@ -0,0 +1,46 @@
/****************************************************************************
*
* Copyright (c) 2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* ASP5033 differential pressure sensor (external I2C)
*
* @reboot_required true
* @group Sensors
* @boolean
*/
PARAM_DEFINE_INT32(SENS_EN_ASP5033, 0);

View File

@ -115,6 +115,7 @@
#define DRV_DIFF_PRESS_DEVTYPE_SDP32 0x4B
#define DRV_DIFF_PRESS_DEVTYPE_SDP33 0x4C
#define DRV_BARO_DEVTYPE_TCBP001TA 0x4D
#define DRV_BARO_DEVTYPE_MS5837 0x4E
#define DRV_BARO_DEVTYPE_SPL06 0x4F
@ -234,6 +235,7 @@
#define DRV_INS_DEVTYPE_VN100 0xE1
#define DRV_INS_DEVTYPE_VN200 0xE2
#define DRV_INS_DEVTYPE_VN300 0xE3
#define DRV_DIFF_PRESS_DEVTYPE_ASP5033 0xE4
#define DRV_DEVTYPE_UNUSED 0xff