weak current limit on spickles
[aversive.git] / projects / microb2009 / tests / oa / graph.py
1 import sys, re, math
2 import numpy as np
3 import matplotlib
4 import matplotlib.path as mpath
5 import matplotlib.patches as mpatches
6 import matplotlib.pyplot as plt
7 from matplotlib.patches import Arrow, Circle, Wedge, Polygon
8 from matplotlib.collections import PatchCollection
9 import popen2, random
10
11 Path = mpath.Path
12
13
14 def build_poly(ptlist):
15     polydata = []
16     polydata.append((Path.MOVETO, (ptlist[0])))
17     for pt in ptlist[1:]:
18         polydata.append((Path.LINETO, (pt)))
19     polydata.append((Path.CLOSEPOLY, (ptlist[0])))
20     codes, verts = zip(*polydata)
21     poly = mpath.Path(verts, codes)
22     x, y = zip(*poly.vertices)
23     return x,y
24
25 def build_path(ptlist):
26     polydata = []
27     polydata.append((Path.MOVETO, (ptlist[0])))
28     for pt in ptlist[1:]:
29         polydata.append((Path.LINETO, (pt)))
30     codes, verts = zip(*polydata)
31     poly = mpath.Path(verts, codes)
32     x, y = zip(*poly.vertices)
33     return x,y
34
35 def graph(filename, stx, sty, sta, enx, eny, opx, opy):
36     cmd = "./main %d %d %d %d %d %d %d"%(stx, sty, sta, enx, eny, opx, opy)
37     o,i = popen2.popen2(cmd)
38     i.close()
39     s = o.read(1000000)
40     o.close()
41
42     open(filename + ".txt", "w").write(s)
43
44     if len(s) == 1000000:
45         gloupix()
46
47     fig = plt.figure()
48     ax = fig.add_subplot(111)
49
50     # area
51     x,y = build_poly([(0,0), (3000,0), (3000,2100), (0,2100)])
52     ax.plot(x, y, 'g-')
53     
54     x,y = build_poly([(250,250), (2750,250), (2750,1850), (250,1850)])
55     ax.plot(x, y, 'g--')
56     
57     # central disc
58     patches = [ Circle((1500, 1050), 150) ]
59     
60     poly = None
61     poly_wait_pts = 0
62     start = None
63     path = None
64     for l in s.split("\n"):
65         m = re.match("robot at: (-?\d+) (-?\d+) (-?\d+)", l)
66         if m:
67             x,y,a = (int(m.groups()[0]), int(m.groups()[1]), int(m.groups()[2]))
68             path = [ (x,y) ]
69             a_rad = (a * math.pi / 180.)
70             dx = 150 * math.cos(a_rad)
71             dy = 150 * math.sin(a_rad)
72             patches += [ Circle((x, y), 50) ]
73             patches += [ Arrow(x, y, dx, dy, 50) ]
74
75         m = re.match("oa_start_end_points\(\) \((-?\d+),(-?\d+)\) \((-?\d+),(-?\d+)\)", l)
76         if m:
77             dst_x,dst_y = (int(m.groups()[2]), int(m.groups()[3]))
78             patches += [ Circle((dst_x, dst_y), 50) ]
79
80         m = re.match("oa_new_poly\(size=(-?\d+)\)", l)
81         if m:
82             poly_wait_pts = int(m.groups()[0])
83             poly = []
84
85         m = re.match("oponent at: (-?\d+) (-?\d+)", l)
86         if m:
87             poly_wait_pts = 4
88             poly = []
89
90         m = re.match("oa_poly_set_point\(\) \((-?\d+),(-?\d+)\)", l)
91         if m:
92             poly.append((int(m.groups()[0]), int(m.groups()[1])))
93             poly_wait_pts -= 1
94             if poly_wait_pts == 0:
95                 x,y = build_poly(poly)
96                 ax.plot(x, y, 'r-')
97
98         m = re.match("GOTO (-?\d+),(-?\d+)", l)
99         if m:
100             path.append((int(m.groups()[0]), int(m.groups()[1])))
101
102         m = re.match("With avoidance .: x=(-?\d+) y=(-?\d+)", l)
103         if m:
104             path.append((int(m.groups()[0]), int(m.groups()[1])))
105
106     p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
107     ax.add_collection(p)
108
109     x,y = build_path(path)
110     ax.plot(x, y, 'bo-')
111
112     ax.grid()
113     ax.set_xlim(-100, 3100)
114     ax.set_ylim(-100, 2200)
115     #ax.set_title('spline paths')
116     #plt.show()
117     fig.savefig(filename)
118
119 # args are: startx, starty, starta, endx, endy, oppx, oppy
120 graph("normal1.png", 500, 1000, 180, 2000, 1500, 1500, 2000)
121 graph("normal2.png", 500, 300, 50, 2000, 1500, 2500, 2000)
122 graph("normal3.png", 500, 1000, 100, 1000, 1500, 2500, 1000)
123 graph("normal4.png", 500, 300, 0, 2500, 1700, 1000, 1000)
124 graph("normal5.png", 500, 1500, 0, 2500, 1700, 500, 1000)
125
126 # echappement
127 graph("escape1.png", 5200, 1000, 0, 2500, 1700, 2500, 1000)
128 graph("escape2.png", 500, 1300, 0, 2500, 1700, 500, 1000)
129 graph("escape3.png", 500, 1000, 180, 2000, 1500, 1500, 2000)
130
131 # pas d'echappement possible... petit carre
132 graph("small_square1.png", 500, 300, 0, 2500, 1700, 500, 500)
133 graph("small_square2.png", 500, 1000, 0, 2500, 1700, 500, 1000)
134 graph("small_square3.png", 590, 1000, 0, 2500, 1700, 500, 1000)
135 graph("small_square4.png", 560, 1000, 0, 2500, 1700, 300, 1000)
136 graph("small_square5.png", 550, 1000, 0, 2500, 1700, 250, 1000)
137 graph("small_square6.png", 5750, 1000, 0, 2500, 1700, 2750, 1000)
138
139 # impossible
140 graph("impossible1.png", 500, 300, 0, 2700, 1800, 2700, 1800)
141 graph("impossible2.png", 500, 300, 0, 2700, 1800, 2400, 1500)
142 graph("impossible3.png", 500, 300, 0, 1500, 1150, 2700, 1800 )
143
144 #test current
145 graph("current01.png", 1274,709, 58, 1312, 400, 2274, 1241 )
146
147
148 random.seed(0)
149 for i in range(100):
150     stx = random.randint(-50, 3050)
151     sty = random.randint(-50, 2050)
152     enx = random.randint(-50, 3050)
153     eny = random.randint(-50, 2050)
154     opx = random.randint(-50, 3050)
155     opy = random.randint(-50, 2050)
156     name = "random%d.png"%(i)
157     print (name, stx, sty, 0, enx, eny, opx, opy)
158     graph(name, stx, sty, 0, enx, eny, opx, opy)
159