From 51f797cc4bed1bf37510c10c9a24bade00bd0fc6 Mon Sep 17 00:00:00 2001 From: Simon Pirkelmann Date: Fri, 12 Apr 2019 19:17:12 +0200 Subject: [PATCH] implemented basic motor class --- micropython_firmware/main.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/micropython_firmware/main.py b/micropython_firmware/main.py index fa81ada..1466360 100644 --- a/micropython_firmware/main.py +++ b/micropython_firmware/main.py @@ -1 +1,35 @@ -# empty file +import machine +import time + +class Motor: + def __init__(self, enable, dir1, dir2): + self.enable_pin = machine.Pin(enable, machine.Pin.OUT) + self.dir1_pin = machine.Pin(dir1, machine.Pin.OUT) + self.dir2_pin = machine.Pin(dir2, machine.Pin.OUT) + + self.direction = 1 # default direction: dir1_pin = HIGH, dir2_pin = LOW + self.reverse() + + def reverse(self): + print("reversing direction") + self.direction = not self.direction + self.dir1_pin.value(self.direction) + self.dir2_pin.value(not self.direction) + + def spin(self,seconds): + self.enable_pin.value(1) + time.sleep(seconds) + self.enable_pin.value(0) + +print("setting up pins") +m1 = Motor(4, 16, 5) +m2 = Motor(14, 0, 2) +print("setup complete") + +print("Motor test") +print("Motor 1:") +m1.spin(0.1) +print("Motor 2:") +m2.spin(0.1) + +print("done!")