summaryrefslogtreecommitdiff
path: root/digital/avr/modules/math/geometry/test
diff options
context:
space:
mode:
authorNicolas Schodet2010-04-07 23:51:28 +0200
committerNicolas Schodet2010-04-07 23:51:28 +0200
commitbed724fe33b2dc903b89f8f8517738370c751cd1 (patch)
treee4b12a550040d37c09aa820e0d386d61623bb26f /digital/avr/modules/math/geometry/test
parent36e5745b7b72749d95f71689478b06cc6b76245d (diff)
digital/avr/modules/math/geometry: add new module
Diffstat (limited to 'digital/avr/modules/math/geometry/test')
-rw-r--r--digital/avr/modules/math/geometry/test/Makefile13
-rw-r--r--digital/avr/modules/math/geometry/test/avrconfig.h76
-rw-r--r--digital/avr/modules/math/geometry/test/test_geometry.c151
3 files changed, 240 insertions, 0 deletions
diff --git a/digital/avr/modules/math/geometry/test/Makefile b/digital/avr/modules/math/geometry/test/Makefile
new file mode 100644
index 00000000..5d12dc0a
--- /dev/null
+++ b/digital/avr/modules/math/geometry/test/Makefile
@@ -0,0 +1,13 @@
+BASE = ../../../..
+HOST_PROGS = test_geometry
+SIMU_PROGS = test_geometry
+test_geometry_SOURCES = test_geometry.c
+MODULES = utils uart math/fixed math/geometry
+CONFIGFILE = avrconfig.h
+# atmega8, atmega8535, atmega128...
+AVR_MCU = atmega128
+# -O2 : speed
+# -Os : size
+OPTIMIZE = -O2
+
+include $(BASE)/make/Makefile.gen
diff --git a/digital/avr/modules/math/geometry/test/avrconfig.h b/digital/avr/modules/math/geometry/test/avrconfig.h
new file mode 100644
index 00000000..608e38a9
--- /dev/null
+++ b/digital/avr/modules/math/geometry/test/avrconfig.h
@@ -0,0 +1,76 @@
+#ifndef avrconfig_h
+#define avrconfig_h
+/* avrconfig.h */
+/* avr.math.geometry - Geometry math module. {{{
+ *
+ * Copyright (C) 2010 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.
+ *
+ * }}} */
+
+/* global */
+/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800,
+ * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */
+#define AC_FREQ 14745600
+
+/* uart - UART module. */
+/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */
+#define AC_UART0_PORT 0
+/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800,
+ * 115200, 230400, 250000, 500000, 1000000. */
+#define AC_UART0_BAUDRATE 115200
+/** Send mode:
+ * - POLLING: no interrupts.
+ * - RING: interrupts, ring buffer. */
+#define AC_UART0_SEND_MODE RING
+/** Recv mode, same as send mode. */
+#define AC_UART0_RECV_MODE RING
+/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */
+#define AC_UART0_CHAR_SIZE 8
+/** Parity : ODD, EVEN, NONE. */
+#define AC_UART0_PARITY EVEN
+/** Stop bits : 1, 2. */
+#define AC_UART0_STOP_BITS 1
+/** Send buffer size, should be power of 2 for RING mode. */
+#define AC_UART0_SEND_BUFFER_SIZE 32
+/** Recv buffer size, should be power of 2 for RING mode. */
+#define AC_UART0_RECV_BUFFER_SIZE 32
+/** If the send buffer is full when putc:
+ * - DROP: drop the new byte.
+ * - WAIT: wait until there is room in the send buffer. */
+#define AC_UART0_SEND_BUFFER_FULL WAIT
+/** In HOST compilation:
+ * - STDIO: use stdin/out.
+ * - PTS: use pseudo terminal. */
+#define AC_UART0_HOST_DRIVER STDIO
+/** Same thing for secondary port. */
+#define AC_UART1_PORT -1
+#define AC_UART1_BAUDRATE 115200
+#define AC_UART1_SEND_MODE RING
+#define AC_UART1_RECV_MODE RING
+#define AC_UART1_CHAR_SIZE 8
+#define AC_UART1_PARITY EVEN
+#define AC_UART1_STOP_BITS 1
+#define AC_UART1_SEND_BUFFER_SIZE 32
+#define AC_UART1_RECV_BUFFER_SIZE 32
+#define AC_UART1_SEND_BUFFER_FULL WAIT
+#define AC_UART1_HOST_DRIVER STDIO
+
+#endif /* avrconfig_h */
diff --git a/digital/avr/modules/math/geometry/test/test_geometry.c b/digital/avr/modules/math/geometry/test/test_geometry.c
new file mode 100644
index 00000000..59d1f854
--- /dev/null
+++ b/digital/avr/modules/math/geometry/test/test_geometry.c
@@ -0,0 +1,151 @@
+/* test_geometry.c */
+/* avr.math.geometry - Geometry math module. {{{
+ *
+ * Copyright (C) 2010 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.
+ *
+ * }}} */
+#include "common.h"
+#include "modules/math/geometry/geometry.h"
+#include "modules/math/geometry/vect.h"
+
+#include "modules/utils/utils.h"
+#include "modules/uart/uart.h"
+
+#ifdef HOST
+#define test assert
+#else
+#define test(p) \
+ do { \
+ const char *pp; \
+ for (pp = #p; *pp; pp++) \
+ uart0_putc (*pp); \
+ uart0_putc ('\n'); \
+ if (!(p)) while (1); \
+ } while (0);
+#endif
+
+void
+test_geometry (void)
+{
+ test (G_ANGLE_UF016_DEG (0) == 0);
+ test (G_ANGLE_UF016_DEG (45) == 0x2000);
+ test (G_ANGLE_UF016_DEG (90) == 0x4000);
+ test (G_ANGLE_UF016_DEG (135) == 0x6000);
+ test (G_ANGLE_UF016_DEG (180) == 0x8000);
+ test (G_ANGLE_UF016_DEG (225) == 0xa000);
+ test (G_ANGLE_UF016_DEG (270) == 0xc000);
+ test (G_ANGLE_UF016_DEG (315) == 0xe000);
+ test (G_ANGLE_UF016_DEG (360) == 0x0000);
+ test (G_ANGLE_UF016_DEG (-45) == 0xe000);
+ test (G_ANGLE_UF016_DEG (-90) == 0xc000);
+ test (G_ANGLE_UF016_DEG (-135) == 0xa000);
+ test (G_ANGLE_UF016_DEG (-180) == 0x8000);
+ test (G_ANGLE_UF016_DEG (-225) == 0x6000);
+ test (G_ANGLE_UF016_DEG (-270) == 0x4000);
+ test (G_ANGLE_UF016_DEG (-315) == 0x2000);
+ test (G_ANGLE_UF016_DEG (-360) == 0x0000);
+ test (G_ANGLE_UF016_RAD (0) == 0);
+ test (G_ANGLE_UF016_RAD (M_PI / 4) == 0x2000);
+ test (G_ANGLE_UF016_RAD (M_PI / 2) == 0x4000);
+ test (G_ANGLE_UF016_RAD (M_PI) == 0x8000);
+ test (G_ANGLE_UF016_RAD (3 * M_PI / 2) == 0xc000);
+ test (G_ANGLE_UF016_RAD (2 * M_PI) == 0x0000);
+ test (G_ANGLE_UF016_RAD (-M_PI / 4) == 0xe000);
+ test (G_ANGLE_UF016_RAD (-M_PI / 2) == 0xc000);
+ test (G_ANGLE_UF016_RAD (-M_PI) == 0x8000);
+ test (G_ANGLE_UF016_RAD (-3 * M_PI / 2) == 0x4000);
+ test (G_ANGLE_UF016_RAD (-2 * M_PI) == 0x0000);
+}
+
+void
+test_vect (void)
+{
+ int i;
+ int16_t n;
+ vect_t a, b;
+ /* vect_*. */
+ a.x = 50; a.y = 0;
+ b.x = 500; b.y = 500;
+ vect_scale_f824 (&a, 0x2000000);
+ vect_scale_f824 (&b, -0x2000000);
+ test (a.x == 100 && a.y == 0);
+ test (b.x == -1000 && b.y == -1000);
+ for (i = 0; i < 6; i++)
+ vect_rotate_uf016 (&a, G_ANGLE_UF016_DEG (30));
+ /* There is inevitable rounding errors. */
+ test (a.x >= -101 && a.x <= -99 && a.y >= -1 && a.y <= 1);
+ for (i = 0; i < 4; i++)
+ vect_rotate_uf016 (&b, G_ANGLE_UF016_DEG (45));
+ test (b.x == 1000 && b.y == 1000);
+ a.x = 2; a.y = 4;
+ b.x = 8; b.y = 16;
+ vect_translate (&a, &b);
+ test (a.x == 10 && a.y == 20);
+ vect_sub (&a, &b);
+ test (a.x == 2 && a.y == 4);
+ /* vect_norm. */
+ a.x = 100; a.y = 0;
+ n = vect_norm (&a);
+ test (n == 100);
+ a.y = 100;
+ n = vect_norm (&a);
+ test (n == 141);
+ a.x = 0;
+ n = vect_norm (&a);
+ test (n == 100);
+ /* vect_from_polar_uf016. */
+ vect_from_polar_uf016 (&a, 141, G_ANGLE_UF016_DEG (0));
+ test (a.x == 141 && a.y == 0);
+ vect_from_polar_uf016 (&a, 141, G_ANGLE_UF016_DEG (45));
+ test (a.x == 100 && a.y == 100);
+ vect_from_polar_uf016 (&a, 141, G_ANGLE_UF016_DEG (90));
+ test (a.x == 0 && a.y == 141);
+ vect_from_polar_uf016 (&a, 141, G_ANGLE_UF016_DEG (180));
+ test (a.x == -141 && a.y == 0);
+ /* vect_array_*. */
+ vect_t c[4] =
+ {
+ { 282, 0 },
+ { 200, 200 },
+ { 0, 282 },
+ { -200, 200 },
+ };
+ vect_array_rotate_uf016 (c, UTILS_COUNT (c), G_ANGLE_UF016_DEG (135));
+ vect_array_scale_f824 (c, UTILS_COUNT (c), 0x400000);
+ a.x = 60; a.y = 20;
+ vect_array_translate (c, UTILS_COUNT (c), &a);
+ test (c[0].x == -50 + 60 && c[0].y == 50 + 20);
+ test (c[1].x == -70 + 60 && c[1].y == 0 + 20);
+ test (c[2].x == -50 + 60 && c[2].y == -50 + 20);
+ test (c[3].x == 0 + 60 && c[3].y == -70 + 20);
+}
+
+int
+main (void)
+{
+ uart0_init ();
+ test_geometry ();
+ test_vect ();
+ uart0_putc ('o');
+ uart0_putc ('k');
+ uart0_putc ('\n');
+ return 0;
+}