From b755173c6b825ce8e39ffc68086a6c9dec65661f Mon Sep 17 00:00:00 2001 From: spirkelmann Date: Wed, 26 Jun 2019 13:02:37 +0200 Subject: [PATCH] moved code for L293D motor driver to seperate file --- micropython_firmware/l293motor.py | 29 ++++++++++++++++++++++ micropython_firmware/main.py | 40 +++++++------------------------ 2 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 micropython_firmware/l293motor.py diff --git a/micropython_firmware/l293motor.py b/micropython_firmware/l293motor.py new file mode 100644 index 0000000..afe62de --- /dev/null +++ b/micropython_firmware/l293motor.py @@ -0,0 +1,29 @@ +import machine + +class Motor: + def __init__(self, enable, dir1, dir2): + self.enable_pin = machine.Pin(enable, machine.Pin.OUT) + self.enable_pwm = machine.PWM(self.enable_pin) + self.enable_pwm.freq(250) + + 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): + self.direction = not self.direction + self.dir1_pin.value(self.direction) + self.dir2_pin.value(not self.direction) + + def speed(self, value): + if value > 0: # forward + if not self.direction: # switch direction if necessary + self.reverse() + else: # backward + if self.direction: # switch direction if necessary + self.reverse() + # apply value as pwm signal + self.enable_pwm.duty(int(abs(value)*10.23)) + diff --git a/micropython_firmware/main.py b/micropython_firmware/main.py index c66a392..ea79278 100644 --- a/micropython_firmware/main.py +++ b/micropython_firmware/main.py @@ -2,43 +2,18 @@ import machine import sys from machine import I2C, Pin -import d1motor +i2c = False +if i2c: + import d1motor +else: + import l293motor import time import usocket import esp -class Motor: - def __init__(self, enable, dir1, dir2): - self.enable_pin = machine.Pin(enable, machine.Pin.OUT) - self.enable_pwm = machine.PWM(self.enable_pin) - self.enable_pwm.freq(250) - - 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): - self.direction = not self.direction - self.dir1_pin.value(self.direction) - self.dir2_pin.value(not self.direction) - - def speed(self, value): - if value > 0: # forward - if not self.direction: # switch direction if necessary - self.reverse() - else: # backward - if self.direction: # switch direction if necessary - self.reverse() - # apply value as pwm signal - self.enable_pwm.duty(int(abs(value)*10.23)) - - class Robot: def __init__(self): - i2c = False if i2c: print("setting up I2C ...") #d1 = Pin(5) @@ -59,8 +34,9 @@ class Robot: print("error: no i2c interfaces found!") sys.exit(1) else: - self.m1 = Motor(14, 13, 12) - self.m2 = Motor(5, 0, 4) + print("setting up L293D motor") + self.m1 = l293motor.Motor(14, 13, 12) + self.m2 = l293motor.Motor(5, 0, 4) ip = my_ip[0] # setup socket for remote control