NM500 / Mbed 2 deprecated NeuroShield_SimpleScript

Dependencies:   mbed NeuroShield

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002  *  NM500 NeuroShield Board SimpleScript
00003  *  Simple Test Script to understand how the neurons learn and recognize
00004  *  revision 1.1.5, 2020/02/11
00005  *  Copyright (c) 2017 nepes inc.
00006  *
00007  *  Please use the NeuroShield library v1.1.4 or later
00008  ******************************************************************************/
00009 
00010 #include "mbed.h"
00011 #include <NeuroShield.h>
00012 #include <NeuroShieldSPI.h>
00013 
00014 #define VECTOR_LENGTH 4
00015 #define READ_COUNT 3
00016 
00017 NeuroShield hnn;
00018 
00019 DigitalOut sdcard_ss(D6);       // SDCARD_SSn
00020 DigitalOut arduino_con(D5);     // SPI_SEL
00021 
00022 uint8_t vector[NEURON_SIZE];
00023 uint16_t vector16[NEURON_SIZE];   
00024 uint16_t dists[READ_COUNT], cats[READ_COUNT], nids[READ_COUNT];
00025 uint16_t response_nbr, norm_lsup = 0;
00026 uint16_t fpga_version;
00027 
00028 void displayNeurons()
00029 {
00030     uint16_t nm_ncr, nm_aif, nm_cat;
00031     uint16_t ncount = hnn.getNcount();
00032     printf("Display the neurons, ncount = %d\n", ncount);
00033     uint16_t temp_nsr = hnn.getNsr();
00034     hnn.setNsr(0x0010);
00035     hnn.resetChain();
00036     for (int i = 1; i <= ncount; i++) {
00037         nm_ncr = hnn.getNcr();
00038         hnn.readCompVector(vector16, VECTOR_LENGTH);
00039         nm_aif = hnn.getAif();
00040         nm_cat = hnn.getCat();
00041         
00042         printf("neuron#%d \tvector=", i);
00043         for (int j = 0; j < VECTOR_LENGTH; j++) {
00044             printf("%d, ", vector16[j]);
00045         }
00046         if (nm_cat & 0x8000) {
00047             printf(" \tncr=%d \taif=%d \tcat=%d (degenerated)\n", nm_ncr, nm_aif, (nm_cat & 0x7FFF));
00048         }
00049         else {
00050             printf(" \tncr=%d \taif=%d \tcat=%d\n", nm_ncr, nm_aif, nm_cat);
00051         }
00052     }
00053     hnn.setNsr(temp_nsr);
00054 }
00055 
00056 int main()
00057 {
00058     arduino_con = LOW;
00059     sdcard_ss = HIGH;
00060     wait(0.5);
00061     
00062     if (hnn.begin() != 0) {
00063         fpga_version = hnn.fpgaVersion();
00064         if ((fpga_version & 0xFF00) == 0x0000) {
00065             printf("\n\n#### NeuroShield Board (Board v%d.0 / FPGA v%d.0) ####\n", ((fpga_version >> 4) & 0x000F), (fpga_version & 0x000F));
00066         }
00067         else if ((fpga_version & 0xFF00) == 0x0100) {
00068             printf("\n\n#### Prodigy Board (Board v%d.0 / FPGA v%d.0) ####\n", ((fpga_version >> 4) & 0x000F), (fpga_version & 0x000F));
00069         }
00070         else {
00071             printf("\n\n#### Unknown Board (Board v%d.0 / FPGA v%d.0) ####\n", ((fpga_version >> 4) & 0x000F), (fpga_version & 0x000F));
00072         }
00073         printf("\nStart NM500 initialization...\n");
00074         printf("  NM500 is initialized!\n");
00075         printf("  There are %d neurons\n", hnn.total_neurons);
00076     }
00077     else {
00078         printf("\n\nStart NM500 initialization...\n");
00079         printf("  NM500 is not connected properly!!\n");
00080         printf("  Please check the connection and reboot!\n");
00081         while (1);
00082     }
00083     
00084     // if you want to run in lsup mode, uncomment below
00085     //norm_lsup = 0x80;
00086     hnn.setGcr(1 + norm_lsup);
00087     
00088     // build knowledge by learning 3 patterns with each constant values (respectively 11, 15 and 20)
00089     printf("\nLearning three patterns...\n");
00090     for (int i = 0; i < VECTOR_LENGTH; i++)
00091         vector[i] = 11;
00092     hnn.learn(vector, VECTOR_LENGTH, 55);
00093     for (int i = 0; i < VECTOR_LENGTH; i++)
00094         vector[i] = 15;
00095     hnn.learn(vector, VECTOR_LENGTH, 33);
00096     for (int i = 0; i < VECTOR_LENGTH; i++)
00097         vector[i] = 20;
00098     hnn.learn(vector, VECTOR_LENGTH, 100);
00099     displayNeurons();
00100     
00101     for (uint8_t value = 12; value < 16; value++) {
00102         for (int i = 0; i < VECTOR_LENGTH; i++)
00103             vector[i] = value;
00104         printf("\nRecognizing a new pattern: ");
00105         for (int i = 0; i < VECTOR_LENGTH; i++)
00106             printf("%d, ", vector[i]);
00107         printf("\n");
00108         response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
00109         for (int i = 0; i < response_nbr; i++) {
00110             if (cats[i] & 0x8000) {
00111                 printf("Firing neuron#%d, category=%d (degenerated), distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
00112             }
00113             else {
00114                 printf("Firing neuron#%d, category=%d, distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
00115             }
00116         }
00117     }
00118     
00119     for (int i = 0; i < VECTOR_LENGTH; i++)
00120         vector[i] = 20;
00121     printf("\nRecognizing a new pattern using KNN classifier: ");
00122     for (int i = 0; i < VECTOR_LENGTH; i++)
00123         printf("%d, ", vector[i]);
00124     printf("\n");
00125     hnn.setKnnClassifier();
00126     response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
00127     hnn.setRbfClassifier();
00128     for (int i = 0; i < READ_COUNT; i++) {
00129         if (cats[i] & 0x8000) {
00130             printf("Firing neuron#%d, category=%d (degenerated), distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
00131         }
00132         else {
00133             printf("Firing neuron#%d, category=%d, distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
00134         }
00135     }
00136     
00137     printf("\nLearning a new example (13) falling between neuron1 and neuron2\n");
00138     for (int i = 0; i < VECTOR_LENGTH; i++)
00139         vector[i] = 13;
00140     hnn.learn(vector, VECTOR_LENGTH, 100);
00141     displayNeurons();
00142     printf("=> Notice the addition of neuron 4 and the shrinking of the influence fields of neuron1 and 2\n");
00143     
00144     printf("\nLearning a same example (13) using a different category 77\n");
00145     for (int i = 0; i < VECTOR_LENGTH; i++)
00146         vector[i] = 13;
00147     hnn.learn(vector, VECTOR_LENGTH, 77);
00148     displayNeurons();
00149     printf("=> Notice if the AIF of a neuron reaches the MINIF, the neuron will be degenerated\n");
00150     
00151     printf("\nLearning a new example (12) using context 5, category 200\n");
00152     for (int i = 0; i < VECTOR_LENGTH; i++)
00153         vector[i] = 12;
00154     hnn.setContext(5);
00155     hnn.learn(vector, VECTOR_LENGTH, 200);
00156     hnn.setContext(1);
00157     displayNeurons();
00158     
00159     for (int i = 0; i < VECTOR_LENGTH; i++)
00160         vector[i] = 20;
00161     printf("\nRecognizing a new pattern using context 5: ");
00162     for (int i = 0; i < VECTOR_LENGTH; i++)
00163         printf("%d, ", vector[i]);
00164     printf("\n");
00165     hnn.setContext(5);
00166     response_nbr = hnn.classify(vector, VECTOR_LENGTH, READ_COUNT, dists, cats, nids);
00167     hnn.setContext(1);
00168     for (int i = 0; i < response_nbr; i++) {
00169         if (cats[i] & 0x8000) {
00170             printf("Firing neuron#%d, category=%d (degenerated), distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
00171         }
00172         else {
00173             printf("Firing neuron#%d, category=%d, distance=%d\n", nids[i], (cats[i] & 0x7FFF), dists[i]);
00174         }
00175     }
00176     printf("=> Notice the neurons will not be recognize and shrink if the value of context is not equal\n");
00177 }