Add basic Motor class
This commit is contained in:
44
motor_passo/motor.py
Normal file
44
motor_passo/motor.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import RPi.GPIO as gpio
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from time import sleep
|
||||
|
||||
|
||||
@dataclass
|
||||
class Motor:
|
||||
pins: list[int] = field(default_factory=list)
|
||||
rev_steps: int = 24
|
||||
|
||||
_delay: int = 0
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
self.set_speed(4)
|
||||
|
||||
def setup(self) -> None:
|
||||
for pin in self.pins:
|
||||
gpio.setmode(pin, gpio.OUT)
|
||||
|
||||
def step(self, steps: int) -> None:
|
||||
direction = int(steps / abs(steps))
|
||||
steps = abs(steps)
|
||||
|
||||
if direction > 0:
|
||||
_steps = range(0, steps)
|
||||
else:
|
||||
_steps = range(steps, 0, -1)
|
||||
|
||||
for i in _steps:
|
||||
self._step(i % 3)
|
||||
sleep(self._delay)
|
||||
|
||||
def set_speed(self, rpm: int) -> None:
|
||||
self._delay = 60 * 1000 / self.rev_steps / rpm
|
||||
|
||||
def _step(self, step_num: int):
|
||||
assert (step_num >= 0 and step_num < 3)
|
||||
|
||||
confs = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
||||
conf = confs[step_num]
|
||||
|
||||
for p, c in zip(self.pins, conf):
|
||||
gpio.output(p, c)
|
||||
Reference in New Issue
Block a user