summaryrefslogtreecommitdiff
path: root/n/line-follower/src/linesensor.c
blob: 30d70fd14ded0d9bbab9cb8503d2b766e0f22751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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