Skip to content

Commit

Permalink
Program can successfully drive motors!
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesevickery committed Aug 1, 2016
1 parent dc73948 commit 48287ab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
61 changes: 35 additions & 26 deletions l293d/l293d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
from time import sleep
from threading import Thread

verbose = True # Prints stuff to the terminal
test_mode = False # Disables GPIO calls when true
pins_in_use = [] # Lists pins in use (all motors)

from info import version
print('L293D driver version ' + version.num_string)

try:
import RPi.GPIO as GPIO
except Exception as e:
print('RPi.GPIO not installed.')
print("Can't import RPi.GPIO. Please (re)install.")
test_mode = True
print('Test mode has been enabled. Please view README for more info.')

if not test_mode:
try:
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
if verbose: print('GPIO mode set (GPIO.BOARD)')
except Exception as e:
print('Can\'t set GPIO mode (GPIO.BOARD)')

verbose = True # Prints stuff to the terminal
test_mode = True # Disables GPIO calls when true
pins_in_use = []
print("Can't set GPIO mode (GPIO.BOARD)")

class motor(object):
"""
Expand All @@ -36,59 +37,67 @@ class motor(object):
motor_pins = [0 for x in range(3)]

def __init__(self, pinA=0, pinB=0, pinC=0):
self.pinA = pinA
self.pinB = pinB
self.pinC = pinC
self.motor_pins[0] = pinA
self.motor_pins[1] = pinB
self.motor_pins[2] = pinC

self.pins_are_valid(pinA, pinB, pinC)
pins_in_use.append([pinA, pinB, pinC])
self.gpio_setup(pinA, pinB, pinC)
self.pins_are_valid(self.motor_pins)
pins_in_use.append(self.motor_pins)
self.gpio_setup(self.motor_pins)


def pins_are_valid(self, pinA, pinB, pinC):
pins = [pinA, pinB, pinC]
def pins_are_valid(self, pins):
for pin in pins:
pin_int = int(pin)
if (pin_int < 1) or (pin_int > 40):
raise ValueError('GPIO pin number needs to be between 1 and 40 inclusively.')
for motor in pins_in_use:
if pin_int in motor:
for pin_in_use in pins_in_use:
if pin_int in pin_in_use:
raise ValueError('GPIO pin {} already in use.'.format(pin_int))
self.motor_pins = pins
return True


def gpio_setup(self, A, B, C):
for pin in [A, B, C]:
def gpio_setup(self, pins):
for pin in pins:
if not test_mode: GPIO.setup(pin, GPIO.OUT)


def spin(self, direction=1, duration=None, wait=True):
if verbose: print('spinning motor at {} clockwise.'.format(self.pins_string_list()))
# Code to start to spin motor (self) according to direction
if duration is not None:
def drive_motor(self, direction=1, duration=None, wait=True):
if not test_mode:
if (direction == 0):
GPIO.output(self.motor_pins[0], GPIO.LOW)
else:
GPIO.output(self.motor_pins[direction], GPIO.HIGH)
GPIO.output(self.motor_pins[direction*-1], GPIO.LOW)
GPIO.output(self.motor_pins[0], GPIO.HIGH)
if (duration is not None) and (direction != 0):
stop_thread = Thread(target=self.stop, args = (duration, ))
stop_thread.start()
if wait:
stop_thread.join()


def pins_string_list(self):
return '[{}, {} and {}]'.format(str(self.pinA), str(self.pinB), str(self.pinC))
return '[{}, {} and {}]'.format(tuple(self.motor_pins))


def spin_clockwise(self, duration=None, wait=True):
self.spin(direction=1, duration=duration, wait=wait)
if verbose: print('spinning motor at pins {} clockwise.'.format(str(self)))
self.drive_motor(direction=1, duration=duration, wait=wait)


def spin_anticlockwise(self, duration=None, wait=True):
self.spin(direction=-1, duration=duration, wait=wait)
if verbose: print('spinning motor at pins {} anticlockwise.'.format(str(self)))
self.drive_motor(direction=-1, duration=duration, wait=wait)


def stop(self, after=0):
if after > 0: sleep(after)
if verbose: print('stopping {}.'.format(str(self)))
if verbose: print('stopping motor at pins {}.'.format(str(self)))
if not test_mode: self.drive_motor(direction=0, duration=after, wait=True)


def cleanup():
if not test_mode: GPIO.cleanup()

10 changes: 10 additions & 0 deletions l293d/tellus-l392d_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from time import sleep
import l293d

m1 = l293d.motor(22, 16, 18)

m1.spin_clockwise(1)
sleep(1)
m1.spin_anticlockwise()
m1.stop(2)

0 comments on commit 48287ab

Please sign in to comment.