Improve encoder precision.

pull/16/head
Peter Hinch 2022-04-17 14:42:23 +01:00
rodzic 4d98d7aecf
commit 982005736a
1 zmienionych plików z 11 dodań i 13 usunięć

Wyświetl plik

@ -29,22 +29,20 @@ class Encoder:
yirq = pin_y.irq(trigger=trig, handler=self._y_cb)
asyncio.create_task(self._run(vmin, vmax, div, callback, args))
# Hardware IRQ's. Duration 36μs on Pyboard 1.
# Hardware IRQ's. Duration 36μs on Pyboard 1 ~50μs on ESP32.
# IRQ latency: 2nd edge may have occured by the time ISR runs, in
# which case there is no movement.
def _x_cb(self, pin_x):
if (x := pin_x()) == self._x: # IRQ latency: if 2nd edge has
return # occurred there is no movement.
self._x = x
fwd = x ^ self._pin_y()
self._v += 1 if fwd else -1
self._tsf.set()
if (x := pin_x()) != self._x:
self._x = x
self._v += 1 if x ^ self._pin_y() else -1
self._tsf.set()
def _y_cb(self, pin_y):
if (y := pin_y()) == self._y:
return
self._y = y
fwd = y ^ self._pin_x() ^ 1
self._v += 1 if fwd else -1
self._tsf.set()
if (y := pin_y()) != self._y:
self._y = y
self._v += 1 if y ^ self._pin_x() ^ 1 else -1
self._tsf.set()
async def _run(self, vmin, vmax, div, cb, args):
pv = self._v # Prior hardware value