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.
main.cpp
00001 #include "mbed.h" 00002 #include "dataset.h" 00003 #include "funcionesKNN.h" 00004 #include <string> 00005 Serial pc(USBTX, USBRX); 00006 #define FI 100 // filas de la base de datos 00007 #define CO 4 // numero de caracteristicas 00008 00009 // variables 00010 /*char* clasesSt[3]; // strings con las clases 00011 char* nomAtr[CO]; // strings con los nombres de atributos 00012 double atributos[FI][CO]; // atributos 00013 int clasesNo[FI]; // clases 00014 */ 00015 double dato[CO]; // dato a clasificar 00016 long actual = 0, previo = 0; 00017 00018 //----------------------------------------------------------------------- 00019 // Extrae caracteres dados los identificadores y los convierte a double 00020 //----------------------------------------------------------------------- 00021 void extraeDatos(string str, double dato[], char dlm[]){ 00022 00023 string auxstr = ""; 00024 int posdlm[5], tamstr = 0; 00025 int i= 0, j = 0; 00026 00027 // buscar caracteres de identificacion 00028 posdlm[0] = str.indexOf(dlm[0]); // ( 00029 posdlm[1] = str.indexOf(dlm[1]); // , 00030 posdlm[2] = str.indexOf(dlm[1], posdlm[1] + 1); // , 00031 posdlm[3] = str.indexOf(dlm[1], posdlm[2] + 1); // , 00032 posdlm[4] = str.indexOf(dlm[2]); // ) 00033 00034 for(j=0; j<CO; j++){ 00035 // extraer datos numericos 00036 for(i=posdlm[j]+1; i<posdlm[j+1]; i++){ 00037 auxstr = auxstr + str[i]; 00038 } 00039 00040 //convertir a double 00041 int y = auxstr.length(); 00042 char buf[y+1]; 00043 auxstr.toCharArray(buf, y+1); 00044 dato[j] = atof(buf); 00045 00046 //Serial.println(dato[j],3); 00047 auxstr=""; // reset del auxiliar 00048 } 00049 } 00050 00051 //----------------------------------------------------------------------- 00052 // Impresion de confirmacion 00053 //----------------------------------------------------------------------- 00054 void imprime(double dato[], int clase){ 00055 00056 pc.printf("-----------------------------------------------\n"); 00057 pc.printf("%i",(int16_t)nomAtr[0]); pc.printf(": "); pc.printf("%i",(int16_t)dato[0],3); pc.printf(" cms\n"); 00058 pc.printf("%i",(int16_t)nomAtr[1]); pc.printf(": "); pc.printf("%i",(int16_t)dato[1],3); pc.printf(" cms\n"); 00059 pc.printf("%i",(int16_t)nomAtr[2]); pc.printf(": "); pc.printf("%i",(int16_t)dato[2],3); pc.printf(" cms\n"); 00060 pc.printf("%i",(int16_t)nomAtr[3]); pc.printf(": "); pc.printf("%i",(int16_t)dato[3],3); pc.printf(" cms\b"); 00061 pc.printf(" >> Clase: "); pc.printf("%i\n",(int16_t)clasesSt[clase]); 00062 } 00063 00064 //----------------------------------------------------------------------- 00065 // Configuracion del microcontrolador 00066 //----------------------------------------------------------------------- 00067 int main() 00068 { 00069 pc.baud(9600); 00070 //Serial.begin(9600); 00071 wait(0.1); 00072 pc.printf("===============================================\n"); 00073 pc.printf("=== KKN Classification ===\n"); 00074 pc.printf("===============================================\n"); 00075 wait(0.05); 00076 00077 //----------------------------------------------------------------------- 00078 // Programa principal 00079 //----------------------------------------------------------------------- 00080 while(1) 00081 { 00082 00083 // recibir los datos a clasificar 00084 if(Serial.available()) 00085 { 00086 00087 // recibir una cadena hasta que se tenga el '\n' 00088 string str = Serial.readStringUntil('\n'); 00089 extraeDatos(str, dato, "{,}"); 00090 00091 // tic 00092 previo = millis(); 00093 00094 // encontrar la clase, con 5 vecinos cercanos 00095 int cl = clasificaKNN((double**)atributos, clasesNo, dato, 11, FI, CO); 00096 pc.printf("%i\n",(int16_t)cl); 00097 00098 // tac 00099 actual = millis(); 00100 00101 // imprimir 00102 imprime(dato, cl); 00103 pc.printf("Tiempo: "); pc.printf("%i\n",(int16_t)actual-previo); pc.printf(" mS\n"); 00104 } 00105 } 00106 } 00107 00108
Generated on Sun Jul 17 2022 10:16:44 by
1.7.2