-
Notifications
You must be signed in to change notification settings - Fork 1
/
motor.py
68 lines (59 loc) · 2.32 KB
/
motor.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
import RPi.GPIO as gpio
class Motor:
__MOTOR1A = 17
__MOTOR1B = 22
__MOTOR2A = 23
__MOTOR2B = 24
__Frequency = 20
#change if car doesnt drive straight
__DutyCycleA = 25
__DutyCycleB = 31
__DutyCycleT = 20
__Stop = 0
def __init__(self):
gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(self.__MOTOR1A, gpio.OUT)
gpio.setup(self.__MOTOR1B, gpio.OUT)
gpio.setup(self.__MOTOR2A, gpio.OUT)
gpio.setup(self.__MOTOR2B, gpio.OUT)
self.pwmMOTOR1A = gpio.PWM(self.__MOTOR1A, self.__Frequency)
self.pwmMOTOR1B = gpio.PWM(self.__MOTOR1B, self.__Frequency)
self.pwmMOTOR2A = gpio.PWM(self.__MOTOR2A, self.__Frequency)
self.pwmMOTOR2B = gpio.PWM(self.__MOTOR2B, self.__Frequency)
self.pwmMOTOR1A.start(self.__Stop)
self.pwmMOTOR1B.start(self.__Stop)
self.pwmMOTOR2A.start(self.__Stop)
self.pwmMOTOR2B.start(self.__Stop)
def forward(self):
print("GO FORWARD")
self.pwmMOTOR1A.ChangeDutyCycle(self.__DutyCycleA)
self.pwmMOTOR1B.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR2A.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR2B.ChangeDutyCycle(self.__DutyCycleB)
def backward(self):
print("GO BACK")
self.pwmMOTOR1A.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR1B.ChangeDutyCycle(self.__DutyCycleA)
self.pwmMOTOR2A.ChangeDutyCycle(self.__DutyCycleB)
self.pwmMOTOR2B.ChangeDutyCycle(self.__Stop)
def turnLeft(self):
print("TURN LEFT")
self.pwmMOTOR1A.ChangeDutyCycle(self.__DutyCycleT)
self.pwmMOTOR1B.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR2A.ChangeDutyCycle(self.__DutyCycleT)
self.pwmMOTOR2B.ChangeDutyCycle(self.__Stop)
def turnRight(self):
print("TURN RIGHT")
self.pwmMOTOR1A.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR1B.ChangeDutyCycle(self.__DutyCycleT)
self.pwmMOTOR2A.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR2B.ChangeDutyCycle(self.__DutyCycleT)
def stop(self):
print("STOP")
self.pwmMOTOR1A.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR1B.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR2A.ChangeDutyCycle(self.__Stop)
self.pwmMOTOR2B.ChangeDutyCycle(self.__Stop)
def __del__(self):
gpio.cleanup()