moved code for L293D motor driver to seperate file

This commit is contained in:
Simon Pirkelmann 2019-06-26 13:02:37 +02:00
parent 7d69c91752
commit b755173c6b
2 changed files with 37 additions and 32 deletions

View File

@ -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))

View File

@ -2,43 +2,18 @@ import machine
import sys import sys
from machine import I2C, Pin from machine import I2C, Pin
import d1motor i2c = False
if i2c:
import d1motor
else:
import l293motor
import time import time
import usocket import usocket
import esp 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: class Robot:
def __init__(self): def __init__(self):
i2c = False
if i2c: if i2c:
print("setting up I2C ...") print("setting up I2C ...")
#d1 = Pin(5) #d1 = Pin(5)
@ -59,8 +34,9 @@ class Robot:
print("error: no i2c interfaces found!") print("error: no i2c interfaces found!")
sys.exit(1) sys.exit(1)
else: else:
self.m1 = Motor(14, 13, 12) print("setting up L293D motor")
self.m2 = Motor(5, 0, 4) self.m1 = l293motor.Motor(14, 13, 12)
self.m2 = l293motor.Motor(5, 0, 4)
ip = my_ip[0] ip = my_ip[0]
# setup socket for remote control # setup socket for remote control