diff --git a/ROMFS/cannode/init.d/rcS b/ROMFS/cannode/init.d/rcS index 6e53056245..9242ee23f8 100644 --- a/ROMFS/cannode/init.d/rcS +++ b/ROMFS/cannode/init.d/rcS @@ -65,8 +65,11 @@ unset BOARD_RC_DEFAULTS # # Start system state indicator. # -rgbled start -X -q -rgbled_ncp5623c start -X -q +if [ $UCAN146 == none ] +then + rgbled start -X -q + rgbled_ncp5623c start -X -q +fi # # board sensors: rc.sensors @@ -86,10 +89,16 @@ unset BOARD_RC_SENSORS . ${R}etc/init.d/rc.serial # Check for flow sensor -if param compare SENS_EN_PX4FLOW 1 +if [ $UCAN146 == none ] then - px4flow start -X & + if param compare SENS_EN_PX4FLOW 1 + then + px4flow start -X & + fi fi -uavcannode start +if [ $UCAN146 == none ] +then + uavcannode start +fi unset R diff --git a/boards/nxp/ucans32k146/init/rc.board_defaults b/boards/nxp/ucans32k146/init/rc.board_defaults index f9775ee814..adf945bb4a 100644 --- a/boards/nxp/ucans32k146/init/rc.board_defaults +++ b/boards/nxp/ucans32k146/init/rc.board_defaults @@ -3,7 +3,9 @@ # board specific defaults #------------------------------------------------------------------------------ -pwm_out mode_pwm1 start +set UCAN146 1 + +pca9685_ucan start -X ifup can0 cyphal start diff --git a/boards/nxp/ucans32k146/pca9685.px4board b/boards/nxp/ucans32k146/pca9685.px4board index 85d92f3ebf..78c01dd376 100644 --- a/boards/nxp/ucans32k146/pca9685.px4board +++ b/boards/nxp/ucans32k146/pca9685.px4board @@ -1 +1,3 @@ CONFIG_DRIVERS_PCA9685_UCAN=y +CONFIG_DRIVERS_GPS=n +CONFIG_EXAMPLES_PCA_PWM_TEST=y diff --git a/src/drivers/pca9685_ucan/pca9685_ucan.cpp b/src/drivers/pca9685_ucan/pca9685_ucan.cpp index 00de50fbca..840ea2dcea 100644 --- a/src/drivers/pca9685_ucan/pca9685_ucan.cpp +++ b/src/drivers/pca9685_ucan/pca9685_ucan.cpp @@ -240,19 +240,15 @@ PCA9685::RunImpl() _pca_pwm.pwm_period <= PWM_PERIOD_MAX_US) { _pwm_period_us = _pca_pwm.pwm_period; _pwm_freq = 1000000.0f / (float)_pwm_period_us; - DEVICE_DEBUG("freq: %.2f, period: %u", (double)_pwm_freq, _pwm_period_us); } for (int i = 0; i < NUMBER_PWM_CHANNELS; i++) { - uint16_t new_value = (uint16_t)(((float)_pwm_period_us/(float)_pca_pwm.pulse_width[i])*4096.0f); - DEVICE_DEBUG("%d: current: %u, new: %u, pulse width: %u", i, _current_values[i], new_value, _pca_pwm.pulse_width[i]); - - if (new_value != _current_values[i] && new_value < 4096) { + uint16_t new_value = (uint16_t)(4096.0f*(float)_pca_pwm.pulse_width[i]/ + (float)_pwm_period_us); + /*PX4_INFO("PWM_%d: pulse_width: %u, new_value: %u, pwm_period: %u", + i, _pca_pwm.pulse_width[i], new_value, _pwm_period_us);*/ + if (new_value != _current_values[i] && new_value <= 4096) { setPin(i, new_value); _current_values[i] = new_value; - } else { - if (new_value < 4096) { - DEVICE_DEBUG("pwm new value: %u is out of range [0, 4096]", new_value); - } } } } diff --git a/src/examples/pca_pwm_test/CMakeLists.txt b/src/examples/pca_pwm_test/CMakeLists.txt new file mode 100644 index 0000000000..c7bb84f29b --- /dev/null +++ b/src/examples/pca_pwm_test/CMakeLists.txt @@ -0,0 +1,45 @@ +############################################################################ +# +# Copyright (c) 2022 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 examples__pca_pwm_test + MAIN pca_pwm_test + COMPILE_FLAGS + #-DDEBUG_BUILD # uncomment for PX4_DEBUG output + #-O0 # uncomment when debugging + SRCS + PCAPWMTest.cpp + PCAPWMTest.hpp + DEPENDS + px4_work_queue + ) diff --git a/src/examples/pca_pwm_test/Kconfig b/src/examples/pca_pwm_test/Kconfig new file mode 100644 index 0000000000..e40384ceaf --- /dev/null +++ b/src/examples/pca_pwm_test/Kconfig @@ -0,0 +1,5 @@ +menuconfig EXAMPLES_PCA_PWM_TEST + bool "pca_pwm_test" + default n + ---help--- + Enable support for pca_pwm_test diff --git a/src/examples/pca_pwm_test/PCAPWMTest.cpp b/src/examples/pca_pwm_test/PCAPWMTest.cpp new file mode 100644 index 0000000000..5c5f9f93da --- /dev/null +++ b/src/examples/pca_pwm_test/PCAPWMTest.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** + * + * Copyright (c) 2022 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 "PCAPWMTest.hpp" + +PCAPWMTest::PCAPWMTest() : + ModuleParams(nullptr), + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::test1) +{ +} + +PCAPWMTest::~PCAPWMTest() +{ + perf_free(_loop_perf); + perf_free(_loop_interval_perf); +} + +bool PCAPWMTest::init() +{ + ScheduleOnInterval(100_ms); // 2000 us interval, 200 Hz rate + + return true; +} + +void PCAPWMTest::Run() +{ + if (should_exit()) { + ScheduleClear(); + exit_and_cleanup(); + return; + } + + perf_begin(_loop_perf); + perf_count(_loop_interval_perf); + + pca_pwm.pwm_period = period_us; + + if(val > period_us) val = 0; + + + for(int i = 0; i < 16; i++) { + pca_pwm.pulse_width[i] = val; + } + + pca_pwm.timestamp = hrt_absolute_time(); + _pca_pwm_pub.publish(pca_pwm); + + val += period_us/20; + + perf_end(_loop_perf); +} + +int PCAPWMTest::task_spawn(int argc, char *argv[]) +{ + PCAPWMTest *instance = new PCAPWMTest(); + + 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 PCAPWMTest::print_status() +{ + perf_print_counter(_loop_perf); + perf_print_counter(_loop_interval_perf); + return 0; +} + +int PCAPWMTest::custom_command(int argc, char *argv[]) +{ + return print_usage("unknown command"); +} + +int PCAPWMTest::print_usage(const char *reason) +{ + if (reason) { + PX4_WARN("%s\n", reason); + } + + PRINT_MODULE_DESCRIPTION( + R"DESCR_STR( +### Description +Example of a simple module running out of a work queue. + +)DESCR_STR"); + + PRINT_MODULE_USAGE_NAME("pca_pwm_test", "template"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + + return 0; +} + +extern "C" __EXPORT int pca_pwm_test_main(int argc, char *argv[]) +{ + return PCAPWMTest::main(argc, argv); +} diff --git a/src/examples/pca_pwm_test/PCAPWMTest.hpp b/src/examples/pca_pwm_test/PCAPWMTest.hpp new file mode 100644 index 0000000000..0bc09b42bf --- /dev/null +++ b/src/examples/pca_pwm_test/PCAPWMTest.hpp @@ -0,0 +1,85 @@ +/**************************************************************************** + * + * Copyright (c) 2022 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 + +using namespace time_literals; + +class PCAPWMTest : public ModuleBase, public ModuleParams, public px4::ScheduledWorkItem +{ +public: + PCAPWMTest(); + ~PCAPWMTest() override; + + /** @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(); + + int print_status() override; + +private: + void Run() override; + + // Publications + uORB::Publication _pca_pwm_pub{ORB_ID(pca_pwm)}; + + // Performance (perf) counters + perf_counter_t _loop_perf{perf_alloc(PC_ELAPSED, MODULE_NAME": cycle")}; + perf_counter_t _loop_interval_perf{perf_alloc(PC_INTERVAL, MODULE_NAME": interval")}; + + + pca_pwm_s pca_pwm; + uint16_t val = 0; + uint16_t period_us = 10000; +};