From f94073e1ea6647f9bbd1ae86b0ac3c652aead3c7 Mon Sep 17 00:00:00 2001 From: xGlc Date: Tue, 14 Apr 2026 21:31:59 +0200 Subject: [PATCH] feat: stepper logic --- drv8825.py | 40 ++++++++++++++++++++++++++++++++++++++++ main.py | 8 ++++++-- motor.py | 27 +++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 drv8825.py create mode 100644 motor.py diff --git a/drv8825.py b/drv8825.py new file mode 100644 index 0000000..8f4f35a --- /dev/null +++ b/drv8825.py @@ -0,0 +1,40 @@ +from time import sleep_us + +from machine import Pin + + +class DRV8825: + LEFT = 0 + RIGHT = 1 + + def __init__( + self, + dir_pin: Pin, + step_pin: Pin, + sleep_pin: Pin, + rst_pin: Pin, + # microstep_pins: tuple[Pin, Pin, Pin], + ) -> None: + self.sleep_pin = sleep_pin + self.step_pin = step_pin + self.dir_pin = dir_pin + self.rst_pin = rst_pin + # self.microstep_pins = microstep_pins + pass + + def init(self): + self.sleep_pin.on() + self.rst_pin.on() + + def set_direction(self, direction: int) -> None: + if direction == self.LEFT: + self.dir_pin.off() + else: + self.dir_pin.on() + + def make_step(self, delay: int = 1000) -> None: + self.step_pin.on() + sleep_us(delay) + self.step_pin.off() + sleep_us(delay) + pass diff --git a/main.py b/main.py index 26a4b3d..48c543d 100644 --- a/main.py +++ b/main.py @@ -9,5 +9,9 @@ import display_program # led program import led_program -_thread.start_new_thread(led_program.boostrap, ()) -_thread.start_new_thread(display_program.display, ()) +# motor +import motor + +# _thread.start_new_thread(led_program.boostrap, ()) +# _thread.start_new_thread(display_program.display, ()) +_thread.start_new_thread(motor.motor_run, ()) diff --git a/motor.py b/motor.py new file mode 100644 index 0000000..6d88cd0 --- /dev/null +++ b/motor.py @@ -0,0 +1,27 @@ +from time import sleep + +from machine import Pin + +from drv8825 import DRV8825 + + +def motor_run(): + motor = DRV8825( + Pin(21, Pin.OUT), Pin(19, Pin.OUT), Pin(18, Pin.OUT), Pin(5, Pin.OUT) + ) + motor.init() + + while True: + motor.set_direction(DRV8825.LEFT) + + for i in range(0, 1000): + motor.make_step(2000) + + sleep(1) + + motor.set_direction(DRV8825.RIGHT) + + for i in range(0, 1000): + motor.make_step(2000) + + sleep(1)