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

Add H-Bridge switch component #7421

Merged
merged 39 commits into from
Dec 2, 2024
Merged

Add H-Bridge switch component #7421

merged 39 commits into from
Dec 2, 2024

Conversation

dwmw2
Copy link
Contributor

@dwmw2 dwmw2 commented Sep 7, 2024

What does this implement/fix?

Add an H-Bridge switch component for driving the relay in devices like https://devices.esphome.io/devices/Tongou-TO-Q-SY1-JWT

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)
  • Other

Related issue or feature (if applicable): fixes #4087

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

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

# Example config.yaml

switch:
  - platform: hbridge
    on_pin: P24
    off_pin: P26
    id: relay
    # On by default should be fine as this is not a safety device
    restore_mode: RESTORE_DEFAULT_ON
    pulse_length: 100ms
    wait_time: 50ms

Checklist:

  • 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:

@codecov-commenter
Copy link

codecov-commenter commented Sep 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.80%. Comparing base (4d8b5ed) to head (5b9c9b7).
Report is 1729 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #7421      +/-   ##
==========================================
+ Coverage   53.70%   53.80%   +0.09%     
==========================================
  Files          50       50              
  Lines        9408     9812     +404     
  Branches     1654     1353     -301     
==========================================
+ Hits         5053     5279     +226     
- Misses       4056     4207     +151     
- Partials      299      326      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dwmw2
Copy link
Contributor Author

dwmw2 commented Sep 8, 2024

There is some debate about whether "H-Bridge" is the right name for this. It certainly matches OpenBeken, and the feature requests such as esphome/feature-requests#1690 and #4087 so it looks like it would be well-named for users to actually find it?

Not sure it's strictly technically accurate though, and maybe it would be better as platform: bistable or something like that?

@raidnet-ms
Copy link

raidnet-ms commented Sep 8, 2024

Adding a delay time for on and off signal would be great.

About the name well If i search anything related with the reverse of polarity I look for h-bridge keyword.

Bistable solenoid is only a usecase but there could be more (open and close motorized valves for istance)

@dwmw2
Copy link
Contributor Author

dwmw2 commented Sep 8, 2024

Adding a delay time for on and off signal would be great.

Can you help me understand what you need that pulse_length and wait_time don't already cover? (I have now added them to the example YAML above.)

Docs at
https://deploy-preview-4233--esphome.netlify.app/components/switch/hbridge.html

@raidnet-ms
Copy link

Adding a delay time for on and off signal would be great.

Can you help me understand what you need that pulse_length and wait_time don't already cover? (I have now added them to the example YAML above.)

Docs at
https://deploy-preview-4233--esphome.netlify.app/components/switch/hbridge.html

Sure they are perfect. For some reason I didn't see those values before!!

@sorrypqa
Copy link

sorrypqa commented Nov 15, 2024

GS1102

The function I hope for is:
The H-bridge component specifies the zero crossing detection IO and enables the zero crossing trigger function. Whether the H-bridge switch is turned on or off, pin_forward or pin_deverse is in a waiting state until the zero crossing detection IO is triggered by the low-level zero crossing signal of GS1102. After a custom delay time, pin_forward or pin_deverse outputs the relay drive signal

@dwmw2
Copy link
Contributor Author

dwmw2 commented Nov 15, 2024

Do we need to build that into the H-Bridge switch component? Wouldn't that be a useful thing for any switch, using a separate zero-detection? I'm imagining a generic switch-when-zero switch type, which takes a slave switch type and a trigger GPIO input, and sets the slave to match its own on/off state whenever the zero trigger is asserted?

@sorrypqa
Copy link

That is to say, the H-bridge component has two options:

  1. Directly triggered, like the current function, whether the H-bridge switch is turned on or off, pin_fforwards or pin_deverse will directly output the driving signal
  2. Zero crossing trigger, specifying the zero crossing detection IO and setting a delay time. Whether the H-bridge switch is turned on or off, pin_forward or pin_deverse is in a waiting state until the zero crossing detection IO is triggered by the low-level zero crossing signal of GS1102. After a custom delay time, pin_forward or pin_deverse outputs the relay drive signal

