-
Notifications
You must be signed in to change notification settings - Fork 0
/
owmr.ino
165 lines (148 loc) · 5.17 KB
/
owmr.ino
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
156
157
158
159
160
161
162
163
164
165
#define PIN_LASER 3
#define PIN_LED LED_BUILTIN
#define PIN_SENSOR A6
#define PIN_PULSE_OUTPUT 10
// Qualitätsstandards
#define OFFSET_LIMIT 950 // Reading bei der Dunkelmessung, das nicht unterschritten werden darf
#define MIN_MEASUREMENT 25 // Mindestwert einer gültigen Vollmessung (Hellmessung abzüglich Dunkelmessung)
#define MAX_VARIANCE 30 // Max. tolerierte Fluktuation bei der Dunkelmessung
#define MIN_TRAINING_BANDWIDTH 30 // Mindest-Bandbreite zwischen höchster und niedrigster Reflektion im Zyklus
#define TRAINING_CYCLES 3 // Anzahl der Zyklen beim Training
#define STATE_UNTRAINED 0
#define STATE_TRAINING 1
#define STATE_TRAINED 2
#define CYCLE_UNKNOWN 0
#define CYCLE_RISING 1
#define CYCLE_FALLING 2
int state = STATE_UNTRAINED;
uint16_t cycleMax = 0;
uint16_t cycleMin = 1023;
uint8_t cyclePosition = CYCLE_UNKNOWN;
uint8_t trainedCycles = 0;
uint16_t measurementLoopCounter = 0;
float measurementAvgSum = 0;
uint16_t measurementAvgCount = 0;
float offsetAvgSum = 0;
uint16_t offsetMin = 1023;
uint16_t offsetMax = 0;
uint16_t ledBlink = 0;
uint16_t ledBlinkInterval = 0;
void setup() {
Serial.begin(9600);
pinMode(PIN_LASER, OUTPUT);
pinMode(PIN_PULSE_OUTPUT, OUTPUT);
pinMode(PIN_LED, OUTPUT);
}
void loop() {
measurementLoopCounter++;
if(measurementLoopCounter >= 500) {
int offset = analogRead(PIN_SENSOR);
if(offset > offsetMax) offsetMax = offset;
if(offset < offsetMin) offsetMin = offset;
offsetAvgSum += offset;
digitalWrite(PIN_LASER, true);
delay(10);
int reading = offset - analogRead(PIN_SENSOR);
if(reading < 0) reading = 0;
digitalWrite(PIN_LASER, false);
measurementLoopCounter = 0;
measurementAvgSum += reading;
measurementAvgCount++;
}
if(measurementAvgCount >= 10) {
float measurementAvg = measurementAvgSum / measurementAvgCount;
float offsetAvg = offsetAvgSum / measurementAvgCount;
uint16_t variance = offsetMax - offsetMin;
bool cycleCounterActive = false;
measurementAvgCount = 0;
measurementAvgSum = 0;
offsetAvgSum = 0;
offsetMin = 1023;
offsetMax = 0;
Serial.print(F("Messung: "));
Serial.print(measurementAvg);
Serial.print(F("\tOffset:"));
Serial.print(offsetAvg);
Serial.print(F("\tVariance:"));
Serial.print(variance);
Serial.print(F("\tStatus: "));
if(offsetAvg < OFFSET_LIMIT || variance > MAX_VARIANCE) { // Signal der Dunkelmessung zu stark oder zu unstet
if(measurementAvg < MIN_MEASUREMENT) {
Serial.print(F("Reflektion zu schwach!"));
} else {
Serial.print(F("Zu viel Streulichteinfall!"));
}
state = STATE_UNTRAINED;
ledBlinkInterval = 20000;
} else if(state == STATE_UNTRAINED) {
cycleMax = 0;
cycleMin = 1023;
trainedCycles = 0;
cyclePosition = CYCLE_UNKNOWN;
Serial.print(F("Nicht kalibriert!"));
state = STATE_TRAINING;
ledBlinkInterval = 15000;
} else if(state == STATE_TRAINING) {
if(measurementAvg < cycleMin) cycleMin = measurementAvg;
if(measurementAvg > cycleMax) cycleMax = measurementAvg;
Serial.print(F("Kalibriere... Q="));
float quality = round(((float)(cycleMax - cycleMin) / MIN_TRAINING_BANDWIDTH) * 100);
Serial.print(quality);
Serial.print(F("%, C="));
Serial.print(trainedCycles);
Serial.print('/');
Serial.print(TRAINING_CYCLES);
if(cycleMax - cycleMin >= MIN_TRAINING_BANDWIDTH) {
ledBlinkInterval = 5000;
cycleCounterActive = true;
} else {
ledBlinkInterval = 10000;
}
if(trainedCycles >= TRAINING_CYCLES) state = STATE_TRAINED;
} else if(state == STATE_TRAINED) {
cycleCounterActive = true;
ledBlinkInterval = 0;
Serial.print(F("OK"));
}
if(cycleCounterActive) {
uint16_t lowerThreshold = cycleMin + (MIN_TRAINING_BANDWIDTH / 3);
uint16_t upperThreshold = cycleMax - (MIN_TRAINING_BANDWIDTH / 3);
Serial.print('\t');
Serial.print(lowerThreshold);
Serial.print('\t');
Serial.print(upperThreshold);
if(cyclePosition == CYCLE_UNKNOWN && measurementAvg > upperThreshold) {
cyclePosition = CYCLE_RISING;
} else if(cyclePosition == CYCLE_FALLING && measurementAvg > upperThreshold) {
cyclePosition = CYCLE_RISING;
digitalWrite(PIN_PULSE_OUTPUT, false);
} else if(cyclePosition == CYCLE_UNKNOWN && measurementAvg < lowerThreshold){
cyclePosition = CYCLE_FALLING;
} else if(cyclePosition == CYCLE_RISING && measurementAvg < lowerThreshold) {
cyclePosition = CYCLE_FALLING;
if(state == STATE_TRAINING) {
trainedCycles++;
}
digitalWrite(PIN_PULSE_OUTPUT, true);
}
if(cyclePosition == CYCLE_RISING) {
Serial.print(F(" /"));
} else if(cyclePosition == CYCLE_FALLING) {
Serial.print(F(" \\"));
}
}
Serial.println("");
}
if(ledBlinkInterval == 0) {
digitalWrite(PIN_LED, cyclePosition == CYCLE_FALLING);
ledBlink = 0;
} else {
ledBlink++;
if(ledBlink > ledBlinkInterval) {
ledBlink = 0;
digitalWrite(PIN_LED, false);
} else if(ledBlink > (ledBlinkInterval / 2)) {
digitalWrite(PIN_LED, true);
}
}
}