Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pulse Width Accumulate Sensor #8143

Closed
wants to merge 8,037 commits into from
Closed

Pulse Width Accumulate Sensor #8143

wants to merge 8,037 commits into from

Conversation

patrick-kiwi
Copy link

@patrick-kiwi patrick-kiwi commented Jan 26, 2025

What does this implement/fix?

Hi there. I hope I'm submitting this correctly.

This is a new pulse_width_accumulate sensor. It allows you to sum the total on-time of
a GPIO input. For example, this can be used to externally measure the on-time
of rapidly switched MOSFET or TRIAC. The sensor zeros itself every polling
interval, and units of measurement are on-time seconds per update interval.
An optional frequency sensor is also provided.

Threshold stability values were experimentally determined for an esp32 devkit_v4.
Pulse width >= 22 microseconds, pulse width delay >= 70 microseconds
(ie. implied maximum frequency ~10.87 KHz)
There are no overflow problems ie. the pulse width can be infinitely long.
However, the polling window (default 1 minute) must not be set > 71.58 minutes.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

esphome/feature-requests#3009

  • fixes

Pull request in esphome-docs with documentation (if applicable):

  • esphome/esphome-docs#

Test Environment

  • [ X] ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

# Example configuration entry
    sensor:
      - platform: pulse_width_accumulate
        pin: GPIO32
        name: Cumulative on-time sensor
        frequency:
          name: aAverage frequency sensor

Checklist:

  • [X ] The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

jesserockz and others added 30 commits November 20, 2024 17:50
Co-authored-by: Jonathan Swoboda <jonathan.swoboda>
@DjordjeMandic
Copy link
Contributor

DjordjeMandic commented Jan 26, 2025

Did you forget to edit pull request title? 😁

@patrick-kiwi
Copy link
Author

patrick-kiwi commented Jan 26, 2025 via email

@patrick-kiwi patrick-kiwi changed the title Add files via upload Pulse Width Accumulator Sensor Jan 26, 2025
@patrick-kiwi patrick-kiwi changed the title Pulse Width Accumulator Sensor Pulse Width Accumulate Sensor Jan 26, 2025
@DjordjeMandic
Copy link
Contributor

DjordjeMandic commented Jan 26, 2025

@patrick-kiwi Read this page https://esphome.io/guides/contributing.html#setting-up-a-development-environment

When everything is set-up, use command line for git. Precommit jobs will do most of simple fixes and patches automatically. If it fails, just stage new changes again with git add and retype same commit command.

https://github.com/esphome/esphome/blob/fc847c1de8927871e783db1a35e967efabda9f04/.pre-commit-config.yaml

Copy link
Contributor

@DjordjeMandic DjordjeMandic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your approach to checking parameters in code for invalid configurations is definitely effective in preventing unintended behavior. However, I’d like to suggest an improvement that could enhance the efficiency of the overall workflow.

Performing thorough configuration validation in Python before code generation is generally preferred. By detecting invalid configurations during the YAML validation phase (errors shown to the user either in the YAML editor or during config verification), errors can be caught early, significantly reducing the overhead of proceeding to code generation and compilation just to encounter an error. This not only saves time but also helps users quickly identify and fix issues.

In addition, the configuration schema should be designed to prevent invalid configurations altogether. ESPHome uses the voluptuous library for defining configuration schemas, and it provides a powerful way to enforce constraints, data types, and valid parameter combinations directly at the validation stage. Leveraging this schema can eliminate the need for certain runtime checks, resulting in cleaner code and better performance.

Moreover, handling invalid configurations during config validation reduces the size of the generated binary, as runtime checks become unnecessary. This optimization is especially beneficial for projects where binary size is a concern. That said, if your class is designed to be used manually without the auto-generated code, then retaining parameter checks in the code is important to maintain robustness.

In summary, I’d recommend shifting as many checks as possible to the config validation phase and enforcing valid configurations through the voluptuous schema. This provides immediate feedback to users, minimizes compilation overhead, and ensures an efficient development process. It’s a small change that can have a big impact on the overall experience. Keep up the great work!

float cumulative_width = this->store_.get_cumulative_pulse_width_s();
float polling_interval_s = static_cast<float>(this->get_update_interval()) / 1000.0f;
// Check and fix errors, and issue warnings if necessary.
if (polling_interval_s > 4294.9f) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be checked in config validation

Comment on lines 82 to 81
if (cumulative_width < 0) {
ESP_LOGW(TAG,
"Warning, cumulative pulse width %.1f s doesn't make sense! "
"Setting to zero.",
cumulative_width);
cumulative_width = 0.0f;
}
if (cumulative_width > polling_interval_s) {
ESP_LOGW(TAG,
"Warning, cumulative pulse width: %.4f s exceeds the polling "
"window: %.4f s.",
cumulative_width, polling_interval_s);
if ((cumulative_width - polling_interval_s) > 3.0e-3f) {
ESP_LOGW(TAG, "Clamping cumulative pulse width to range: %.4f", polling_interval_s);
cumulative_width = polling_interval_s;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These warnings can stay, errors should be thrown during config validation if possible.

@patrick-kiwi
Copy link
Author

patrick-kiwi commented Jan 27, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.