-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motor_Class.py
88 lines (77 loc) · 2.42 KB
/
Motor_Class.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
79
80
81
82
83
84
85
86
87
88
from imports import *
class Motor_Control:
def __init__(self):
GPIO.setmode(GPIO.BOARD)
# LEFT MOTOR
self.motor1A = 13 # GPIO 27
self.motor1B = 15 # GPIO 22
# RIGHT MOTOR
self.motor2B = 16 # GPIO 23
self.motor2A = 18 # GPIO 24
# Set the GPIO pins as outputs
GPIO.setup(self.motor1A, GPIO.OUT)
GPIO.setup(self.motor1B, GPIO.OUT)
GPIO.setup(self.motor2A, GPIO.OUT)
GPIO.setup(self.motor2B, GPIO.OUT)
def go_forward(self, speed=100):
# L +
GPIO.output(self.motor1A, GPIO.LOW)
GPIO.output(self.motor1B, GPIO.HIGH)
# R +
GPIO.output(self.motor2A, GPIO.HIGH)
GPIO.output(self.motor2B, GPIO.LOW)
def go_backward(self, speed=100):
# L -
GPIO.output(self.motor1A, GPIO.HIGH)
GPIO.output(self.motor1B, GPIO.LOW)
# R -
GPIO.output(self.motor2A, GPIO.LOW)
GPIO.output(self.motor2B, GPIO.HIGH)
def turn_left(self, speed=100):
# L +
GPIO.output(self.motor1A, GPIO.LOW)
GPIO.output(self.motor1B, GPIO.HIGH)
# R -
GPIO.output(self.motor2A, GPIO.LOW)
GPIO.output(self.motor2B, GPIO.HIGH)
def u_turn(self, speed=100):
# L +
GPIO.output(self.motor1A, GPIO.LOW)
GPIO.output(self.motor1B, GPIO.HIGH)
# R 0
GPIO.output(self.motor2A, GPIO.LOW)
GPIO.output(self.motor2B, GPIO.HIGH)
def turn_right(self, speed=100):
# L -
GPIO.output(self.motor1A, GPIO.HIGH)
GPIO.output(self.motor1B, GPIO.LOW)
# R +
GPIO.output(self.motor2A, GPIO.HIGH)
GPIO.output(self.motor2B, GPIO.LOW)
def stop(self):
# L 0
GPIO.output(self.motor1A, GPIO.LOW)
GPIO.output(self.motor1B, GPIO.LOW)
# R 0
GPIO.output(self.motor2A, GPIO.LOW)
GPIO.output(self.motor2B, GPIO.LOW)
def cleanup(self):
GPIO.cleanup()
if __name__ == "__main__":
Motor_Control = Motor_Control()
# print('Go')
# Motor_Control.go_forward(80)
# time.sleep(5)
# Motor_Control.go_backward(100)
# time.sleep(5)
# print('Back')
# Motor_Control.go_backward(100)
# time.sleep(5)
# print('turning LEFT ...')
# Motor_Control.turn_left(100)
# time.sleep(5)
#print('turning RIGHT ...')
#Motor_Control.turn_right(100)
#time.sleep(5)
Motor_Control.stop()
print('stopped')