ESP32: Add Quadrature Encoder and Pulse Counter classes.#19239
ESP32: Add Quadrature Encoder and Pulse Counter classes.#19239IhorNehrutsa wants to merge 7 commits into
Conversation
Co-Authored-By: robert-hh <robert@hammelrath.com> Co-Authored-By: Jonathan Hogg <me@jonathanhogg.com> Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
|
Code size report: |
927084c to
6044972
Compare
1205c14 to
8f64762
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #19239 +/- ##
=======================================
Coverage 98.47% 98.47%
=======================================
Files 176 176
Lines 22845 22845
=======================================
Hits 22497 22497
Misses 348 348 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
d486b81 to
b7a8ded
Compare
Update machine_encoder.c
f8dc3e9 to
82e2bfe
Compare
|
@robert-hh |
|
@IhorNehrutsa self.counter.init(in_pin, direction=Counter.UP, match=100)
self.counter.irq(handler=callback, trigger=Counter.IRQ_MATCH) |
82e2bfe to
676b293
Compare
|
micropython/docs/library/machine.Counter.rst Line 144 in 8d81160 Does |
c7195db to
b3527aa
Compare
There is no Counter.status() or Encoder.status() method. irq().flags() returns the flag which caused the IRQ to happen. And that may be different for Counter and Encoder. |
|
@robert-hh |
|
Try to call |
|
@robert-hh micropython/ports/mimxrt/machine_encoder.c Line 685 in dc33f04 after line micropython/ports/mimxrt/machine_encoder.c Line 691 in dc33f04 |
5512719 to
508af32
Compare
508af32 to
bd50aab
Compare
|
@robert-hh |
|
Tested with Teensy 4.0: works. |
|
I shall note that setting the Counting direction with a Pin works as well with the MIMXRT port, with |
|
@robert-hh |
The values should be 0 for UP and 1 for DOWN to match the documentation. Instead they were -2 and -3, causing a test with a count direction set by a Pin to fail, when the symbols were used instead for numbers.. Tested with the machine_encoder.py and machine_counter.py script and the extended machine_counter.py script from PR micropython#19239. Signed-off-by: robert-hh <robert@hammelrath.com>
|
Technically it may be possible, but it breaks existing behavior. At the moment, a call to |
The values should be 0 for UP and 1 for DOWN to match the documentation. Instead they were -2 and -3, causing a test with a count direction set by a Pin to fail, when the symbols were used instead of numbers.. Tested with the machine_encoder.py and machine_counter.py script and the extended machine_counter.py script from PR micropython#19239. Signed-off-by: robert-hh <robert@hammelrath.com>
Yes, I understand. But was this behavior discussed, or did it just happen on its own?
This is not the same behavior: init() modifies only explicitly provided parameters and preserves all omitted parameters, but zeroes the counter. If init() saves the counter code is simpler. If the user needs to reset the counter it can do it explicitly. The Zen of Python: |
|
@robert-hh |
|
I did not plan to make any changes to the test scripts. I'll leave that to you.The existing test scripts work, since they to not test counting direction. |
|
I'm afraid this PR won't be approved as #16743 ;) |
In general I think we'd really like to replace If you can keep the behaviour the same as the existing |
I don't like that init() resets the counter to zero. What are your thoughts on this? |
|
@robert-hh |
6f0153d to
45b396e
Compare
Which existing behaviour must I use? micropython/ports/mimxrt/machine_encoder.c Lines 852 to 853 in dc33f04 or IRQ_MIN and IRQ_MAX micropython/ports/esp32/esp32_pcnt.c Lines 499 to 500 in dc33f04 |
681a080 to
2eff13e
Compare
|
With respect to min and max counting, the ESP32 and MIMXRT hardware behaves different. The ESP32 creates and IRQ when the counter is AT min (or max), The MIMXRT creates an IRQ when the counter rolls under from min to max (or over from max to min). So for ESP32, use IRQ_MIN and IRQ_MAX. |
|
@robert-hh |
I don't feel strongly about this, but I think it is better to keep the current behaviour if possible. Apart from test code, is there a use case where this behaviour causes a major problem? |
I'm keeping the current behaviour (init() zeros the counter) now, but I think it's better for the end user to have a flat/straight counter scale ranging from MIN to MAX with the ability to set MATCH to a new value without resetting the counter scale. |
|
At MIMXRT, |
8079951 to
ad6dc09
Compare
ad6dc09 to
f44c864
Compare
I did not use generative AI tools when creating this PR.
Edit: This PR will be pending approval #19268