summaryrefslogtreecommitdiff
path: root/n/es-2006/src
diff options
context:
space:
mode:
authordufourj2006-05-03 17:22:36 +0000
committerdufourj2006-05-03 17:22:36 +0000
commit0f1dec71f0581546e6afc0d3e0f991968059b174 (patch)
tree30dfcb1ff74cc65ca8e544c21712fe39d269f593 /n/es-2006/src
parent82a13c07d0a08cbb33ac7e073c32ee5e56786d62 (diff)
ES :
- amélioration de la gestion des capteurs rvb ; - meilleur nomage des fonctions ; - gestion de l'overflow. TODO : - à tester sur AVR ; - fonction de debug pour surveiller qu'un capteur ; - regarder la solution pour les stats par capteurs.
Diffstat (limited to 'n/es-2006/src')
-rw-r--r--n/es-2006/src/Makefile2
-rw-r--r--n/es-2006/src/main.c29
-rw-r--r--n/es-2006/src/rvb_sensor.c218
-rw-r--r--n/es-2006/src/sensor_rvb.c274
-rw-r--r--n/es-2006/src/sensor_rvb.h (renamed from n/es-2006/src/rvb_sensor.h)44
5 files changed, 313 insertions, 254 deletions
diff --git a/n/es-2006/src/Makefile b/n/es-2006/src/Makefile
index 6bbb85a..0d16f6a 100644
--- a/n/es-2006/src/Makefile
+++ b/n/es-2006/src/Makefile
@@ -1,6 +1,6 @@
BASE = ../../avr
PROGS = es
-es_SOURCES = main.c rvb_sensor.c
+es_SOURCES = main.c sensor_rvb.c
MODULES = proto uart utils
CONFIGFILE = avrconfig.h
# atmega8, atmega8535, atmega128...
diff --git a/n/es-2006/src/main.c b/n/es-2006/src/main.c
index dc4f14f..a269dfe 100644
--- a/n/es-2006/src/main.c
+++ b/n/es-2006/src/main.c
@@ -29,13 +29,13 @@
#include "modules/proto/proto.h"
#include "modules/utils/utils.h"
-#include "timer.h"
-#include "rvb_sensor.h"
+#include "timer.h" /* main timer */
+#include "sensor_rvb.h" /* RVB sensors management */
/* Statistics for sensors */
-uint8_t rvb_sensors_stats = 0;
-uint8_t rvb_sensors_stats_enable = 0;
+uint8_t sensor_rvb_stat_enable = 0;
+uint8_t sensor_rvb_stats = 0;
/** Call when we receive some data from proto (uart) */
void
@@ -50,10 +50,10 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
utils_reset ();
break;
case c ('S', 1):
- rvb_sensors_stats = rvb_sensors_stats_enable = args[0];
+ sensor_rvb_stats = sensor_rvb_stat_enable = args[0];
break;
case c ('s', 2):
- rvb_sensor_watch (args[0], args[1]);
+ sensor_rvb_watch (args[0], args[1]);
break;
/* Unknown command */
default:
@@ -68,23 +68,26 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
int
main (void)
{
+ int compt;
+
uart0_init ();
timer_init ();
- rvb_sensors_init ();
+ sensor_rvb_init ();
sei ();
proto_send0 ('z');
while (1)
{
timer_wait ();
- if (rvb_sensors_stats_enable && !--rvb_sensors_stats)
+ if (sensor_rvb_stat_enable && !--sensor_rvb_stats)
{
- rvb_sensors_stats = rvb_sensors_stats_enable;
- proto_send4w ('S', rvb_sensors_values[7][0],
- rvb_sensors_values[7][1], rvb_sensors_values[7][2],
- rvb_sensors_values[7][3]);
+ sensor_rvb_stats = sensor_rvb_stat_enable;
+ for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
+ proto_send4w ('S', sensor_rvb_values[compt][0],
+ sensor_rvb_values[compt][1], sensor_rvb_values[compt][2],
+ sensor_rvb_values[compt][3]);
}
- rvb_sensors_start_update ();
+ sensor_rvb_start_capture ();
while (uart0_poll ())
proto_accept (uart0_getc ());
}
diff --git a/n/es-2006/src/rvb_sensor.c b/n/es-2006/src/rvb_sensor.c
deleted file mode 100644
index 0315e1c..0000000
--- a/n/es-2006/src/rvb_sensor.c
+++ /dev/null
@@ -1,218 +0,0 @@
-/* rvb_sensor.c */
-/* es - Input/Output general purpose board. {{{
- *
- * Copyright (C) 2006 Dufour Jérémy
- *
- * Robot APB Team/Efrei 2004.
- * Web: http://assos.efrei.fr/robot/
- * Email: robot AT efrei DOT fr
- *
- * 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 "rvb_sensor.h"
-
-#include "io.h"
-#include "modules/utils/utils.h" /* regv */
-#include "modules/uart/uart.h" /* */
-
-#define S1RVB 7
-#define S0RVB 6
-/* Wait time between IO change in ns */
-#define RVB_SENSOR_DELAY_IO 125
-
-/** Internal counter */
-static volatile uint16_t sensor_value_;
-/** Sensor state flag */
-static volatile uint8_t sensor_state_;
-/** Sensor used actually */
-volatile uint8_t sensor_number_;
-/** Color of the actual sensor */
-volatile uint8_t sensor_color_;
-/** Number of overflow interruption since last edge change */
-uint8_t sensor_overflow_count_;
-
-volatile uint16_t rvb_sensors_values [9][5];
-
-uint8_t sensor_disable = 0;
-
-/** Update everything for you. */
-static void
-rvb_sensors_update_state (void)
-{
-// uart0_putc ('u');
- if (sensor_state_ == 1)
- {
- /* Disable sensors */
- rvb_sensors_disable ();
- /* Wait a little */
- utils_delay_ns (RVB_SENSOR_DELAY_IO);
- switch (++sensor_number_)
- {
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
-// PORTC = ~_BV(sensor_number_);
-// break;
- case 8:
-// PORTD &= ~_BV(6);
-// break;
- case 9:
-// PORTC = ~_BV(0);
-// break;
- PORTD &= ~_BV(6);
- break;
- default:
- /* We are at the end */
- sensor_state_ = 0;
- return;
- }
- /* Wait for output of sensor to be ready */
- utils_delay_ns (RVB_SENSOR_DELAY_IO);
- /* Start with first color */
- sensor_color_ = 0;
- }
- /* Select the color */
- PORTF = (PORTF & ~0xC0) | (sensor_color_ << 6);
- /* Enable interrupt for IC1 */
- TIMSK |= _BV (TICIE1);
-// uart0_putc ('e');
- // XXX
-// TIMSK |= _BV (TOIE1);
-}
-
-/** Counter 1 overflow. */
-SIGNAL (SIG_OVERFLOW1)
-{
- // XXX For the moment.
- if (++sensor_overflow_count_ == 2)
- {
- sensor_state_ = 1;
- rvb_sensors_update_state ();
- }
-}
-
-/** Interrupt on falling edge for Input Capture 1. */
-SIGNAL (SIG_INPUT_CAPTURE1)
-{
- switch (sensor_state_)
- {
- case 1:
- /* Ignore this capture */
- sensor_state_++;
- break;
- case 2:
- /* Save capture start */
- sensor_value_ = ICR1;
- /* New capture */
- sensor_overflow_count_ = 0;
- /* Enable interruption for overflow */
- sensor_state_++;
-// TIMSK |= _BV (TOIE1);
- break;
- case 3:
- /* Disable this interruption */
- TIMSK &= ~_BV (TICIE1);
- /* Disable interruption for overflow */
- TIMSK &= ~_BV (TOIE1);
- /* Compute value */
- //TODO
- sensor_value_ = ICR1 - sensor_value_;
- /* Save */
- rvb_sensors_values[sensor_number_ - 1][sensor_color_] = sensor_value_;
- /* Next color */
- sensor_color_++;
- /* If no more colour, next sensor */
- if (sensor_color_ == 4)
- sensor_state_ = 1;
- rvb_sensors_update_state ();
- break;
- }
-}
-
-/** Start updating RVBC values for sensors. */
-void
-rvb_sensors_start_update (void)
-{
-// uart0_putc ('i');
- /* Are we already getting a sensor ? */
- if (!sensor_state_ && !sensor_disable)
- {
- sensor_state_ = 1;
- /* Start with sensor one */
- sensor_number_ = 0;
- /* Select it and launch capture */
- rvb_sensors_update_state ();
- }
-}
-
-/** Initialisation. */
-void
-rvb_sensors_init (void)
-{
- /* Disable sensors */
- rvb_sensors_disable ();
- /* Put enable ports in output mode */
- DDRC = 0xff;
- DDRD |= _BV(6);
- /* Put color selector mode in output mode */
- DDRF |= _BV(S0RVB) | _BV(S1RVB);
- /* Initialisation of counter :
- * Noise canceler, 1 for prescaling and that's all */
- TCCR1B |= _BV(ICNC1) | _BV(CS10);
- /* Nothing to do for the moment */
- sensor_state_ = 0;
-}
-
-void
-rvb_sensor_watch (uint8_t sensor_num, uint8_t sensor_col)
-{
- /* Disable this interruption */
- TIMSK &= ~_BV (TICIE1);
- /* Disable interruption for overflow */
- TIMSK &= ~_BV (TOIE1);
- sensor_disable = 1;
- /* Disable sensors */
- rvb_sensors_disable ();
- /* Wait a little */
- utils_delay_ns (RVB_SENSOR_DELAY_IO);
- switch (++sensor_num)
- {
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- PORTC = ~_BV(sensor_num);
- break;
- case 8:
- PORTD &= ~_BV(6);
- break;
- case 9:
- PORTC = ~_BV(0);
- break;
- }
- /* Wait for output of sensor to be ready */
- utils_delay_ns (RVB_SENSOR_DELAY_IO);
- /* Select the color */
- PORTF = (PORTF & ~0xC0) | (sensor_col << 6);
-}
-
diff --git a/n/es-2006/src/sensor_rvb.c b/n/es-2006/src/sensor_rvb.c
new file mode 100644
index 0000000..ac8b0aa
--- /dev/null
+++ b/n/es-2006/src/sensor_rvb.c
@@ -0,0 +1,274 @@
+/* sensor_rvb.c */
+/* es - Input/Output general purpose board. {{{
+ *
+ * Copyright (C) 2006 Dufour Jérémy
+ *
+ * Robot APB Team/Efrei 2004.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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 "sensor_rvb.h"
+
+#include "io.h"
+#include "modules/utils/utils.h" /* regv */
+#include "modules/uart/uart.h"
+
+/**
+ * Somes defines.
+ **/
+/** Color pins. */
+#define S1RVB 7
+#define S0RVB 6
+/** Max input capture before considering we can start the real capture. */
+#define RVB_MAX_FALSE_IC 3
+/** Wait time between IO change in ns. */
+#define RVB_SENSOR_DELAY_IO 125
+/** All the possible states */
+#define RVB_STATE_SLEEP 0 /* Doing nothing */
+#define RVB_STATE_NEXT_SENSOR 1 /* Selecting a sensor */
+#define RVB_STATE_WAIT_IC 20 /* Waiting for the first input capture */
+#define RVB_STATE_WAIT_IC2 (RVB_STATE_WAIT_IC + 1) /* 2nd input capture */
+#define RVB_STATE_START_CAPTURE 2 /* After waiting enough IC, starting capture */
+#define RVB_STATE_STOP_CAPTURE 3 /* We can compute a value */
+#define RVB_STATE_NEXT_COLOR 4 /* Selecting another color */
+#define RVB_STATE_WATCH 5 /* For debuging */
+
+/** RVB sensor state flag. */
+volatile uint8_t sensor_rvb_state_;
+/** Internal computed result for one color. */
+volatile uint16_t sensor_rvb_value_;
+/** Sensor used actually for computing value. */
+volatile uint8_t sensor_rvb_number_;
+/** Color of the actual sensor. */
+volatile uint8_t sensor_rvb_color_;
+/** Number of overflow interruption since last edge change. */
+uint8_t sensor_rvb_overflow_count_;
+/** Results table for 9 RVB sensors (RVCB) */
+volatile uint16_t sensor_rvb_values[RVB_MAX_SENSOR][4];
+/** Geting stats only for one sensors and disabling computing the others. */
+uint8_t sensor_rvb_enable;
+/** Count the number of IC before starting a good capture. */
+uint8_t sensor_rvb_ic_count_;
+
+/** Select a color :
+ * 0 : red ;
+ * 1 : blue ;
+ * 2 : clear ;
+ * 3 : green. */
+inline uint8_t
+sensor_rvb_color_select (uint8_t sensor_rvb_color)
+{
+ /* Check */
+ if (sensor_rvb_color > 3)
+ return 0;
+ /* Select the color */
+ PORTF = (PORTF & ~0xC0) | (sensor_rvb_color << 6);
+ /* Wait a little */
+ utils_delay_ns (RVB_SENSOR_DELAY_IO);
+ return 1;
+}
+
+/** Enable a RVB sensor. */
+inline uint8_t
+sensor_rvb_sensor_select (uint8_t sensor_rvb_num)
+{
+ switch (sensor_rvb_num)
+ {
+ case 1:
+ case 2:
+ case 3:
+ case 4:
+ case 5:
+ case 6:
+ case 7:
+ PORTC = ~_BV(sensor_rvb_num);
+ break;
+ case 8:
+ PORTD &= ~_BV(6);
+ break;
+ case 9:
+ PORTC = ~_BV(0);
+ break;
+ default:
+ /* High for disable */
+ PORTC = 0xff;
+ PORTD |= _BV(6);
+ /* Wait a little */
+ utils_delay_ns (RVB_SENSOR_DELAY_IO);
+ return 0;
+ }
+ /* Wait a little */
+ utils_delay_ns (RVB_SENSOR_DELAY_IO);
+ return 1;
+}
+
+/** Update everything for you. */
+void sensor_rvb_update (void);
+
+/** Initialisation of the RVB sensors module. */
+void
+sensor_rvb_init (void)
+{
+ /* We enable the system */
+ sensor_rvb_enable = 1;
+ /* No sensor selected */
+ sensor_rvb_sensor_select (0);
+ /* Put ENA* pins in output */
+ DDRC = 0xff;
+ DDRD |= _BV(6);
+ /* Put color selector pins in output */
+ DDRF |= _BV(S0RVB) | _BV(S1RVB);
+ /* Initialisation of counter :
+ * Noise canceler, 1 for prescaling */
+ TCCR1B |= _BV(ICNC1) | _BV(CS10);
+ /* XXX Fast PWM 10-bit */
+ TCCR1B |= _BV(WGM12);
+ TCCR1A |= _BV(WGM11) | _BV(WGM10);
+ /* XXX Overflow IR */
+ TIMSK |= _BV (TOIE1);
+ /* Nothing to do for the moment */
+ sensor_rvb_state_ = RVB_STATE_SLEEP;
+}
+
+/** Start updating RVBC values for sensors. */
+void
+sensor_rvb_start_capture (void)
+{
+ /* Are we already getting a sensor ? */
+ if (!sensor_rvb_state_ && sensor_rvb_enable)
+ {
+ sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
+ /* Start with sensor one */
+ sensor_rvb_number_ = 0;
+ /* Select it and launch capture */
+ sensor_rvb_update ();
+ }
+}
+
+/** Watch only one sensor. Usefull for debugging. *//*{{{*/
+void
+sensor_rvb_watch (uint8_t sensor_num, uint8_t sensor_col)
+{
+ // XXX Better approach.
+ /* Disable all interrupt that could be enabled */
+ TIMSK &= ~_BV (TICIE1);
+ TIMSK &= ~_BV (TOIE1);
+ /* Put ourself in off mode */
+ sensor_rvb_enable = 0;
+ sensor_rvb_state_ = RVB_STATE_WATCH;
+ /* Select the color */
+ sensor_rvb_color_select (sensor_rvb_color_ = sensor_col);
+ /* Disable sensors */
+ sensor_rvb_sensor_select (0);
+ /* Wait a little */
+ utils_delay_ns (RVB_SENSOR_DELAY_IO);
+ /* Select sensor here */
+ sensor_rvb_sensor_select (sensor_num + 1);
+ /* Wait for output of sensor to be ready */
+ utils_delay_ns (RVB_SENSOR_DELAY_IO);
+}
+/*}}}*/
+
+/** Update everything for you. */
+void
+sensor_rvb_update (void)
+{
+ if (sensor_rvb_state_ == RVB_STATE_NEXT_COLOR)
+ {
+ /* Select the color */
+ if (!sensor_rvb_color_select (++sensor_rvb_color_))
+ {
+ /* No more colors for this sensor, next sensor please */
+ sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
+ }
+ else
+ {
+ sensor_rvb_state_ = RVB_STATE_WAIT_IC;
+ /* Enable interrupt for IC1 and counter overflow */
+ TIMSK |= _BV (TICIE1);
+ }
+ }
+ if (sensor_rvb_state_ == RVB_STATE_NEXT_SENSOR)
+ {
+ /* Disable sensors */
+ sensor_rvb_sensor_select (0);
+ /* Select sensor */
+ if (!sensor_rvb_sensor_select (++sensor_rvb_number_))
+ {
+ /* Disable IC interrupt */
+ TIMSK &= ~_BV (TICIE1);
+ sensor_rvb_state_ = RVB_STATE_SLEEP;
+ return;
+ }
+ /* Start with first color */
+ sensor_rvb_color_select (sensor_rvb_color_ = 0);
+ sensor_rvb_state_ = RVB_STATE_WAIT_IC;
+ /* Enable interrupt for IC1 and counter overflow */
+ TIMSK |= _BV (TICIE1);
+ }
+}
+
+/** Timer 1 overflow. */
+SIGNAL (SIG_OVERFLOW1)
+{
+ if ((sensor_rvb_state_ == RVB_STATE_STOP_CAPTURE) &&
+ (++sensor_rvb_overflow_count_ == 4))
+ {
+ /* Disable IC interrupt */
+ TIMSK &= ~_BV (TICIE1);
+ /* Ask for next sensor */
+ sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
+ sensor_rvb_update ();
+ }
+}
+
+/** Interrupt on falling edge for Input Capture 1. */
+SIGNAL (SIG_INPUT_CAPTURE1)
+{
+ switch (sensor_rvb_state_)
+ {
+ case RVB_STATE_WAIT_IC:
+ sensor_rvb_ic_count_ = RVB_MAX_FALSE_IC;
+ sensor_rvb_state_ = RVB_STATE_WAIT_IC2;
+ case RVB_STATE_WAIT_IC2:
+ if (!--sensor_rvb_ic_count_)
+ /* Last ignored capture */
+ sensor_rvb_state_ = RVB_STATE_START_CAPTURE;
+ break;
+ case RVB_STATE_START_CAPTURE:
+ /* Save capture start */
+ sensor_rvb_value_ = ICR1;
+ /* New capture */
+ sensor_rvb_overflow_count_ = 0;
+ sensor_rvb_state_ = RVB_STATE_STOP_CAPTURE;
+ break;
+ case RVB_STATE_STOP_CAPTURE:
+ /* Disable IC interrupt */
+ TIMSK &= ~_BV (TICIE1);
+ /* Compute value */
+ sensor_rvb_value_ = ICR1 + sensor_rvb_overflow_count_ * TC1_TOP -
+ sensor_rvb_value_;
+ /* Save */
+ sensor_rvb_values[sensor_rvb_number_ - 1][sensor_rvb_color_] = sensor_rvb_value_;
+ sensor_rvb_state_ = RVB_STATE_NEXT_COLOR;
+ /* Next please */
+ sensor_rvb_update ();
+ break;
+ }
+}
+
diff --git a/n/es-2006/src/rvb_sensor.h b/n/es-2006/src/sensor_rvb.h
index cddf20d..c060f68 100644
--- a/n/es-2006/src/rvb_sensor.h
+++ b/n/es-2006/src/sensor_rvb.h
@@ -1,6 +1,6 @@
-#ifndef rvb_sensor_h
-#define rvb_sensor_h
-// rvb_sensor.h
+#ifndef sensor_rvb_h
+#define sensor_rvb_h
+// sensor_rvb.h
// es - Input/Output general purpose board. {{{
//
// Copyright (C) 2006 Dufour Jérémy
@@ -42,29 +42,29 @@ Connectors :
OUTRVB PORTD7/PORTD4(IC1)
S0RVB PORTF6
S1RVB PORTF7
+
+ Sensor actualy send us a frequency for a specific color. We have pins for
+ selecting.
*/
-/* Wonderful table of data */
-// XXX
-extern volatile uint16_t rvb_sensors_values [9][5];
+/** Define of the TOP of the timer 1. */
+#define TC1_TOP 0x03FF
+/** Maximum number of sensors we manage. */
+#define RVB_MAX_SENSOR 9
-/** Disable all sensors. */
-extern inline void rvb_sensors_disable (void)
-{
- /* High for disable */
- PORTC = 0xff;
- PORTD |= _BV(6);
-}
+/** Results table for 9 RVB sensors (RVCB) :
+ * Values are in the folowing order : Red, Blue, Clear, Green. */
+extern volatile uint16_t sensor_rvb_values [RVB_MAX_SENSOR][4];
+/** Geting stats only for one sensors and disabling computing the others. */
+extern uint8_t sensor_rvb_enable;
-/** Initialisation. */
-void rvb_sensors_init (void);
+/** Initialisation of the RVB sensors module. */
+void sensor_rvb_init (void);
-/** Start updating RVBC values for sensors. */
-void rvb_sensors_start_update (void);
+/** Start a capture RVBC values for sensors if we are not already doing one. */
+void sensor_rvb_start_capture (void);
-// XXX
-extern uint8_t sensor_disable;
-void
-rvb_sensor_watch (uint8_t sensor_num, uint8_t color_num);
+/** Watch only one sensor. Usefull for debugging. */
+void sensor_rvb_watch (uint8_t sensor_num, uint8_t color_num);
-#endif // rvb_sensor_h
+#endif // sensor_rvb_h