Repository navigation
ESP32: Add Quadrature Encoder and Pulse Counter classes. - #19239
IhorNehrutsa wants to merge 1 commit into
Conversation
|
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.51% 98.51%
=======================================
Files 177 177
Lines 22927 22927
=======================================
Hits 22586 22586
Misses 341 341 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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 |
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 |
|
Technically it may be possible, but it breaks existing behavior. At the moment, a call to |
8285c07 to
18c8e35
Compare
Actually, I almost managed to set the same levels for the MIMXRT and the ESP32. In this case |
|
It runs fine with MIMXRT, but fails now again with ESP32, after it worked before. |
| // self->mp_irq_flags |= status; | ||
| self->mp_irq_flags = status; | ||
| MICROPY_END_ATOMIC_SECTION(atomic_state); | ||
|
|
There was a problem hiding this comment.
I do not know if that is intentional, but you schedule a IRQ handler separately for every possible event. Usually the events are collected and then the IRQ handler is called only once. The IRQ handler has to check any any event again.
| // when counting up | ||
| self->counter += self->count_max; | ||
| if ((self->mp_irq_trigger & PCNT_EVT_H_LIM) && (self->mp_irq_obj->handler)) { | ||
| mp_sched_schedule(self->mp_irq_obj->handler, MP_OBJ_FROM_PTR(self)); |
There was a problem hiding this comment.
Please look at mp_irq_handler(self->mp_irq_obj) to call the IRQ handler. It checks as well whether a callback exists and supports hard IRQ as well.
|
Both MIMXRT and ESP32 pass the test with machine_counter.py now. |
| static mp_obj_t machine_pcnt_cycles(size_t n_args, const mp_obj_t *args) { | ||
| mp_pcnt_obj_t *self = MP_OBJ_TO_PTR(args[0]); | ||
| counter_t value = get_counter_value(self); | ||
| int32_t counts_per_cycle = self->count_max - self->count_min; |
There was a problem hiding this comment.
This formula is only right if count_max is excluded from the counting range. If the counting range is count_min to and including count_max, then
counts_per_cycle = self->count_max - self->count_min + 1
MIMXRT's range includes the max value.
|
The behavior of the Counter class differs between MIMXRT and ESP32 with respect to setting min and max. |
0fd9623 to
8e6227f
Compare
8b79b17 to
ab7666b
Compare
|
WIP @robert-hh |
|
@IhorNehrutsa Can do so, but it may take a few days. |
|
The test scripts work with your new ESP32 code, but fails with MIMXRT (Teensy 4.0). See below. So it seems that the behavior you implemented for ESP32 does not match the MIMXRT one. I did not look where the test scripts are wrong. The existing tests pass on MIMXRT. |
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 <[email protected]>
Co-Authored-By: robert-hh <[email protected]> Co-Authored-By: Jonathan Hogg <[email protected]> Signed-off-by: IhorNehrutsa <[email protected]>
ab7666b to
a9ac008
Compare
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 <[email protected]>
Designed for high-resolution rotary encoders (40000 - 240000 PPR)

https://www.usdigital.com/products/encoders/incremental/shaft/s2/?q=S2-?-?-?-D
https://www.usdigital.com/products/encoders/incremental/kit/ec35/?q=EC35-?-?-?-?-?-?-B
I did not use generative AI tools when creating this PR.