summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas Schodet2011-06-01 15:26:22 +0200
committerNicolas Schodet2011-06-01 15:26:22 +0200
commit51b917f7994d6d53fde11263ba4da3470fd2a6c4 (patch)
treebcddd256b54a9be8a77eb190ed891b75ba91edc4
parentb2c12931a8378cdb2112da8b21b63099683516f5 (diff)
digital/io-hub, host/simu: add codebar
-rw-r--r--digital/io-hub/src/robospierre/Makefile1
-rw-r--r--digital/io-hub/src/robospierre/codebar.avr.c64
-rw-r--r--digital/io-hub/src/robospierre/codebar.h36
-rw-r--r--digital/io-hub/src/robospierre/codebar.host.c56
-rw-r--r--digital/io-hub/src/robospierre/main.c15
-rw-r--r--digital/io-hub/src/robospierre/pawn_sensor.c5
-rw-r--r--digital/io-hub/tools/io_hub/mex.py43
-rw-r--r--host/simu/robots/robospierre/model/bag.py2
-rw-r--r--host/simu/robots/robospierre/model/clamp.py15
9 files changed, 231 insertions, 6 deletions
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;
}
diff --git a/digital/io-hub/tools/io_hub/mex.py b/digital/io-hub/tools/io_hub/mex.py
index 8d758382..9b72ccde 100644
--- a/digital/io-hub/tools/io_hub/mex.py
+++ b/digital/io-hub/tools/io_hub/mex.py
@@ -121,6 +121,46 @@ class Mex:
m.push ('L', self.contacts)
self.node.send (m)
+ class Codebar (Observable):
+ """Codebar stub.
+
+ - element_type: 'queen', 'king', or anything else.
+
+ """
+
+ def __init__ (self, pack, index):
+ Observable.__init__ (self)
+ self.pack = pack
+ self.index = index
+ self.element_type = None
+ self.register (self.__notified)
+
+ def __notified (self):
+ self.pack.set (self.index, self.element_type)
+
+ class Pack:
+ """Handle emission of several codebar for one message."""
+
+ def __init__ (self, node, instance):
+ self.node = node
+ self.codebars = [0, 0]
+ self.mtype = node.reserve (instance + ':codebar')
+
+ def set (self, index, element_type):
+ if element_type == 'queen':
+ self.codebars[index] = 4
+ elif element_type == 'king':
+ self.codebars[index] = 8
+ else:
+ self.codebars[index] = 0
+ self.__send ()
+
+ def __send (self):
+ m = simu.mex.msg.Msg (self.mtype)
+ for c in self.codebars:
+ m.push ('b', c)
+ self.node.send (m)
+
class Path (Observable):
"""Path finding algorithm report.
@@ -167,6 +207,9 @@ class Mex:
self.__contact_pack = self.Contact.Pack (node, instance)
self.contact = tuple (self.Contact (self.__contact_pack, i)
for i in range (CONTACT_NB))
+ self.__codebar_pack = self.Codebar.Pack (node, instance)
+ self.codebar = tuple (self.Codebar (self.__codebar_pack, i)
+ for i in (0, 1))
self.path = self.Path (node, instance)
self.pos_report = self.PosReport (node, instance)
diff --git a/host/simu/robots/robospierre/model/bag.py b/host/simu/robots/robospierre/model/bag.py
index 284e5bb8..b9d3bc3f 100644
--- a/host/simu/robots/robospierre/model/bag.py
+++ b/host/simu/robots/robospierre/model/bag.py
@@ -44,7 +44,7 @@ class Bag:
8 * pi, 0, 0.5 * pi) for i in (0, 1, 3, 4) ]
self.clamp = Clamp (table, self.position, link_bag.mimot.aux[0],
link_bag.mimot.aux[1], self.clamping_motor, self.door_motors,
- self.contact[0:7])
+ self.contact[0:7], link_bag.io_hub.codebar)
self.distance_sensor = [
DistanceSensorSensopart (link_bag.io_hub.adc[0], scheduler, table,
(20, 20), pi * 10 / 180, (self.position, ), 2),
diff --git a/host/simu/robots/robospierre/model/clamp.py b/host/simu/robots/robospierre/model/clamp.py
index 0af4ccf3..dbcab5f2 100644
--- a/host/simu/robots/robospierre/model/clamp.py
+++ b/host/simu/robots/robospierre/model/clamp.py
@@ -30,13 +30,14 @@ from math import pi, cos, sin
class Slot:
"""Slot which can contain a pawn."""
- def __init__ (self, x, y, z, side, door_motor, contact):
+ def __init__ (self, x, y, z, side, door_motor, contact, codebar = None):
self.x = x
self.y = y
self.z = z
self.side = side
self.door_motor = door_motor
self.contact = contact
+ self.codebar = codebar
self.pawn = None
class Clamp (Observable):
@@ -62,7 +63,8 @@ class Clamp (Observable):
SLOT_SIDE = 6
def __init__ (self, table, robot_position, elevation_motor,
- rotation_motor, clamping_motor, door_motors, slot_contacts):
+ rotation_motor, clamping_motor, door_motors, slot_contacts,
+ codebars):
Observable.__init__ (self)
self.table = table
self.robot_position = robot_position
@@ -75,13 +77,13 @@ class Clamp (Observable):
door_motors[2], None, door_motors[3], None)
self.slots = (
Slot (self.BAY_OFFSET, 0, 0 * self.BAY_ZOFFSET, 0,
- door_motors[0], slot_contacts[0]),
+ door_motors[0], slot_contacts[0], codebars[0]),
Slot (self.BAY_OFFSET, 0, 1 * self.BAY_ZOFFSET, 0,
None, slot_contacts[1]),
Slot (self.BAY_OFFSET, 0, 2 * self.BAY_ZOFFSET, 0,
door_motors[1], slot_contacts[2]),
Slot (-self.BAY_OFFSET, 0, 0 * self.BAY_ZOFFSET, 1,
- door_motors[2], slot_contacts[3]),
+ door_motors[2], slot_contacts[3], codebars[1]),
Slot (-self.BAY_OFFSET, 0, 1 * self.BAY_ZOFFSET, 1,
None, slot_contacts[4]),
Slot (-self.BAY_OFFSET, 0, 2 * self.BAY_ZOFFSET, 1,
@@ -203,6 +205,11 @@ class Clamp (Observable):
and slots[0].pawn.kind == 'tower'))
# This one is really high.
slots[2].contact.state = not (slots[2].pawn is not None)
+ if slots[0].pawn:
+ slots[0].codebar.element_type = slots[0].pawn.kind
+ else:
+ slots[0].codebar.element_type = None
+ slots[0].codebar.notify ()
slot_side = self.slots[self.SLOT_SIDE]
slot_side.contact.state = slot_side.pawn is None
clamp_slot = self.__get_clamp_slot ()