49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
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
|
|
_step_configs: list[list[int]] = field(default_factory=lambda: [
|
|
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
|
|
])
|
|
_next_step: int = 0
|
|
_period: int = 3
|
|
|
|
def __post_init__(self) -> None:
|
|
self.set_speed(4)
|
|
|
|
def setup(self) -> None:
|
|
if not gpio.getmode():
|
|
gpio.setmode(gpio.BCM)
|
|
|
|
for pin in self.pins:
|
|
gpio.setup(pin, gpio.OUT)
|
|
gpio.output(pin, gpio.LOW)
|
|
|
|
def step(self, steps: int) -> None:
|
|
direction = int(steps / abs(steps))
|
|
steps = abs(steps)
|
|
|
|
for i in range(steps):
|
|
self._step(direction)
|
|
sleep(self._delay)
|
|
|
|
def set_speed(self, rpm: int) -> None:
|
|
self._delay = 60 / self.rev_steps / rpm
|
|
|
|
def _step(self, direction: int):
|
|
print(self._next_step, self._next_step % self._period)
|
|
|
|
conf = self._step_configs[0][self._next_step % self._period]
|
|
self._next_step += direction
|
|
|
|
for p, c in zip(self.pins, conf):
|
|
gpio.output(p, c)
|