-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoprobot.py
More file actions
128 lines (118 loc) · 3.12 KB
/
coprobot.py
File metadata and controls
128 lines (118 loc) · 3.12 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!usr/bin/python
import RPi.GPIO as GPIO
import time
import sys
import getopt
import string
from distance import measure_average
GPIO.setmode(GPIO.BCM)
L1 = 5
R1 = 6
L2 = 23
R2 = 24
recoveryCount = 0
GPIO.setup(L1,GPIO.OUT)
GPIO.setup(R1,GPIO.OUT)
GPIO.setup(L2,GPIO.OUT)
GPIO.setup(R2,GPIO.OUT)
def forward():
GPIO.output(L1,GPIO.LOW)
GPIO.output(R1,GPIO.HIGH)
GPIO.output(L2,GPIO.HIGH)
GPIO.output(R2,GPIO.LOW)
def backward():
GPIO.output(L1,GPIO.HIGH)
GPIO.output(R1,GPIO.LOW)
GPIO.output(L2,GPIO.LOW)
GPIO.output(R2,GPIO.HIGH)
def turn_right():
GPIO.output(L1,GPIO.LOW)
GPIO.output(R1,GPIO.LOW)
GPIO.output(L2,GPIO.HIGH)
GPIO.output(R2,GPIO.LOW)
def turn_left():
GPIO.output(L1,GPIO.LOW)
GPIO.output(R1,GPIO.HIGH)
GPIO.output(L2,GPIO.LOW)
GPIO.output(R2,GPIO.LOW)
def stop():
GPIO.output(L1,GPIO.LOW)
GPIO.output(R1,GPIO.LOW)
GPIO.output(L2,GPIO.LOW)
GPIO.output(R2,GPIO.LOW)
def main(argv):
try:
opts, args = getopt.getopt(argv[1:], "a:t:")
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
for op,value in opts:
if op == "-a":
if value == "f":
print "Foward..."
stop()
forward()
if value == "b":
print "Backward..."
stop()
backward()
if value == "l":
print "Turn left..."
stop()
turn_left()
if value == "r":
print "Turn right..."
stop()
turn_right()
if value == "s":
print "Stop..."
stop()
if op == "-t":
time.sleep(string.atoi(value))
def autoMove():
try:
while True:
distance = measure_average()
if distance > 35:
print "Distance : %.1fcm, forward" % distance
forward()
if distance <= 35:
global recoveryCount
recoveryCount = recoveryCount + 1
if recoveryCount < 4:
stop()
time.sleep(0.5)
print "Distance : %.1fcm < 35cm , need turn left " % distance
turn_left()
time.sleep(1.45)
stop()
if recoveryCount >= 4:
print "Recovery count = %.1f, need to backward" % recoveryCount
backward()
recoveryCount = 0
time.sleep(1)
turn_right()
time.sleep(1.45)
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
recoveryCount = 0
if __name__ == '__main__':
print "Hi,I'm coprobot, ready to go"
main(sys.argv)
autoMove()
# forward()
# time.sleep(2220)
# print "Turn right..."
# turn_right()
# time.sleep(200)
# print "Turn left..."
# turn_left()
# time.sleep(200)
# print "Forward..."
# backward()
# time.sleep(100)
print "Byebye!"
stop()
GPIO.cleanup()
recoveryCount = 0