kopia lustrzana https://github.com/peterhinch/micropython-samples
Encoders: add to algorithm notes.
rodzic
55955fa8d8
commit
b19fa14715
|
@ -31,7 +31,7 @@ latency or input pulse rates.
|
||||||
# 2. Basic encoder script
|
# 2. Basic encoder script
|
||||||
|
|
||||||
This comes from `encoder_portable.py` in this repo. It uses the simplest and
|
This comes from `encoder_portable.py` in this repo. It uses the simplest and
|
||||||
fastest algorithm I know. It should run on any MicrPython platform, but please
|
fastest algorithm I know. It should run on any MicroPython platform, but please
|
||||||
read the following notes as there are potential issues.
|
read the following notes as there are potential issues.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
@ -40,7 +40,6 @@ from machine import Pin
|
||||||
class Encoder:
|
class Encoder:
|
||||||
def __init__(self, pin_x, pin_y, scale=1):
|
def __init__(self, pin_x, pin_y, scale=1):
|
||||||
self.scale = scale
|
self.scale = scale
|
||||||
self.forward = True
|
|
||||||
self.pin_x = pin_x
|
self.pin_x = pin_x
|
||||||
self.pin_y = pin_y
|
self.pin_y = pin_y
|
||||||
self._pos = 0
|
self._pos = 0
|
||||||
|
@ -51,13 +50,13 @@ class Encoder:
|
||||||
self.x_interrupt = pin_x.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.x_callback)
|
self.x_interrupt = pin_x.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.x_callback)
|
||||||
self.y_interrupt = pin_y.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.y_callback)
|
self.y_interrupt = pin_y.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self.y_callback)
|
||||||
|
|
||||||
def x_callback(self, pin):
|
def x_callback(self, pin_x):
|
||||||
self.forward = pin() ^ self.pin_y()
|
forward = pin_x() ^ self.pin_y()
|
||||||
self._pos += 1 if self.forward else -1
|
self._pos += 1 if forward else -1
|
||||||
|
|
||||||
def y_callback(self, pin):
|
def y_callback(self, pin_y):
|
||||||
self.forward = self.pin_x() ^ pin() ^ 1
|
forward = self.pin_x() ^ pin_y() ^ 1
|
||||||
self._pos += 1 if self.forward else -1
|
self._pos += 1 if forward else -1
|
||||||
|
|
||||||
def position(self, value=None):
|
def position(self, value=None):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
|
@ -178,7 +177,7 @@ are shown in this state diagram.
|
||||||

|

|
||||||
|
|
||||||
The truth table includes the previous (`xp` and `yp`) and current (`x` and `y`)
|
The truth table includes the previous (`xp` and `yp`) and current (`x` and `y`)
|
||||||
values of the signals. It includes all logically possible cobinations of these
|
values of the signals. It includes all logically possible combinations of these
|
||||||
signals. These include cases where no change occurs (marked `n`) and cases
|
signals. These include cases where no change occurs (marked `n`) and cases
|
||||||
which are physically impossible (`x`). The latter arise because both signals
|
which are physically impossible (`x`). The latter arise because both signals
|
||||||
cannot change state simultaneously.
|
cannot change state simultaneously.
|
||||||
|
@ -223,7 +222,7 @@ the edge occurs and consequently varies in real time. Another ISR may be
|
||||||
running. Higher priority interrupts may be pending service. In the case of soft
|
running. Higher priority interrupts may be pending service. In the case of soft
|
||||||
IRQ's garbage collection may be in progress.
|
IRQ's garbage collection may be in progress.
|
||||||
|
|
||||||
Consider the fllowing ISR:
|
Consider the following ISR:
|
||||||
```python
|
```python
|
||||||
def x_callback(self, pin_x):
|
def x_callback(self, pin_x):
|
||||||
forward = pin_x() ^ self.pin_y()
|
forward = pin_x() ^ self.pin_y()
|
||||||
|
|
Ładowanie…
Reference in New Issue