fabianoriccardi/dimmable-light

Activating filtering prevents frequency detection

Opened this issue · 0 comments

  • I'd like to use frequency detection
  • I am using a Robodyn and ESP32 (so not a nice pulse), so I set semiPeriodShrinkMargin == 400

When using freq detection + filtering, semiPeriodLength == 0 so this code always makes the function return because the comparison leads to true because there is a comparison with an unsigned and signed value.

#ifdef FILTER_INT_PERIOD
    // Filters out spurious interrupts. The effectiveness of this simple
    // filter could vary depending on noise on electrical networ.
    if (diff < semiPeriodLength - Thyristor::semiPeriodShrinkMargin) { return; }
#endif

This code could be fixed like that, but the problem is that wrong "diff" could end up in the queue, so I don't really know how to fix that.

#ifdef FILTER_INT_PERIOD
    // Filters out spurious interrupts. The effectiveness of this simple
    // filter could vary depending on noise on electrical networ.
    if (semiPeriodLength && diff < semiPeriodLength - Thyristor::semiPeriodShrinkMargin) { return; }
#endif

With this fix, I can see the "wrong" detected frequencies.

It seems to me that filtering is quite linked to DimmableLightLinearized::setFrequency(), meaning as soon as we need filtering, we also need to set a frequency value we with it is good for where we are, so that filtering can work properly.

#if defined(ESP32) && defined (FILTER_INT_PERIOD)
  // https://github.com/fabianoriccardi/dimmable-light/wiki/Notes-about-specific-architectures#interrupt-issue
  Thyristor::semiPeriodShrinkMargin = 400;
#endif
  
  DimmableLightLinearized::setFrequency(ZCDConfig.getGridFrequency());
  DimmableLightLinearized::frequencyMonitorAlwaysOn(true);
  DimmableLightLinearized::setSyncPin(_pin);
  DimmableLightLinearized::begin();