Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed NeuroShield
Diff: main.cpp
- Revision:
- 2:995d7426e3ba
- Parent:
- 1:2d0abf41b7a3
--- a/main.cpp Thu Jan 25 02:37:42 2018 +0000
+++ b/main.cpp Tue Feb 11 00:55:05 2020 +0000
@@ -1,10 +1,10 @@
/******************************************************************************
* NM500 NeuroShield Board SimpleScript
* Simple Test Script to understand how the neurons learn and recognize
- * revision 1.1.3, 01/03, 2018
+ * revision 1.1.5, 2020/02/11
* Copyright (c) 2017 nepes inc.
*
- * Please use the NeuroShield library v1.1.3 or later
+ * Please use the NeuroShield library v1.1.4 or later
******************************************************************************/
#include "mbed.h"
@@ -19,16 +19,42 @@
DigitalOut sdcard_ss(D6); // SDCARD_SSn
DigitalOut arduino_con(D5); // SPI_SEL
+uint8_t vector[NEURON_SIZE];
+uint16_t vector16[NEURON_SIZE];
+uint16_t dists[READ_COUNT], cats[READ_COUNT], nids[READ_COUNT];
+uint16_t response_nbr, norm_lsup = 0;
+uint16_t fpga_version;
+
+void displayNeurons()
+{
+ uint16_t nm_ncr, nm_aif, nm_cat;
+ uint16_t ncount = hnn.getNcount();
+ printf("Display the neurons, ncount = %d\n", ncount);
+ uint16_t temp_nsr = hnn.getNsr();
+ hnn.setNsr(0x0010);
+ hnn.resetChain();
+ for (int i = 1; i <= ncount; i++) {
+ nm_ncr = hnn.getNcr();
+ hnn.readCompVector(vector16, VECTOR_LENGTH);
+ nm_aif = hnn.getAif();
+ nm_cat = hnn.getCat();
+
+ printf("neuron#%d \tvector=", i);
+ for (int j = 0; j < VECTOR_LENGTH; j++) {
+ printf("%d, ", vector16[j]);
+ }
+ if (nm_cat & 0x8000) {
+ printf(" \tncr=%d \taif=%d \tcat=%d (degenerated)\n", nm_ncr, nm_aif, (nm_cat & 0x7FFF));
+ }
+ else {
+ printf(" \tncr=%d \taif=%d \tcat=%d\n", nm_ncr, nm_aif, nm_cat);
+ }
+ }
+ hnn.setNsr(temp_nsr);
+}
+
int main()
{
- int i, j;
- uint8_t value;
- uint8_t vector[NEURON_SIZE];
- uint16_t vector16[NEURON_SIZE];
- uint16_t ncr, cat, aif, ncount, minif, response_nbr, norm_lsup = 0;
- uint16_t dists[READ_COUNT], cats[READ_COUNT], nids[READ_COUNT];
- uint16_t fpga_version;
-
arduino_con = LOW;
sdcard_ss = HIGH;
wait(0.5);
@@ -44,14 +70,14 @@
else {
printf("\n\n#### Unknown Board (Board v%d.0 / FPGA v%d.0) ####\n", ((fpga_version >> 4) & 0x000F), (fpga_version & 0x000F));
}
- printf("\nNM500 is initialized!\n");
- printf("There are %d neurons\n", hnn.total_neurons);
+ printf("\nStart NM500 initialization...\n");
+ printf(" NM500 is initialized!\n");
+ printf(" There are %d neurons\n", hnn.total_neurons);
}
else {
- minif = hnn.getMinif();
- printf("\nread value = %d", minif);
- printf("\nWarning!!! NM500 Shield board not properly responding!!");
- printf("\nCheck the connection and Reboot again!");
+ printf("\n\nStart NM500 initialization...\n");
+ printf(" NM500 is not connected properly!!\n");
+ printf(" Please check the connection and reboot!\n");
while (1);
}
@@ -60,127 +86,92 @@
hnn.setGcr(1 + norm_lsup);
// build knowledge by learning 3 patterns with each constant values (respectively 11, 15 and 20)
- printf("\nLearning three patterns...");
- for (i = 0; i < VECTOR_LENGTH; i++)
+ printf("\nLearning three patterns...\n");
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 11;
hnn.learn(vector, VECTOR_LENGTH, 55);
- for (i = 0; i < VECTOR_LENGTH; i++)
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 15;
hnn.learn(vector, VECTOR_LENGTH, 33);
- for (i = 0; i < VECTOR_LENGTH; i++)
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 20;
- ncount = hnn.learn(vector, VECTOR_LENGTH, 100);
- // display the content of the committed neurons
- printf("\nDisplay the neurons, count = %d", ncount);
- for (i = 1; i <= ncount; i++) {
- hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
- printf("\nneuron#%d \tmodel=", i);
- for (j = 0; j < VECTOR_LENGTH; j++)
- printf("%d, ", vector16[j]);
- if (cat & 0x8000)
- printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
- else
- printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
- }
+ hnn.learn(vector, VECTOR_LENGTH, 100);
+ displayNeurons();
- for (value = 12; value < 16; value++) {
- for (i = 0; i < VECTOR_LENGTH; i++)
+ for (uint8_t value = 12; value < 16; value++) {
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = value;
- printf("\n\nRecognizing a new pattern: ");
- for (i = 0; i < VECTOR_LENGTH; i++)
+ printf("\nRecognizing a new pattern: ");
+ for (int i = 0; i < VECTOR_LENGTH; i++)
printf("%d, ", vector[i]);
+ printf("\n");
response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
- for (i = 0; i < response_nbr; i++) {
- if (cats[i] & 0x8000)
- printf("\nFiring neuron#%d, category=%d (degenerated), distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
- else
- printf("\nFiring neuron#%d, category=%d, distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ for (int i = 0; i < response_nbr; i++) {
+ if (cats[i] & 0x8000) {
+ printf("Firing neuron#%d, category=%d (degenerated), distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ }
+ else {
+ printf("Firing neuron#%d, category=%d, distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ }
}
}
- for (i = 0; i < VECTOR_LENGTH; i++)
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 20;
- printf("\n\nRecognizing a new pattern using KNN classifier: ");
- for (i = 0; i < VECTOR_LENGTH; i++)
+ printf("\nRecognizing a new pattern using KNN classifier: ");
+ for (int i = 0; i < VECTOR_LENGTH; i++)
printf("%d, ", vector[i]);
+ printf("\n");
hnn.setKnnClassifier();
response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
hnn.setRbfClassifier();
- for (i = 0; i < READ_COUNT; i++) {
- if (cats[i] & 0x8000)
- printf("\nFiring neuron#%d, category=%d (degenerated), distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
- else
- printf("\nFiring neuron#%d, category=%d, distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ for (int i = 0; i < READ_COUNT; i++) {
+ if (cats[i] & 0x8000) {
+ printf("Firing neuron#%d, category=%d (degenerated), distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ }
+ else {
+ printf("Firing neuron#%d, category=%d, distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ }
}
- printf("\n\nLearning a new example (13) falling between neuron1 and neuron2");
- for (i = 0; i < VECTOR_LENGTH; i++)
+ printf("\nLearning a new example (13) falling between neuron1 and neuron2\n");
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 13;
- ncount = hnn.learn(vector, VECTOR_LENGTH, 100);
- // display the content of the committed neurons
- printf("\nDisplay the neurons, count = %d", ncount);
- for (i = 1; i <= ncount; i++) {
- hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
- printf("\nneuron#%d \tmodel=", i);
- for (j = 0; j < VECTOR_LENGTH; j++)
- printf("%d, ", vector16[j]);
- if (cat & 0x8000)
- printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
- else
- printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
- }
- printf("\n=> Notice the addition of neuron 4 and the shrinking of the influence fields of neuron1 and 2");
+ hnn.learn(vector, VECTOR_LENGTH, 100);
+ displayNeurons();
+ printf("=> Notice the addition of neuron 4 and the shrinking of the influence fields of neuron1 and 2\n");
- printf("\n\nLearning a same example (13) using a different category 77");
- for (i = 0; i < VECTOR_LENGTH; i++)
+ printf("\nLearning a same example (13) using a different category 77\n");
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 13;
- ncount = hnn.learn(vector, VECTOR_LENGTH, 77);
- // display the content of the committed neurons
- printf("\nDisplay the neurons, count = %d", ncount);
- for (i = 1; i <= ncount; i++) {
- hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
- printf("\nneuron#%d \tmodel=", i);
- for (j = 0; j < VECTOR_LENGTH; j++)
- printf("%d, ", vector16[j]);
- if (cat & 0x8000)
- printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
- else
- printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
- }
- printf("\n=> Notice if the AIF of a neuron reaches the MINIF, the neuron will be degenerated");
+ hnn.learn(vector, VECTOR_LENGTH, 77);
+ displayNeurons();
+ printf("=> Notice if the AIF of a neuron reaches the MINIF, the neuron will be degenerated\n");
- printf("\n\nLearning a new example (12) using context 5, category 200");
- for (i = 0; i < VECTOR_LENGTH; i++)
+ printf("\nLearning a new example (12) using context 5, category 200\n");
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 12;
hnn.setContext(5);
- ncount = hnn.learn(vector, VECTOR_LENGTH, 200);
+ hnn.learn(vector, VECTOR_LENGTH, 200);
hnn.setContext(1);
- // display the content of the committed neurons
- printf("\nDisplay the neurons, count = %d", ncount);
- for (i = 1; i <= ncount; i++) {
- hnn.readNeuron(i, vector16, &ncr, &aif, &cat);
- printf("\nneuron#%d \tmodel=", i);
- for (j = 0; j < VECTOR_LENGTH; j++)
- printf("%d, ", vector16[j]);
- if (cat & 0x8000)
- printf("\tncr=%d \taif=%d \tcat=%d (degenerated)", ncr, aif, (cat & 0x7FFF));
- else
- printf("\tncr=%d \taif=%d \tcat=%d", ncr, aif, (cat & 0x7FFF));
- }
+ displayNeurons();
- for (i = 0; i < VECTOR_LENGTH; i++)
+ for (int i = 0; i < VECTOR_LENGTH; i++)
vector[i] = 20;
- printf("\n\nRecognizing a new pattern using context 5: ");
- for (i = 0; i < VECTOR_LENGTH; i++)
+ printf("\nRecognizing a new pattern using context 5: ");
+ for (int i = 0; i < VECTOR_LENGTH; i++)
printf("%d, ", vector[i]);
+ printf("\n");
hnn.setContext(5);
response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
hnn.setContext(1);
- for (i = 0; i < response_nbr; i++) {
- if (cats[i] & 0x8000)
- printf("\nFiring neuron#%d, category=%d (degenerated), distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
- else
- printf("\nFiring neuron#%d, category=%d, distance=%d", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ for (int i = 0; i < response_nbr; i++) {
+ if (cats[i] & 0x8000) {
+ printf("Firing neuron#%d, category=%d (degenerated), distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ }
+ else {
+ printf("Firing neuron#%d, category=%d, distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
+ }
}
- printf("\n=> Notice the neurons will not be recognize and shrink if the value of context is not equal");
+ printf("=> Notice the neurons will not be recognize and shrink if the value of context is not equal\n");
}
\ No newline at end of file