diff --git a/boards/px4/fmu-v4/cannode.cmake b/boards/px4/fmu-v4/cannode.cmake index 934dea5e9c..7e4b1172e7 100644 --- a/boards/px4/fmu-v4/cannode.cmake +++ b/boards/px4/fmu-v4/cannode.cmake @@ -56,6 +56,7 @@ px4_add_board( #tone_alarm uavcannode MODULES + analog_measurement #ekf2 #load_mon sensors diff --git a/src/drivers/uavcannode/UavcanNode.cpp b/src/drivers/uavcannode/UavcanNode.cpp index 478bac5719..3656ed95d3 100644 --- a/src/drivers/uavcannode/UavcanNode.cpp +++ b/src/drivers/uavcannode/UavcanNode.cpp @@ -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]; diff --git a/src/drivers/uavcannode/UavcanNode.hpp b/src/drivers/uavcannode/UavcanNode.hpp index b2bf4ab4e2..11a0227f4f 100644 --- a/src/drivers/uavcannode/UavcanNode.hpp +++ b/src/drivers/uavcannode/UavcanNode.hpp @@ -64,7 +64,7 @@ #include #include #include -#include +#include #include diff --git a/src/drivers/uavcannode/dsdl/com/volansi/equipment/adc/20201.Report.uavcan b/src/drivers/uavcannode/dsdl/com/volansi/equipment/adc/20201.AnalogMeasurement.uavcan similarity index 100% rename from src/drivers/uavcannode/dsdl/com/volansi/equipment/adc/20201.Report.uavcan rename to src/drivers/uavcannode/dsdl/com/volansi/equipment/adc/20201.AnalogMeasurement.uavcan diff --git a/src/modules/analog_measurement/CMakeLists.txt b/src/modules/analog_measurement/CMakeLists.txt new file mode 100644 index 0000000000..850b3e9171 --- /dev/null +++ b/src/modules/analog_measurement/CMakeLists.txt @@ -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 + ) diff --git a/src/modules/analog_measurement/analog_measurement.cpp b/src/modules/analog_measurement/analog_measurement.cpp new file mode 100644 index 0000000000..3e58c273ea --- /dev/null +++ b/src/modules/analog_measurement/analog_measurement.cpp @@ -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); +} \ No newline at end of file diff --git a/src/modules/analog_measurement/analog_measurement.hpp b/src/modules/analog_measurement/analog_measurement.hpp new file mode 100644 index 0000000000..7abee90988 --- /dev/null +++ b/src/modules/analog_measurement/analog_measurement.hpp @@ -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 + +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +extern "C" __EXPORT int analog_measurement_main(int argc, char *argv[]); + +class AnalogMeasurement : public ModuleBase, 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_pub{ORB_ID(analog_measurement)}; + + DEFINE_PARAMETERS( + (ParamFloat) _adc1_scale, + (ParamFloat) _adc2_scale, + (ParamFloat) _adc3_scale, + (ParamFloat) _adc4_scale, + + (ParamInt) _adc1_unit, + (ParamInt) _adc2_unit, + (ParamInt) _adc3_unit, + (ParamInt) _adc4_unit + ) +}; + diff --git a/src/modules/analog_measurement/analog_measurement_params.c b/src/modules/analog_measurement/analog_measurement_params.c new file mode 100644 index 0000000000..7302e5ad2c --- /dev/null +++ b/src/modules/analog_measurement/analog_measurement_params.c @@ -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); + +