diff --git a/motor_passo/encoder.py b/motor_passo/encoder.py index 051a47f..b5fbf57 100644 --- a/motor_passo/encoder.py +++ b/motor_passo/encoder.py @@ -11,7 +11,8 @@ class Encoder: _curr_steps: int = 0 def setup(self): - gpio.setmode(gpio.BCM) + if not gpio.getmode(): + gpio.setmode(gpio.BCM) gpio.setup(self.pin_a, gpio.IN) gpio.setup(self.pin_b, gpio.IN) diff --git a/motor_passo/exec/motor/__main__.py b/motor_passo/exec/motor/__main__.py index 96bdbbc..7f55fb6 100644 --- a/motor_passo/exec/motor/__main__.py +++ b/motor_passo/exec/motor/__main__.py @@ -7,7 +7,10 @@ def main(): motor = Motor([13, 19, 26]) motor.setup() motor.set_speed(10) - motor.step(24 * 2) + + while True: + input() + motor.step(1) if __name__ == "__main__": diff --git a/motor_passo/motor.py b/motor_passo/motor.py index 2e6ca07..a43d739 100644 --- a/motor_passo/motor.py +++ b/motor_passo/motor.py @@ -13,12 +13,15 @@ class Motor: _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: - gpio.setmode(gpio.BCM) + if not gpio.getmode(): + gpio.setmode(gpio.BCM) for pin in self.pins: gpio.setup(pin, gpio.OUT) @@ -28,22 +31,18 @@ class Motor: 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) + 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, step_num: int): - assert (step_num >= 0 and step_num < 3) + def _step(self, direction: int): + print(self._next_step, self._next_step % self._period) - conf = self._step_configs[0][step_num] + 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) diff --git a/motor_passo/utils.py b/motor_passo/utils.py index f9ea39e..6a7f6e0 100644 --- a/motor_passo/utils.py +++ b/motor_passo/utils.py @@ -1,7 +1,7 @@ import signal -def _cleanup(sig, frame): +def _cleanup(*args, **kwargs): import sys import RPi.GPIO as gpio gpio.cleanup()