-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaspberry Pi code.py
463 lines (434 loc) · 14.1 KB
/
Raspberry Pi code.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
452
453
454
455
456
457
458
459
460
461
462
# x axis of the ball: x_ball
# y axis of the ball: y_ball
# height of the ball: height_ball
# width of the ball : width_ball
# home goal coordinates: (home_x,home_y)
# opp goal coordinate: (opp_x,opp_y)
# our robot coordinate : (hodor_x,hodor_y)
# opponent robot coordinate: (ene_x,ene_y)
# x axis of the ball from radio: ball_x
# y axis of the ball from radio: ball_y
#----------------------------------------------------#
import serial
import functools
import math
import numpy as np
ser = serial.Serial('/dev/ttyACM0',115200)
ser1 = serial.Serial('/dev/ttyACM1',38400)
s = [0,1]
z=[]
center_box_x = 263
center_box_y = 190
cx =0
cy=0
height=0
width=0
gx=0
gy=0
bx=0
by=0
yx=0
yy=0
orr = 1
def from_pixy(x_ball,y_ball,height_ball,width_ball):
area = height_ball * width_ball
if x_ball > -0.15 and x_ball < 0.15:
if area > 1 or area < 90:
ser.write(b'2\n')
print("fwd")
#verdict = forward
else:
ser.write(b'5\n')
print("stop")
#verdict = stop
elif x_ball > 0.15 and x_ball < 1:
ser.write(b'6\n')
print("right")
#verdict = right
elif x_ball < -0.15 and x_ball > -1:
ser.write(b'4\n')
print("left")
#verdict = left
else:
print("none")
return
def distance(a_x1,a_y1,a_x2,a_y2):
ax = a_x1 - a_x2
ay = a_y1 - a_y2
dist = math.sqrt((ax*ax) + (ay*ay))
return dist
def orientation():
yaw = recieve_aurdino1()
if yaw in range(-50,50)
print("right orientation")
orr = 1
elif dist_hodor_goal > dist_ball_goal:
print("wrong orientation")
global orr
orr = -1
wrong_orr(ball_y)
return orr
def goal(button):
if button == 1:
#green
opp_x = 508
opp_y = 193
our_color = 'green'
elif button == 0:
#blue
opp_x = 18
opp_y = 187
our_color = 'blue'
return opp_x,opp_y,our_color;
def score_yaw():
def score(hodor_center_dist,ball_center_dist,yaw):
#yaw = 60
# range 59 to 0 from center
if hodor_center_dist < 59 and hodor_center_dist > 0:
ball_center_dist = abs(ball_center_dist)
hodor_center_dist = abs(hodor_center_dist)
if ball_center_dist > hodor_center_dist:
print("move left")
#ser.write(b'4\n')
if yaw < -1:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range (-1,1):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 1:
print("rotate left a bit")
#ser.write(b'48\n')
elif ball_center_dist < hodor_center_dist:
print("move right")
#ser.write(b'6\n')
if yaw < -1:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range (-1,1):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 1:
print("rotate left a bit")
#ser.write(b'48\n')
elif ball_center_dist == hodor_center_dist:
print("fwd")
#ser.write(b'2\n')
#range 2nd part of areana towards wall
elif hodor_center_dist > 59 and hodor_center_dist < 92:
ball_center_dist = abs(ball_center_dist)
hodor_center_dist = abs(hodor_center_dist)
if ball_center_dist > hodor_center_dist:
print("move left")
#ser.write(b'4\n')
if yaw < 25:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range(25,35):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 35:
print("rotate left a bit")
#ser.write(b'48\n')
elif ball_center_dist < hodor_center_dist:
if yaw < 25:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range (25,35):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 35:
print("rotate left a bit")
#ser.write(b'48\n')
elif ball_center_dist == hodor_center_dist:
print("move left")
#ser.write(b'4\n')
if yaw < 25:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range(25,35):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 35:
print("rotate left a bit")
#ser.write(b'48\n')
#range 3rd part of arena towards wall
elif hodor_center_dist > 59 and hodor_center_dist > 92:
ball_center_dist = abs(ball_center_dist)
hodor_center_dist = abs(hodor_center_dist)
if ball_center_dist > hodor_center_dist:
print("move left")
#ser.write(b'4\n')
if yaw < 55:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range(55,65):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 65:
print("rotate left a bit")
#ser.write(b'48\n')
elif ball_center_dist < hodor_center_dist:
if yaw < 55:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range (55,65):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 65:
print("rotate left a bit")
#ser.write(b'48\n')
elif ball_center_dist == hodor_center_dist:
print("move left")
#ser.write(b'4\n')
if yaw < 55:
print("rotate right a bit")
#ser.write(b'68\n')
elif yaw in range(55,65):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 65:
print("rotate left a bit")
#ser.write(b'48\n')
#center range away from wall
elif hodor_center_dist > -59 and hodor_center_dist < 0:
ball_center_dist = abs(ball_center_dist)
hodor_center_dist = abs(hodor_center_dist)
if ball_center_dist > hodor_center_dist:
print("move right")
#ser.write(b'6\n')
if yaw < -1:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range (-1,1):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 1:
print("rotate right a bit")
#ser.write(b'68\n')
elif ball_center_dist < hodor_center_dist:
print("move left")
#ser.write(b'4\n')
if yaw < -1:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range (-1,1):
print("go fwd")
#ser.write(b'2\n')
elif yaw > 1:
print("rotate right a bit")
#ser.write(b'68\n')
elif ball_center_dist == hodor_center_dist:
print("fwd")
#ser.write(b'2\n')
#range 2 of arena away from wall
elif hodor_center_dist < -59 and hodor_center_dist > -92:
ball_center_dist = abs(ball_center_dist)
hodor_center_dist = abs(hodor_center_dist)
if ball_center_dist > hodor_center_dist:
print("move right")
#ser.write(b'6\n')
if yaw > -25:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range(-35,-25):
print("go fwd")
#ser.write(b'2\n')
elif yaw < -35:
print("rotate right a bit")
#ser.write(b'68\n')
elif ball_center_dist < hodor_center_dist:
if yaw > -25:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range(-35,-25):
print("go fwd")
#ser.write(b'2\n')
elif yaw < -35:
print("rotate right a bit")
#ser.write(b'68\n')
elif ball_center_dist == hodor_center_dist:
print("move right")
#ser.write(b'6\n')
if yaw > -25:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range(-35,-25):
print("go fwd")
#ser.write(b'2\n')
elif yaw < -35:
print("rotate right a bit")
#ser.write(b'68\n')
#range 3 of arena away from wall
elif hodor_center_dist < -59 and hodor_center_dist < -92:
ball_center_dist = abs(ball_center_dist)
hodor_center_dist = abs(hodor_center_dist)
if ball_center_dist > hodor_center_dist:
print("move right")
#ser.write(b'6\n')
if yaw > -55:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range(-65,-55):
print("go fwd")
#ser.write(b'2\n')
elif yaw < -65:
print("rotate right a bit")
#ser.write(b'68\n')
elif ball_center_dist < hodor_center_dist:
if yaw > -55:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range(-65,-55):
print("go fwd")
#ser.write(b'2\n')
elif yaw < -65:
print("rotate right a bit")
#ser.write(b'68\n')
elif ball_center_dist == hodor_center_dist:
print("move right")
#ser.write(b'6\n')
if yaw > -55:
print("rotate left a bit")
#ser.write(b'48\n')
elif yaw in range(-65,-55):
print("go fwd")
#ser.write(b'2\n')
elif yaw < -65:
print("rotate right a bit")
#ser.write(b'68\n')
else:
print("go fwd")
#ser.write(b'2\n')
return
def wrong_orr(ball_y):
ball_center_dist = ball_y - 190
if ball_center_dist > 0:
print("diagonal left")
ser.write(b'1\n')
elif ball_center_dist < 0:
print("diagonal right")
ser.write(b'3\n')
return
def hitter(hodor_x):
if hodor_x < 138:
print("robot stop and hitter hit")
ser.write(b'55\n')
else:
print("move fwd")
ser.write(b'2\n')
return
def decision(hodor_x,hodor_y,ene_x,ene_y,ball_x,ball_y):
#yet to code
enemy_distance_ball = distance(ene_x,ene_y,ball_x,ball_y)
hodor_distance_ball = distance(hodor_x,hodoe_y,ball_x,ball_y)
if enemey_distance_ball > hodor_distance_ball:
print("offence")
verdict = 'defence'
elif enemey_distance_ball < hodor_distance_ball:
print("defence")
verdict = 'defence'
return verdict
def recieve_arduino1():
line = ser1.readline().decode('utf-8').rstrip()
yaw = float(line)
return yaw
def recieve_arduino():
global cx,cy,height,width,gx,gy,bx,by,yx,yy
line = ser.readline().decode('utf-8').rstrip()
print(line)
pixydata = line.split(",")
z = pixydata
length = len(z)
while length > 0:
if length > 3:
z1 = pixydata.pop(0)
z2 = pixydata.pop(0)
z3 = pixydata.pop(0)
z4 = pixydata.pop(0)
try:
cx = float(z1)
cy = float(z2)
#global height
height = float(z3)
#global width
width = float(z4)
except:
continue
elif length > 2:
data = pixydata.pop(0)
if data == 'Green:':
#global gx
gx1 = pixydata.pop(0)
gx = float(gx1)
#global gy
gy1 = pixydata.pop(0)
gy = float(gy1)
elif data =='blue:':
print("hi hi blue")
#global bx
bx1 = pixydata.pop(0)
bx = float(bx1)
#global by
by1 = pixydata.pop(0)
by = float(by1)
elif data == 'yellow:':
print("hi hi yellow")
#global yx
yx1 = pixydata.pop(0)
yx = float(yx1)
#global yy
yy1 = pixydata.pop(0)
yy = float(yy1)
print (cx,cy,height,width,gx,gy,bx,by,yx,yy)
return cx,cy,height,width,gx,gy,bx,by,yx,yy
#return cx,cy,height,width,gx,gy,bx,by,yx,yy
def our_color_decision(button,gx,gy,bx,by):
opp_x,opp_y,our_color = goal(button)
if our_color == 'green':
hodor_x = gx
hodor_y = gy
ene_x = bx
ene_y = by
elif our_color == 'blue':
hodor_x = bx
hodor_y = by
ene_x = gx
ene_y = gy
return hodor_x,hodor_y,ene_x,ene_y;
def penalty_box(button):
opp_x,opp_y,our_color = goal(button)
if our_color == 'green':
our_goal_x = 18
our_goal_y = 187
elif our_color == 'blue':
our_goal_x = 508
our_goal_y = 183
while True:
#hodor= gx,gy; ball = yx,yy; enemey = bx,by
print("im alive")
button = 0
cx1,cy1,height1,width1,gx1,gy1,bx1,by1,yx1,yy1 = recieve_arduino()
hodor_x,hodor_y,ene_x,ene_y = our_color_decision(button,gx1,gy1,bx1,by1)
#verdict = decision(hodor_x,hodor_y,ene_x,ene_y,yx,yy)
# print(yx1)
# print(yy1)
# print(hodor_x)
# print(hodor_y)
verdict = 'offence'
if verdict == 'offence':
from_pixy(cx1,cy1,height1,width1)
okay = orientation(yx1,yy1,hodor_x,hodor_y)
if (goal == 0):
hodor_center_dist = hodor_y - 190
ball_center_dist = ball_y - 190
print(hodor_center_dist,ball_center_dist)
score(hodor_center_dist,ball_center_dist,yaw)
elif(goal == 1):
hodor_center_dist = -hodor_y + 190
ball_center_dist = -ball_y + 190
print(hodor_center_dist,ball_center_dist)
score(hodor_center_dist,ball_center_dist,yaw)
hitter(hodor_x)
elif verdict == 'defence':
print("ETA go home")