-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL298N.py
78 lines (68 loc) · 2.17 KB
/
L298N.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
####################################################################
# L298N.py #
# Created by Ethan Reker 2017 #
# Module to simplify using the motor controller from the GPIO on #
# a raspberry pi #
####################################################################
import threading
import config as conf
import RPi.GPIO as GPIO
import time
class L298N(threading.Thread):
A0 = 0
A1 = 0
B0 = 0
B1 = 0
mode = "STOP"
def __init__(self, A0, A1, B0, B1):
print "init L298N"
print self.mode
threading.Thread.__init__(self)
self.A0 = A0
self.A1 = A1
self.B0 = B0
self.B1 = B1
GPIO.setup(self.A0, GPIO.OUT)
GPIO.setup(self.A1, GPIO.OUT)
GPIO.setup(self.B0, GPIO.OUT)
GPIO.setup(self.B1, GPIO.OUT)
def run(self):
print "self L298N"
while True:
print "run call"
print self.mode
conf.thread_lock.acquire()
mode = self.mode
conf.thread_lock.release()
if mode == "STOP":
GPIO.output(self.A0, GPIO.LOW)
GPIO.output(self.A1, GPIO.LOW)
GPIO.output(self.B0, GPIO.LOW)
GPIO.output(self.B1, GPIO.LOW)
if mode == "FORWARD":
GPIO.output(self.A0, GPIO.HIGH)
GPIO.output(self.A1, GPIO.LOW)
GPIO.output(self.B0, GPIO.HIGH)
GPIO.output(self.B1, GPIO.LOW)
print "RUNNING FORWARD"
if mode == "BACKWARD":
GPIO.output(self.A0, GPIO.LOW)
GPIO.output(self.A1, GPIO.HIGH)
GPIO.output(self.B0, GPIO.LOW)
GPIO.output(self.B1, GPIO.HIGH)
if mode == "LEFT":
GPIO.output(self.A0, GPIO.HIGH)
GPIO.output(self.A1, GPIO.LOW)
GPIO.output(self.B0, GPIO.LOW)
GPIO.output(self.B1, GPIO.HIGH)
if mode == "RIGHT":
GPIO.output(self.A0, GPIO.LOW)
GPIO.output(self.A1, GPIO.HIGH)
GPIO.output(self.B0, GPIO.HIGH)
GPIO.output(self.B1, GPIO.LOW)
time.sleep(0.1)
def setMode(self, mode):
print "Setting mode to ", mode
conf.thread_lock.acquire()
self.mode = mode
conf.thread_lock.release()