-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
78 lines (56 loc) · 1.86 KB
/
Copy pathrobot.py
File metadata and controls
78 lines (56 loc) · 1.86 KB
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
import RPi.GPIO as GPIO
from time import sleep
class Robot:
def __init__(self, Motor1A, Motor1B, Motor2A, Motor2B):
#Initialize instance variables
self.Motor1A = Motor1A
self.Motor1B = Motor1B
self.Motor2A = Motor2A
self.Motor2B = Motor2B
#Set GPIO to use board pin numbers
GPIO.setmode(GPIO.BOARD)
#Turn GPIO warnings OFF
GPIO.setwarnings(False)
#Setup pins as outputs
GPIO.setup(Motor1A, GPIO.OUT)
GPIO.setup(Motor1B, GPIO.OUT)
GPIO.setup(Motor2A, GPIO.OUT)
GPIO.setup(Motor2B, GPIO.OUT)
# Setup PWM output
self.M1A = GPIO.PWM(Motor1A, 60)
self.M1A.start(0)
self.M1B = GPIO.PWM(Motor1B, 60)
self.M1B.start(0)
self.M2A = GPIO.PWM(Motor2A, 60)
self.M2A.start(0)
self.M2B = GPIO.PWM(Motor2B, 60)
self.M2B.start(0)
def forward(self, speed=100):
self.M1A.ChangeDutyCycle(speed)
GPIO.output(self.Motor1B, GPIO.LOW)
self.M2A.ChangeDutyCycle(speed)
GPIO.output(self.Motor2B, GPIO.LOW)
def backward(self, speed=100):
GPIO.output(self.Motor1A, GPIO.LOW)
self.M1B.ChangeDutyCycle(speed)
GPIO.output(self.Motor2A, GPIO.LOW)
self.M2B.ChangeDutyCycle(speed)
def left(self, speed=100):
self.M1A.ChangeDutyCycle(speed)
GPIO.output(self.Motor1B, GPIO.LOW)
GPIO.output(self.Motor2A, GPIO.LOW)
self.M2B.ChangeDutyCycle(speed)
def right(self, speed=100):
GPIO.output(self.Motor1A, GPIO.LOW)
self.M1B.ChangeDutyCycle(speed)
self.M2A.ChangeDutyCycle(speed)
GPIO.output(self.Motor2B, GPIO.LOW)
def stop(self):
self.M1A.ChangeDutyCycle(0)
self.M1B.ChangeDutyCycle(0)
self.M2A.ChangeDutyCycle(0)
self.M2B.ChangeDutyCycle(0)
def cleanup(self):
self.M1A.stop()
self.M2A.stop()
GPIO.cleanup()