RPMCapture: Detect rpm sensing timeout

This commit is contained in:
Matthias Grob
2024-12-10 16:19:58 +01:00
parent 643dc1a05e
commit d32aa8dd1d
2 changed files with 18 additions and 10 deletions
+17 -10
View File
@@ -43,6 +43,7 @@ RPMCapture::RPMCapture() :
ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default) ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default)
{ {
_pwm_input_pub.advertise(); _pwm_input_pub.advertise();
ScheduleOnInterval(1_s);
} }
RPMCapture::~RPMCapture() RPMCapture::~RPMCapture()
@@ -100,21 +101,27 @@ void RPMCapture::Run()
} }
hrt_abstime now = hrt_absolute_time(); hrt_abstime now = hrt_absolute_time();
const hrt_abstime period = _hrt_timestamp - _hrt_timestamp_prev;
pwm_input_s pwm_input{}; if (!_value_processed.load()) {
pwm_input.timestamp = now; _period = _hrt_timestamp - _hrt_timestamp_prev;
pwm_input.period = period; _hrt_timestamp_prev = _hrt_timestamp;
pwm_input.error_count = _error_count; _value_processed.store(true);
_hrt_timestamp_prev = _hrt_timestamp;
_value_processed.store(true); pwm_input_s pwm_input{};
_pwm_input_pub.publish(pwm_input); pwm_input.timestamp = now;
pwm_input.period = _period;
pwm_input.error_count = _error_count;
_pwm_input_pub.publish(pwm_input);
} else if (now > _hrt_timestamp_prev + 1_s) {
_period = 0;
}
float rpm_raw{0.f}; float rpm_raw{0.f};
if ((1 < period) && (period < 1_s)) { if ((1 < _period) && (_period < 1_s)) {
// 1'000'000 / [us] -> pulses per second * 60 -> pulses per minute // 1'000'000 / [us] -> pulses per second * 60 -> pulses per minute
rpm_raw = 60.f * 1e6f / (static_cast<float>(period) * 1.f); rpm_raw = 60.f * 1e6f / (static_cast<float>(_period) * 1.f);
} }
const float dt = math::constrain((now - _timestamp_last_update) * 1e-6f, 0.01f, 1.f); const float dt = math::constrain((now - _timestamp_last_update) * 1e-6f, 0.01f, 1.f);
+1
View File
@@ -77,6 +77,7 @@ private:
hrt_abstime _hrt_timestamp{0}; hrt_abstime _hrt_timestamp{0};
hrt_abstime _hrt_timestamp_prev{0}; hrt_abstime _hrt_timestamp_prev{0};
uint32_t _period{0};
uint32_t _error_count{0}; uint32_t _error_count{0};
px4::atomic<bool> _value_processed{true}; px4::atomic<bool> _value_processed{true};