mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-25 01:07:34 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1396b9d36e | |||
| 2dac4f83dc | |||
| fb04d313fc | |||
| ca3eb138b9 |
@@ -219,6 +219,12 @@ then
|
||||
pcf8583 start -X -a 0x51
|
||||
fi
|
||||
|
||||
# ADC sensor ADS7953 external SPI
|
||||
if param compare -s ADC_ADS7953_EN 1
|
||||
then
|
||||
ads7953 start -S
|
||||
fi
|
||||
|
||||
# probe for optional external I2C devices
|
||||
if param compare SENS_EXT_I2C_PRB 1
|
||||
then
|
||||
|
||||
@@ -8,6 +8,7 @@ CONFIG_BOARD_SERIAL_TEL2="/dev/ttyS4"
|
||||
CONFIG_BOARD_SERIAL_TEL3="/dev/ttyS1"
|
||||
CONFIG_BOARD_SERIAL_EXT2="/dev/ttyS3"
|
||||
CONFIG_BOARD_SERIAL_RC="/dev/ttyS5"
|
||||
CONFIG_DRIVERS_ADC_ADS7953=y
|
||||
CONFIG_DRIVERS_ADC_ADS1115=y
|
||||
CONFIG_DRIVERS_ADC_BOARD_ADC=y
|
||||
CONFIG_DRIVERS_BAROMETER_BMP388=y
|
||||
|
||||
+5
-2
@@ -1,6 +1,9 @@
|
||||
# ADC raw data.
|
||||
#
|
||||
# Communicates raw data from an analog-to-digital converter (ADC) to other modules, such as battery status.
|
||||
uint64 timestamp # time since system start (microseconds)
|
||||
uint32 device_id # unique device ID for the sensor that does not change between power cycles
|
||||
int16[12] channel_id # ADC channel IDs, negative for non-existent, TODO: should be kept same as array index
|
||||
int32[12] raw_data # ADC channel raw value, accept negative value, valid if channel ID is positive
|
||||
int16[16] channel_id # ADC channel IDs, negative for non-existent, TODO: should be kept same as array index
|
||||
int32[16] raw_data # ADC channel raw value, accept negative value, valid if channel ID is positive
|
||||
uint32 resolution # ADC channel resolution
|
||||
float32 v_ref # ADC channel voltage reference, use to calculate LSB voltage(lsb=scale/resolution)
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
|
||||
#include "ADS7953.h"
|
||||
#include <px4_platform_common/getopt.h>
|
||||
#include <px4_platform_common/module.h>
|
||||
#include <drivers/drv_adc.h>
|
||||
|
||||
ADS7953::ADS7953(const I2CSPIDriverConfig &config) :
|
||||
SPI(config),
|
||||
I2CSPIDriver(config)
|
||||
{
|
||||
}
|
||||
|
||||
int ADS7953::init()
|
||||
{
|
||||
int ret = SPI::init();
|
||||
|
||||
if (ret != PX4_OK) {
|
||||
DEVICE_DEBUG("SPI::init failed (%i)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
float ref_volt = 2.5f;
|
||||
param_get(param_find("ADC_ADS7953_REFV"), &ref_volt);
|
||||
|
||||
_adc_report.device_id = this->get_device_id();
|
||||
_adc_report.v_ref = ref_volt;
|
||||
_adc_report.resolution = 4096;
|
||||
|
||||
for (unsigned i = 0; i < PX4_MAX_ADC_CHANNELS; ++i) {
|
||||
_adc_report.channel_id[i] = -1;
|
||||
}
|
||||
|
||||
ScheduleNow();
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int ADS7953::probe()
|
||||
{
|
||||
// The ADS7953 has no ID register which we can check, so we verify the device via the returned channel ID.
|
||||
// We set the mode to "manual mode" and the channel to measure to 1.
|
||||
// If the returned channel ID on the third message is 1, we assume the ADS7953 is connected.
|
||||
uint8_t recv_data[2];
|
||||
|
||||
int ret = rw_msg(&recv_data[0], 1, true);
|
||||
|
||||
if (ret != PX4_OK) {
|
||||
DEVICE_DEBUG("ADS7953 probing failed (%i)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = rw_msg(&recv_data[0], 0, false);
|
||||
ret = rw_msg(&recv_data[0], 0, true);
|
||||
|
||||
if (ret != PX4_OK || (recv_data[0] >> 4) != 1U) {
|
||||
DEVICE_DEBUG("ADS7953 probing failed (%i)", ret);
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
PX4_INFO("ADS7953 was found");
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
|
||||
int ADS7953::rw_msg(uint8_t *recv_data, uint8_t ch, bool change_channel)
|
||||
{
|
||||
uint8_t send_data[2];
|
||||
|
||||
if (change_channel) {
|
||||
send_data[0] = 0x10 | (ch >> 1);
|
||||
send_data[1] = 0x00 | (ch << 7);
|
||||
|
||||
} else {
|
||||
send_data[0] = 0x00;
|
||||
send_data[1] = 0x00;
|
||||
}
|
||||
|
||||
return transfer(&send_data[0], &recv_data[0], 2);
|
||||
}
|
||||
|
||||
int ADS7953::get_measurements()
|
||||
{
|
||||
uint8_t recv_data[2];
|
||||
uint8_t ch_id = 0;
|
||||
|
||||
int count = 0;
|
||||
uint16_t mask = 0x00;
|
||||
uint8_t idx = 0;
|
||||
|
||||
while (count < 16) {
|
||||
if (rw_msg(&recv_data[0], idx, true) == PX4_OK) {
|
||||
ch_id = (recv_data[0] >> 4);
|
||||
|
||||
//check if we already have a measurement for the returned channel
|
||||
if (!(mask & (1U << ch_id))) {
|
||||
mask |= (1U << ch_id);
|
||||
count++;
|
||||
_adc_report.channel_id[ch_id] = ch_id;
|
||||
_adc_report.raw_data[ch_id] = ((((uint16_t) recv_data[0]) & 0x0F) << 8) | recv_data[1]; //data_value;
|
||||
}
|
||||
}
|
||||
|
||||
// Find index to measure next
|
||||
for (int i = 1; i <= 16; i++) {
|
||||
uint8_t candidate_id = (idx + i) % 16;
|
||||
|
||||
if (!(mask & (1U << candidate_id))) {
|
||||
idx = candidate_id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void ADS7953::RunImpl()
|
||||
{
|
||||
get_measurements();
|
||||
_adc_report.timestamp = hrt_absolute_time();
|
||||
_to_adc_report.publish(_adc_report);
|
||||
|
||||
for (unsigned i = 0; i < PX4_MAX_ADC_CHANNELS; ++i) {
|
||||
_adc_report.channel_id[i] = -1;
|
||||
}
|
||||
|
||||
ScheduleDelayed(10_ms);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <drivers/device/spi.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <px4_platform_common/i2c_spi_buses.h>
|
||||
#include <lib/drivers/device/spi.h>
|
||||
#include <lib/parameters/param.h>
|
||||
#include <uORB/topics/adc_report.h>
|
||||
#include <uORB/PublicationMulti.hpp>
|
||||
using namespace time_literals;
|
||||
|
||||
|
||||
class ADS7953 : public device::SPI, public I2CSPIDriver<ADS7953>
|
||||
{
|
||||
public:
|
||||
ADS7953(const I2CSPIDriverConfig &config);
|
||||
virtual ~ADS7953() = default;
|
||||
static void print_usage();
|
||||
|
||||
int init() override;
|
||||
void RunImpl();
|
||||
int probe() override;
|
||||
|
||||
|
||||
private:
|
||||
uORB::PublicationMulti<adc_report_s> _to_adc_report{ORB_ID(adc_report)};
|
||||
|
||||
static const hrt_abstime SAMPLE_INTERVAL{50_ms};
|
||||
|
||||
adc_report_s _adc_report{};
|
||||
|
||||
int get_measurements();
|
||||
int rw_msg(uint8_t *recv_data, uint8_t ch, bool change_channel);
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
############################################################################
|
||||
#
|
||||
# Copyright (c) 2025 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__adc__ads7953
|
||||
MAIN ads7953
|
||||
COMPILE_FLAGS
|
||||
SRCS
|
||||
ADS7953.cpp
|
||||
ADS7953.h
|
||||
ads7953_main.cpp
|
||||
DEPENDS
|
||||
px4_work_queue
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
menuconfig DRIVERS_ADC_ADS7953
|
||||
bool "ADS7953 driver"
|
||||
default n
|
||||
---help---
|
||||
Enable support for ADS7953
|
||||
@@ -0,0 +1,77 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2025 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 <px4_platform_common/getopt.h>
|
||||
#include <px4_platform_common/module.h>
|
||||
#include "ADS7953.h"
|
||||
|
||||
void ADS7953::print_usage()
|
||||
{
|
||||
PRINT_MODULE_USAGE_NAME("ads7953", "driver");
|
||||
PRINT_MODULE_USAGE_SUBCATEGORY("adc");
|
||||
PRINT_MODULE_USAGE_COMMAND("start");
|
||||
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(false, true);
|
||||
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
|
||||
}
|
||||
|
||||
extern "C" int ads7953_main(int argc, char *argv[])
|
||||
{
|
||||
using ThisDriver = ADS7953;
|
||||
BusCLIArguments cli{false, true};
|
||||
cli.spi_mode = SPIDEV_MODE0;
|
||||
cli.default_spi_frequency = 10 * 1000 * 1000;
|
||||
const char *name = MODULE_NAME;
|
||||
const char *verb = cli.parseDefaultArguments(argc, argv);
|
||||
|
||||
if (!verb) {
|
||||
ThisDriver::print_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
BusInstanceIterator iterator(name, cli, DRV_ADC_DEVTYPE_ADS7953);
|
||||
|
||||
if (!strcmp(verb, "start")) {
|
||||
return ThisDriver::module_start(cli, iterator);
|
||||
}
|
||||
|
||||
if (!strcmp(verb, "stop")) {
|
||||
return ThisDriver::module_stop(iterator);
|
||||
}
|
||||
|
||||
if (!strcmp(verb, "status")) {
|
||||
return ThisDriver::module_status(iterator);
|
||||
}
|
||||
|
||||
ThisDriver::print_usage();
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2025 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* Enable external ADS7953 ADC
|
||||
*
|
||||
* @category driver
|
||||
* @boolean
|
||||
* @reboot_required true
|
||||
* @group Sensors
|
||||
*/
|
||||
PARAM_DEFINE_INT32(ADC_ADS7953_EN, 0);
|
||||
|
||||
/**
|
||||
* Reference voltage supplied to ADS7953 board
|
||||
*
|
||||
* @reboot_required true
|
||||
* @category driver
|
||||
* @group Sensors
|
||||
* @unit V
|
||||
* @min 2.0
|
||||
* @max 3.0
|
||||
* @decimal 2
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(ADC_ADS7953_REFV, 2.5f);
|
||||
|
||||
@category system
|
||||
* @group Sensors
|
||||
@@ -260,6 +260,8 @@
|
||||
|
||||
#define DRV_INS_DEVTYPE_SBG 0xEC
|
||||
|
||||
#define DRV_ADC_DEVTYPE_ADS7953 0xED
|
||||
|
||||
#define DRV_DEVTYPE_UNUSED 0xff
|
||||
|
||||
#endif /* _DRV_SENSOR_H */
|
||||
|
||||
Reference in New Issue
Block a user