-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover.py
74 lines (64 loc) · 2.13 KB
/
rover.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
import RPi.GPIO as GPIO
import pigpio
from time import sleep
import sys
class Rover():
def __init__(self, in1, in2, en1, in3, in4, en2, pan, tilt):
self.in1 = in1
self.in2 = in2
self.en1 = en1
self.in3 = in3
self.in4 = in4
self.en2 = en2
self.pan = pan
self.tilt = tilt
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
GPIO.setup(en1, GPIO.OUT)
GPIO.setup(in3, GPIO.OUT)
GPIO.setup(in4, GPIO.OUT)
GPIO.setup(en2, GPIO.OUT)
GPIO.setup(pan, GPIO.OUT)
GPIO.setup(tilt, GPIO.OUT)
self.motors_low()
self.motor_right = GPIO.PWM(en1, 1000)
self.motor_right.start(80)
self.motor_left = GPIO.PWM(en2, 1000)
self.motor_left.start(80)
self.pi = pigpio.pi()
self.pi.set_servo_pulsewidth(pan, 1500)
self.pi.set_servo_pulsewidth(tilt, 1500)
sleep(0.3)
def motors_low(self):
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.LOW)
def cleanup(self):
self.pi.set_servo_pulsewidth(self.pan, 1500)
self.pi.set_servo_pulsewidth(self.tilt, 1500)
sleep(0.3)
self.pi.set_servo_pulsewidth(self.pan, 0)
self.pi.set_servo_pulsewidth(self.tilt, 0)
self.pi.stop()
GPIO.cleanup()
def forward(self):
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.HIGH)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.HIGH)
def backward(self):
GPIO.output(self.in1, GPIO.HIGH)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.HIGH)
GPIO.output(self.in4, GPIO.LOW)
def left(self):
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.HIGH)
GPIO.output(self.in3, GPIO.HIGH)
GPIO.output(self.in4, GPIO.LOW)
def right(self):
GPIO.output(self.in1, GPIO.HIGH)
GPIO.output(self.in2, GPIO.LOW)
GPIO.output(self.in3, GPIO.LOW)
GPIO.output(self.in4, GPIO.HIGH)