summaryrefslogtreecommitdiff
path: root/n/line-follower
diff options
context:
space:
mode:
Diffstat (limited to 'n/line-follower')
-rw-r--r--n/line-follower/src/linesensor.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/n/line-follower/src/linesensor.c b/n/line-follower/src/linesensor.c
new file mode 100644
index 0000000..8daf00f
--- /dev/null
+++ b/n/line-follower/src/linesensor.c
@@ -0,0 +1,114 @@
+/* linesensor.c */
+/* linefol - Line follower robot on a ATmega128. {{{
+ *
+ * Copyright (C) 2004 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2005.
+ * 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.
+ *
+ * }}} */
+
+/** The line position between -8 and +8. */
+static int8_t linepos;
+/** The number of bits seeing the line. */
+static int8_t lineActiveBits;
+
+/* +AutoDec */
+
+/** Initialize the line sensor. */
+static inline void
+linesensor_init (void);
+
+/** Poll linesensor, should be called as often as possible. */
+static inline void
+linesensor_poll (void);
+
+/** Poll linesensor and returns the state on the RS232 output.
+ * For test, calibration and verification. */
+static inline void
+linesensor_poll_human (void);
+
+/* -AutoDec */
+
+/** Initialize the line sensor. */
+static inline void
+linesensor_init (void)
+{
+ linepos=0;
+ sei ();
+}
+
+/** Poll linesensor, should be called as often as possible. */
+static inline void
+linesensor_poll (void)
+{
+ uint8_t c, a, i, sum;
+ c = PINC;
+ a = PINA;
+ sum=0;
+ lineActiveBits = 0;
+
+ for (i = 0; i < 8; i++)
+ {
+ if (!(c & 1))
+ {
+ sum += i;
+ lineActiveBits += 1;
+ }
+ c >>= 1;
+
+ if (!(a & 1))
+ {
+ sum += (i+8);
+ lineActiveBits += 1;
+ }
+ a >>= 1;
+ }
+
+ linepos = sum / lineActiveBits;
+}
+
+/** Poll linesensor and returns the state on the RS232 output.
+ * For test, calibration and verification. */
+static inline void
+linesensor_poll_human (void)
+{
+ uint8_t c, a, i;
+ c = PINC;
+ a = PINA;
+
+ for (i = 0; i < 8; i++)
+ {
+ if (!(c & 1))
+ rs232_putc ('#');
+ else
+ rs232_putc ('.');
+ c >>= 1;
+ }
+ rs232_putc (' ');
+ for (i = 0; i < 8; i++)
+ {
+ if (!(a & 1))
+ rs232_putc ('#');
+ else
+ rs232_putc ('.');
+ a >>= 1;
+ }
+ rs232_putc ('\r');
+}
+