-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
executable file
·180 lines (146 loc) · 3.22 KB
/
Copy pathrobot.py
File metadata and controls
executable file
·180 lines (146 loc) · 3.22 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import RPi.GPIO as GPIO
import sys
import time
import sys,tty,termios
from _thread import start_new_thread
import numpy as np
GPIO.setmode (GPIO.BOARD)
GPIO.setwarnings (False)
pin1=15
pin2=16
pin3=18
pin4=22
global moving
moving = False
global ultrasonicValues
ultrasonicValues = []
TRIG = 12
ECHO = 11
timesleep = 0.1
# Motor Control Setup
GPIO.setup(pin1, GPIO.OUT)
GPIO.setup(pin2, GPIO.OUT)
GPIO.setup(pin3, GPIO.OUT)
GPIO.setup(pin4, GPIO.OUT)
GPIO.output(pin1, True)
GPIO.output(pin2, True)
GPIO.output(pin3, True)
GPIO.output(pin4, True)
# Ultrasonic Setup
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
def get_distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
pulse_start = time.time()
pulse_end = time.time()
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 0)
#print (distance)
return distance
def drive(input):
print ("Setting GPIO")
print (input)
if input[0] == "1":
GPIO.output(pin1, False)
else:
GPIO.output(pin1, True)
time.sleep (timesleep)
if input[1] == "1":
GPIO.output(pin2, False)
else:
GPIO.output(pin2, True)
time.sleep (timesleep)
if input[2] == "1":
GPIO.output(pin3, False)
else:
GPIO.output(pin3, True)
time.sleep (timesleep)
if input[3] == "1":
GPIO.output(pin4, False)
else:
GPIO.output(pin4, True)
return
def prevent_crash():
global moving
ultrasonicValues
while True:
distance = get_distance()
if len(ultrasonicValues) > 10:
ultrasonicValues.pop(0)
ultrasonicValues.append(int(distance))
#print ("Distance: " + str(ultrasonicValues))
my_arr = np.array(ultrasonicValues)
time.sleep (0.1)
if distance < 30 and moving and my_arr.std() < 20:
drive ('1010')
time.sleep(1)
drive ('0100')
# time.sleep(random())
time.sleep(1)
drive ('0101')
# moving = False
# print ("Std: " + str(round(my_arr.std(),0)))
return
print ("Robot initializing...")
print ("Waiting For Ultrasonic sensor to settle")
time.sleep(2)
print ("Ready!")
start_new_thread (prevent_crash, ())
class _Getch:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(3)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def get():
global moving
inkey = _Getch()
while(1):
k=inkey()
print (k)
if k!='':break
if k==(chr(27)+chr(91)+chr(65)):
if moving:
print ("Stop")
drive('0000')
moving = False
else:
print ("Forward")
drive('0101')
moving = True
elif k==(chr(27)+chr(91)+chr(66)):
if moving:
print ("Stop")
drive('0000')
moving = False
else:
print ("Backward")
drive('1010')
moving = True
elif k=='\x1b[C':
print ("Right")
drive('0100')
moving = True
elif k=='\x1b[D':
print ("Left")
drive('0001')
moving = True
else:
print ("not an arrow key!", ord(k))
def main():
while True:
get()
if __name__=='__main__':
main()