-
Notifications
You must be signed in to change notification settings - Fork 0
/
TB6612Library.py
62 lines (53 loc) · 1.89 KB
/
TB6612Library.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
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
class Motor:
in1 = ""
in2 = ""
pwm = ""
standbyPin = ""
#Defaults
hertz = 1000
reverse = False #Reverse flips the direction of the motor
#Constructor
def __init__(self, in1, in2, pwm, standbyPin, reverse):
self.in1 = in1
self.in2 = in2
self.pwm = pwm
self.standbyPin = standbyPin
self.reverse = reverse
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)
GPIO.setup(pwm,GPIO.OUT)
GPIO.setup(standbyPin,GPIO.OUT)
GPIO.output(standbyPin,GPIO.HIGH)
self.p = GPIO.PWM(pwm, self.hertz)
self.p.start(0)
def forward(self, speed):
if speed > 0:
dutyCycle = speed
if(self.reverse):
speed = speed * -1
GPIO.output(self.in1,GPIO.HIGH)
GPIO.output(self.in2,GPIO.LOW)
else:
GPIO.output(self.in1,GPIO.LOW)
GPIO.output(self.in2,GPIO.LOW)
self.p.ChangeDutyCycle(dutyCycle)
def backward(self, speed):
if speed > 0:
dutyCycle = speed
if(self.reverse):
speed = speed * -1
GPIO.output(self.in1,GPIO.LOW)
GPIO.output(self.in2,GPIO.HIGH)
else:
GPIO.output(self.in1,GPIO.LOW)
GPIO.output(self.in2,GPIO.LOW)
self.p.ChangeDutyCycle(dutyCycle)
def brake(self):
self.p.ChangeDutyCycle(0)
GPIO.output(self.in1,GPIO.HIGH)
GPIO.output(self.in2,GPIO.HIGH)
def standby(self, value):
self.p.ChangeDutyCycle(0)
GPIO.output(self.standbyPin,value)