beautypg.com

E. 3pi simplified schematic diagram – Pololu 3pi Robot User Manual

Page 19

background image

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;

last_time += 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;

}

}

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), the loop

ends, and the time values are returned.

5.e. 3pi Simplified Schematic Diagram

A full understanding of how your 3pi works cannot be achieved without first understanding its schematic diagram:

Pololu 3pi Robot User's Guide

© 2001–2014 Pololu Corporation

5. How Your 3pi Works

Page 19 of 63