added analog_measurement module

This commit is contained in:
Jacob Dahl 2020-08-18 13:13:03 -08:00
parent d409860ff1
commit 03416f4d70
8 changed files with 282 additions and 2 deletions

View File

@ -56,6 +56,7 @@ px4_add_board(
#tone_alarm
uavcannode
MODULES
analog_measurement
#ekf2
#load_mon
sensors

View File

@ -541,7 +541,7 @@ void UavcanNode::send_analog_measurements()
com::volansi::equipment::adc::AnalogMeasurement report{};
for (size_t i = 0; i < sizeof(measurement.values)/sizeof(values[0])) {
for (size_t i = 0; i < sizeof(measurement.values)/sizeof(measurement.values[0]); i++) {
if (measurement.unit_type[i]) {
report.unit_type[i] = measurement.unit_type[i];
report.values[i] = measurement.values[i];

View File

@ -64,7 +64,7 @@
#include <uavcan/equipment/gnss/Fix2.hpp>
#include <uavcan/equipment/power/BatteryInfo.hpp>
#include <uavcan/equipment/range_sensor/Measurement.hpp>
#include <com/volansi/equipment/adc/Report.hpp>
#include <com/volansi/equipment/adc/AnalogMeasurement.hpp>
#include <lib/parameters/param.h>

View File

@ -0,0 +1,11 @@
px4_add_module(
MODULE modules__analog_measurement
MAIN analog_measurement
COMPILE_FLAGS
SRCS
analog_measurement.cpp
analog_measurement.hpp
DEPENDS
px4_work_queue
)

View File

@ -0,0 +1,151 @@
/****************************************************************************
*
* Copyright (c) 2013-2018 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 "analog_measurement.hpp"
AnalogMeasurement::AnalogMeasurement() :
ModuleParams(nullptr),
WorkItem(MODULE_NAME, px4::wq_configurations::lp_default)
{
}
bool
AnalogMeasurement::init()
{
if (!_adc_report_sub.registerCallback()) {
PX4_ERR("adc_report callback registration failed!");
return false;
}
return true;
}
void
AnalogMeasurement::Run()
{
if (should_exit()) {
_adc_report_sub.unregisterCallback();
exit_and_cleanup();
return;
}
adc_report_s report{};
if (_adc_report_sub.update(&report)) {
analog_measurement_s measurement{};
int adc1_unit = _adc1_unit.get();
int adc2_unit = _adc2_unit.get();
int adc3_unit = _adc3_unit.get();
int adc4_unit = _adc4_unit.get();
// TODO: determine size. Only read first 4 ADCs for now.
if (adc1_unit) {
measurement.values[0] = report.raw_data[0] *_adc1_scale.get();
measurement.unit_type[0] = adc1_unit;
}
if (adc2_unit) {
measurement.values[1] = report.raw_data[1] *_adc2_scale.get();
measurement.unit_type[1] = adc2_unit;
}
if (adc3_unit) {
measurement.values[2] = report.raw_data[2] *_adc3_scale.get();
measurement.unit_type[2] = adc3_unit;
}
if (adc4_unit) {
measurement.values[3] = report.raw_data[3] *_adc4_scale.get();
measurement.unit_type[3] = adc4_unit;
}
_analog_measurement_pub.publish(measurement);
}
}
int AnalogMeasurement::task_spawn(int argc, char *argv[])
{
AnalogMeasurement *instance = new AnalogMeasurement();
if (instance) {
_object.store(instance);
_task_id = task_id_is_work_queue;
if (instance->init()) {
return PX4_OK;
}
} else {
PX4_ERR("alloc failed");
}
delete instance;
_object.store(nullptr);
_task_id = -1;
return PX4_ERROR;
}
int AnalogMeasurement::custom_command(int argc, char *argv[])
{
return print_usage("unknown command");
}
int AnalogMeasurement::print_usage(const char *reason)
{
if (reason) {
PX4_WARN("%s\n", reason);
}
PRINT_MODULE_DESCRIPTION(
R"DESCR_STR(
### Description
This module reads the adc_report and converts the raw adc values into voltage,
current, or temperature depending on the parameter ADC[N]_UNIT and applies the
scale factor ADC[N]_SCALE.
)DESCR_STR");
PRINT_MODULE_USAGE_NAME("analog_measurement", "driver");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
return 0;
}
int analog_measurement_main(int argc, char *argv[])
{
return AnalogMeasurement::main(argc, argv);
}

View File

@ -0,0 +1,92 @@
/****************************************************************************
*
* Copyright (c) 2013-2019 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.
*
****************************************************************************/
#pragma once
#include <matrix/matrix/math.hpp>
#include <perf/perf_counter.h>
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/defines.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/module_params.h>
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
#include <uORB/Publication.hpp>
#include <uORB/Subscription.hpp>
#include <uORB/SubscriptionCallback.hpp>
#include <uORB/topics/adc_report.h>
#include <uORB/topics/analog_measurement.h>
extern "C" __EXPORT int analog_measurement_main(int argc, char *argv[]);
class AnalogMeasurement : public ModuleBase<AnalogMeasurement>, public ModuleParams,
public px4::WorkItem
{
public:
AnalogMeasurement();
/** @see ModuleBase */
static int task_spawn(int argc, char *argv[]);
/** @see ModuleBase */
static int custom_command(int argc, char *argv[]);
/** @see ModuleBase */
static int print_usage(const char *reason = nullptr);
bool init();
private:
void Run() override;
uORB::SubscriptionCallbackWorkItem _adc_report_sub{this, ORB_ID(adc_report)};
uORB::Publication<analog_measurement_s> _analog_measurement_pub{ORB_ID(analog_measurement)};
DEFINE_PARAMETERS(
(ParamFloat<px4::params::ADC1_SCALE>) _adc1_scale,
(ParamFloat<px4::params::ADC2_SCALE>) _adc2_scale,
(ParamFloat<px4::params::ADC3_SCALE>) _adc3_scale,
(ParamFloat<px4::params::ADC4_SCALE>) _adc4_scale,
(ParamInt<px4::params::ADC1_UNIT>) _adc1_unit,
(ParamInt<px4::params::ADC2_UNIT>) _adc2_unit,
(ParamInt<px4::params::ADC3_UNIT>) _adc3_unit,
(ParamInt<px4::params::ADC4_UNIT>) _adc4_unit
)
};

View File

@ -0,0 +1,25 @@
/**
* Scale factor for ADC to convert raw to units
*/
PARAM_DEFINE_FLOAT(ADC1_SCALE, 1.0);
PARAM_DEFINE_FLOAT(ADC2_SCALE, 1.0);
PARAM_DEFINE_FLOAT(ADC3_SCALE, 1.0);
PARAM_DEFINE_FLOAT(ADC4_SCALE, 1.0);
/**
* Unit type:
* 0: none
* 1: mV
* 2: mA
* 3: cK
*
* @min 0
* @max 3
* @group Analog Measurement
*/
PARAM_DEFINE_INT32(ADC1_UNIT, 0);
PARAM_DEFINE_INT32(ADC2_UNIT, 0);
PARAM_DEFINE_INT32(ADC3_UNIT, 0);
PARAM_DEFINE_INT32(ADC4_UNIT, 0);