From 135537ec2f0fa680752f3585f54d510ad79984ab Mon Sep 17 00:00:00 2001 From: "Tanner Collin (aider)" Date: Thu, 5 Jun 2025 11:24:16 -0600 Subject: [PATCH] feat: Filter RPM jitter caused by single pulse drops --- tach/tach.ino | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tach/tach.ino b/tach/tach.ino index e120caa..0458045 100644 --- a/tach/tach.ino +++ b/tach/tach.ino @@ -16,6 +16,10 @@ volatile unsigned long g_isr_pulse_count_total = 0; // ISR increments this conti unsigned long g_main_last_pulse_count_snapshot = 0; // Main loop stores previous snapshot here unsigned long last_measurement_time = 0; +// --- RPM Display Filtering Variables --- +int g_rpm_value_on_display = 0; // Stores the last RPM value shown on the 7-segment display by the loop +bool g_is_first_rpm_calc_in_loop = true; // Flag to handle the first RPM calculation + // --- Interrupt Service Routine (ISR) --- void count_pulse() { g_isr_pulse_count_total++; @@ -76,7 +80,37 @@ void loop() { } int rpm = (int)rpm_float; - show(rpm); + // RPM Filtering Logic + if (g_is_first_rpm_calc_in_loop) { + show(rpm); + g_rpm_value_on_display = rpm; + g_is_first_rpm_calc_in_loop = false; + } else { + if (rpm == 0 && g_rpm_value_on_display != 0) { + // If RPM is actually zero, update the display to show zero + show(rpm); + g_rpm_value_on_display = rpm; + } else if (rpm != 0) { // Apply jitter filter only if current rpm is not zero + float rpm_float_if_one_more_pulse = 0.0; + if (actual_elapsed_time > 0) { // Avoid division by zero + rpm_float_if_one_more_pulse = ( (float)(collected_pulses + 1) / PULSES_PER_REVOLUTION ) * (60000.0 / actual_elapsed_time); + } + int rpm_if_one_more_pulse = (int)rpm_float_if_one_more_pulse; + + // Check for the specific 1-pulse drop jitter + if (rpm < g_rpm_value_on_display && rpm_if_one_more_pulse == g_rpm_value_on_display) { + // Jitter detected (e.g., displayed 6000, current calc is 5925, but 5925 with +1 pulse would be 6000) + // Do not update the display; keep showing g_rpm_value_on_display. + Serial.print("Filtered (jitter) RPM: "); Serial.print(rpm); + Serial.print(" -> Kept displaying: "); Serial.println(g_rpm_value_on_display); + } else { + // Not a jitter, or a rise, or a larger drop. Update display. + show(rpm); + g_rpm_value_on_display = rpm; + } + } + // If rpm is 0 and g_rpm_value_on_display is already 0, do nothing to avoid redundant show(0) + } // Debugging output Serial.print("Pulses: "); Serial.print(collected_pulses);