From 9e380155a787100998f78170892556efc10991b1 Mon Sep 17 00:00:00 2001 From: prot Date: Sat, 11 Dec 2004 17:35:29 +0000 Subject: Added : init, poll and poll_human --- n/line-follower/src/linesensor.c | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 n/line-follower/src/linesensor.c 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'); +} + -- cgit v1.2.3