-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
PWM: Reduce inconsistencies between ports 2. #12084
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
Comments
TODO:
TODO in ports:
|
Thanks for the pointer. Indeed, the value range could be limited. ATM it accepts any value, but out-of-band values work as one could assume. Values < 0 behave like 0, and values > 65536 like 65536. |
It would allow a motor driver to be board-agnostic if it was there. But if it's too complicated: One could try that argument and catch the exception.. |
The start message is changed, please reread. |
Sorry. I'm not willing to continue this discussion and testing PR 10854 over and over again. Besides the start-up behavior, it is an excellent contribution and compatible with the other ports. |
@robert-hh Sorry, I didn't mean to offend you. |
From my perspective, after working with the ESP32 port, here is my position: When the user writes If the user wants to update the parameters, he can use This works because the GPIO pin is the ineternal ID, not the Python object itself. Reusing the same pin in code simply updates the parameters associated with that pin. |
Do not worry. I'm just tired about this discussion. I think all is said, but maybe not by everyone. It's up to Damien to decide, which way to go. |
@robert-hh Ok. I have something to say about the duty_u16 0-65535 vs 0-65536, but after decision Question 1). |
@yoann-darche What ESP32 board are you using? May you test #10854 ? |
Tests of this PR against the script from PR #16147 improve. With the most recent push the special if.. for ESP32 is not needed any more. It is now possible to specify freq and duty_u16 separately. Intermittent problems exist still with duty_16 == 0 and 65535. With duty_u16 == 65536 the result is even worse. Test platform: MicroPython v1.25.0-preview.20.g9d9471f65 on 2024-11-07; Generic ESP32 module with ESP32 Max duty 65535:
Max duty 65536:
|
Do you mean PR 10845? |
Try to use
after setting PWM duty. Note: New PWM parameters take effect in the next PWM cycle.
|
Indeed PR #10854. |
That is a common behavior but adding this sleep does not change the results. Even time.sleep(10/freq) does not pass all test cases. You can run the test yourself. |
Closed in favor esp32/PWM: Reduce inconsitencies between ports. #10854 |
Let me continue the discussion started here: PWM: Reduce inconsistencies between ports.#10817
I express my deepest respect for all who have contributed to bringing the API to a unified form, especially @robert-hh.
I consider this discussion unfinished.
EDITED 4 November 2024:
Only 3 states are possible for the PWM pin output: LOW(duty=0%), HIGH(duty=100%), and WAVE(0%<duty<100%). WAVE is the more likely state of the PWM pin that the user wants to see. WAVE is described by two parameters: frequency and duty. Both are required for WAVE!
Several unresolved questions remain in the PWM API. I suggest discussing them one by one and fixing the result at the top of the thread.
Question 1):
Most of the ports have
Constructor:
Method:
The difference between ports is that when executed
Variant 1) Unless freq and duty is chosen by the user there must be no signal on the pin output.
Variant 2) PWM WAVE starts with some predefined frequency and duty immediately.
Variant 1)
I see disadvantages in two hypothetical "bad" codes:
case 1) Reset MCU.
The user expects LOW because 0% duty means LOW for any frequency in the electronic world.
BUT THERE IS HIGH SIGNAL during 4 lines of the "bad" code.
Output becomes LOW as the user expects after 5 lines of the "bad" code.
"Good" code is:
case 2) Reset MCU.
The user expects HIGH because 100% duty means HIGH for any frequency.
THERE IS LOW SIGNAL during 4 lines of the "bad" code.
Output becomes HIGH as the user expects after 5 lines of the "bad" code.
pwm.init(duty_ns=) - can't set 100% duty without the frequency.
"Good" code is:
Variant 2)
case 1) Reset MCU.
There is a glitch in line 2.
"Good" code is:
case 2) Reset MCU.
There is a glitch in line 2.
"Good" code is:
Let's consider new user:
Variant 1)
pwm = PWM(Pin(2)) - output pin save previous state, a user is confused
print(pwm) - the user try to find out the reason
output is >>> PWM(Pin(2)) - nothing else new, the user is confused still
pwm.freq() or pwm.duty() - raise an exception, the user is confused again
one of pwm.int(freq=123) or pwm.freq(123) or pwm.int(duty=456) or pwm.duty(456) - output pin save previous state, the user is angry
when both freq and duty is chosen by one of pwm.int(duty=456) or pwm.duty(456) or pwm.int(freq=123) or pwm.freq(123) - resolved, user is happy
Variant 2)
pwm = PWM(Pin(2)) - output WAVE with unexpected freq and duty, a user is confused
print(pwm) - the user try to find out the reason
output is >>> PWM(Pin(2), freq=5000, duty_u16=32768) - Ok, according to an oscillograph
pwm.init(freq=my_freq, duty=my_duty) - resolved, user is happy
Conclusion:
Variant 1) allows surprises of unexpectedness to the user
The user sees PWM statement in code and the user sees the previous state on the pin.
Variant 2) has the glitch in "bad" code, but works as expected in all lines of the code,
The user sees PWM statement in code and the user sees PWM WAVE on the pin.
I vote for Variant 2).
Question 2):
duty_u16 0-65535 vs 0-65536
please after a solution to Question 1)
Dear maintainers, please make the volitional decision.
To the attention of @dpgeorge @projectgus @robert-hh @jonathanhogg @yoann-darche @Ayush1325 @rkompass @karlp
@andrewleech @pdg137
and all interested parties.
The text was updated successfully, but these errors were encountered: