The length of time that the digital input stays at 1 is very short when over white, and very long
when over black. The function
read_line_sensors()
in the Pololu AVR Library switches the port as
described above and returns the time for each of the five sensors. Here is a simplified version of the
code that reads the sensors:
This piece of code is found in the file
src\PololuQTRSensors\PololuQTRSensors.cpp
. The code makes
use of timer TCNT2, which is a special register in the AVR that we have configured to count up
continuously, incrementing every 0.4 μs. Basically, the code waits until one of the sensors changes
value, counting up the elapsed time in the variable
time
. (It is important to use a separate variable for
the elapsed time since the timer TCNT2 periodically overflows, dropping back to zero.) Upon detecting
a transition from a 1 to a 0 on one of the sensors (by measuring a change in the input port PINC), the
code determines which sensor changed and records the time in the array
sensor_values[i]
. After the
time limit
_maxValue
is reached (this is set to 2000 by default on the 3pi, corresponding to 800 μs),
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
time
= 0;
last_time = TCNT2;
while
(
time
< _maxValue)
{
// Keep track of the total time.
// This implicity casts the difference to unsigned char, so
// we don't add negative values.
unsigned
char
delta_time = TCNT2 - last_time;
time
+= delta_time;
las= delta_time;
// continue immediately if there is no change
if
(PINC == last_c)
continue
;
// save the last observed values
last_c = PINC;
// figure out which pins changed
for
(i = 0; i < _numSensors; i++)
{
if
(sensor_values[i] == 0 && !(*_register[i] & _bitmask[i]))
sensor_values[i] =
time
;
}
}
Pololu 3pi Robot User’s Guide
© 2001–2019 Pololu Corporation
5. How Your 3pi Works
Page 22 of 85
Содержание 0J5840
Страница 24: ...Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 5 How Your 3pi Works Page 24 of 85...
Страница 67: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 67 of 85...
Страница 77: ...Source code Pololu 3pi Robot User s Guide 2001 2019 Pololu Corporation 10 Expansion Information Page 77 of 85...