-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtarget_sense.ino
98 lines (79 loc) · 1.61 KB
/
target_sense.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
/*
--------------------------------------------------------------------
wombatpi.net
target_sense.ino
Target ID and discrimination
Modified 01-Jun-2024
--------------------------------------------------------------------
*/
#include "target_sense.h"
#define MIN_FLOAT (-32000.0)
#define MAX_FLOAT (32000.0)
// normalise the array to values 1.0 to 0.0
// Quickest, used on curves where the min and max are known.
//
void normalise(double arr[], int sz, int minIndex, int maxIndex)
{
double min = arr[minIndex];
double max = arr[maxIndex];
double range;
// normalise
//
range = max-min;
if(range == 0)
{
range = 1.0; // catch divide-by-zero
}
for(int i = 0 ; i < sz; i++)
{
arr[i] -= min;
arr[i] /= range;
}
}
// normalise the array to values 1.0 to 0.0
// used on unsorted curves where the min and max are not obvious and unknown
// i.e the Target-shape curve
//
void normalise(double arr[], int sz)
{
double min = MAX_FLOAT;
double max = MIN_FLOAT;
double range;
// find max and min
//
for(int i = 0 ; i < sz; i++)
{
if(min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
}
// normalise
//
range = max-min;
if(range == 0)
{
range = 1.0; // catch divide-by-zero
}
for(int i = 0 ; i < sz; i++)
{
arr[i] -= min;
arr[i] /= range;
}
}
// Debugging utility
//
void printArray(double arr[], int sz, double multiplier)
{
for(int i = 0; i < sz-1; i++)
{
Serial.println(arr[i] * multiplier);
// Serial.print(",");
}
Serial.println(arr[sz-1] * multiplier);
Serial.println() ;
}