Add executables dir & Encoder class
This commit is contained in:
34
motor_passo/encoder.py
Normal file
34
motor_passo/encoder.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import RPi.GPIO as gpio
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Encoder:
|
||||
pin_a: int
|
||||
pin_b: int
|
||||
|
||||
_curr_steps: int = 0
|
||||
|
||||
def setup(self):
|
||||
gpio.setmode(gpio.BCM)
|
||||
|
||||
gpio.setup(self.pin_a, gpio.IN)
|
||||
gpio.setup(self.pin_b, gpio.IN)
|
||||
gpio.add_event_detect(self.pin_a,
|
||||
gpio.RISING,
|
||||
callback=self._event_detect)
|
||||
gpio.add_event_detect(self.pin_b,
|
||||
gpio.RISING,
|
||||
callback=self._event_detect)
|
||||
|
||||
def _event_detect(self, pin):
|
||||
a = gpio.input(self.pin_a)
|
||||
b = gpio.input(self.pin_b)
|
||||
|
||||
if a ^ b:
|
||||
self._curr_steps += 1 if a else -1
|
||||
|
||||
@property
|
||||
def angle(self) -> float:
|
||||
return self._curr_steps / 5000 * 360
|
||||
Reference in New Issue
Block a user