Include pca_pwm test.

This commit is contained in:
Benjamin Perseghetti
2022-06-02 21:40:07 -04:00
parent 38a3e000cf
commit b7f8b589d7
8 changed files with 300 additions and 15 deletions
+14 -5
View File
@@ -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
@@ -3,7 +3,9 @@
# board specific defaults
#------------------------------------------------------------------------------
pwm_out mode_pwm1 start
set UCAN146 1
pca9685_ucan start -X
ifup can0
cyphal start
+2
View File
@@ -1 +1,3 @@
CONFIG_DRIVERS_PCA9685_UCAN=y
CONFIG_DRIVERS_GPS=n
CONFIG_EXAMPLES_PCA_PWM_TEST=y
+5 -9
View File
@@ -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);
}
}
}
}
+45
View File
@@ -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
)
+5
View File
@@ -0,0 +1,5 @@
menuconfig EXAMPLES_PCA_PWM_TEST
bool "pca_pwm_test"
default n
---help---
Enable support for pca_pwm_test
+141
View File
@@ -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);
}
+85
View File
@@ -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 <px4_platform_common/defines.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/module_params.h>
#include <px4_platform_common/posix.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <drivers/drv_hrt.h>
#include <lib/perf/perf_counter.h>
#include <uORB/Publication.hpp>
#include <uORB/Subscription.hpp>
#include <uORB/SubscriptionCallback.hpp>
#include <uORB/topics/pca_pwm.h>
using namespace time_literals;
class PCAPWMTest : public ModuleBase<PCAPWMTest>, 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_s> _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;
};