From bed724fe33b2dc903b89f8f8517738370c751cd1 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 7 Apr 2010 23:51:28 +0200 Subject: digital/avr/modules/math/geometry: add new module --- digital/avr/modules/math/geometry/Makefile.module | 1 + digital/avr/modules/math/geometry/README | 26 ++++ digital/avr/modules/math/geometry/geometry.h | 51 +++++++ digital/avr/modules/math/geometry/test/Makefile | 13 ++ digital/avr/modules/math/geometry/test/avrconfig.h | 76 +++++++++++ .../avr/modules/math/geometry/test/test_geometry.c | 151 +++++++++++++++++++++ digital/avr/modules/math/geometry/vect.c | 114 ++++++++++++++++ digital/avr/modules/math/geometry/vect.h | 107 +++++++++++++++ 8 files changed, 539 insertions(+) create mode 100644 digital/avr/modules/math/geometry/Makefile.module create mode 100644 digital/avr/modules/math/geometry/README create mode 100644 digital/avr/modules/math/geometry/geometry.h create mode 100644 digital/avr/modules/math/geometry/test/Makefile create mode 100644 digital/avr/modules/math/geometry/test/avrconfig.h create mode 100644 digital/avr/modules/math/geometry/test/test_geometry.c create mode 100644 digital/avr/modules/math/geometry/vect.c create mode 100644 digital/avr/modules/math/geometry/vect.h (limited to 'digital') diff --git a/digital/avr/modules/math/geometry/Makefile.module b/digital/avr/modules/math/geometry/Makefile.module new file mode 100644 index 00000000..945e0937 --- /dev/null +++ b/digital/avr/modules/math/geometry/Makefile.module @@ -0,0 +1 @@ +math_geometry_SOURCES = vect.c diff --git a/digital/avr/modules/math/geometry/README b/digital/avr/modules/math/geometry/README new file mode 100644 index 00000000..76dc9956 --- /dev/null +++ b/digital/avr/modules/math/geometry/README @@ -0,0 +1,26 @@ +avr.math.geometry - Geometry math module. + +Provides geometry utilities. See modules README for more details about AVR +modules. + + + +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. diff --git a/digital/avr/modules/math/geometry/geometry.h b/digital/avr/modules/math/geometry/geometry.h new file mode 100644 index 00000000..6888daca --- /dev/null +++ b/digital/avr/modules/math/geometry/geometry.h @@ -0,0 +1,51 @@ +#ifndef geometry_h +#define geometry_h +/* geometry.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. + * + * }}} */ +#include "modules/math/math.h" + +/** Compute an angle from radians. This is supposed to be used with + * constants. */ +#define G_ANGLE_UF016_RAD(a) \ + ((uint16_t) /* Final type is uint16_t, we want to drop MSB. */ \ + (int32_t) /* Do no want funny compiler trick when converting from \ + double to integer (overflow detection). */ \ + ((a) >= 0 /* Need rounding. */ \ + ? ((a) * 0x8000l + M_PI / 2) / M_PI \ + : ((a) * 0x8000l - M_PI / 2) / M_PI \ + )) + +/** Compute an angle from degrees. This is supposed to be used with + * constants. */ +#define G_ANGLE_UF016_DEG(a) \ + ((uint16_t) /* Final type is uint16_t, we want to drop MSB. */ \ + (int32_t) /* Do no want funny compiler trick when converting from \ + double to integer (overflow detection). */ \ + ((a) >= 0 /* Need rounding. */ \ + ? ((a) * 0x10000l + 180) / 360 \ + : ((a) * 0x10000l - 180) / 360 \ + )) + +#endif /* geometry_h */ 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; +} diff --git a/digital/avr/modules/math/geometry/vect.c b/digital/avr/modules/math/geometry/vect.c new file mode 100644 index 00000000..57b5a4d4 --- /dev/null +++ b/digital/avr/modules/math/geometry/vect.c @@ -0,0 +1,114 @@ +/* vect.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 "vect.h" + +#include "modules/math/fixed/fixed.h" + +void +vect_scale_f824 (vect_t *v, int32_t s_f824) +{ + assert (v); + v->x = fixed_mul_f824 (v->x, s_f824); + v->y = fixed_mul_f824 (v->y, s_f824); +} + +void +vect_rotate_uf016 (vect_t *v, uint16_t a_uf016) +{ + assert (v); + int32_t c, s; + uint32_t a_uf824 = ((uint32_t) a_uf016) << 8; + c = fixed_cos_f824 (a_uf824); + s = fixed_sin_f824 (a_uf824); + int16_t tx = v->x, ty = v->y; + v->x = fixed_mul_f824 (tx, c) - fixed_mul_f824 (ty, s); + v->y = fixed_mul_f824 (tx, s) + fixed_mul_f824 (ty, c); +} + +void +vect_translate (vect_t *v, const vect_t *t) +{ + assert (v); + assert (t); + v->x = v->x + t->x; + v->y = v->y + t->y; +} + +void +vect_sub (vect_t *v, const vect_t *n) +{ + assert (v); + assert (n); + v->x = v->x - n->x; + v->y = v->y - n->y; +} + +vect_value_t +vect_norm (const vect_t *v) +{ + assert (v); + int16_t tx = v->x, ty = v->y; + return fixed_sqrt_ui32 (tx * tx + ty * ty); +} + +void +vect_from_polar_uf016 (vect_t *v, int16_t l, uint16_t a_uf016) +{ + assert (v); + uint32_t a_uf824 = ((uint32_t) a_uf016) << 8; + v->x = fixed_mul_f824 (l, fixed_cos_f824 (a_uf824)); + v->y = fixed_mul_f824 (l, fixed_sin_f824 (a_uf824)); +} + +void +vect_array_scale_f824 (vect_t *va, uint8_t vn, int32_t s_f824) +{ + for (; vn; va++, vn--) + vect_scale_f824 (va, s_f824); +} + +void +vect_array_rotate_uf016 (vect_t *va, uint8_t vn, uint16_t a_uf016) +{ + assert (va); + int32_t c, s; + uint32_t a_uf824 = ((uint32_t) a_uf016) << 8; + c = fixed_cos_f824 (a_uf824); + s = fixed_sin_f824 (a_uf824); + for (; vn; va++, vn--) + { + int16_t tx = va->x, ty = va->y; + va->x = fixed_mul_f824 (tx, c) - fixed_mul_f824 (ty, s); + va->y = fixed_mul_f824 (tx, s) + fixed_mul_f824 (ty, c); + } +} + +void +vect_array_translate (vect_t *va, uint8_t vn, const vect_t *t) +{ + for (; vn; va++, vn--) + vect_translate (va, t); +} diff --git a/digital/avr/modules/math/geometry/vect.h b/digital/avr/modules/math/geometry/vect.h new file mode 100644 index 00000000..6a1cc570 --- /dev/null +++ b/digital/avr/modules/math/geometry/vect.h @@ -0,0 +1,107 @@ +#ifndef vect_h +#define vect_h +/* vect.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. + * + * }}} */ + +/* See fixed.h for numbers notation convention. Angles are in the [0, 1) + * interval, counter clock wise, uf0.16 unless noted. */ + +/** Vector single value. As we are working with millimeters most of the time, + * 16 bits are enough. There is no magic, if you change this, code should be + * reviewed. */ +typedef int16_t vect_value_t; + +/** 2D vector. */ +struct vect_t +{ + /** X coordinate. */ + vect_value_t x; + /** Y coordinate. */ + vect_value_t y; +}; +typedef struct vect_t vect_t; + +/** Scale (multiply) a vector by a fixed point value. + * - v: input/result vector. + * - s_f824: scale (f8.24). */ +void +vect_scale_f824 (vect_t *v, int32_t s_f824); + +/** Rotate a vector. + * - v: input/result vector. + * - a_uf016: angle of rotation (uf0.16). */ +void +vect_rotate_uf016 (vect_t *v, uint16_t a_uf016); + +/** Translate a vector (add two vectors). + * - v: input/result vector. + * - t: translation vector. */ +void +vect_translate (vect_t *v, const vect_t *t); + +/** Same as vect_translate. */ +#define vect_add vect_translate + +/** Substract two vectors. + * - v: input/result vector. + * - n: substracted vector. */ +void +vect_sub (vect_t *v, const vect_t *n); + +/** Compute vector norm (length). + * - v: input vector. + * - returns: norm. */ +vect_value_t +vect_norm (const vect_t *v); + +/** Compute a vector from polar coordinates. + * - v: result vector. + * - l: vector length. + * - a_uf016: vector angle (uf0.16). */ +void +vect_from_polar_uf016 (vect_t *v, int16_t l, uint16_t a_uf016); + +/** Scale (multiply) vectors by a fixed point value. + * - va: input/result vectors array. + * - vn: number of vectors. + * - s_f824: scale (f8.24). */ +void +vect_array_scale_f824 (vect_t *va, uint8_t vn, int32_t s_f824); + +/** Rotate vectors. + * - va: input/result vectors array. + * - vn: number of vectors. + * - a_uf016: angle of rotation (uf0.16). */ +void +vect_array_rotate_uf016 (vect_t *va, uint8_t vn, uint16_t a_uf016); + +/** Translate vectors. + * - va: input/result vectors array. + * - vn: number of vectors. + * - t: translation vector. */ +void +vect_array_translate (vect_t *va, uint8_t vn, const vect_t *t); + +#endif /* vect_h */ -- cgit v1.2.3