From 2d782959a2965cd7af5187b706c54e89ec4399d3 Mon Sep 17 00:00:00 2001 From: marisa Date: Sun, 29 Oct 2023 20:56:03 -0300 Subject: [PATCH] Add executables dir & Encoder class --- motor_passo/encoder.py | 34 ++++++++++++++++++++++++ motor_passo/exec/encoder/__init__.py | 0 motor_passo/exec/encoder/__main__.py | 17 ++++++++++++ motor_passo/exec/motor/__init__.py | 0 motor_passo/{ => exec/motor}/__main__.py | 6 +++-- motor_passo/utils.py | 12 +++++++++ 6 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 motor_passo/encoder.py create mode 100644 motor_passo/exec/encoder/__init__.py create mode 100644 motor_passo/exec/encoder/__main__.py create mode 100644 motor_passo/exec/motor/__init__.py rename motor_passo/{ => exec/motor}/__main__.py (55%) create mode 100644 motor_passo/utils.py diff --git a/motor_passo/encoder.py b/motor_passo/encoder.py new file mode 100644 index 0000000..051a47f --- /dev/null +++ b/motor_passo/encoder.py @@ -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 diff --git a/motor_passo/exec/encoder/__init__.py b/motor_passo/exec/encoder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/motor_passo/exec/encoder/__main__.py b/motor_passo/exec/encoder/__main__.py new file mode 100644 index 0000000..b1eda93 --- /dev/null +++ b/motor_passo/exec/encoder/__main__.py @@ -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() diff --git a/motor_passo/exec/motor/__init__.py b/motor_passo/exec/motor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/motor_passo/__main__.py b/motor_passo/exec/motor/__main__.py similarity index 55% rename from motor_passo/__main__.py rename to motor_passo/exec/motor/__main__.py index 1ec6b40..96bdbbc 100644 --- a/motor_passo/__main__.py +++ b/motor_passo/exec/motor/__main__.py @@ -1,11 +1,13 @@ from motor_passo.motor import Motor +from motor_passo.utils import setup_cleanup def main(): + setup_cleanup() motor = Motor([13, 19, 26]) motor.setup() - motor.set_speed(90) - motor.step(24 * 100) + motor.set_speed(10) + motor.step(24 * 2) if __name__ == "__main__": diff --git a/motor_passo/utils.py b/motor_passo/utils.py new file mode 100644 index 0000000..f9ea39e --- /dev/null +++ b/motor_passo/utils.py @@ -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)