X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=blobdiff_plain;f=projects%2Fmicrob2010%2Ftests%2Ftourel_beacon%2Fgraph.py;fp=projects%2Fmicrob2010%2Ftests%2Ftourel_beacon%2Fgraph.py;h=23193b7a4e57f7347d4b2d6df4ca04df153307c5;hp=0000000000000000000000000000000000000000;hb=80386d268a58da2be3eeca83f254c980a047b34f;hpb=bd8acd4f11e6720c96946a7537df5538c0a9a5d4 diff --git a/projects/microb2010/tests/tourel_beacon/graph.py b/projects/microb2010/tests/tourel_beacon/graph.py new file mode 100644 index 0000000..23193b7 --- /dev/null +++ b/projects/microb2010/tests/tourel_beacon/graph.py @@ -0,0 +1,229 @@ +import sys, re, math +import numpy as np +import matplotlib +import matplotlib.path as mpath +import matplotlib.patches as mpatches +import matplotlib.pyplot as plt +from matplotlib.patches import Arrow, Circle, Wedge, Polygon +from matplotlib.collections import PatchCollection +from numpy.random import randn +import pylab +import popen2, random + +Path = mpath.Path +FLOAT = "([-+]?[0-9]*\.?[0-9]+)" +INT = "([-+]?[0-9][0-9]*)" +RANDOM_ERROR = 0.3 # deg +beacons = [ (0.0, 1050.0), (3000.0, 0.0), (3000.0, 2100.0) ] + +def build_poly(ptlist): + polydata = [] + polydata.append((Path.MOVETO, (ptlist[0]))) + for pt in ptlist[1:]: + polydata.append((Path.LINETO, (pt))) + polydata.append((Path.CLOSEPOLY, (ptlist[0]))) + codes, verts = zip(*polydata) + poly = mpath.Path(verts, codes) + x, y = zip(*poly.vertices) + return x,y + +def build_path(ptlist): + polydata = [] + polydata.append((Path.MOVETO, (ptlist[0]))) + for pt in ptlist[1:]: + polydata.append((Path.LINETO, (pt))) + codes, verts = zip(*polydata) + poly = mpath.Path(verts, codes) + x, y = zip(*poly.vertices) + return x,y + +def get_angle(ref, b): + """get angle from robot point of view (ref) of beacon 'b'""" + a = math.atan2(b[1]-ref[1], b[0]-ref[0]) + ea = (math.pi/180.) * RANDOM_ERROR * random.random() + ea = random.choice([ea, -ea]) + return a + ea, ea + + alpha = math.atan2(a[1]-ref[1], a[0]-ref[0]) + beta = math.atan2(b[1]-ref[1], b[0]-ref[0]) + gamma = beta-alpha + if gamma < 0: + gamma = gamma + 2*math.pi + return gamma + error, error + +def dist(p1, p2): + return math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2) + +def graph(filename, real_x, real_y): + + real_pt = (real_x, real_y) + + # display beacons + patches = [] + for b in beacons: + patches += [ Circle((b[0], b[1]), 40, alpha=0.4) ] + + patches += [ Circle((real_x, real_y), 20, alpha=0.4, facecolor="red") ] + + # process angles from robot position + a0,ea0 = get_angle((real_x, real_y), beacons[0]) + a1,ea1 = get_angle((real_x, real_y), beacons[1]) + a2,ea2 = get_angle((real_x, real_y), beacons[2]) + text = "a0 = %2.2f (%+2.2f deg)\n"%(a0, ea0*(180./math.pi)) + text += "a1 = %2.2f (%+2.2f deg)\n"%(a1, ea1*(180./math.pi)) + text += "a2 = %2.2f (%+2.2f deg)\n"%(a2, ea2*(180./math.pi)) + + a01 = a1-a0 + if a01 < 0: + a01 += 2*math.pi + a12 = a2-a1 + if a12 < 0: + a12 += 2*math.pi + a20 = a0-a2 + if a20 < 0: + a20 += 2*math.pi + + cmd = "./main angle2pos %f %f %f"%(a01, a12, a20) + o,i = popen2.popen2(cmd) + i.close() + s = o.read(1000000) + o.close() + + open(filename + ".txt", "w").write(s) + + if len(s) == 1000000: + gloupix() + + fig = plt.figure() + ax = fig.add_subplot(111) + ax.set_title("Erreur de position en mm lorsqu'on ajoute une erreur de mesure\n" + "d'angle aleatoire comprise entre - %1.1f et %1.1f deg"%(RANDOM_ERROR, + RANDOM_ERROR)) + + # area + x,y = build_poly([(0,0), (3000,0), (3000,2100), (0,2100)]) + ax.plot(x, y, 'g-') + + for l in s.split("\n"): + m = re.match("circle: x=%s y=%s r=%s"%(FLOAT, FLOAT, FLOAT), l) + if m: + x,y,r = (float(m.groups()[0]), float(m.groups()[1]), float(m.groups()[2])) + print x,y,r + patches += [ Circle((x, y), r, facecolor="none") ] + m = re.match("p%s: x=%s y=%s"%(INT, FLOAT, FLOAT), l) + if m: + n,x,y = (float(m.groups()[0]), float(m.groups()[1]), float(m.groups()[2])) + if (n == 0): + patches += [ Circle((x, y), 20, alpha=0.4, facecolor="yellow") ] + result_pt = (x, y) + text += l + "\n" + + p = PatchCollection(patches, cmap=matplotlib.cm.jet, match_original=True) + + # text area, far from the point + l = [(800., 1800.), (800., 500.), (1500., 1800.), (1500., 500.), + (2200., 1800.), (2200., 500.)] + l.sort(cmp=lambda p1,p2: (dist(p1,real_pt) 2cm (pour un deplacement\n" + 'a %2.2f m/s vers %d deg et une periode tourelle = %d ms)'%(speed, angle_deg, period)) + +#do_random_test() +#do_graph_2d_simple_error() +do_graph_2d_move_error()