-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoil_20_1364.ino
353 lines (273 loc) · 7.66 KB
/
coil_20_1364.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
---------------------------------------------------------------------
1364 uH coil
Diameter: 20cm
Inductance: 1364
Turns: 58
Wire: 0.5mm copper
Damping resistor ~
---------------------------------------------------------------------
*/
#include "coil_20_1364.h"
#include "voice.h"
// target sense calculation happens if above this threshold
//
#define THRESHOLD (20)
double COIL_20_1364::soundSignal()
{
double tempF = 0.0;
tempF += (averages[0]-longAverages[0]); // 33uSEC
soundSignalValue = tempF;
tempF = averages[4]-longAverages[4];
soundSignalConductivity = tempF / soundSignalValue;
return(soundSignalValue); // strongest response for particular coil
}
//--------------------------------------------------------------------------------------
// called after each set of samples, i.e at ~ 10Hz
//
//--------------------------------------------------------------------------------------
void COIL_20_1364::targetSense()
{
static double maxF = 0;
double tempF = 0;
double S1 ;
double S2 ;
tempF = soundSignalValue;
if(FLAG_TARGET_ID)
{
timerTargetID++;
// we need to have held over target for at least '10' counts
if(timerTargetID < 10 )
{
}
else
{
// we exit FLAG_TARGET_ID state when the signal falls below the locked average
// looking at the earliest time-sample only
// i.e we moved off the target
//
if ((averages[0] - lockedAverage[0]) < 0)
{
timerTargetID = 0;
FLAG_TARGET_ID = false;
}
// we are holding above the target and taking a long sample to get accurate ID
// stay here until we drop below running average, OR we time-out
//
for(int i = 0 ; i < TIME_POINTS; i++)
{
lockedSignal[i][lockedSignalCount] = (averages[i] - lockedAverage[i]);
}
lockedSignalCount++;
if(lockedSignalCount > (ID_SET-1))
{
// We held on target until we collected a complete set of samples
// time to do target ID
//
targetDiscrimination();
FLAG_TARGET_ID = false;
// reset the locked signal
//
for(int i = 0; i < TIME_POINTS; i++)
{
lockedSignal[TIME_POINTS][0] = 0;
}
lockedSignalCount = 0;
}
}
}
// signal ?
//
if(tempF > THRESHOLD) // above our threshold ?
{
//targetID = NO_TARGET; // reset
if(tempF > maxF)
{
maxF = tempF;
// going up (signal is increasing)
// store the average signal (i.e our reference)
//
if ( !FLAG_TARGET_ID) // not in ID mode yet
{
// Set the Target ID flag 'we are in target ID mode'
//
FLAG_TARGET_ID = true;
for(int i = 0; i < TIME_POINTS; i++)
{
lockedAverage[i] = longAverages[i];
}
lockedSignalCount = 0; // new set of samples
// Serial.println("*");
// reset the targetID count
//
timerTargetID = 0;
}
}
}
else
{
if(tempF < 0)
{
maxF = 0; // reset the peak
}
}
}
void COIL_20_1364::targetDiscrimination()
{
double tempF;
double tempF2;
int tempIndex ;;
lockedSignalCount = 0;
for(int i = 0 ; i < TIME_POINTS; i++)
{
// calculate averages
//
tempF = 0;
for(int i2 = 0 ; i2 < ID_SET; i2++)
{
tempF += lockedSignal[i][i2] ;
}
tempF /= ID_SET;
signalCurve[i] = tempF;
//Serial.println(lockedSignal[i]);
}
//normalise(signalCurve, TIME_POINTS, TIME_POINTS-1, 0);
// this ratio is our conductivity (slope or speed of the discharge curve)
//
conductivity = signalCurve[4] / signalCurve[0] ;
tempF = signalCurve[7];
tempF += signalCurve[8];
tempF += signalCurve[9];
tempF /= 3.0;
iron = tempF / signalCurve[4];
// caluclate the IRON number using the IRON table
//
// Is our actual value above or below this ?
// If above, then is iron
//
tempF = getIRONValue(conductivity);
Serial.print(conductivity);
Serial.print(",");
Serial.print(iron);
Serial.print(",");
iron -= tempF;
Serial.println(iron); // is it above (+iron) or below (-noniron) the line ?
/*
Serial.print("expected iron:" );
Serial.println(tempF);
*/
// Discrimination algorithm
//
TARGET_SENSE::targetID = OK_BIG;
if(iron > 0)
{
if((conductivity < 0.77) && (conductivity > 0.50))
{
TARGET_SENSE::targetID = Fe;
}
}
if(TARGET_SENSE::targetID == Fe )
{
}
else
{
// non iron target, ring the Bell
//
queueSound(BELL);
}
queueNumber(conductivity);
startSounds();
//Serial.println(TARGET_SENSE::targetID);
#ifdef USB_SERIAL_ENABLED
// Serial.print(sampleArray[SAMPLE_COUNT_MAX-1]); // print the last raw reading for offset adjustment
// Serial.println();
#endif
}
// called repeatedly every sample, i.e at up to 500Hz,
// must be efficient
//
void COIL_20_1364::doSampleAveraging()
{
static int averageCount = 0;
double tempF;
int index;
double oldSample;
// reference sample
//
uint16_t lastSample = sampleArray[SAMPLE_COUNT_MAX-1];
for(index = 0 ; index < TIME_POINTS; index++)
{
oldSample = samples[index][averageCount];
samples[index][averageCount] = sampleArray[( INDEX_30uSEC + index )]; // new raw sample from the set of samples
//samples[index][averageCount] -= lastSample;
sums[index] -= oldSample; // subtract old value from the sum
sums[index] += samples[index][averageCount]; // add the new value to the sum
}
// recalculate the running averages
//
for(index = 0; index < TIME_POINTS; index++)
{
averages[index] = (sums[index] / SAMPLE_BUFFER_LENGTH);
}
averageCount++;
if(averageCount >= SAMPLE_BUFFER_LENGTH)
{
// full set of samples complete
//
averageCount = 0;
for(index = 0 ; index < TIME_POINTS; index++)
{
// re-calculate long averages
//
tempF = (averages[index] - longAverages[index]);
tempF /= LONG_AVERAGE_FACTOR;
longAverages[index] += tempF;
// fast recovery, after the average has 'wound-up' due to being held on a target.
// i.e The average follows the signal down quicker than it follows the signal up.
//
if(tempF < 0)
{
// add it again
//
longAverages[index] += tempF;
}
}
}
}
// use the ratio to look up a value in the iron table
//
float COIL_20_1364::getIRONValue(float ratio)
{
int index1 ;
int index2 ;
float tempF ;
float v1;
float v2;
if(ratio >= 1)
{
return(0); // invalid
}
if (ratio < 0)
{
return(0); // invalid
}
// convert the value of 0.00 and 1.00 to an index between 0 and 10
//
tempF = ratio * 10;
index1 = (int)tempF;
index2 = index1 + 1;
// lookup the two values in the table and interpolate between them
//
v2 = IRON_TABLE[index2];
v1 = IRON_TABLE[index1];
v2 -= v1;
tempF = tempF - (float)index1; // the remainder
tempF /- 10.0;
tempF *= v2;
tempF += v1;
return(tempF);
}
void COIL_20_1364::debug()
{
Serial.println(averages[3]-longAverages[3]);
}