From a750fc58646c6f1ab5319d9bcf786be7a525a77c Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sun, 22 Apr 2012 22:07:20 +0200 Subject: digital/{ai,beacon,io-hub}: add beacon simulation stub --- digital/beacon/src/stub/Makefile | 12 +++ digital/beacon/src/stub/avrconfig.h | 61 +++++++++++++++ digital/beacon/src/stub/simu_stub.host.c | 126 +++++++++++++++++++++++++++++++ digital/beacon/tools/beacon/__init__.py | 1 + digital/beacon/tools/beacon/mex.py | 57 ++++++++++++++ 5 files changed, 257 insertions(+) create mode 100644 digital/beacon/src/stub/Makefile create mode 100644 digital/beacon/src/stub/avrconfig.h create mode 100644 digital/beacon/src/stub/simu_stub.host.c create mode 100644 digital/beacon/tools/beacon/__init__.py create mode 100644 digital/beacon/tools/beacon/mex.py (limited to 'digital/beacon') diff --git a/digital/beacon/src/stub/Makefile b/digital/beacon/src/stub/Makefile new file mode 100644 index 00000000..2f97d775 --- /dev/null +++ b/digital/beacon/src/stub/Makefile @@ -0,0 +1,12 @@ +BASE = ../../../avr +HOST_PROGS = simu_stub +simu_stub_SOURCES = simu_stub.host.c +MODULES = utils host twi +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega128 +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 + +include $(BASE)/make/Makefile.gen diff --git a/digital/beacon/src/stub/avrconfig.h b/digital/beacon/src/stub/avrconfig.h new file mode 100644 index 00000000..e88759d5 --- /dev/null +++ b/digital/beacon/src/stub/avrconfig.h @@ -0,0 +1,61 @@ +#ifndef stub_avrconfig_h +#define stub_avrconfig_h +/* stub_avrconfig.h */ +/* lol - Laser Opponent Location finding system. {{{ + * + * Copyright (C) 2012 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. + * + * }}} */ + +/* Config for simu_stub. */ + +/* twi - TWI module. */ +/** Driver to implement TWI: HARD, SOFT, or USI. */ +#define AC_TWI_DRIVER HARD +/** Do not use interrupts. */ +#define AC_TWI_NO_INTERRUPT 0 +/** TWI frequency, should really be 100 kHz. */ +#define AC_TWI_FREQ 100000 +/** Enable slave part. */ +#define AC_TWI_SLAVE_ENABLE 1 +/** Enable master part. */ +#define AC_TWI_MASTER_ENABLE 0 +/** Use polled slave mode: received data is stored in a buffer which can be + * polled using twi_slave_poll. */ +#define AC_TWI_SLAVE_POLLED 1 +/** Slave reception callback to be defined by the user when not in polled + * mode. */ +#undef AC_TWI_SLAVE_RECV +/** Master transfer completion callback, optionally defined by the user, called + * at end of master transfer. */ +#undef AC_TWI_MASTER_DONE +/** Use internal pull up. */ +#define AC_TWI_PULL_UP 0 +/** Slave reception buffer size. */ +#define AC_TWI_SLAVE_RECV_BUFFER_SIZE 16 +/** Slave transmission buffer size. */ +#define AC_TWI_SLAVE_SEND_BUFFER_SIZE 16 + +/* beacon. */ +/** TWI address. */ +#define AC_BEACON_TWI_ADDRESS 10 + +#endif /* stub_avrconfig_h */ diff --git a/digital/beacon/src/stub/simu_stub.host.c b/digital/beacon/src/stub/simu_stub.host.c new file mode 100644 index 00000000..cdbacaa3 --- /dev/null +++ b/digital/beacon/src/stub/simu_stub.host.c @@ -0,0 +1,126 @@ +/* simu_stub.c */ +/* lol - Laser Opponent Location finding system. {{{ + * + * Copyright (C) 2012 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/host/host.h" +#include "modules/host/mex.h" +#include "modules/twi/twi.h" +#include "modules/utils/utils.h" +#include "modules/utils/byte.h" +#include "modules/utils/crc.h" + +/** This is a stub only used in simulation to bypass all beacon computations + * and directly provide a position via TWI. */ + +struct position_t +{ + uint16_t x, y; +}; + +struct position_t simu_positions[2]; + +uint8_t simu_seq; + +static void +simu_twi_proto_poll (void) +{ + /* Do nothing interresting with the received value, but check it + * anyway. */ + uint8_t recv_buf[AC_TWI_SLAVE_RECV_BUFFER_SIZE]; + uint8_t recv_size; + while ((recv_size = twi_slave_poll (recv_buf, sizeof (recv_buf)))) + { + if (crc_compute (recv_buf + 1, recv_size - 1) != recv_buf[0]) + continue; + if (recv_buf[1] == simu_seq) + continue; + if (recv_size != 3) + continue; + simu_seq = recv_buf[1]; + } +} + +static void +simu_twi_proto_update (void) +{ + /* Update TWI slave buffer after each position update. */ + uint8_t status[4 + 5 * UTILS_COUNT (simu_positions)]; + uint8_t i, index = 1; + status[index++] = 0; + status[index++] = 0; + status[index++] = simu_seq; + for (i = 0; i < UTILS_COUNT (simu_positions); i++) + { + status[index++] = v16_to_v8 (simu_positions[i].x, 1); + status[index++] = v16_to_v8 (simu_positions[i].x, 0); + status[index++] = v16_to_v8 (simu_positions[i].y, 1); + status[index++] = v16_to_v8 (simu_positions[i].y, 0); + status[index++] = simu_positions[i].x == (uint16_t) -1 ? 0 : 100; + } + status[0] = crc_compute (&status[1], sizeof (status) - 1); + twi_slave_update (status, sizeof (status)); +} + +static void +simu_positions_handle (void *user, mex_msg_t *msg) +{ + uint8_t i; + struct position_t pos; + mex_msg_pop (msg, "BHH", &i, &pos.x, &pos.y); + simu_positions[i] = pos; + simu_twi_proto_update (); +} + +static void +simu_init (void) +{ + const char *mex_instance; + mex_node_connect (); + mex_instance = host_get_instance ("beacon0", 0); + uint8_t mtype = mex_node_reservef ("%s:position", mex_instance); + mex_node_register (mtype, simu_positions_handle, 0); + uint8_t i; + for (i = 0; i < UTILS_COUNT (simu_positions); i++) + { + simu_positions[i].x = (uint16_t) -1; + simu_positions[i].y = (uint16_t) -1; + } +} + +int +main (int argc, char ** argv) +{ + avr_init (argc, argv); + simu_init (); + twi_init (AC_BEACON_TWI_ADDRESS); + simu_twi_proto_update (); + while (1) + { + simu_twi_proto_poll (); + mex_node_wait_date (mex_node_date () + 1); + } + return 0; +} + diff --git a/digital/beacon/tools/beacon/__init__.py b/digital/beacon/tools/beacon/__init__.py new file mode 100644 index 00000000..af42b67b --- /dev/null +++ b/digital/beacon/tools/beacon/__init__.py @@ -0,0 +1 @@ +from mex import Mex diff --git a/digital/beacon/tools/beacon/mex.py b/digital/beacon/tools/beacon/mex.py new file mode 100644 index 00000000..fcc27741 --- /dev/null +++ b/digital/beacon/tools/beacon/mex.py @@ -0,0 +1,57 @@ +# lol - Laser Opponent Location finding system. {{{ +# +# Copyright (C) 2012 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. +# +# }}} +"""Mex interface to simulated beacon.""" + +import simu.mex.msg + +POSITION_NB = 2 + +class Mex: + """Handle communications with stub.""" + + class Position: + """Send a position to the stub.""" + + def __init__ (self, node, instance, index): + self.__node = node + self.__mtype = node.reserve (instance + ':position') + self.__index = index + self.pos = None + + def register_to (self, pos): + """Register to given observable position.""" + assert self.pos is None + self.pos = pos + self.pos.register (self.__update) + + def __update (self): + """Called on position update.""" + if self.pos.pos is not None: + m = simu.mex.msg.Msg (self.__mtype) + m.push ('BHH', self.__index, int (self.pos.pos[0]), int (self.pos.pos[1])) + self.__node.send (m) + + def __init__ (self, node, instance = 'beacon0'): + self.position = [ self.Position (node, instance, i) + for i in xrange (POSITION_NB) ] -- cgit v1.2.3