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
|
||||||
0
motor_passo/exec/encoder/__init__.py
Normal file
0
motor_passo/exec/encoder/__init__.py
Normal file
17
motor_passo/exec/encoder/__main__.py
Normal file
17
motor_passo/exec/encoder/__main__.py
Normal 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()
|
||||||
0
motor_passo/exec/motor/__init__.py
Normal file
0
motor_passo/exec/motor/__init__.py
Normal 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
12
motor_passo/utils.py
Normal 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)
|
||||||
Reference in New Issue
Block a user