@sorrypqa
Copy link

sorrypqa commented Nov 15, 2024

Because magnetic holding relays are energy-saving and do not generate heat, they are now quite popular, and this H-bridge switch component is perfect for showcasing its capabilities.

@dwmw2
Copy link
Contributor Author

dwmw2 commented Nov 15, 2024

Sure, but the zero-crossing thing is surely orthogonal to that? What if we build a generic implementation of that?

 switch:
   - platform: trigger
     name: "Zero-triggered switch"
     trigger_pin: GPIO10 # The zero detection pin
     subordinate_switch_id: the_real_one # Either an hbridge switch or any other type
     trigger_delay: 10ms
   - platform: whatever
     id: the_real_one
     …

I'd knock up an implementation of that to play with, but contributing to ESPHome isn't sufficiently feeding my dopamine addition because things are taking so long to get merged :)

@sorrypqa
Copy link

sorrypqa commented Nov 15, 2024

Sure, but the zero-crossing thing is surely orthogonal to that? What if we build a generic implementation of that?

 switch:
   - platform: trigger
     name: "Zero-triggered switch"
     trigger_pin: GPIO10 # The zero detection pin
     subordinate_switch_id: the_real_one # Either an hbridge switch or any other type
     trigger_delay: 10ms
   - platform: whatever
     id: the_real_one
     …

I'd knock up an implementation of that to play with, but contributing to ESPHome isn't sufficiently feeding my dopamine addition because things are taking so long to get merged :)

It looks pretty good and I can't wait to try it out. I wonder if this series control method can accurately and quickly respond,Because the zero crossing point of alternating current changes too quickly

@kbx81 kbx81 self-assigned this Nov 26, 2024
@kbx81
Copy link
Member

kbx81 commented Nov 26, 2024

I think this looks good -- @dwmw2 any chance you'd be willing to consolidate the tests into a single common.yaml file? Have a look at the bme280 for a hint.

@dwmw2
Copy link
Contributor Author

dwmw2 commented Nov 26, 2024

ISTR I had deliberately made the tests different, to exercise different combinations of options. Will take a look...

@kbx81
Copy link
Member

kbx81 commented Nov 26, 2024

ISTR I had deliberately made the tests different, to exercise different combinations of options. Will take a look...

You can still accomplish that -- use extend and/or remove if necessary.
Thanks for working on this! 🍻

@dwmw2
Copy link
Contributor Author

dwmw2 commented Nov 28, 2024

Attempted the common template in 49c8ab5

@dwmw2
Copy link
Contributor Author

dwmw2 commented Nov 29, 2024

ISTR I had deliberately made the tests different, to exercise different combinations of options. Will take a look...

You can still accomplish that -- use extend and/or remove if necessary. Thanks for working on this! 🍻

@kbx81 I had to do the !include differently to the bme280 example you gave, because it wasn't managing to extend the configuration. But it should now be working with that latest 'Make tests actually pass...' commit.

Copy link
Member

@kbx81 kbx81 left a comment

Choose a reason for hiding this comment

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

LGTM -- thanks again! 🍻

@kbx81 kbx81 merged commit fb96e35 into esphome:dev Dec 2, 2024
25 checks passed
@dwmw2 dwmw2 deleted the hbridge-switch branch December 2, 2024 11:59
@dwmw2
Copy link
Contributor Author

dwmw2 commented Dec 2, 2024

I wonder if this series control method can accurately and quickly respond,Because the zero crossing point of alternating current changes too quickly

Yes, as I said above you probably want hardware assistance. But even with hardware assistance for the trigger I'm not sure a latching H-Bridge switch will work well for that use case. These things typically have a pulse length of 100ms — meaning that a 50Hz signal will have completed five full cycles (crossing zero ten times) in the time it takes to know that the switch has completed its movement.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants