Add executables dir & Encoder class

This commit is contained in:
2023-10-29 20:56:03 -03:00
parent a066b13d20
commit 2d782959a2
6 changed files with 67 additions and 2 deletions

34
motor_passo/encoder.py Normal file
View 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

View File

View File

@@ -0,0 +1,17 @@
from motor_passo.encoder import Encoder
from motor_passo.utils import setup_cleanup
from time import sleep
def main():
setup_cleanup()
encoder = Encoder(6, 5)
encoder.setup()
while True:
print(encoder.angle)
sleep(1)
if __name__ == "__main__":
main()

View File

View File

@@ -1,11 +1,13 @@
from motor_passo.motor import Motor from motor_passo.motor import Motor
from motor_passo.utils import setup_cleanup
def main(): def main():
setup_cleanup()
motor = Motor([13, 19, 26]) motor = Motor([13, 19, 26])
motor.setup() motor.setup()
motor.set_speed(90) motor.set_speed(10)
motor.step(24 * 100) motor.step(24 * 2)
if __name__ == "__main__": if __name__ == "__main__":

12
motor_passo/utils.py Normal file
View File

@@ -0,0 +1,12 @@
import signal
def _cleanup(sig, frame):
import sys
import RPi.GPIO as gpio
gpio.cleanup()
sys.exit(0)
def setup_cleanup():
signal.signal(signal.SIGINT, _cleanup)