1 import math, sys, time, os, random
7 area = [ (0.0, 0.0, -0.2), (3000.0, 2100.0, 0.2) ]
8 areasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , area)
9 area_box = box(size=areasize, color=(0.0, 1.0, 0.0))
13 # all positions of robot every 5ms
16 robot = box(pos = (0, 0, 150),
18 color = (0.3, 0.3, 0.3) )
20 last_pos = robot.pos.x, robot.pos.y, robot.pos.z
21 hcenter_line = curve()
22 hcenter_line.pos = [(-AREA_X/2, 0., 0.3), (AREA_X/2, 0., 0.3)]
23 vcenter_line = curve()
24 vcenter_line.pos = [(0., -AREA_Y/2, 0.3), (0., AREA_Y/2, 0.3)]
26 yellowarea = [ (0.0, 0.0, -0.5), (500.0, 500.0, 0.5) ]
27 yellowareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , yellowarea)
28 yellowarea_box = box(pos=(-AREA_X/2+250,-AREA_Y/2+250,0), size=yellowareasize, color=(1.0, 1.0, 0.0))
30 bluearea = [ (0.0, 0.0, -0.5), (500.0, 500.0, 0.5) ]
31 blueareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , bluearea)
32 bluearea_box = box(pos=(AREA_X/2-250,-AREA_Y/2+250,0), size=blueareasize, color=(0.0, 0.0, 1.0))
34 greyarea = [ (0.0, 0.0, -0.5), (1520.0, 500.0, 0.5) ]
35 greyareasize = reduce(lambda x,y:tuple([abs(x[i])+abs(y[i]) for i in range(len(x))]) , greyarea)
36 greyarea_box = box(pos=(0,-AREA_Y/2+250,0), size=greyareasize, color=(0.3, 0.6, 0.3))
40 sq.pos = [(-sz, -sz, 0.3),
72 col = [TYPE_WAYPOINT] * WAYPOINTS_NBY
73 waypoints = [col[:] for i in range(WAYPOINTS_NBX)]
74 corn_table = [TYPE_WHITE_CORN]*18
95 if i == 0 and j == 2: return 0
96 if i == 0 and j == 4: return 1
97 if i == 0 and j == 6: return 2
98 if i == 2 and j == 3: return 3
99 if i == 2 and j == 5: return 4
100 if i == 2 and j == 7: return 5
101 if i == 4 and j == 4: return 6
102 if i == 4 and j == 6: return 7
103 if i == 6 and j == 5: return 8
104 if i == 6 and j == 7: return 9
105 if i == 8 and j == 4: return 10
106 if i == 8 and j == 6: return 11
107 if i == 10 and j == 3: return 12
108 if i == 10 and j == 5: return 13
109 if i == 10 and j == 7: return 14
110 if i == 12 and j == 2: return 15
111 if i == 12 and j == 4: return 16
112 if i == 12 and j == 6: return 17
116 sym = [15, 16, 17, 12, 13, 14, 10, 11, 8, 9, 6, 7, 3, 4, 5, 0, 1, 2]
119 def init_corn_table(conf_side, conf_center):
120 global corn_table, corn_side_confs, corn_center_confs
121 print "confs = %d, %d"%(conf_side, conf_center)
123 if i in corn_side_confs[conf_side]:
124 corn_table[i] = TYPE_BLACK_CORN
126 if corn_get_sym(i) in corn_side_confs[conf_side]:
127 corn_table[i] = TYPE_BLACK_CORN
129 if i in corn_center_confs[conf_center]:
130 corn_table[i] = TYPE_BLACK_CORN
132 if corn_get_sym(i) in corn_center_confs[conf_center]:
133 corn_table[i] = TYPE_BLACK_CORN
135 corn_table[i] = TYPE_WHITE_CORN
137 def init_waypoints():
138 global waypoints, corn_table
140 for i in range(WAYPOINTS_NBX):
141 for j in range(WAYPOINTS_NBY):
145 waypoints[i][j] = corn_table[c]
149 if (i & 1) == 0 and j > 3:
150 waypoints[i][j] = TYPE_BALL
152 if (i == 0 or i == WAYPOINTS_NBX-1) and j > 2:
153 waypoints[i][j] = TYPE_BALL
156 # too close of border
157 if (i & 1) == 1 and j == WAYPOINTS_NBY -1:
158 waypoints[i][j] = TYPE_OBSTACLE
161 if i >= 2 and i < WAYPOINTS_NBX - 2 and j < 2:
162 waypoints[i][j] = TYPE_OBSTACLE
165 if i == 0 or i == WAYPOINTS_NBX-1:
166 waypoints[i][j] = TYPE_DANGEROUS
168 if (i&1) == 0 and j == WAYPOINTS_NBY-1:
169 waypoints[i][j] = TYPE_DANGEROUS
172 waypoints[i][j] = TYPE_WAYPOINT
174 print i, waypoints[i]
178 def toggle_obj_disp():
181 if area_objects == []:
189 y = OFFSET_CORN_Y + STEP_CORN_Y/2
193 if waypoints[i][j] == TYPE_WHITE_CORN:
194 c = cylinder(axis=(0,0,1), length=150,
195 radius=25, color=(0.8,0.8,0.8),
196 pos=(x-AREA_X/2,y-AREA_Y/2,75))
197 area_objects.append(c)
198 elif waypoints[i][j] == TYPE_BLACK_CORN:
199 c = cylinder(axis=(0,0,1), length=150,
200 radius=25, color=(0.2,0.2,0.2),
201 pos=(x-AREA_X/2,y-AREA_Y/2,75))
202 area_objects.append(c)
203 elif waypoints[i][j] == TYPE_BALL:
204 c = sphere(radius=50, color=(1., 0.,0.),
205 pos=(x-AREA_X/2,y-AREA_Y/2,50))
206 area_objects.append(c)
208 c = sphere(radius=5, color=(0., 0.,1.),
209 pos=(x-AREA_X/2,y-AREA_Y/2,5))
210 area_objects.append(c)
216 for o in area_objects:
223 def set_robot(x, y, a):
224 global robot, last_pos, robot_trail, robot_trail_list
227 robot.pos = (x - AREA_X/2, y - AREA_Y/2, 150)
228 robot.axis = (math.cos(a*math.pi/180),
229 math.sin(a*math.pi/180),
231 robot.size = (250, 320, 350)
234 save_pos.append((robot.pos.x, robot.pos, a))
236 pos = robot.pos.x, robot.pos.y, 0.3
238 robot_trail_list.append(pos)
240 robot_trail_l = len(robot_trail_list)
241 if robot_trail_l > max_trail:
242 robot_trail_list = robot_trail_list[robot_trail_l - max_trail:]
243 robot_trail.pos = robot_trail_list
249 f = open("/tmp/robot_save", "w")
251 f.write("%f %f %f\n"%(p[0], p[1], p[2]))
254 def silent_mkfifo(f):
260 init_corn_table(random.randint(0,8), random.randint(0,3))
261 waypoints = init_waypoints()
265 silent_mkfifo("/tmp/.robot_sim2dis")
266 silent_mkfifo("/tmp/.robot_dis2sim")
268 fr = open("/tmp/.robot_sim2dis", "r")
269 fw = open("/tmp/.robot_dis2sim", "w", 0)
273 x,y,a = map(lambda x:int(x), l[:-1].split(" "))
278 if scene.kb.keys == 0:
281 k = scene.kb.getkey()
284 scene.center = x-10,y,z
286 scene.center = x+10,y,z
288 scene.center = x,y+10,z
290 scene.center = x,y-10,z
296 robot_trail_list = []