moved code for L293D motor driver to seperate file
This commit is contained in:
parent
7d69c91752
commit
b755173c6b
29
micropython_firmware/l293motor.py
Normal file
29
micropython_firmware/l293motor.py
Normal 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))
|
||||
|
|
@ -2,43 +2,18 @@ import machine
|
|||
import sys
|
||||
from machine import I2C, Pin
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user