new base mech plate
[aversive.git] / projects / microb2010 / mainboard / display.py
1 import math, sys, time, os, random, re
2 from visual import *
3
4 FLOAT = "([-+]?[0-9]*\.?[0-9]+)"
5 INT = "([-+]?[0-9][0-9]*)"
6
7 AREA_X = 3000.
8 AREA_Y = 2100.
9
10 ROBOT_HEIGHT=5 # 350
11 CORN_HEIGHT=5  # 150
12
13 ROBOT_WIDTH=320
14 ROBOT_LENGTH=360
15
16 area = [ (0.0, 0.0, -0.2), (3000.0, 2100.0, 0.2) ]
17 areasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , area)
18 area_box = box(size=areasize, color=(0.0, 1.0, 0.0))
19
20 scene.autoscale=0
21
22 # all positions of robot every 5ms
23 save_pos = []
24
25 robot = box(color=(0.4, 0.4, 0.4))
26 lspickle = box(color=(0.4, 0.4, 0.4))
27 rspickle = box(color=(0.4, 0.4, 0.4))
28
29 last_pos = (0.,0.,0.)
30 hcenter_line = curve()
31 hcenter_line.pos = [(-AREA_X/2, 0., 0.3), (AREA_X/2, 0., 0.3)]
32 vcenter_line = curve()
33 vcenter_line.pos = [(0., -AREA_Y/2, 0.3), (0., AREA_Y/2, 0.3)]
34
35 yellowarea = [ (0.0, 0.0, -0.5), (500.0, 500.0, 0.5) ]
36 yellowareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , yellowarea)
37 yellowarea_box = box(pos=(-AREA_X/2+250,-AREA_Y/2+250,0), size=yellowareasize, color=(1.0, 1.0, 0.0))
38
39 bluearea = [ (0.0, 0.0, -0.5), (500.0, 500.0, 0.5) ]
40 blueareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , bluearea)
41 bluearea_box = box(pos=(AREA_X/2-250,-AREA_Y/2+250,0), size=blueareasize, color=(0.0, 0.0, 1.0))
42
43 greyarea = [ (0.0, 0.0, -0.5), (1520.0, 500.0, 0.5) ]
44 greyareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , greyarea)
45 greyarea_box = box(pos=(0,-AREA_Y/2+250,0), size=greyareasize, color=(0.3, 0.6, 0.3))
46
47 YELLOW = 0
48 BLUE = 1
49 color = YELLOW
50
51 def square(sz):
52     sq = curve()
53     sq.pos = [(-sz, -sz, 0.3),
54               (-sz, sz, 0.3),
55               (sz, sz, 0.3),
56               (sz, -sz, 0.3),
57               (-sz, -sz, 0.3),]
58     return sq
59
60 sq1 = square(250)
61 sq2 = square(500)
62
63 robot_x = 0.
64 robot_y = 0.
65 robot_a = 0.
66 robot_lspickle = 0
67 robot_rspickle = 0
68 robot_trail = curve()
69 robot_trail_list = []
70 max_trail = 500
71
72 area_objects = []
73
74 OFFSET_CORN_X=150
75 OFFSET_CORN_Y=222
76 STEP_CORN_X=225
77 STEP_CORN_Y=250
78
79 WAYPOINTS_NBX = 13
80 WAYPOINTS_NBY = 8
81
82 TYPE_WAYPOINT=0
83 TYPE_DANGEROUS=1
84 TYPE_WHITE_CORN=2
85 TYPE_BLACK_CORN=3
86 TYPE_OBSTACLE=4
87 TYPE_BALL=5
88 TYPE_NEIGH=6
89
90 col = [TYPE_WAYPOINT] * WAYPOINTS_NBY
91 waypoints = [col[:] for i in range(WAYPOINTS_NBX)]
92 corn_table = [TYPE_WHITE_CORN]*18
93
94 corn_side_confs = [
95     [ 1, 4 ],
96     [ 0, 4 ],
97     [ 2, 4 ],
98     [ 2, 3 ],
99     [ 0, 3 ],
100     [ 1, 3 ],
101     [ 1, 6 ],
102     [ 0, 6 ],
103     [ 2, 6 ],
104     ]
105 corn_center_confs = [
106     [ 5, 8 ],
107     [ 7, 8 ],
108     [ 5, 9 ],
109     [ 7, 8 ],
110     ]
111
112 def pt2corn(i,j):
113     if i == 0 and j == 2: return 0
114     if i == 0 and j == 4: return 1
115     if i == 0 and j == 6: return 2
116     if i == 2 and j == 3: return 3
117     if i == 2 and j == 5: return 4
118     if i == 2 and j == 7: return 5
119     if i == 4 and j == 4: return 6
120     if i == 4 and j == 6: return 7
121     if i == 6 and j == 5: return 8
122     if i == 6 and j == 7: return 9
123     if i == 8 and j == 4: return 10
124     if i == 8 and j == 6: return 11
125     if i == 10 and j == 3: return 12
126     if i == 10 and j == 5: return 13
127     if i == 10 and j == 7: return 14
128     if i == 12 and j == 2: return 15
129     if i == 12 and j == 4: return 16
130     if i == 12 and j == 6: return 17
131     return -1
132
133 def corn_get_sym(i):
134     sym = [15, 16, 17, 12, 13, 14, 10, 11, 8, 9, 6, 7, 3, 4, 5, 0, 1, 2]
135     return sym[i]
136
137 def init_corn_table(conf_side, conf_center):
138     global corn_table, corn_side_confs, corn_center_confs
139     print "confs = %d, %d"%(conf_side, conf_center)
140     for i in range(18):
141         if i in corn_side_confs[conf_side]:
142             corn_table[i] = TYPE_BLACK_CORN
143             continue
144         if corn_get_sym(i) in corn_side_confs[conf_side]:
145             corn_table[i] = TYPE_BLACK_CORN
146             continue
147         if i in corn_center_confs[conf_center]:
148             corn_table[i] = TYPE_BLACK_CORN
149             continue
150         if corn_get_sym(i) in corn_center_confs[conf_center]:
151             corn_table[i] = TYPE_BLACK_CORN
152             continue
153         corn_table[i] = TYPE_WHITE_CORN
154
155 def init_waypoints():
156     global waypoints, corn_table
157
158     for i in range(WAYPOINTS_NBX):
159         for j in range(WAYPOINTS_NBY):
160             # corn
161             c = pt2corn(i, j)
162             if c >= 0:
163                 waypoints[i][j] = corn_table[c]
164                 continue
165
166             # balls
167             if (i & 1) == 0 and j > 3:
168                 waypoints[i][j] = TYPE_BALL
169                 continue
170             if (i == 0 or i == WAYPOINTS_NBX-1) and j > 2:
171                 waypoints[i][j] = TYPE_BALL
172                 continue
173
174             # too close of border
175             if (i & 1) == 1 and j == WAYPOINTS_NBY -1:
176                 waypoints[i][j] = TYPE_OBSTACLE
177                 continue
178             # hill
179             if i >= 2 and i < WAYPOINTS_NBX - 2 and j < 2:
180                 waypoints[i][j] = TYPE_OBSTACLE
181                 continue
182             # dangerous points
183             if i == 0 or i == WAYPOINTS_NBX-1:
184                 waypoints[i][j] = TYPE_DANGEROUS
185                 continue
186             if (i&1) == 0 and j == WAYPOINTS_NBY-1:
187                 waypoints[i][j] = TYPE_DANGEROUS
188                 continue
189
190             waypoints[i][j] = TYPE_WAYPOINT
191
192         print i, waypoints[i]
193     return waypoints
194
195
196 def toggle_obj_disp():
197     global area_objects
198
199     """
200     if area_objects == []:
201         c = sphere(radius=5, color=(0., 0.,1.),
202                    pos=(1238.-AREA_X/2, 1313.-AREA_Y/2, 5))
203         area_objects.append(c)
204         c = sphere(radius=5, color=(0., 0.,1.),
205                    pos=(1364.-AREA_X/2, 1097.-AREA_Y/2, 5))
206         area_objects.append(c)
207         c = sphere(radius=5, color=(0., 0.,1.),
208                    pos=(1453.-AREA_X/2, 1176.-AREA_Y/2, 5))
209         area_objects.append(c)
210         c = sphere(radius=5, color=(0., 0.,1.),
211                    pos=(1109.-AREA_X/2, 1050.-AREA_Y/2, 5))
212         area_objects.append(c)
213 """
214     if area_objects == []:
215         i = 0
216         j = 0
217         x = OFFSET_CORN_X
218         while x < 3000:
219             if (i & 1) == 0:
220                 y = OFFSET_CORN_Y
221             else:
222                 y = OFFSET_CORN_Y + STEP_CORN_Y/2
223             j = 0
224             while y < 2100:
225                 print x,y
226                 if waypoints[i][j] == TYPE_WHITE_CORN:
227                     c = cylinder(axis=(0,0,1), length=CORN_HEIGHT,
228                                  radius=25, color=(0.8,0.8,0.8),
229                                  pos=(x-AREA_X/2,y-AREA_Y/2,CORN_HEIGHT/2))
230                     area_objects.append(c)
231                 elif waypoints[i][j] == TYPE_BLACK_CORN:
232                     c = cylinder(axis=(0,0,1), length=CORN_HEIGHT,
233                                  radius=25, color=(0.2,0.2,0.2),
234                                  pos=(x-AREA_X/2,y-AREA_Y/2,CORN_HEIGHT/2))
235                     area_objects.append(c)
236                 elif waypoints[i][j] == TYPE_BALL:
237                     c = sphere(radius=50, color=(1., 0.,0.),
238                                pos=(x-AREA_X/2,y-AREA_Y/2,50))
239                     area_objects.append(c)
240                 else:
241                     c = sphere(radius=5, color=(0., 0.,1.),
242                                pos=(x-AREA_X/2,y-AREA_Y/2,5))
243                     area_objects.append(c)
244                 j += 1
245                 y += STEP_CORN_Y
246             i += 1
247             x += STEP_CORN_X
248     else:
249         for o in area_objects:
250             if o.visible:
251                 o.visible = 0
252             else:
253                 o.visible = 1
254
255 def toggle_color():
256     global color
257     global BLUE, YELLOW
258     if color == YELLOW:
259         color = BLUE
260     else:
261         color = YELLOW
262
263 def set_robot():
264     global robot, last_pos, robot_trail, robot_trail_list
265     global save_pos, robot_x, robot_y, robot_a
266
267     if color == YELLOW:
268         tmp_x = robot_x - AREA_X/2
269         tmp_y = robot_y - AREA_Y/2
270         tmp_a = robot_a
271     else:
272         tmp_x = -robot_x + AREA_X/2
273         tmp_y = -robot_y + AREA_Y/2
274         tmp_a = robot_a
275
276     robot.pos = (tmp_x, tmp_y, ROBOT_HEIGHT/2)
277     axis = (math.cos(tmp_a*math.pi/180),
278             math.sin(tmp_a*math.pi/180),
279             0)
280
281     robot.axis = axis
282     robot.size = (ROBOT_LENGTH, ROBOT_WIDTH, ROBOT_HEIGHT)
283
284     robot_lspickle = 2 # XXX
285     lspickle.pos = (tmp_x + (robot_lspickle*60) * math.cos((tmp_a+90)*math.pi/180),
286                     tmp_y + (robot_lspickle*60) * math.sin((tmp_a+90)*math.pi/180),
287                     ROBOT_HEIGHT/2)
288     lspickle.axis = axis
289     lspickle.size = (20, ROBOT_WIDTH, 5)
290
291     robot_rspickle = 2 # XXX
292     rspickle.pos = (tmp_x + (robot_rspickle*60) * math.cos((tmp_a-90)*math.pi/180),
293                     tmp_y + (robot_rspickle*60) * math.sin((tmp_a-90)*math.pi/180),
294                     ROBOT_HEIGHT/2)
295     rspickle.axis = axis
296     rspickle.size = (20, ROBOT_WIDTH, 5)
297
298     # save position
299     save_pos.append((robot.pos.x, robot.pos.y, tmp_a))
300
301     pos = robot.pos.x, robot.pos.y, 0.3
302     if pos != last_pos:
303         robot_trail_list.append(pos)
304         last_pos = pos
305     robot_trail_l = len(robot_trail_list)
306     if robot_trail_l > max_trail:
307         robot_trail_list = robot_trail_list[robot_trail_l - max_trail:]
308     robot_trail.pos = robot_trail_list
309
310 def graph():
311     pass
312
313 def save():
314     f = open("/tmp/robot_save", "w")
315     for p in save_pos:
316         f.write("%f %f %f\n"%(p[0], p[1], p[2]))
317     f.close()
318
319 def silent_mkfifo(f):
320     try:
321         os.mkfifo(f)
322     except:
323         pass
324
325 #init_corn_table(random.randint(0,8), random.randint(0,3))
326 init_corn_table(0, 0)
327 waypoints = init_waypoints()
328 toggle_obj_disp()
329
330 while True:
331     silent_mkfifo("/tmp/.robot_sim2dis")
332     silent_mkfifo("/tmp/.robot_dis2sim")
333     while True:
334         fr = open("/tmp/.robot_sim2dis", "r")
335         fw = open("/tmp/.robot_dis2sim", "w", 0)
336         while True:
337             m = None
338             l = fr.readline()
339
340             # parse position
341             if not m:
342                 m = re.match("pos=%s,%s,%s"%(INT,INT,INT), l)
343                 if m:
344                     robot_x = int(m.groups()[0])
345                     robot_y = int(m.groups()[1])
346                     robot_a = int(m.groups()[2])
347                     set_robot()
348
349             # parse ballboard
350             if not m:
351                 m = re.match("ballboard=%s"%(INT), l)
352                 if m:
353                     print "ballboard: %d"%(int(m.groups()[0]))
354
355             # parse cobboard
356             if not m:
357                 m = re.match("cobboard=%s,%s"%(INT,INT), l)
358                 if m:
359                     print "cobboard: %x,%x"%(int(m.groups()[0]),int(m.groups()[1]))
360                     side = int(m.groups()[0])
361                     flags = int(m.groups()[1])
362                     if side == 0:
363                         if (flags & 0x01) == 0:
364                             robot_lspickle = 1
365                         else:
366                             robot_lspickle = 2
367                     else:
368                         if (flags & 0x01) == 0:
369                             robot_rspickle = 1
370                         else:
371                             robot_rspickle = 2
372
373             if scene.kb.keys == 0:
374                 continue
375
376             k = scene.kb.getkey()
377             x,y,z = scene.center
378             if k == "left":
379                 scene.center = x-10,y,z
380             elif k == "right":
381                 scene.center = x+10,y,z
382             elif k == "up":
383                 scene.center = x,y+10,z
384             elif k == "down":
385                 scene.center = x,y-10,z
386             elif k == "l":
387                 fw.write("l")
388             elif k == "r":
389                 fw.write("r")
390             elif k == "c":
391                 robot_trail_list = []
392             elif k == "x":
393                 save_pos = []
394             elif k == "g":
395                 graph()
396             elif k == "s":
397                 save()
398             elif k == "h":
399                 toggle_obj_disp()
400             elif k == "i":
401                 toggle_color()
402
403             # EOF
404             if l == "":
405                 break
406
407         fr.close()
408         fw.close()
409