summaryrefslogtreecommitdiffhomepage
path: root/digital
diff options
context:
space:
mode:
authorJérémy Dufour2008-03-19 11:34:01 +0100
committerJérémy Dufour2008-03-19 11:34:01 +0100
commit193dd6732798199a4a94749188a56635fda6dd90 (patch)
tree5475506a41cf72460a2e7d6326d7a48bedcc412f /digital
parent624f2f2aade9f87ff22f56e0b5a9999cdd1db458 (diff)
* digital/io/src
- add trap module (not integrated in the io program); - add eeprom module to support load/store parameters (for the trap module); - partial integration of eeprom in the io program.
Diffstat (limited to 'digital')
-rw-r--r--digital/io/src/Makefile2
-rw-r--r--digital/io/src/eeprom.c96
-rw-r--r--digital/io/src/eeprom.h56
-rw-r--r--digital/io/src/main.c3
-rw-r--r--digital/io/src/servo.h1
-rw-r--r--digital/io/src/trap.c118
-rw-r--r--digital/io/src/trap.h99
7 files changed, 374 insertions, 1 deletions
diff --git a/digital/io/src/Makefile b/digital/io/src/Makefile
index 60d037f3..428f2418 100644
--- a/digital/io/src/Makefile
+++ b/digital/io/src/Makefile
@@ -1,6 +1,6 @@
BASE = ../../avr
AVR_PROGS = io
-io_SOURCES = main.c asserv.c servo.c
+io_SOURCES = main.c asserv.c servo.c eeprom.c trap.c
MODULES = proto uart twi
CONFIGFILE = avrconfig.h
# atmega8, atmega8535, atmega128...
diff --git a/digital/io/src/eeprom.c b/digital/io/src/eeprom.c
new file mode 100644
index 00000000..3972e04f
--- /dev/null
+++ b/digital/io/src/eeprom.c
@@ -0,0 +1,96 @@
+/* eeprom.c */
+/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
+ *
+ * Copyright (C) 2008 Dufour Jrmy
+ *
+ * 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 "eeprom.h"
+
+#include "servo.h" /* SERVO_NUMBER */
+#include "trap.h" /* trap_high_time_pos */
+#include <avr/eeprom.h> /* eeprom_{read,write}_byte */
+
+/**
+ * @defgroup EepromPrivate EEPROM module private variables and functions
+ * declarations
+ * @{
+ */
+
+/**
+ * What is the address, in the EEPROM, where are stored the parameters values?
+ */
+#define EEPROM_PARAM_START 0
+
+/**
+ * Actual version of the parameters organization.
+ */
+#define EEPROM_PARAM_KEY 0x01
+
+/** @} */
+
+/* Load parameters from the EEPROM. */
+void
+eeprom_load_param ()
+{
+ uint8_t compt;
+ /* The parameters start at the given address */
+ uint8_t *ptr8 = (uint8_t *) EEPROM_PARAM_START;
+ /* Check if the key/version is correct */
+ if (eeprom_read_byte (ptr8++) != EEPROM_PARAM_KEY)
+ /* Error, stop here */
+ return;
+
+ /* Load trap module data */
+ /* Extreme position of the servos motor (high time value of the PWM) */
+ for (compt = 0; compt < SERVO_NUMBER; compt++)
+ {
+ trap_high_time_pos[0][compt] = eeprom_read_byte(ptr8++);
+ trap_high_time_pos[1][compt] = eeprom_read_byte(ptr8++);
+ }
+}
+
+/* Store parameters in the EEPROM. */
+void
+eeprom_save_param ()
+{
+ uint8_t compt;
+ /* The parameters start at the given address */
+ uint8_t *ptr8 = (uint8_t *) EEPROM_PARAM_START;
+ /* Store the key */
+ eeprom_write_byte (ptr8++, EEPROM_PARAM_KEY);
+
+ /* Store trap module data */
+ /* Extreme position of the servos motor (high time value of the PWM) */
+ for (compt = 0; compt < SERVO_NUMBER; compt++)
+ {
+ eeprom_write_byte (ptr8++, trap_high_time_pos[0][compt]);
+ eeprom_write_byte (ptr8++, trap_high_time_pos[1][compt]);
+ }
+}
+
+/* Clear parameters in the EEPROM by invalidating the key. */
+void
+eeprom_clear_param ()
+{
+ /* Write an invalid key */
+ eeprom_write_byte ((uint8_t *) EEPROM_PARAM_START, 0xFF);
+}
diff --git a/digital/io/src/eeprom.h b/digital/io/src/eeprom.h
new file mode 100644
index 00000000..1bd7d213
--- /dev/null
+++ b/digital/io/src/eeprom.h
@@ -0,0 +1,56 @@
+#ifndef eeprom_h
+#define eeprom_h
+/* eeprom.h */
+/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
+ *
+ * Copyright (C) 2008 Dufour Jrmy
+ *
+ * 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.
+ *
+ * }}} */
+
+/**
+ * @file Store and load configuration parameters from the EEPROM of the AVR.
+ * This module is able to store some parameters for the io program and its
+ * sub-modules, to load them.
+ * To prevent problem when you change the format of your the parameters (or
+ * add/remove some), it uses a key to store the version of the parameters
+ * organization. So when you change something, update the key.
+ */
+
+/**
+ * Load parameters from the EEPROM.
+ * Check if the key is correct.
+ */
+void
+eeprom_load_param ();
+
+/**
+ * Store parameters in the EEPROM.
+ */
+void
+eeprom_save_param ();
+
+/**
+ * Clear parameters in the EEPROM by invalidating the key.
+ */
+void
+eeprom_clear_param ();
+
+#endif /* eeprom_h */
diff --git a/digital/io/src/main.c b/digital/io/src/main.c
index 9573991a..76594bbd 100644
--- a/digital/io/src/main.c
+++ b/digital/io/src/main.c
@@ -36,6 +36,7 @@
#include "asserv.h" /* Functions to control the asserv board */
#include "switch.h" /* Manage switches (jack, color selector) */
+#include "eeprom.h" /* Parameters loaded/stored in the EEPROM */
/**
* Initialize the main and all its subsystems.
@@ -57,6 +58,8 @@ main_init (void)
uart0_init ();
/* Main timer */
main_timer_init ();
+ /* Load parameters */
+ eeprom_load_param ();
/* Asserv communication */
asserv_init ();
diff --git a/digital/io/src/servo.h b/digital/io/src/servo.h
index b52b02bc..2e957714 100644
--- a/digital/io/src/servo.h
+++ b/digital/io/src/servo.h
@@ -51,6 +51,7 @@
/**
* Number of servos motor managed by this module.
+ * If you change it, you _must_ to update the key of the eeprom module!
*/
#define SERVO_NUMBER 5
diff --git a/digital/io/src/trap.c b/digital/io/src/trap.c
new file mode 100644
index 00000000..0995478c
--- /dev/null
+++ b/digital/io/src/trap.c
@@ -0,0 +1,118 @@
+/* trap.c */
+/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
+ *
+ * Copyright (C) 2008 Dufour Jrmy
+ *
+ * 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 "trap.h"
+
+#include "servo.h" /* servo_* */
+
+/**
+ * @todo
+ * - how to manage traps collision for the 3 and 4.
+ * - who map servo/pin to trap?
+ */
+
+/**
+ * @defgroup TrapPrivate Trap module private variables and functions
+ * declarations
+ * @{
+ */
+
+/**
+ * Possible positions of a trap.
+ * It is used for the index of the table @see trap_high_time_pos.
+ */
+typedef enum trap_position_e
+{
+ /** Horizontal. */
+ horizontal = 0,
+ /** Vertical. */
+ vertical,
+
+ /** Length of the enum, always left it at the end. */
+ lenght
+} trap_position_e;
+
+/**
+ * Trap high time values for the different positions (horizontal and vertical).
+ * In this two dimensions table, the first index represent horizontal or
+ * vertical values.
+ */
+uint8_t trap_high_time_pos[lenght][SERVO_NUMBER];
+
+/** @} */
+
+/* Trap module initialization. */
+void
+trap_init (void)
+{
+ /* Initialize servo module */
+ servo_init ();
+}
+
+/* Configure traps to open a path to a box. */
+void
+trap_setup_path_to_box (trap_box_id_e box)
+{
+ switch (box)
+ {
+ case garbage:
+ /* 0:H, 1:H, 2:H, 3:V, 4:? */
+ servo_set_high_time (0, trap_high_time_pos[horizontal][0]);
+ servo_set_high_time (1, trap_high_time_pos[horizontal][1]);
+ servo_set_high_time (2, trap_high_time_pos[horizontal][2]);
+ servo_set_high_time (3, trap_high_time_pos[vertical][3]);
+ break;
+ case out_left_box:
+ /* 0:V, 1:H, 2:H, 3:V, 4:? */
+ servo_set_high_time (0, trap_high_time_pos[vertical][0]);
+ servo_set_high_time (1, trap_high_time_pos[horizontal][1]);
+ servo_set_high_time (2, trap_high_time_pos[horizontal][2]);
+ servo_set_high_time (3, trap_high_time_pos[vertical][3]);
+ break;
+ case middle_left_box:
+ /* 0:?, 1:V, 2:H, 3:V, 4:? */
+ servo_set_high_time (1, trap_high_time_pos[vertical][1]);
+ servo_set_high_time (2, trap_high_time_pos[horizontal][2]);
+ servo_set_high_time (3, trap_high_time_pos[vertical][3]);
+ break;
+ case middle_box:
+ /* 0:?, 1:?, 2:V, 3:V, 4:? */
+ servo_set_high_time (2, trap_high_time_pos[vertical][2]);
+ servo_set_high_time (3, trap_high_time_pos[vertical][3]);
+ break;
+ case middle_right_box:
+ /* 0:?, 1:?, 2:V, 3:H, 4:V */
+ servo_set_high_time (2, trap_high_time_pos[vertical][2]);
+ servo_set_high_time (3, trap_high_time_pos[horizontal][3]);
+ servo_set_high_time (4, trap_high_time_pos[vertical][4]);
+ break;
+ case out_right_box:
+ /* 0:?, 1:?, 2:V, 3:H, 4:H */
+ servo_set_high_time (2, trap_high_time_pos[vertical][2]);
+ servo_set_high_time (3, trap_high_time_pos[horizontal][3]);
+ servo_set_high_time (4, trap_high_time_pos[horizontal][4]);
+ break;
+ }
+}
diff --git a/digital/io/src/trap.h b/digital/io/src/trap.h
new file mode 100644
index 00000000..9a1af575
--- /dev/null
+++ b/digital/io/src/trap.h
@@ -0,0 +1,99 @@
+#ifndef trap_h
+#define trap_h
+/* trap.h */
+/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
+ *
+ * Copyright (C) 2008 Dufour Jrmy
+ *
+ * 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.
+ *
+ * }}} */
+
+/**
+ * @file Module to control the traps (using servo module).
+ * It is a higher interface to the servo module more simple to use.
+ * The traps and the servos motor are organized in the following ways:
+ * @verbatim
+ * +---+----+---+----+---+
+ * G | L | ML | M | MR | R |
+ * +---+----+---+----+---+
+ * 0 1 2 ^ 3 4
+ * @endverbatim
+ * In the scheme, the target boxes for the balls are identified by letters,
+ * and the servo motors that control the traps are identified by a number:
+ * - L: left;
+ * - ML: middle left;
+ * - M: middle;
+ * - MR: middle right;
+ * - R: right.
+ * Balls are entering in the system between the 2 and 3 traps, where the ^
+ * symbol is.
+ * If the ball want to go M, traps 2 and 3 must be vertical.
+ * To go to ML, trap 4 must be vertical, 2 must be horizontal and 1 must be
+ * vertical.
+ * To go to the garbage, trap 3 must be vertical and traps 0, 1 and 2 must be
+ * horizontal.
+ */
+
+#include "servo.h" /* SERVO_NUMBER */
+#include "common.h" /* uint8_t */
+
+/**
+ * Trap high time values for the different positions (horizontal and
+ * vertical).
+ * We need to declare it as external to be able to use it from the EEPROM
+ * module.
+ */
+extern uint8_t trap_high_time_pos[2][SERVO_NUMBER];
+
+/**
+ * Trap module initialization.
+ * It initializes the sub-module servo.
+ */
+void
+trap_init (void);
+
+/**
+ * List of boxes where to store balls.
+ */
+typedef enum trap_box_id_e
+{
+ /** Throw away ball. */
+ garbage = 0,
+ /** Most left box. */
+ out_left_box,
+ /** Middle left box. */
+ middle_left_box,
+ /** Box for the middle slot. */
+ middle_box,
+ /** Middle right box. */
+ middle_right_box,
+ /** Most right box. */
+ out_right_box,
+} trap_box_id_e;
+
+/**
+ * Configure traps to open a path to a box.
+ * This function will setup all the servo open a path to the desired box.
+ * @param box the box where you want the path to lead to.
+ */
+void
+trap_setup_path_to_box (trap_box_id_e box);
+
+#endif /* trap_h */