summaryrefslogtreecommitdiff
path: root/digital
diff options
context:
space:
mode:
authorNicolas Schodet2008-07-03 00:15:11 +0200
committerNicolas Schodet2008-07-03 00:15:11 +0200
commit27b65b8927800d150d1e7c50856704a227bdeb3c (patch)
tree7a5e82c1aee59c84ace3a6612bec9cdeb4c5ac40 /digital
parentc908ba7058ae9bc8f7a9446e17e3482d125234de (diff)
* digital/avr/modules/path/test:
- added python test GUI.
Diffstat (limited to 'digital')
-rw-r--r--digital/avr/modules/path/test/test_path.c55
-rw-r--r--digital/avr/modules/path/test/test_path.py125
2 files changed, 175 insertions, 5 deletions
diff --git a/digital/avr/modules/path/test/test_path.c b/digital/avr/modules/path/test/test_path.c
index 8b88e5d7..beb0a6a6 100644
--- a/digital/avr/modules/path/test/test_path.c
+++ b/digital/avr/modules/path/test/test_path.c
@@ -26,14 +26,59 @@
#include "modules/path/path.h"
#include <stdio.h>
+#include <stdlib.h>
+
+void
+syntax (void)
+{
+ fprintf (stderr,
+ "test_path borders source destination [obstacles (0-%d)]\n"
+ " borders: xmin,ymin,xmax,ymax\n"
+ " source, destination: x,y\n"
+ " obstacles: x,y,r\n",
+ AC_PATH_OBSTACLES_NB);
+ exit (1);
+}
+
+/** Read a comma separated list of N integers into TAB from S. Exit on
+ * error. */
+void
+read_tab (const char *s, int *tab, int n)
+{
+ assert (n > 0);
+ while (n)
+ {
+ char *sp;
+ int v;
+ v = strtol (s, &sp, 10);
+ if (sp == s)
+ syntax ();
+ if ((n > 1 && *sp != ',')
+ || (n == 1 && *sp != '\0'))
+ syntax ();
+ s = sp + 1;
+ *tab++ = v;
+ n--;
+ }
+}
int
-main (void)
+main (int argc, char **argv)
{
- path_init (0, 0, 3000, 2100);
- path_endpoints (300, 1000, 1200, 1000);
- path_obstacle (0, 600, 930, 100, 1);
- path_obstacle (1, 900, 1070, 100, 1);
+ if (argc < 4 || argc > 4 + AC_PATH_OBSTACLES_NB)
+ syntax ();
+ int tab[4];
+ read_tab (argv[1], tab, 4);
+ path_init (tab[0], tab[1], tab[2], tab[3]);
+ read_tab (argv[2], tab, 2);
+ read_tab (argv[3], tab + 2, 2);
+ path_endpoints (tab[0], tab[1], tab[2], tab[3]);
+ int i;
+ for (i = 0; i + 4 < argc; i++)
+ {
+ read_tab (argv[4 + i], tab, 3);
+ path_obstacle (i, tab[0], tab[1], tab[2], 1);
+ }
path_update ();
path_print_graph ();
uint16_t x, y;
diff --git a/digital/avr/modules/path/test/test_path.py b/digital/avr/modules/path/test/test_path.py
new file mode 100644
index 00000000..a510a44d
--- /dev/null
+++ b/digital/avr/modules/path/test/test_path.py
@@ -0,0 +1,125 @@
+# avr.path - Path finding module. {{{
+#
+# Copyright (C) 2008 Nicolas Schodet
+#
+# APBTeam:
+# Web: http://apbteam.org/
+# Email: team AT apbteam DOT org
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# }}}
+"""Graphic interface for test_path."""
+import sys
+sys.path.append ('../../../../../host')
+from Tkinter import *
+from inter.drawable import *
+from subprocess import Popen, PIPE
+import re
+
+class Obstacle:
+ def __init__ (self, pos, radius):
+ self.pos = pos
+ self.radius = radius
+
+class Area (Drawable):
+
+ def __init__ (self, onto, border_min, border_max):
+ Drawable.__init__ (self, onto)
+ self.border_min = border_min
+ self.border_max = border_max
+ self.border = None
+ self.src = None
+ self.dst = None
+ self.obstacles = [ ]
+ self.path = [ ]
+
+ def draw (self):
+ self.reset ()
+ self.draw_rectangle (self.border_min, self.border_max, fill = 'white')
+ for o in self.obstacles:
+ self.draw_circle (o.pos, o.radius, fill = 'gray25')
+ self.draw_circle (self.src, 10, fill = 'green')
+ self.draw_circle (self.dst, 10, fill = 'red')
+ if len (self.path) > 1:
+ fmt = dict (fill = 'blue', arrow = LAST)
+ self.draw_line (*self.path, **fmt)
+
+ def test (self):
+ self.src = (300, 750)
+ self.dst = (1200, 750)
+ self.obstacles.append (Obstacle ((600, 680), 100))
+ self.obstacles.append (Obstacle ((900, 820), 100))
+
+ def update (self):
+ args = [ [ self.border_min[0], self.border_min[1], self.border_max[0],
+ self.border_max[1] ], self.src, self.dst ]
+ for o in self.obstacles:
+ args.append ([o.pos[0], o.pos[1], o.radius])
+ args = [ ','.join (str (ai) for ai in a) for a in args ]
+ args[0:0] = [ './test_path.host' ]
+ p = Popen (args, stdout = PIPE)
+ output = p.communicate ()[0]
+ del p
+ output = output.split ('\n')
+ r = re.compile ('^// (\d+), (\d+)$')
+ self.path = [ ]
+ for line in output:
+ m = r.match (line)
+ if m is not None:
+ self.path.append (tuple (int (s) for s in m.groups ()))
+
+class AreaView (DrawableCanvas):
+
+ def __init__ (self, border_min, border_max, master = None):
+ self.border_min = border_min
+ self.border_max = border_max
+ width = border_max[0] - border_min[0]
+ height = border_max[1] - border_min[0]
+ DrawableCanvas.__init__ (self, width * 1.1, height * 1.1, -width / 2,
+ -height / 2,
+ master, borderwidth = 1, relief = 'sunken',
+ background = 'white')
+ self.area = Area (self, border_min, border_max)
+
+ def draw (self):
+ self.area.draw ()
+
+class TestPath (Frame):
+
+ def __init__ (self, border_min, border_max, master = None):
+ Frame.__init__ (self, master)
+ self.pack (expand = 1, fill = 'both')
+ self.createWidgets (border_min, border_max)
+
+ def createWidgets (self, border_min, border_max):
+ self.rightFrame = Frame (self)
+ self.rightFrame.pack (side = 'right', fill = 'y')
+ self.quitButton = Button (self.rightFrame, text = 'Quit', command = self.quit)
+ self.quitButton.pack (side = 'top', fill = 'x')
+ self.updateButton = Button (self.rightFrame, text = 'Update', command
+ = self.update)
+ self.updateButton.pack (side ='top', fill = 'x')
+ self.areaview = AreaView (border_min, border_max, self)
+ self.areaview.pack (expand = True, fill = 'both')
+
+ def update (self):
+ self.areaview.area.update ()
+ self.areaview.area.draw ()
+
+if __name__ == '__main__':
+ app = TestPath ((0, 0), (1500, 1500))
+ app.areaview.area.test ()
+ app.mainloop ()