-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrobot6.py
More file actions
149 lines (126 loc) · 2.91 KB
/
robot6.py
File metadata and controls
149 lines (126 loc) · 2.91 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import RPi.GPIO as gpio
import time
import sys
import Tkinter as tk
from sensor import distance
import random
def init():
gpio.setmode(gpio.BOARD)
gpio.setup(7, gpio.OUT)
gpio.setup(11, gpio.OUT)
gpio.setup(13, gpio.OUT)
gpio.setup(15, gpio.OUT)
def forward(tf):
gpio.output(7, False)
gpio.output(11, True)
gpio.output(13, True)
gpio.output(15, False)
time.sleep(tf)
gpio.cleanup()
def reverse(tf):
gpio.output(7, True)
gpio.output(11, False)
gpio.output(13, False)
gpio.output(15, True)
time.sleep(tf)
gpio.cleanup()
def turn_left(tf):
gpio.output(7, True)
gpio.output(11, True)
gpio.output(13, True)
gpio.output(15, False)
time.sleep(tf)
gpio.cleanup()
def turn_right(tf):
gpio.output(7, False)
gpio.output(11, True)
gpio.output(13, False)
gpio.output(15, False)
time.sleep(tf)
gpio.cleanup()
def pivot_left(tf):
gpio.output(7, True)
gpio.output(11, False)
gpio.output(13, True)
gpio.output(15, False)
time.sleep(tf)
gpio.cleanup()
def pivot_right(tf):
gpio.output(7, False)
gpio.output(11, True)
gpio.output(13, False)
gpio.output(15, True)
time.sleep(tf)
gpio.cleanup()
'''
def key_input(event):
init()
print 'Key:', event.char
key_press = event.char
sleep_time = 0.030
if key_press.lower() == 'w':
forward(sleep_time)
elif key_press.lower() == 's':
reverse(sleep_time)
elif key_press.lower() == 'a':
turn_left(sleep_time)
elif key_press.lower() == 'd':
turn_right(sleep_time)
elif key_press.lower() == 'q':
pivot_left(sleep_time)
elif key_press.lower() == 'e':
pivot_right(sleep_time)
else:
pass
curDis = distance('cm')
print('curdis is',curDis)
if curDis < 20:
init()
reverse(2)
command = tk.Tk()
command.bind('<KeyPress>', key_input)
command.mainloop()
'''
def check_front():
init()
dist = distance()
if dist < 25:
print('Too close,',dist)
init()
reverse(2)
dist = distance()
if dist < 25:
print('Too close,',dist)
init()
pivot_left(3)
init()
reverse(2)
dist = distance()
if dist < 25:
print('Too close, giving up',dist)
sys.exit()
def autonomy():
tf = 0.030
x = random.randrange(0,4)
if x == 0:
for y in range(30):
check_front()
init()
forward(tf)
elif x == 1:
for y in range(30):
check_front()
init()
pivot_left(tf)
elif x == 2:
for y in range(30):
check_front()
init()
turn_right(tf)
elif x == 3:
for y in range(30):
check_front()
init()
turn_left(tf)
for z in range(10):
autonomy()