-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensorArray.cpp
More file actions
155 lines (122 loc) · 2.86 KB
/
SensorArray.cpp
File metadata and controls
155 lines (122 loc) · 2.86 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "SensorArray.h"
#include "Sensor.h"
#include <vector>
#include <Arduino.h>
#include <math.h>
using namespace std;
namespace LFRobot
{
SensorArray::SensorArray(const int pins[], const int nSensors, const long microsWhite[], const long microsBlack[], const long MICROS_TIMEOUT)
: nSensors(nSensors), MICROS_TIMEOUT(MICROS_TIMEOUT)
{
sensors = new Sensor*[nSensors];
sensorValues = new long[nSensors];
this->microsWhite = new long[nSensors];
this->microsBlack = new long[nSensors];
for (int i = 0; i < nSensors; i++)
{
sensors[i] = new Sensor(pins[i], 2.0f*i / (nSensors - 1.0f) - 1.0f);
Serial.println(sensors[i]->getPosition());
sensorValues[i] = 0.0f;
this->microsWhite[i] = microsWhite[i];
this->microsBlack[i] = microsBlack[i];
}
}
SensorArray::~SensorArray()
{
for (int i = 0; i < nSensors; i++)
{
delete sensors[i];
}
delete[] sensors;
delete[] sensorValues;
delete[] microsWhite;
delete[] microsBlack;
}
void SensorArray::prepSensors()
{
for (int i = 0; i < nSensors; i++)
{
sensors[i]->setMode(OUT);
}
delayMicroseconds(10);
}
void SensorArray::readSensorValues()
{
prepSensors();
long startTime;
int numSensorsFinished = 0;
startTime = micros();
for (int i = 0; i < nSensors; i++)
{
sensors[i]->setMode(IN);
sensors[i]->setRead(false);
}
while (numSensorsFinished < nSensors)
{
for (int i = 0; i < nSensors; i++)
{
if (!sensors[i]->isRead())
{
long endTime = micros();
if (sensors[i]->isLow() || endTime - startTime > MICROS_TIMEOUT) // if low, record time.
{
long lengthOfTime = endTime - startTime;
sensorValues[i] = MICROS_TIMEOUT * (lengthOfTime - microsWhite[i]) / (MICROS_TIMEOUT - microsWhite[i]);
//sensorValues[i] = lengthOfTime;
numSensorsFinished++;
sensors[i]->setRead(true);
}
}
}
}
}
float SensorArray::mapMicrosToValue(int i, long lenTime)
{
if (lenTime <= microsWhite[i])
{
return 0.0f;
}
else if (lenTime >= microsBlack[i])
{
return 1.0f;
}
else
{
return (float)(lenTime - microsWhite[i]) / (float)(microsBlack[i] - microsWhite[i]);
}
}
float SensorArray::getLineOffset()
{
readSensorValues();
float lineCenter = 0;
long totalSensorValue = 0;
for (int i = 0; i < nSensors; i++)
{
lineCenter += sensors[i]->getPosition() * sensorValues[i];
totalSensorValue += sensorValues[i];
}
lineCenter /= totalSensorValue;
if (totalSensorValue == 0)
{
lineCenter = 0;
}
float lineThickness = float(totalSensorValue) / MICROS_TIMEOUT / nSensors;
if(lineThickness <= MIN_LINE_VALUE || lineThickness >= .6f)
{
if (lineData.getAverage() > 0)
{
lineCenter = 2;
}
else
{
lineCenter = -2;
}
}
else {
lineData.push(lineCenter * lineThickness);
}
Serial.println(lineCenter);
return lineCenter;
}
}