-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
451 lines (366 loc) · 14.4 KB
/
hello.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env python3
'''Hello to the world from ev3dev.org'''
import os
import sys
import time
import json
import math
import urllib.request
from ev3dev.ev3 import LargeMotor, Sound, ColorSensor, GyroSensor
# state constants
ON = True
OFF = False
def debug_print(*args, **kwargs):
'''Print debug messages to stderr.
This shows up in the output panel in VS Code.
'''
print(*args, **kwargs, file=sys.stderr)
def get_angle(gyro, readings_to_average):
angle = 0.0
for _ in range(readings_to_average):
angle += gyro.value()
return angle/readings_to_average
rotate_log_number = 0
def rotate_to_angle(angle, mL, mR, gyro):
mL.reset()
mR.reset()
e_old = integral = 0
start_time = t_beg = t_old = time.time()
errors = 0xffffffff
check_stuck = 0xffffffff
stuck_value = 0
k_p = 0.5
k_i = 33; max_i = 0.25
k_d = 0
speed = 30
# global rotate_log_number
# file = open("logs/rotate_to_angle_" + str(rotate_log_number) + ".txt", "w")
# file.write("Time\tError\tIntegral\tU\n")
# rotate_log_number += 1
global left_rotations
global right_rotations
e = angle - get_angle(gyro, 3)
if e < -20:
left_rotations += 1
if e > 20:
right_rotations += 1
global gyro_drift
gyro_drift = int((right_rotations * -12/8) + (left_rotations * 0/8))
e += gyro_drift
debug_print("Rotate for ", e, " degrees to ", angle, "degrees")
while errors: # or error_history < 10:
t = time.time()
# increase to 'speed' over 1 seconds
max_power = max(min((t - t_beg)* speed/1, speed), -speed)
dT = t - t_old
e = angle - gyro.value() + gyro_drift # Check if it's better to read the gyro less or even more times
if e == 0:
integral = 0
errors = ((errors << 1) & 0xffffffff) | (e != 0)
dE = e - e_old
u = k_p * e + k_i * integral + k_d * dE / dT
u = max(min(u, max_power), -max_power)
# Bump if stuck on same error (not enough force to move)
check_stuck = ((check_stuck << 1) & 0xffffffff) | (stuck_value != e)
stuck_value = e
if check_stuck == 0:
u *= 2
check_stuck = 0xffffffff
# correction for wheel spin difference so the ev3 stays in same position
if abs(e) > 4:
k_ns_p = 1
e_ns = mR.position + mL.position
u_ns = k_ns_p * e_ns
if u_ns > 0:
u_ns_r = 0
u_ns_l = u_ns
else:
u_ns_r = u_ns
u_ns_l = 0
else:
u_ns_r = 0
u_ns_l = 0
#k_p = 5
print(e, integral, u, sep=', ')
mR.run_direct(duty_cycle_sp=-u - u_ns_r)
mL.run_direct(duty_cycle_sp=u - u_ns_l)
integral = max(min(integral + e * dT, max_i), -max_i)
e_old = e
t_old = t
# comment out when not needed
#file.write(str(t-start_time) + "\t" + str(e) + "\t" + str(integral) + "\t" + str(u) + "\n")
mR.run_direct(duty_cycle_sp=0)
mL.run_direct(duty_cycle_sp=0)
#file.close()
drive_log_number = 0
def drive_for_centimeters(distance, mL, mR, gyro, angle):
distance_count = (distance*mR.count_per_rot)/17.16
distance_count = int(round(distance_count))
debug_print("Move for ", distance_count, "degrees")
mL.reset()
mR.reset()
check_stuck = 0xffffffff
stuck_value = 0
errors = 0xff
powr_corr = e_2_ang = integral_2 = e_old = e_old_2 = integral = 0
start_time = t_beg = t_old = time.time()
e = distance_count
# global drive_log_number
# file = open("logs/drive_for_centimeters_" + str(drive_log_number) + ".txt", "w")
# file2 = open("logs/drive_for_centimeters2_" + str(drive_log_number) + ".txt", "w")
# file.write("Time\tError\tIntegral\tU\n")
# file2.write("Time\tError\tIntegral\tU\n")
# drive_log_number += 1
k_p = 0.2
k_i = 28; max_i = 0.25 # 15 0.5
k_d = 0
k_p_2 = 1 # 1
k_i_2 = 0.0; max_i_2 = 10
k_d_2 = 0
speed = 35
if e > 0:
powr_corr = 1.027
else:
powr_corr = 0.998
global searching_neighbourhood
global gyro_drift
while errors:
t = time.time()
# increase to 'speed' over 2 seconds
max_power = max(min((t - t_beg)* speed/2.0, speed), -speed)
dT = t - t_old
e = distance_count - int((mL.position + mR.position)/2)
errors = ((errors << 1) & 0xff) | ((e > 1) | (e < -1))
dE = e - e_old
u = k_p * e + k_i * integral + k_d * dE / dT
u = max(min(u, max_power), -max_power)
# Bump if stuck on same error (not enough force to move)
check_stuck = ((check_stuck << 1) & 0xffffffff) | (stuck_value != e)
stuck_value = e
if check_stuck == 0:
u *= 2
check_stuck = 0xffffffff
# --------------------------------------------------------------------------------------------
if abs(e) > 15:
measured_angle = get_angle(gyro, 1) - gyro_drift # Check if it's better to read the gyro just one time or even more times
e_2_ang = (measured_angle - angle) # integral of the error is the actual error
dE_2 = e_2_ang - e_old_2
e_old_2 = e_2_ang
u_2 = k_p_2 * e_2_ang + k_i_2 * integral_2 + k_d_2 * dE_2/dT
u_2 = max(min(u_2, 10), -10)
mL.run_direct(duty_cycle_sp=u*powr_corr - u_2)
mR.run_direct(duty_cycle_sp=u + u_2)
integral_2 += e_2_ang * dT
integral_2 = max(min(integral_2, max_i_2), -max_i_2)
else:
mL.run_direct(duty_cycle_sp=u*powr_corr)
mR.run_direct(duty_cycle_sp=u)
# --------------------------------------------------------------------------------------------
#debug_print("Time:", t, "Error:", e, "Integral:", integral, "u:", u, "Integral_2:", integral_2, "u_2:", u_2, "e_2:", e_2, "e_2_ang:", e_2_ang, sep=' ')
print(e_2_ang, sep=', ')
integral = max(min(integral + e * dT, max_i), -max_i)
e_old = e
t_old = t
#file.write(str(t-start_time) + "\t" + str(-e) + "\t" + str(integral) + "\t" + str(u) + "\n")
#file2.write(str(t-start_time) + "\t" + str(-e_2) + "\t" + str(integral_2) + "\t" + str(u_2) + "\n")
if searching_neighbourhood and check_color():
return
mR.run_direct(duty_cycle_sp=0)
mL.run_direct(duty_cycle_sp=0)
# file.close()
# file2.close()
def calculate_angle(angle, current_angle):
factor = int(current_angle / 360)
angle = angle + factor * 360
if angle - current_angle > 180:
angle = angle - 360
elif current_angle - angle > 180:
angle = angle + 360
return angle
def go_to_location(x, y, current_x, current_y, mL, mR, gyro):
global gyro_drift
current_angle = get_angle(gyro, 5) - gyro_drift
relative_angle = abs(current_angle % 360)
delta_x = x - current_x
delta_y = y - current_y
debug_print("Moving to: ", x, ", ", y)#current_angle, relative_angle)
if relative_angle == 0:
if delta_x != 0:
drive_for_centimeters(delta_x, mL, mR, gyro, current_angle)
if delta_y != 0:
angle = calculate_angle(90, current_angle)
rotate_to_angle(angle, mL, mR, gyro)
drive_for_centimeters(delta_y, mL, mR, gyro, angle)
rotate_to_angle(angle, mL, mR, gyro)
else:
rotate_to_angle(current_angle, mL, mR, gyro)
elif relative_angle == 180:
if delta_x != 0:
drive_for_centimeters(-delta_x, mL, mR, gyro, current_angle)
if delta_y != 0:
angle = calculate_angle(90, current_angle)
rotate_to_angle(angle, mL, mR, gyro)
drive_for_centimeters(delta_y, mL, mR, gyro, angle)
rotate_to_angle(angle, mL, mR, gyro)
else:
rotate_to_angle(current_angle, mL, mR, gyro)
elif relative_angle == 90:
if delta_y != 0:
drive_for_centimeters(delta_y, mL, mR, gyro, current_angle)
if delta_x != 0:
angle = calculate_angle(0, current_angle)
rotate_to_angle(angle, mL, mR, gyro)
drive_for_centimeters(delta_x, mL, mR, gyro, angle)
rotate_to_angle(angle, mL, mR, gyro)
else:
rotate_to_angle(current_angle, mL, mR, gyro)
elif relative_angle == 270:
if delta_y != 0:
drive_for_centimeters(-delta_y, mL, mR, gyro, current_angle)
if delta_x != 0:
angle = calculate_angle(0, current_angle)
rotate_to_angle(angle, mL, mR, gyro)
drive_for_centimeters(delta_x, mL, mR, gyro, angle)
rotate_to_angle(angle, mL, mR, gyro)
else:
rotate_to_angle(current_angle, mL, mR, gyro)
def beep(times, beep_duration=1000):
for i in range(times):
Sound.tone(1500, beep_duration).wait()
time.sleep(0.5)
def check_color():
global cl
color = cl.value()
if color == 1: # BLACK: START, 2 second beep
debug_print("Color sensor: START")
return True
elif color == 2: # BLUE: good condition, 1 beep
debug_print("Color sensor: BLUE")
return True
elif color == 4: # YELLOW: critical condition, 2 beeps
debug_print("Color sensor: YELLOW")
return True
elif color == 5: # RED: passed away, 3 beeps
debug_print("Color sensor: RED")
return True
else:
debug_print("Color sensor: UNKNOWN (" + str(color) + ")")
return False
right_rotations = left_rotations = gyro_drift = 0
start_time = None
cl = None
searching_neighbourhood = False
def main():
data = None
if True:
with open('zemljevid.json') as f:
data = json.load(f)
else:
resource = urllib.request.urlopen('http://192.168.0.200:8080/zemljevid.json')
content = resource.read()
content = content.decode("utf-8")
data = json.loads(content)
os.system('setfont Lat15-TerminusBold14')
if os.path.exists("logs"):
import shutil
shutil.rmtree("logs")
os.mkdir("logs")
mL = LargeMotor('outB'); mL.stop_action = 'hold'
mR = LargeMotor('outC'); mR.stop_action = 'hold'
global cl
cl = ColorSensor()
cl.mode = 'COL-COLOR'
gy = GyroSensor()
gy.mode = 'GYRO-RATE'
gy.mode = 'GYRO-ANG'
# Give gyro a bit of time to start
time.sleep(3)
global start_time
start_time = time.time()
debug_print("Start time: ", start_time)
start = [0, 0]
locations = []
for key, item in data.items():
if key == "start":
start = item
else:
locations.append(item)
# Sort by distance, TODO might be better to minimize turns by prioritizing victims that are in the same line
locations = sorted(locations, key=lambda x: abs(start[0] - x[0]) + abs(start[1] - x[1]), reverse=False)
current_location = start
def reset_neighbourhood_search():
global searching_neighbourhood
nonlocal neighbourhood_locations
searching_neighbourhood = False
neighbourhood_locations = []
global searching_neighbourhood
searching_neighbourhood = False
neighbourhood_locations = []
while locations:
next_location = locations.pop(0)
go_to_location(x=next_location[0], y=next_location[1], current_x=current_location[0], current_y=current_location[1], mL=mL, mR=mR, gyro=gy)
current_location = next_location
color = cl.value()
if color == 1: # BLACK: START, 2 second beep
debug_print("Color sensor: START")
beep(1, 2000)
reset_neighbourhood_search()
locations = sorted(locations, key=lambda x: abs(start[0] - x[0]) + abs(start[1] - x[1]), reverse=False)
elif color == 2: # BLUE: good condition, 1 beep
debug_print("Color sensor: BLUE")
beep(1)
reset_neighbourhood_search()
locations.insert(0, start)
elif color == 4: # YELLOW: critical condition, 2 beeps
debug_print("Color sensor: YELLOW")
beep(2)
reset_neighbourhood_search()
locations.insert(0, start)
elif color == 5: # RED: passed away, 3 beeps
debug_print("Color sensor: RED")
beep(3)
reset_neighbourhood_search()
#locations.insert(0, start)
locations = sorted(locations, key=lambda x: abs(current_location[0] - x[0]) + abs(current_location[1] - x[1]), reverse=False)
else:
debug_print("Color sensor: UNKNOWN (" + str(color) + ")")
#locations.insert(0, start)
if not searching_neighbourhood:
searching_neighbourhood = True
radius = 5
for area in range(1,20):
neighbourhood_locations.append([next_location[0]+radius*area, next_location[1]-radius*area])
neighbourhood_locations.append([next_location[0]+radius*area, next_location[1]+radius*area])
neighbourhood_locations.append([next_location[0]-radius*area, next_location[1]+radius*area])
neighbourhood_locations.append([next_location[0]-radius*area, next_location[1]-radius*area])
locations.insert(0, neighbourhood_locations[0])
neighbourhood_locations = neighbourhood_locations[1:]
# # Rotate back to original orientation
# angle = calculate_angle(0, gy.value())
# rotate_to_angle(angle, mL, mR, gy)
# for i in range (16):
# rotate_to_angle(90, mL, mR, gy)
# rotate_to_angle(0, mL, mR, gy)
#for _ in range (5):
#rotate_to_angle(89, mL, mR, gy)
#rotate_to_angle(178, mL, mR, gy)
#rotate_to_angle(270, mL, mR, gy)
#rotate_to_angle(180, mL, mR, gy)
#rotate_to_angle(90, mL, mR, gy)
#rotate_to_angle(0, mL, mR, gy)
#rotate_to_angle(-91, mL, mR, gy)
#rotate_to_angle(-182, mL, mR, gy)
#rotate_to_angle(-2, mL, mR, gy)
#rotate_to_angle(89, mL, mR, gy)
#rotate_to_angle(178, mL, mR, gy)
#rotate_to_angle(-2, mL, mR, gy)
#rotate_to_angle(87, mL, mR, gy)
#rotate_to_angle(176, mL, mR, gy)
# for i in range (5):
# drive_for_centimeters(50, mL, mR, gy, 0)
# rotate_to_angle(0, mL, mR, gy)
# drive_for_centimeters(-50, mL, mR, gy, 0)
# rotate_to_angle(0, mL, mR, gy)
debug_print("End time: ", time.time())
if __name__ == '__main__':
main()