#ifndef linesensor_c #define linesensor_c /* linesensor.c */ /* linefol - Line follower robot on a ATmega128. {{{ * * Copyright (C) 2004 Pierre Prot * * 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 a line ( ~= width of the line). */ static int8_t lineActiveBits; /* +AutoDec */ /** 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_print (void); /* -AutoDec */ /** Poll linesensor, should be called as often as possible. */ static inline void linesensor_poll (void) { uint8_t c, a, i, sum; c = PINC | 0x01; a = PINA | 0x80; 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; } if(lineActiveBits != 0) // 2(sum/lineActiveBits - 7.5) (sum is [0,15]) linepos = (2 * sum) / lineActiveBits - 15; else linepos = 0; } /** Poll linesensor and returns the state on the RS232 output. * For test, calibration and verification. */ static inline void linesensor_print (void) { uint8_t c, a, i; c = PINC | 0x01; a = PINA | 0x80; linesensor_poll (); 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 (' '); proto_send1('P',(linepos + 15)); } #endif // linesensor_c