-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineSensor.h
97 lines (75 loc) · 2.5 KB
/
LineSensor.h
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
#ifndef LineSensor_h
#define LineSensor_h
#include "Arduino.h"
class LineSensor {
private:
byte n_sensors;
byte *pins;
unsigned int *sensors_values;
public:
// Constructor
LineSensor(byte *pins, byte n_sensors){
this->n_sensors = n_sensors;
// Alloc the memory for pins array
if (this->pins == 0) {
this->pins = (byte*)malloc(sizeof(byte)*this->n_sensors);
if (this->pins == 0)
return;
}
// Alloc the memory for pins array
this->sensors_values = (unsigned int*)malloc(sizeof(unsigned int)*this->n_sensors);
if (this->sensors_values == 0)
return;
// Save the pins
for (int i = 0; i < this->n_sensors; i++)
this->pins[i] = pins[i];
// Set up the pins
for (int i = 0; i < this->n_sensors; i++){
pinMode(this->pins[i], INPUT);
}
}
// Read sensors and update values
void readSensors() {
// Read analog chanels (Sensors)
for (int i = 0; i < n_sensors; i++){
// Read Sensor
int reading = analogRead(i+1);
// Constrains the value to be within a range of [0, 1000].
sensors_values[i] = reading;//constrain(reading, 0, 1000);
}
}
unsigned long getLine(bool white_line){
unsigned char i, on_line = 0;
unsigned long avg = 0; // this is for the weighted total, which is long
// before division
unsigned int sum = 0; // this is for the denominator which is <= 64000
static int last_value = 0;
readSensors();
for(i = 0; i < n_sensors; i++) {
int value = sensors_values[i];
// Adjust the value for a white line
if(white_line)
value = 1000 - value;
// Keep track of whether we see the line at all
if(value > 200) {
on_line = 1;
}
// Only average in values that are above a noise threshold
if(value > 50) {
avg += (long)(value) * (i * 1000);
sum += value;
}
}
if(! on_line) {
// If it last read to the left of center, return 0.
if(last_value < (n_sensors - 1) * 1000 / 2)
return 0;
// If it last read to the right of center, return the max.
else
return (n_sensors - 1) * 1000;
}
last_value = avg/sum;
return last_value;
}
};
#endif