-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine learning how to break.py
189 lines (112 loc) · 3.16 KB
/
machine learning how to break.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
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
179
180
181
182
183
from sklearn import svm
import RPi.GPIO as gpio
import time
import random
import simplejson
learn_cases = []
output_test_cases = []
file = open('test_cases.json' ,'r')
learn_cases = simplejson.loads(file)
file.close()
file = open('output_test_cases.json' ,'r')
output_test_cases = simplejson.loads(file)
file.close()
# fit machine learning SVM
from sklearn import svm
clf = svm.SVC()
clf.fit( learn_cases, output_test_cases)
#gpio.setmode(gpio.BCM)
gpio.setmode(gpio.BOARD)
# ultrasonic distance
forw = 7
back = 11
left = 13
right = 15
# set the pin direction
gpio.setup ( forw, gpio.OUT)
gpio.setup ( back, gpio.OUT)
gpio.setup ( left, gpio.OUT)
gpio.setup ( right, gpio.OUT)
# ultrasonic distance
trig = 38
echo = 40
gpio.setup(trig, gpio.OUT)
gpio.setup(echo, gpio.IN)
def trig_measurement():
gpio.output(trig,1)
time.sleep(0.00001)
gpio.output(trig,0)
def actual_distance():
while gpio.input(echo ) == 0:
pass
start = time.time()
while gpio.input(echo) == 1 :
pass
stop = time.time()
gpio.cleanup()
return ( stop - start ) * 170
def if_start_break(start_time):
final_object_distance = clf.predict([time.time() - start_time, actual_distance()])
if (final_object_distance() <= 10 ):
return actual_distance
else:
return 0
end
def go_forward():
gpio.output(forw, True)
gpio.output(back, False)
def go_back():
gpio.output(forw ,False)
gpio.output(back, True)
def engine_off():
gpio.output(forw, False)
gpio.output(back, False)
def return_to_distance(distance):
go_back()
stop = 0
while stop == 0:
if actual_distance() <= distance:
stop = 1
engine_off()
def learn():
start_time =time.time()
go_forward()
stop = 0
while stop == 0:
# stops execution after 6 seconds
if time.time() > start_time + 6 :
distance_start_break = actual_distance()
speed_at_start_break = time.time() - start_time
stop = 1
if (if_start_break(start_time)):
distance_start_break = actual_distance()
speed_at_start_break = time.time() - start_time
stop = 1
#read distance after break:
gpio.output(7, 0)
gpio.output(11, 0)
# todo please wait until the vehicle really stops
# now for simplicity waiting 1 seconds after turning off engine
time.sleep(1)
distance_after_break = actual_distance()
print ' ********* starting break ********* '
print ' distance start break ' + distance_start_break
print ' speed ( time delta from moving ) ' + speed_at_start_break
learn_cases.append[distance_start_break,speed_at_start_break ]
output_test_cases.append(distance_after_break)
print 'distance stop break ' + distance_after_break
# refit
clf.fit(learn_cases, output_test_cases)
return_to_distance(100)
learn()
learn()
learn()
learn()
# save testcases to a file
f= open('test_cases.json' ,'w')
simplejson.dump(learn_cases, f)
f.close()
# save output test cases to a file
f= open('output_test_cases.json' ,'w')
simplejson.dump(output_test_cases, f)
f.close()