1
1
/*
2
2
3
- Perceptron example
3
+ Perceptron example
4
4
5
- Hardware: Arduino Nano BLE Sense
5
+ Hardware: Arduino Nano BLE Sense
6
6
7
- Usage: Follow the prompts in the Serial Monitor and show two objects of different colors to the color sensor onboard the Arduino.
8
- The sketch will train a perceptron using these examples, and then classify objects you show it in the future.
7
+ Usage: Follow the prompts in the Serial Monitor and show two objects of different colors to the color sensor onboard the Arduino.
8
+ The sketch will train a perceptron using these examples, and then classify objects you show it in the future.
9
+
10
+ The color sensor works better in a well lit area
9
11
10
12
*/
11
13
12
14
#include < Arduino_APDS9960.h>
13
15
#include < Arduino_Perceptron.h>
14
16
15
- const int NUM_INPUTS = 3 ; // Classifier input is color sensor data; red , green and blue levels
17
+ const int NUM_INPUTS = 3 ; // Red , green and blue
16
18
const int CLASSES = 2 ; // The perceptron only has 2 possible classes
17
- const int EXAMPLES = 30 ; // Number of samples for each object
18
- const int MAX_EPOCHS = 100 ; // Maximum training iterations
19
+ const int EXAMPLES = 30 ; // Number of samples for each object
20
+ const int NUM_EPOCHS = 100 ; // Maximum training iterations
19
21
const float LEARNING_RATE = 0.001 ; // Perceptron learning rate
20
22
const int THRESHOLD = 5 ; // Color sensor light threshold
21
23
22
- float color[NUM_INPUTS];
24
+ float input[NUM_INPUTS];
25
+ float weights[NUM_INPUTS+1 ]; // Weights for each color input + the bias
23
26
String label[CLASSES] = {" object A" , " object B" };
24
27
25
28
Perceptron perceptron (NUM_INPUTS, LEARNING_RATE);
@@ -29,29 +32,26 @@ void setup() {
29
32
while (!Serial);
30
33
31
34
if (!APDS.begin ()) {
32
- Serial.println (" Failled to initialized APDS!" );
35
+ Serial.println (" Failed to initialize APDS color sensor !" );
33
36
while (1 );
34
37
}
35
38
36
39
Serial.println (" Arduino perceptron" );
37
40
38
- // Get color samples for our two objects
39
- // -------------------------------------
40
- for (int output = 0 ; output < 2 ; output ++) {
41
+ // Get example color data
42
+ // ----------------------
43
+ for (int thisClass = 0 ; thisClass <= 1 ; thisClass ++) {
41
44
42
45
Serial.print (" Show me " );
43
- Serial.println (label[output ]);
46
+ Serial.println (label[thisClass ]);
44
47
45
48
// Wait for the object to move away again
46
49
while (!APDS.proximityAvailable () || APDS.readProximity () == 0 ) {}
47
50
51
+ // Get n color samples from the object
48
52
for (int n = 0 ; n < EXAMPLES; n++) {
49
-
50
- // Sample object color - perceptron input
51
- readColor (color);
52
-
53
- // Add example color to the perceptron training set
54
- perceptron.addExample (color, output);
53
+ readColor (input); // red, green and blue
54
+ perceptron.addExample (input, thisClass); // Add to training set
55
55
}
56
56
}
57
57
@@ -64,23 +64,21 @@ void setup() {
64
64
Serial.print (" Start weights:" );
65
65
printWeights ();
66
66
67
- while ( epoch < MAX_EPOCHS && accuracy < 0.99 ) {
67
+ for ( int epoch = 0 ; epoch < NUM_EPOCHS; epoch++ ) {
68
68
accuracy = perceptron.train ();
69
- epoch++;
70
69
Serial.print (" Accuracy: " );
71
70
Serial.println (accuracy);
72
71
}
73
72
74
73
Serial.print (" End weights:" );
75
74
printWeights ();
76
75
77
-
78
76
Serial.println (" Training complete\n " );
79
77
}
80
78
81
79
82
- // Classify objects shown
83
- // ----------------------
80
+ // Classify objects shown
81
+ // ----------------------
84
82
void loop () {
85
83
86
84
int output;
@@ -90,52 +88,47 @@ void loop() {
90
88
91
89
Serial.println (" Let me guess your object" );
92
90
93
- // Wait for an object then read its color
94
- readColor (color);
91
+ readColor (input);
95
92
96
- // CLassify the object using the perceptron
97
- output = perceptron.classify (color);
93
+ output = perceptron.classify (input);
98
94
99
95
Serial.print (" You showed me " );
100
96
Serial.println (label[output]);
101
97
}
102
98
103
99
104
- void printWeights () {
105
- float weights[NUM_INPUTS];
106
- perceptron.getWeights (weights);
107
-
108
- Serial.print (weights[0 ]);
109
- Serial.print (" ," );
110
- Serial.print (weights[1 ]);
111
- Serial.print (" ," );
112
- Serial.println (weights[2 ]);
113
- }
114
-
115
100
void readColor (float color[]) {
116
- int red, green, blue, proximity, colorTotal = 0 ;
117
-
118
- // Wait until we have a color bright enough
119
- while (colorTotal < THRESHOLD) {
101
+ int red, green, blue;
120
102
121
- // Sample if color is available and object is close
103
+ // Loop until we have a color sample bright enough
104
+ while (red+green+blue < THRESHOLD) {
105
+ // Sample if color sensor is available and the object is close
122
106
if (APDS.colorAvailable () && APDS.proximityAvailable () && APDS.readProximity () == 0 ) {
123
-
124
- // Read color and proximity
125
107
APDS.readColor (red, green, blue);
126
- colorTotal = (red + green + blue);
127
108
}
128
109
}
129
110
130
- // Normalise the color sample data and put it in the classifier input array
131
- color[0 ] = (float )red / colorTotal ;
132
- color[1 ] = (float )green / colorTotal ;
133
- color[2 ] = (float )blue / colorTotal ;
111
+ // Put color sample data in input array
112
+ color[0 ] = (float )red;
113
+ color[1 ] = (float )green;
114
+ color[2 ] = (float )blue;
134
115
135
116
// Print the red, green and blue values
136
- Serial.print (color[0 ]);
137
- Serial.print (" ," );
138
- Serial.print (color[1 ]);
139
- Serial.print (" ," );
140
- Serial.println (color[2 ]);
117
+ printArray (color,NUM_INPUTS);
118
+ }
119
+
120
+
121
+ void printWeights () {
122
+ perceptron.getWeights (weights,NUM_INPUTS+1 ); // input weights plus the bias
123
+ printArray (weights,NUM_INPUTS+1 );
124
+ }
125
+
126
+
127
+ void printArray (float myArray[], int length) {
128
+ Serial.print (myArray[0 ]);
129
+ for (int n = 1 ; n < length; n++) {
130
+ Serial.print (" ," );
131
+ Serial.print (myArray[n]);
132
+ }
133
+ Serial.println ();
141
134
}
0 commit comments