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),
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)]
28 sq.pos = [(-sz, -sz, 0.3),
59 col = [TYPE_WAYPOINT] * WAYPOINTS_NBY
60 waypoints = [col[:] for i in range(WAYPOINTS_NBX)]
61 corn_table = [TYPE_WHITE_CORN]*18
82 if i == 0 and j == 2: return 0
83 if i == 0 and j == 4: return 1
84 if i == 0 and j == 6: return 2
85 if i == 2 and j == 3: return 3
86 if i == 2 and j == 5: return 4
87 if i == 2 and j == 7: return 5
88 if i == 4 and j == 4: return 6
89 if i == 4 and j == 6: return 7
90 if i == 6 and j == 5: return 8
91 if i == 6 and j == 7: return 9
92 if i == 8 and j == 4: return 10
93 if i == 8 and j == 6: return 11
94 if i == 10 and j == 3: return 12
95 if i == 10 and j == 5: return 13
96 if i == 10 and j == 7: return 14
97 if i == 12 and j == 2: return 15
98 if i == 12 and j == 4: return 16
99 if i == 12 and j == 6: return 17
103 sym = [15, 16, 17, 12, 13, 14, 10, 11, 8, 9, 6, 7, 3, 4, 5, 0, 1, 2]
106 def init_corn_table(conf_side, conf_center):
107 global corn_table, corn_side_confs, corn_center_confs
108 print "confs = %d, %d"%(conf_side, conf_center)
110 if i in corn_side_confs[conf_side]:
111 corn_table[i] = TYPE_BLACK_CORN
113 if corn_get_sym(i) in corn_side_confs[conf_side]:
114 corn_table[i] = TYPE_BLACK_CORN
116 if i in corn_center_confs[conf_center]:
117 corn_table[i] = TYPE_BLACK_CORN
119 if corn_get_sym(i) in corn_center_confs[conf_center]:
120 corn_table[i] = TYPE_BLACK_CORN
122 corn_table[i] = TYPE_WHITE_CORN
124 def init_waypoints():
125 global waypoints, corn_table
127 for i in range(WAYPOINTS_NBX):
128 for j in range(WAYPOINTS_NBY):
132 waypoints[i][j] = corn_table[c]
134 # too close of border
135 if (i & 1) == 1 and j == WAYPOINTS_NBY -1:
136 waypoints[i][j] = TYPE_OBSTACLE
139 if i >= 2 and i < WAYPOINTS_NBX - 2 and j < 2:
140 waypoints[i][j] = TYPE_OBSTACLE
143 if i == 0 or i == WAYPOINTS_NBX-1:
144 waypoints[i][j] = TYPE_DANGEROUS
146 if (i&1) == 0 and j == WAYPOINTS_NBY-1:
147 waypoints[i][j] = TYPE_DANGEROUS
150 waypoints[i][j] = TYPE_WAYPOINT
152 print i, waypoints[i]
156 def toggle_obj_disp():
159 if area_objects == []:
167 y = OFFSET_CORN_Y + STEP_CORN_Y/2
171 if waypoints[i][j] == TYPE_WHITE_CORN:
172 c = cylinder(axis=(0,0,1), length=150,
173 radius=25, color=(0.8,0.8,0.8),
174 pos=(x-AREA_X/2,y-AREA_Y/2,75))
175 area_objects.append(c)
176 elif waypoints[i][j] == TYPE_BLACK_CORN:
177 c = cylinder(axis=(0,0,1), length=150,
178 radius=25, color=(0.2,0.2,0.2),
179 pos=(x-AREA_X/2,y-AREA_Y/2,75))
180 area_objects.append(c)
186 for o in area_objects:
193 def set_robot(x, y, a):
194 global robot, last_pos, robot_trail, robot_trail_list
197 robot.pos = (x - AREA_X/2, y - AREA_Y/2, 150)
198 robot.axis = (math.cos(a*math.pi/180),
199 math.sin(a*math.pi/180),
201 robot.size = (250, 320, 350)
204 save_pos.append((robot.pos.x, robot.pos, a))
206 pos = robot.pos.x, robot.pos.y, 0.3
208 robot_trail_list.append(pos)
210 robot_trail_l = len(robot_trail_list)
211 if robot_trail_l > max_trail:
212 robot_trail_list = robot_trail_list[robot_trail_l - max_trail:]
213 robot_trail.pos = robot_trail_list
219 f = open("/tmp/robot_save", "w")
221 f.write("%f %f %f\n"%(p[0], p[1], p[2]))
224 def silent_mkfifo(f):
230 init_corn_table(random.randint(0,8), random.randint(0,3))
231 waypoints = init_waypoints()
235 silent_mkfifo("/tmp/.robot_sim2dis")
236 silent_mkfifo("/tmp/.robot_dis2sim")
238 fr = open("/tmp/.robot_sim2dis", "r")
239 fw = open("/tmp/.robot_dis2sim", "w", 0)
243 x,y,a = map(lambda x:int(x), l[:-1].split(" "))
248 if scene.kb.keys == 0:
251 k = scene.kb.getkey()
254 scene.center = x-10,y,z
256 scene.center = x+10,y,z
258 scene.center = x,y+10,z
260 scene.center = x,y-10,z
266 robot_trail_list = []