From 51b917f7994d6d53fde11263ba4da3470fd2a6c4 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 1 Jun 2011 15:26:22 +0200 Subject: digital/io-hub, host/simu: add codebar --- digital/io-hub/src/robospierre/Makefile | 1 + digital/io-hub/src/robospierre/codebar.avr.c | 64 +++++++++++++++++++++++++++ digital/io-hub/src/robospierre/codebar.h | 36 +++++++++++++++ digital/io-hub/src/robospierre/codebar.host.c | 56 +++++++++++++++++++++++ digital/io-hub/src/robospierre/main.c | 15 +++++++ digital/io-hub/src/robospierre/pawn_sensor.c | 5 ++- 6 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 digital/io-hub/src/robospierre/codebar.avr.c create mode 100644 digital/io-hub/src/robospierre/codebar.h create mode 100644 digital/io-hub/src/robospierre/codebar.host.c (limited to 'digital/io-hub/src') diff --git a/digital/io-hub/src/robospierre/Makefile b/digital/io-hub/src/robospierre/Makefile index 714541dc..88e6c79e 100644 --- a/digital/io-hub/src/robospierre/Makefile +++ b/digital/io-hub/src/robospierre/Makefile @@ -6,6 +6,7 @@ HOST_PROGS = test_element # Sources to compile. io_hub_SOURCES = main.c top.c \ clamp.c logistic.c element.c pawn_sensor.c \ + codebar.avr.c codebar.host.c \ radar_defs.c radar.c path.c move.c \ init.c fsm.host.c fsm_AI_gen.avr.c fsm_queue.c \ pwm.avr.c pwm.host.c \ diff --git a/digital/io-hub/src/robospierre/codebar.avr.c b/digital/io-hub/src/robospierre/codebar.avr.c new file mode 100644 index 00000000..d2609012 --- /dev/null +++ b/digital/io-hub/src/robospierre/codebar.avr.c @@ -0,0 +1,64 @@ +/* codebar.avr.c */ +/* robospierre - Eurobot 2011 AI. {{{ + * + * Copyright (C) 2011 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 "codebar.h" + +#include "defs.h" + +#include "modules/twi/twi.h" +#include "modules/utils/utils.h" +#include "modules/utils/crc.h" +#include "modules/utils/byte.h" + +#define CODEBAR_ADDRESS 0x20 +#define CODEBAR_STATUS_LENGTH 7 + +void +codebar_init (void) +{ +} + +uint8_t +codebar_get (uint8_t direction) +{ + uint8_t buffer[CODEBAR_STATUS_LENGTH]; + /* Read status. */ + twi_master_recv (CODEBAR_ADDRESS, buffer, sizeof (buffer)); + uint8_t ret = twi_master_wait (); + if (ret != CODEBAR_STATUS_LENGTH) + return 0; + uint8_t crc = crc_compute (buffer + 1, CODEBAR_STATUS_LENGTH - 1); + if (crc != buffer[0]) + return 0; + /* Get data. */ + uint8_t offset = direction == DIRECTION_FORWARD ? 1 : 4; + uint16_t age = v8_to_v16 (buffer[offset], buffer[offset + 1]); + uint16_t type = buffer[offset + 2]; + if (age > 225) + return 0; + else + return type; +} + diff --git a/digital/io-hub/src/robospierre/codebar.h b/digital/io-hub/src/robospierre/codebar.h new file mode 100644 index 00000000..d334f131 --- /dev/null +++ b/digital/io-hub/src/robospierre/codebar.h @@ -0,0 +1,36 @@ +#ifndef codebar_h +#define codebar_h +/* codebar.h */ +/* robospierre - Eurobot 2011 AI. {{{ + * + * Copyright (C) 2011 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. + * + * }}} */ + +/** Initialise module. */ +void +codebar_init (void); + +/** Get element type on the specified direction. */ +uint8_t +codebar_get (uint8_t direction); + +#endif /* codebar_h */ diff --git a/digital/io-hub/src/robospierre/codebar.host.c b/digital/io-hub/src/robospierre/codebar.host.c new file mode 100644 index 00000000..68ced300 --- /dev/null +++ b/digital/io-hub/src/robospierre/codebar.host.c @@ -0,0 +1,56 @@ +/* codebar.host.c */ +/* robospierre - Eurobot 2011 AI. {{{ + * + * Copyright (C) 2011 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 "codebar.h" +#include "defs.h" + +#include "modules/host/host.h" +#include "modules/host/mex.h" + +uint8_t codebar_front, codebar_back; + +static void +codebar_handle (void *user, mex_msg_t *msg) +{ + mex_msg_pop (msg, "BB", &codebar_front, &codebar_back); +} + +void +codebar_init (void) +{ + const char *mex_instance = host_get_instance ("io-hub0", 0); + uint8_t mtype = mex_node_reservef ("%s:codebar", mex_instance); + mex_node_register (mtype, codebar_handle, 0); +} + +uint8_t +codebar_get (uint8_t direction) +{ + if (direction == DIRECTION_FORWARD) + return codebar_front; + else + return codebar_back; +} + diff --git a/digital/io-hub/src/robospierre/main.c b/digital/io-hub/src/robospierre/main.c index 79c0fe2a..42a2809a 100644 --- a/digital/io-hub/src/robospierre/main.c +++ b/digital/io-hub/src/robospierre/main.c @@ -40,6 +40,7 @@ #include "pwm.h" #include "contact.h" +#include "codebar.h" #include "radar.h" #define FSM_NAME AI @@ -77,6 +78,9 @@ static uint8_t main_stats_asserv_, main_stats_asserv_cpt_; /** Contact stats counters. */ static uint8_t main_stats_contact_, main_stats_contact_cpt_; +/** Codebar stats counters. */ +static uint8_t main_stats_codebar_, main_stats_codebar_cpt_; + /** US sensors stats counters. */ static uint8_t main_stats_usdist_, main_stats_usdist_cpt_; @@ -103,6 +107,7 @@ main_init (void) /* IO modules. */ pwm_init (); contact_init (); + codebar_init (); usdist_init (); /* AI modules. */ clamp_init (); @@ -223,6 +228,12 @@ main_loop (void) proto_send1d ('P', contact_all ()); main_stats_contact_cpt_ = main_stats_contact_; } + if (main_stats_codebar_ && !--main_stats_codebar_cpt_) + { + proto_send2b ('B', codebar_get (DIRECTION_FORWARD), + codebar_get (DIRECTION_BACKWARD)); + main_stats_codebar_cpt_ = main_stats_codebar_; + } if (main_stats_usdist_ && !--main_stats_usdist_cpt_) { proto_send4w ('U', usdist_mm[0], usdist_mm[1], usdist_mm[2], @@ -331,6 +342,10 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) /* Contact stats. */ main_stats_contact_ = main_stats_contact_cpt_ = args[0]; break; + case c ('B', 1): + /* Codebar stats. */ + main_stats_codebar_ = main_stats_codebar_cpt_ = args[0]; + break; case c ('U', 1): /* US sensors stats. */ main_stats_usdist_ = main_stats_usdist_cpt_ = args[0]; diff --git a/digital/io-hub/src/robospierre/pawn_sensor.c b/digital/io-hub/src/robospierre/pawn_sensor.c index be6f3c87..cb4cfe4a 100644 --- a/digital/io-hub/src/robospierre/pawn_sensor.c +++ b/digital/io-hub/src/robospierre/pawn_sensor.c @@ -31,6 +31,7 @@ #include "element.h" #include "clamp.h" #include "bot.h" +#include "codebar.h" #include "modules/utils/utils.h" #include "modules/math/geometry/distance.h" @@ -53,7 +54,9 @@ struct pawn_sensor_t pawn_sensor_front, pawn_sensor_back; static uint8_t pawn_sensor_get_type (uint8_t direction) { - uint8_t element_type = IO_GET (CONTACT_STRAT) ? ELEMENT_PAWN : ELEMENT_KING; + uint8_t element_type = codebar_get (direction); + if (!element_type) + element_type = ELEMENT_PAWN; return element_type; } -- cgit v1.2.3