mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-03 14:58:53 +08:00
RPMCapture: added pulse per rev parameter. Do not publish if pulses are to fast
This commit is contained in:
@@ -58,6 +58,9 @@ bool RPMCapture::init()
|
|||||||
{
|
{
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
||||||
|
param_get(param_find("RPM_PULSES_PER_REV"), &_pulses_per_revolution);
|
||||||
|
_min_pulse_period_us = static_cast<uint32_t>(60.f * 1e6f / static_cast<float>(RPM_MAX_VALUE * _pulses_per_revolution));
|
||||||
|
|
||||||
for (unsigned i = 0; i < 16; ++i) {
|
for (unsigned i = 0; i < 16; ++i) {
|
||||||
char param_name[17];
|
char param_name[17];
|
||||||
snprintf(param_name, sizeof(param_name), "%s_%s%d", PARAM_PREFIX, "FUNC", i + 1);
|
snprintf(param_name, sizeof(param_name), "%s_%s%d", PARAM_PREFIX, "FUNC", i + 1);
|
||||||
@@ -125,26 +128,28 @@ void RPMCapture::Run()
|
|||||||
ScheduleDelayed(RPM_PULSE_TIMEOUT);
|
ScheduleDelayed(RPM_PULSE_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
float rpm_raw{0.f};
|
|
||||||
|
|
||||||
if ((1 < _period) && (_period < RPM_PULSE_TIMEOUT)) {
|
if (_period > _min_pulse_period_us) {
|
||||||
// 1'000'000 / [us] -> pulses per second * 60 -> pulses per minute
|
// Only update if the period is above the min pulse period threshold
|
||||||
rpm_raw = 60.f * 1e6f / (static_cast<float>(_period) * 1.f);
|
float rpm_raw{0.f};
|
||||||
|
|
||||||
} else {
|
if (_period < RPM_PULSE_TIMEOUT) {
|
||||||
_rpm_filter.reset(rpm_raw);
|
// 1'000'000 / [us] -> pulses per second * 60 -> pulses per minute
|
||||||
|
rpm_raw = 60.f * 1e6f / static_cast<float>(_pulses_per_revolution * _period);
|
||||||
|
}
|
||||||
|
|
||||||
|
const float dt = math::constrain((now - _timestamp_last_update) * 1e-6f, 0.01f, 1.f);
|
||||||
|
_timestamp_last_update = now;
|
||||||
|
_rpm_filter.setParameters(dt, 0.5f);
|
||||||
|
_rpm_filter.update(_rpm_median_filter.apply(rpm_raw));
|
||||||
|
|
||||||
|
rpm_s rpm{};
|
||||||
|
rpm.timestamp = now;
|
||||||
|
rpm.rpm_raw = rpm_raw;
|
||||||
|
rpm.rpm_estimate = _rpm_filter.getState();
|
||||||
|
_rpm_pub.publish(rpm);
|
||||||
}
|
}
|
||||||
|
|
||||||
const float dt = math::constrain((now - _timestamp_last_update) * 1e-6f, 0.01f, 1.f);
|
|
||||||
_timestamp_last_update = now;
|
|
||||||
_rpm_filter.setParameters(dt, 0.5f);
|
|
||||||
_rpm_filter.update(_rpm_median_filter.apply(rpm_raw));
|
|
||||||
|
|
||||||
rpm_s rpm{};
|
|
||||||
rpm.timestamp = now;
|
|
||||||
rpm.rpm_raw = rpm_raw;
|
|
||||||
rpm.rpm_estimate = _rpm_filter.getState();
|
|
||||||
_rpm_pub.publish(rpm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int RPMCapture::gpio_interrupt_callback(int irq, void *context, void *arg)
|
int RPMCapture::gpio_interrupt_callback(int irq, void *context, void *arg)
|
||||||
|
|||||||
@@ -70,11 +70,14 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr hrt_abstime RPM_PULSE_TIMEOUT = 1_s;
|
static constexpr hrt_abstime RPM_PULSE_TIMEOUT = 1_s;
|
||||||
|
static constexpr uint32_t RPM_MAX_VALUE = 15000;
|
||||||
|
|
||||||
void Run() override;
|
void Run() override;
|
||||||
|
|
||||||
int _channel{-1};
|
int _channel{-1};
|
||||||
uint32_t _rpm_capture_gpio{0};
|
uint32_t _rpm_capture_gpio{0};
|
||||||
|
uint32_t _pulses_per_revolution{1};
|
||||||
|
uint32_t _min_pulse_period_us{1}; ///< [us] minimum pulse period
|
||||||
uORB::Publication<pwm_input_s> _pwm_input_pub{ORB_ID(pwm_input)};
|
uORB::Publication<pwm_input_s> _pwm_input_pub{ORB_ID(pwm_input)};
|
||||||
uORB::PublicationMulti<rpm_s> _rpm_pub{ORB_ID(rpm)};
|
uORB::PublicationMulti<rpm_s> _rpm_pub{ORB_ID(rpm)};
|
||||||
|
|
||||||
|
|||||||
@@ -41,3 +41,15 @@
|
|||||||
* @reboot_required true
|
* @reboot_required true
|
||||||
*/
|
*/
|
||||||
PARAM_DEFINE_INT32(RPM_CAP_ENABLE, 0);
|
PARAM_DEFINE_INT32(RPM_CAP_ENABLE, 0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RPM Pulses per Revolution
|
||||||
|
*
|
||||||
|
* Number of pulses per revolution for the RPM sensor.
|
||||||
|
*
|
||||||
|
* @group System
|
||||||
|
* @min 1
|
||||||
|
* @max 50
|
||||||
|
* @reboot_required true
|
||||||
|
*/
|
||||||
|
PARAM_DEFINE_INT32(RPM_PULSES_PER_REV, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user