1 import math, sys, time, os, random, re
4 FLOAT = "([-+]?[0-9]*\.?[0-9]+)"
5 INT = "([-+]?[0-9][0-9]*)"
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))
22 # all positions of robot every 5ms
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))
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)]
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))
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))
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))
53 sq.pos = [(-sz, -sz, 0.3),
90 col = [TYPE_WAYPOINT] * WAYPOINTS_NBY
91 waypoints = [col[:] for i in range(WAYPOINTS_NBX)]
92 corn_table = [TYPE_WHITE_CORN]*18
105 corn_center_confs = [
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
134 sym = [15, 16, 17, 12, 13, 14, 10, 11, 8, 9, 6, 7, 3, 4, 5, 0, 1, 2]
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)
141 if i in corn_side_confs[conf_side]:
142 corn_table[i] = TYPE_BLACK_CORN
144 if corn_get_sym(i) in corn_side_confs[conf_side]:
145 corn_table[i] = TYPE_BLACK_CORN
147 if i in corn_center_confs[conf_center]:
148 corn_table[i] = TYPE_BLACK_CORN
150 if corn_get_sym(i) in corn_center_confs[conf_center]:
151 corn_table[i] = TYPE_BLACK_CORN
153 corn_table[i] = TYPE_WHITE_CORN
155 def init_waypoints():
156 global waypoints, corn_table
158 for i in range(WAYPOINTS_NBX):
159 for j in range(WAYPOINTS_NBY):
163 waypoints[i][j] = corn_table[c]
167 if (i & 1) == 0 and j > 3:
168 waypoints[i][j] = TYPE_BALL
170 if (i == 0 or i == WAYPOINTS_NBX-1) and j > 2:
171 waypoints[i][j] = TYPE_BALL
174 # too close of border
175 if (i & 1) == 1 and j == WAYPOINTS_NBY -1:
176 waypoints[i][j] = TYPE_OBSTACLE
179 if i >= 2 and i < WAYPOINTS_NBX - 2 and j < 2:
180 waypoints[i][j] = TYPE_OBSTACLE
183 if i == 0 or i == WAYPOINTS_NBX-1:
184 waypoints[i][j] = TYPE_DANGEROUS
186 if (i&1) == 0 and j == WAYPOINTS_NBY-1:
187 waypoints[i][j] = TYPE_DANGEROUS
190 waypoints[i][j] = TYPE_WAYPOINT
192 print i, waypoints[i]
196 def toggle_obj_disp():
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)
214 if area_objects == []:
222 y = OFFSET_CORN_Y + STEP_CORN_Y/2
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)
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)
249 for o in area_objects:
264 global robot, last_pos, robot_trail, robot_trail_list
265 global save_pos, robot_x, robot_y, robot_a
268 tmp_x = robot_x - AREA_X/2
269 tmp_y = robot_y - AREA_Y/2
272 tmp_x = -robot_x + AREA_X/2
273 tmp_y = -robot_y + AREA_Y/2
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),
282 robot.size = (ROBOT_LENGTH, ROBOT_WIDTH, ROBOT_HEIGHT)
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),
289 lspickle.size = (20, ROBOT_WIDTH, 5)
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),
296 rspickle.size = (20, ROBOT_WIDTH, 5)
299 save_pos.append((robot.pos.x, robot.pos.y, tmp_a))
301 pos = robot.pos.x, robot.pos.y, 0.3
303 robot_trail_list.append(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
314 f = open("/tmp/robot_save", "w")
316 f.write("%f %f %f\n"%(p[0], p[1], p[2]))
319 def silent_mkfifo(f):
325 #init_corn_table(random.randint(0,8), random.randint(0,3))
326 init_corn_table(0, 0)
327 waypoints = init_waypoints()
331 silent_mkfifo("/tmp/.robot_sim2dis")
332 silent_mkfifo("/tmp/.robot_dis2sim")
334 fr = open("/tmp/.robot_sim2dis", "r")
335 fw = open("/tmp/.robot_dis2sim", "w", 0)
342 m = re.match("pos=%s,%s,%s"%(INT,INT,INT), l)
344 robot_x = int(m.groups()[0])
345 robot_y = int(m.groups()[1])
346 robot_a = int(m.groups()[2])
351 m = re.match("ballboard=%s"%(INT), l)
353 print "ballboard: %d"%(int(m.groups()[0]))
357 m = re.match("cobboard=%s,%s"%(INT,INT), l)
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])
363 if (flags & 0x01) == 0:
368 if (flags & 0x01) == 0:
373 if scene.kb.keys == 0:
376 k = scene.kb.getkey()
379 scene.center = x-10,y,z
381 scene.center = x+10,y,z
383 scene.center = x,y+10,z
385 scene.center = x,y-10,z
391 robot_trail_list = []