1 import math, sys, time, os, random, re
4 FLOAT = "([-+]?[0-9]*\.?[0-9]+)"
5 INT = "([-+]?[0-9][0-9]*)"
13 area = [ (0.0, 0.0, -0.2), (3000.0, 2100.0, 0.2) ]
14 areasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , area)
15 area_box = box(size=areasize, color=(0.0, 1.0, 0.0))
19 # all positions of robot every 5ms
22 robot = box(color=(0.4, 0.4, 0.4))
23 lspickle = box(color=(0.4, 0.4, 0.4))
24 rspickle = box(color=(0.4, 0.4, 0.4))
27 hcenter_line = curve()
28 hcenter_line.pos = [(-AREA_X/2, 0., 0.3), (AREA_X/2, 0., 0.3)]
29 vcenter_line = curve()
30 vcenter_line.pos = [(0., -AREA_Y/2, 0.3), (0., AREA_Y/2, 0.3)]
32 yellowarea = [ (0.0, 0.0, -0.5), (500.0, 500.0, 0.5) ]
33 yellowareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , yellowarea)
34 yellowarea_box = box(pos=(-AREA_X/2+250,-AREA_Y/2+250,0), size=yellowareasize, color=(1.0, 1.0, 0.0))
36 bluearea = [ (0.0, 0.0, -0.5), (500.0, 500.0, 0.5) ]
37 blueareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , bluearea)
38 bluearea_box = box(pos=(AREA_X/2-250,-AREA_Y/2+250,0), size=blueareasize, color=(0.0, 0.0, 1.0))
40 greyarea = [ (0.0, 0.0, -0.5), (1520.0, 500.0, 0.5) ]
41 greyareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , greyarea)
42 greyarea_box = box(pos=(0,-AREA_Y/2+250,0), size=greyareasize, color=(0.3, 0.6, 0.3))
50 sq.pos = [(-sz, -sz, 0.3),
87 col = [TYPE_WAYPOINT] * WAYPOINTS_NBY
88 waypoints = [col[:] for i in range(WAYPOINTS_NBX)]
89 corn_table = [TYPE_WHITE_CORN]*18
102 corn_center_confs = [
110 if i == 0 and j == 2: return 0
111 if i == 0 and j == 4: return 1
112 if i == 0 and j == 6: return 2
113 if i == 2 and j == 3: return 3
114 if i == 2 and j == 5: return 4
115 if i == 2 and j == 7: return 5
116 if i == 4 and j == 4: return 6
117 if i == 4 and j == 6: return 7
118 if i == 6 and j == 5: return 8
119 if i == 6 and j == 7: return 9
120 if i == 8 and j == 4: return 10
121 if i == 8 and j == 6: return 11
122 if i == 10 and j == 3: return 12
123 if i == 10 and j == 5: return 13
124 if i == 10 and j == 7: return 14
125 if i == 12 and j == 2: return 15
126 if i == 12 and j == 4: return 16
127 if i == 12 and j == 6: return 17
131 sym = [15, 16, 17, 12, 13, 14, 10, 11, 8, 9, 6, 7, 3, 4, 5, 0, 1, 2]
134 def init_corn_table(conf_side, conf_center):
135 global corn_table, corn_side_confs, corn_center_confs
136 print "confs = %d, %d"%(conf_side, conf_center)
138 if i in corn_side_confs[conf_side]:
139 corn_table[i] = TYPE_BLACK_CORN
141 if corn_get_sym(i) in corn_side_confs[conf_side]:
142 corn_table[i] = TYPE_BLACK_CORN
144 if i in corn_center_confs[conf_center]:
145 corn_table[i] = TYPE_BLACK_CORN
147 if corn_get_sym(i) in corn_center_confs[conf_center]:
148 corn_table[i] = TYPE_BLACK_CORN
150 corn_table[i] = TYPE_WHITE_CORN
152 def init_waypoints():
153 global waypoints, corn_table
155 for i in range(WAYPOINTS_NBX):
156 for j in range(WAYPOINTS_NBY):
160 waypoints[i][j] = corn_table[c]
164 if (i & 1) == 0 and j > 3:
165 waypoints[i][j] = TYPE_BALL
167 if (i == 0 or i == WAYPOINTS_NBX-1) and j > 2:
168 waypoints[i][j] = TYPE_BALL
171 # too close of border
172 if (i & 1) == 1 and j == WAYPOINTS_NBY -1:
173 waypoints[i][j] = TYPE_OBSTACLE
176 if i >= 2 and i < WAYPOINTS_NBX - 2 and j < 2:
177 waypoints[i][j] = TYPE_OBSTACLE
180 if i == 0 or i == WAYPOINTS_NBX-1:
181 waypoints[i][j] = TYPE_DANGEROUS
183 if (i&1) == 0 and j == WAYPOINTS_NBY-1:
184 waypoints[i][j] = TYPE_DANGEROUS
187 waypoints[i][j] = TYPE_WAYPOINT
189 print i, waypoints[i]
193 def toggle_obj_disp():
197 if area_objects == []:
198 c = sphere(radius=5, color=(0., 0.,1.),
199 pos=(1238.-AREA_X/2, 1313.-AREA_Y/2, 5))
200 area_objects.append(c)
201 c = sphere(radius=5, color=(0., 0.,1.),
202 pos=(1364.-AREA_X/2, 1097.-AREA_Y/2, 5))
203 area_objects.append(c)
204 c = sphere(radius=5, color=(0., 0.,1.),
205 pos=(1453.-AREA_X/2, 1176.-AREA_Y/2, 5))
206 area_objects.append(c)
207 c = sphere(radius=5, color=(0., 0.,1.),
208 pos=(1109.-AREA_X/2, 1050.-AREA_Y/2, 5))
209 area_objects.append(c)
211 if area_objects == []:
219 y = OFFSET_CORN_Y + STEP_CORN_Y/2
223 if waypoints[i][j] == TYPE_WHITE_CORN:
224 c = cylinder(axis=(0,0,1), length=CORN_HEIGHT,
225 radius=25, color=(0.8,0.8,0.8),
226 pos=(x-AREA_X/2,y-AREA_Y/2,CORN_HEIGHT/2))
227 area_objects.append(c)
228 elif waypoints[i][j] == TYPE_BLACK_CORN:
229 c = cylinder(axis=(0,0,1), length=CORN_HEIGHT,
230 radius=25, color=(0.2,0.2,0.2),
231 pos=(x-AREA_X/2,y-AREA_Y/2,CORN_HEIGHT/2))
232 area_objects.append(c)
233 elif waypoints[i][j] == TYPE_BALL:
234 c = sphere(radius=50, color=(1., 0.,0.),
235 pos=(x-AREA_X/2,y-AREA_Y/2,50))
236 area_objects.append(c)
238 c = sphere(radius=5, color=(0., 0.,1.),
239 pos=(x-AREA_X/2,y-AREA_Y/2,5))
240 area_objects.append(c)
246 for o in area_objects:
261 global robot, last_pos, robot_trail, robot_trail_list
262 global save_pos, robot_x, robot_y, robot_a
265 tmp_x = robot_x - AREA_X/2
266 tmp_y = robot_y - AREA_Y/2
269 tmp_x = -robot_x + AREA_X/2
270 tmp_y = -robot_y + AREA_Y/2
273 robot.pos = (tmp_x, tmp_y, ROBOT_HEIGHT/2)
274 axis = (math.cos(tmp_a*math.pi/180),
275 math.sin(tmp_a*math.pi/180),
279 robot.size = (250, 320, ROBOT_HEIGHT)
281 robot_lspickle = 2 # XXX
282 lspickle.pos = (tmp_x + (robot_lspickle*60) * math.cos((tmp_a+90)*math.pi/180),
283 tmp_y + (robot_lspickle*60) * math.sin((tmp_a+90)*math.pi/180),
286 lspickle.size = (20, 320, 5)
288 robot_rspickle = 2 # XXX
289 rspickle.pos = (tmp_x + (robot_rspickle*60) * math.cos((tmp_a-90)*math.pi/180),
290 tmp_y + (robot_rspickle*60) * math.sin((tmp_a-90)*math.pi/180),
293 rspickle.size = (20, 320, 5)
296 save_pos.append((robot.pos.x, robot.pos.y, tmp_a))
298 pos = robot.pos.x, robot.pos.y, 0.3
300 robot_trail_list.append(pos)
302 robot_trail_l = len(robot_trail_list)
303 if robot_trail_l > max_trail:
304 robot_trail_list = robot_trail_list[robot_trail_l - max_trail:]
305 robot_trail.pos = robot_trail_list
311 f = open("/tmp/robot_save", "w")
313 f.write("%f %f %f\n"%(p[0], p[1], p[2]))
316 def silent_mkfifo(f):
322 #init_corn_table(random.randint(0,8), random.randint(0,3))
323 init_corn_table(0, 0)
324 waypoints = init_waypoints()
328 silent_mkfifo("/tmp/.robot_sim2dis")
329 silent_mkfifo("/tmp/.robot_dis2sim")
331 fr = open("/tmp/.robot_sim2dis", "r")
332 fw = open("/tmp/.robot_dis2sim", "w", 0)
339 m = re.match("pos=%s,%s,%s"%(INT,INT,INT), l)
341 robot_x = int(m.groups()[0])
342 robot_y = int(m.groups()[1])
343 robot_a = int(m.groups()[2])
348 m = re.match("ballboard=%s"%(INT), l)
350 print "ballboard: %d"%(int(m.groups()[0]))
354 m = re.match("cobboard=%s"%(INT), l)
356 mode = int(m.groups()[0])
357 if (mode & 0x01) == 0:
359 elif (mode & 0x02) == 0:
363 if (mode & 0x04) == 0:
365 elif (mode & 0x08) == 0:
370 if scene.kb.keys == 0:
373 k = scene.kb.getkey()
376 scene.center = x-10,y,z
378 scene.center = x+10,y,z
380 scene.center = x,y+10,z
382 scene.center = x,y-10,z
388 robot_trail_list = []