NM500 / Mbed 2 deprecated NeuroShield_SimpleScript

Dependencies:   mbed NeuroShield

Revision:
0:c56eb46c7bee
Child:
1:2d0abf41b7a3
diff -r 000000000000 -r c56eb46c7bee main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Aug 17 23:35:06 2017 +0000
@@ -0,0 +1,173 @@
+/******************************************************************************
+ *  NM500 NeuroSheild Board SimpleScript
+ *  Simple Test Script to understand how the neurons learn and recognize
+ *  revision 1.0, 8/18, 2017
+ *  Copyright (c) 2017 nepes inc.
+ ******************************************************************************/
+
+#include "mbed.h"
+#include <NeuroShield.h>
+#include <NeuroShieldSPI.h>
+
+#define VECTOR_LENGTH 4
+#define READ_COUNT 3
+
+NeuroShield hnn;
+
+DigitalOut sdcard_ss(D6);       // SDCARD_SSn
+DigitalOut arduino_con(D5);     // SPI_SEL
+
+int main()
+{
+    int i, j;
+    uint8_t value;
+    uint8_t vector[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];
+    
+    printf("\n#### NM500 NeuroShield Board ####\n");
+    arduino_con = LOW;
+    sdcard_ss = HIGH;
+    wait(0.5);
+    
+    if (hnn.begin() != 0) {
+        printf("\nNM500 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!");
+        while (1);
+    }
+    
+    // if you want to run in lsup mode, uncomment below
+    //norm_lsup = 0x80;
+    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++)
+        vector[i] = 11;
+    hnn.learn(vector, VECTOR_LENGTH, 55);
+    for (i = 0; i < VECTOR_LENGTH; i++)
+        vector[i] = 15;
+    hnn.learn(vector, VECTOR_LENGTH, 33);
+    for (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 = 0; i < ncount; i++) {
+        hnn.readNeuron(i, vector, &ncr, &aif, &cat);
+        printf("\nneuron#%d \tmodel=", (i + 1));
+        for (j = 0; j < VECTOR_LENGTH; j++)
+            printf("%d, ", vector[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));
+    }
+    
+    for (value = 12; value < 16; value++) {
+        for (i = 0; i < VECTOR_LENGTH; i++)
+            vector[i] = value;
+        printf("\n\nRecognizing a new pattern: ");
+        for (i = 0; i < VECTOR_LENGTH; i++)
+            printf("%d, ", vector[i]);
+        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 (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("%d, ", vector[i]);
+    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]);
+    }
+    
+    printf("\n\nLearning a new example (13) falling between neuron1 and neuron2");
+    for (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 = 0; i < ncount; i++) {
+        hnn.readNeuron(i, vector, &ncr, &aif, &cat);
+        printf("\nneuron#%d \tmodel=", (i + 1));
+        for (j = 0; j < VECTOR_LENGTH; j++)
+            printf("%d, ", vector[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");
+    
+    printf("\n\nLearning a same example (13) using a different category 77");
+    for (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 = 0; i < ncount; i++) {
+        hnn.readNeuron(i, vector, &ncr, &aif, &cat);
+        printf("\nneuron#%d \tmodel=", (i + 1));
+        for (j = 0; j < VECTOR_LENGTH; j++)
+            printf("%d, ", vector[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");
+    
+    printf("\n\nLearning a new example (12) using context 5, category 200");
+    for (i = 0; i < VECTOR_LENGTH; i++)
+        vector[i] = 12;
+    hnn.setContext(5);
+    ncount = 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 = 0; i < ncount; i++) {
+        hnn.readNeuron(i, vector, &ncr, &aif, &cat);
+        printf("\nneuron#%d \tmodel=", (i + 1));
+        for (j = 0; j < VECTOR_LENGTH; j++)
+            printf("%d, ", vector[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));
+    }
+    
+    for (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("%d, ", vector[i]);
+    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]);
+    }
+    printf("\n=> Notice the neurons will not be recognize and shrink if the value of context is not equal");
+}
\ No newline at end of file