-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineTracker.cpp
More file actions
64 lines (57 loc) · 1.74 KB
/
LineTracker.cpp
File metadata and controls
64 lines (57 loc) · 1.74 KB
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
/** Abstraction for the Pololu Line sensor to detect
* where the line is in relation to the sensor, intersections,
* and if it is centered on the line
* @author Jordan Burklund
* @author Hans Johnson
* @date Sept. 2015
**/
#include "LineTracker.h"
LineTracker::LineTracker() {}
/**
* Computes the distance that the approximate distance that the line is away from the center of the line sensor in 1/16ths of an inch.
* @return Float error in 1/16ths of an inch
**/
float LineTracker::lineError () {
int s0 = analogRead(0);
int s1 = analogRead(1);
int s2 = analogRead(2);
int s3 = analogRead(3);
int s4 = analogRead(4);
int s5 = analogRead(5);
int s6 = analogRead(6);
int s7 = analogRead(7);
//Compute the weigted sum to find the position
return (m1*(s7-s0)+m2*(s6-s1)+m3*(s5-s2)+m4*(s4-s3))/(s0+s1+s2+s3+s4+s5+s6+s7);
}
/** Detects if the sensor is at an instersection
* @return True if 3 or more sensors detect a line
**/
bool LineTracker::isAtCross () {
//Read all of the sensors
int values[8];
values[0] = analogRead(0);
values[1] = analogRead(1);
values[2] = analogRead(2);
values[3] = analogRead(3);
values[4] = analogRead(4);
values[5] = analogRead(5);
values[6] = analogRead(6);
values[7] = analogRead(7);
//Sum all of the sensors that detect a line
int numSensorsOnLine = 0;
for(int i=0; i<8; i++) {
if(values[i] > 500) {
numSensorsOnLine++;
}
}
//Sensor is on the line if 3 or more detect a line
return numSensorsOnLine >= 3;
}
/** Check if either of the two center sensors are on the line
* @return True if the center of the sensor is on the line
**/
bool LineTracker::centerOnLine () {
int s3 = analogRead(3);
int s2 = analogRead(2);
return (s3 > 500) || (s2 > 500);
}