Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years ago.
How to convert arduino code to mbed online compiler using nRF51822 ?
Hello world. I wanna ask about how to convert arduino code possible for mbed compiler for arduino. first of all, I have tried to use this guy program https://github.com/jruben84/knn_programs.. this is my code
include the mbed library with this snippet
#include "mbed.h" #include "dataset.h" #include "funcionesKNN.h" #include <string> Serial pc(USBTX, USBRX); #define FI 100 // filas de la base de datos #define CO 4 // numero de caracteristicas // variables /*char* clasesSt[3]; // strings con las clases char* nomAtr[CO]; // strings con los nombres de atributos double atributos[FI][CO]; // atributos int clasesNo[FI]; // clases */ double dato[CO]; // dato a clasificar long actual = 0, previo = 0; //----------------------------------------------------------------------- // Extrae caracteres dados los identificadores y los convierte a double //----------------------------------------------------------------------- void extraeDatos(string str, double dato[], char dlm[]){ string auxstr = ""; int posdlm[5], tamstr = 0; int i= 0, j = 0; // buscar caracteres de identificacion posdlm[0] = str.indexOf(dlm[0]); // ( posdlm[1] = str.indexOf(dlm[1]); // , posdlm[2] = str.indexOf(dlm[1], posdlm[1] + 1); // , posdlm[3] = str.indexOf(dlm[1], posdlm[2] + 1); // , posdlm[4] = str.indexOf(dlm[2]); // ) for(j=0; j<CO; j++){ // extraer datos numericos for(i=posdlm[j]+1; i<posdlm[j+1]; i++){ auxstr = auxstr + str[i]; } //convertir a double int y = auxstr.length(); char buf[y+1]; auxstr.toCharArray(buf, y+1); dato[j] = atof(buf); //Serial.println(dato[j],3); auxstr=""; // reset del auxiliar } } //----------------------------------------------------------------------- // Impresion de confirmacion //----------------------------------------------------------------------- void imprime(double dato[], int clase){ pc.printf("-----------------------------------------------\n"); pc.printf("%i",(int16_t)nomAtr[0]); pc.printf(": "); pc.printf("%i",(int16_t)dato[0],3); pc.printf(" cms\n"); pc.printf("%i",(int16_t)nomAtr[1]); pc.printf(": "); pc.printf("%i",(int16_t)dato[1],3); pc.printf(" cms\n"); pc.printf("%i",(int16_t)nomAtr[2]); pc.printf(": "); pc.printf("%i",(int16_t)dato[2],3); pc.printf(" cms\n"); pc.printf("%i",(int16_t)nomAtr[3]); pc.printf(": "); pc.printf("%i",(int16_t)dato[3],3); pc.printf(" cms\b"); pc.printf(" >> Clase: "); pc.printf("%i\n",(int16_t)clasesSt[clase]); } //----------------------------------------------------------------------- // Configuracion del microcontrolador //----------------------------------------------------------------------- int main() { pc.baud(9600); //Serial.begin(9600); wait(0.1); pc.printf("===============================================\n"); pc.printf("=== KKN Classification ===\n"); pc.printf("===============================================\n"); wait(0.05); //----------------------------------------------------------------------- // Programa principal //----------------------------------------------------------------------- while(1) { // recibir los datos a clasificar if(Serial.available()) { // recibir una cadena hasta que se tenga el '\n' string str = Serial.readStringUntil('\n'); extraeDatos(str, dato, "{,}"); // tic previo = millis(); // encontrar la clase, con 5 vecinos cercanos int cl = clasificaKNN((double**)atributos, clasesNo, dato, 11, FI, CO); pc.printf("%i\n",(int16_t)cl); // tac actual = millis(); // imprimir imprime(dato, cl); pc.printf("Tiempo: "); pc.printf("%i\n",(int16_t)actual-previo); pc.printf(" mS\n"); } } }
but there are several error shows like this one..
Quote:
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 28, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 29, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 30, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 31, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 32, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "toCharArray" in "main.cpp", Line: 43, Col: 13
Warning: Variable "tamstr" was declared but never referenced in "main.cpp", Line: 24, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 57, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 58, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 59, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 60, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 61, Col: 47
Error: Expected a declarator in condition declaration in "main.cpp", Line: 84, Col: 19 Error: Type name is not allowed in "main.cpp", Line: 88, Col: 27 Error: Identifier "millis" is undefined in "main.cpp", Line: 92, Col: 23
and also from this library funcionesKNN.cpp, there was error too that show like this after I have change my code
include the mbed library with this snippet
//#include <HardwareSerial.h> // solo para Debug /*------------------------------------------ funciones para el computo del algoritmo KNN --------------------------------------------*/ //#include "dataset.h" #include "funcionesKNN.h" #include <math.h> /*---------------------------------------------- calculo de distancia euclidiana entre dos puntos ------------------------------------------------*/ double distanciaEuclidiana(double pt1[], double pt2[], int co){ int i; double suma = 0; for(i=0; i<co; i++){ suma = pow(pt1[i] - pt2[i], 2) + suma; } return sqrt(suma); } /*--------------------------------------------------------------- calculo de distancia euclidiana entre un punto y la base de datos ----------------------------------------------------------------*/ void todasDistEuclid(double pt1[], double aux[], double** atributos, int fi, int co){ int i = 0, j = 0; double pt2[co]; // falta esto for(i = 0; i<fi; i++){ // extraer un punto de la base de datos for(j=0; j<co; j++){ pt2[j] = atributos[i][j]; } aux[i] = distanciaEuclidiana(pt1, pt2, co); } } /*---------------------------------------------------------------- Ordenamiento de las distancias de menor a mayor ----------------------------------------------------------------*/ void ordena(double datos[], int clases[], int clasesNo[], int fi){ int i =1, j = 1, f = 1, temp[2]; //crear una copia de las clases originales for(i=0; i<fi; i++){ clases[i] = clasesNo[i]; } // ordenar for(i=1; (i<=fi)&&f; i++){ f = 0; for(j=0; j<(fi-1); j++){ if(datos[j+1] < datos[j]){ // orden descendente >, ascendente < temp[0] = datos[j]; temp[1] = clases[j]; datos[j] = datos[j+1]; clases[j] = clases[j+1]; datos[j+1] = temp[0]; clases[j+1] = temp[1]; f = 1; } } } } /*---------------------------------------------------------------- Extraer los N primeros ----------------------------------------------------------------*/ void extraeKPrimeros(double datos[], double kPrimeros[], int clases[], int kClases[], int k){ for(int i=0; i<k; i++){ kPrimeros[i] = datos[i]; kClases[i] = clases[i]; } } /*---------------------------------------------------------------- Calcula la clase mas frecuente: Moda ----------------------------------------------------------------*/ int claseMasFrecuente(int clases[], int k){ int cont = 0, cont2 = 0, pos = 0, num = 0, i = 0; int frec[k], mayor = 0, posmayor = 0, aux[k]; // inicializar el contador de frecuencias for(i=0; i<k; i++){ frec[k] = 0; } // comprobar las repeticiones de cada numero for(cont=0; cont<k; cont++){ num = clases[cont]; pos = cont; for(cont2 = 0; cont2<k; cont2++){ if(clases[cont2] == num){ aux[pos]++; } } } mayor =aux[0]; posmayor = 0; for(cont=0; cont<k; cont++){ if(aux[cont] > mayor){ posmayor = cont; mayor = aux[cont]; } } return clases[posmayor]; } /*---------------------------------------------------------------- Calcula la clase de un conjunto de atributos usando KNN ----------------------------------------------------------------*/ int clasificaKNN(double** datos, int clasesNum[], double dato[], int k, int fi, int co){ double distancias[fi], kPrimeros[k]; int clases[fi]; int kClases[k]; todasDistEuclid(dato, distancias, datos, fi, co); ordena(distancias, clases, clasesNum, fi); extraeKPrimeros(distancias, kPrimeros, clases, kClases, k); // calculo de la moda int cont = 0, cont2 = 0, pos = 0, num = 0, i = 0; int frec[k], mayor = 0, posmayor = 0, aux[k]; // inicializar el contador de frecuencias for(i=0; i<k; i++){ frec[k] = 0; } // comprobar las repeticiones de cada numero for(cont=0; cont<k; cont++){ num = clases[cont]; pos = cont; for(cont2 = 0; cont2<k; cont2++){ if(clases[cont2] == num){ aux[pos]++; } } } mayor =aux[0]; posmayor = 0; for(cont=0; cont<k; cont++){ if(aux[cont] > mayor){ posmayor = cont; mayor = aux[cont]; } } return clases[posmayor]; }
and error message show like this one..
Quote:
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 28, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 29, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 30, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 31, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "indexOf" in "main.cpp", Line: 32, Col: 20
Error: Class "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" has no member "toCharArray" in "main.cpp", Line: 43, Col: 13
Warning: Variable "tamstr" was declared but never referenced in "main.cpp", Line: 24, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 57, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 58, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 59, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 60, Col: 19 Warning: Conversion from pointer to smaller integer in "main.cpp", Line: 61, Col: 47
Error: Expected a declarator in condition declaration in "main.cpp", Line: 84, Col: 19 Error: Type name is not allowed in "main.cpp", Line: 88, Col: 27 Error: Identifier "millis" is undefined in "main.cpp", Line: 92, Col: 23
here's my full code from mbed online compiler, KNN mbed please how to solve this problem.. please to help me soon..
1 Answer
7 years ago.
Hello Rakha,
Try to modify the extraeDatos
function as follows:
void extraeDatos(string& str, double dato[], char dlm[]) { string auxstr = ""; int posdlm[5], tamstr = 0; int i = 0, j = 0; // buscar caracteres de identificacion / Identification of characters posdlm[0] = str.find(dlm[0], 0); // ( posdlm[1] = str.find(dlm[1], posdlm[0]); // , posdlm[2] = str.find(dlm[1], posdlm[1] + 1); // , posdlm[3] = str.find(dlm[1], posdlm[2] + 1); // , posdlm[4] = str.find(dlm[2], posdlm[3]); // ) for (j = 0; j < co; j++) { // extraer datos numericos / Extract numerical data for (i = posdlm[j] + 1; i < posdlm[j + 1]; i++) { auxstr = auxstr + str[i]; } //convertir a double / Convert to double //int y = auxstr.length(); //char buf[y + 1]; //auxstr.c_str(buf, y + 1); dato[j] = atof(auxstr.c_str()); //printf(dato[j],3); auxstr = ""; // reset del auxiliar } }
thanks for answering.. how about this one sir.. there's several error message that code like this one..
code that error statement
while(1) { // recibir los datos a clasificar if(Serial.available()) { // recibir una cadena hasta que se tenga el '\n' string str = Serial.readStringUntil('\n'); extraeDatos(str, dato, "{,}"); // tic previo = millis(); // encontrar la clase, con 5 vecinos cercanos int cl = clasificaKNN((double**)atributos, clasesNo, dato, 11, FI, CO); pc.printf("%i\n",(int16_t)cl); // tac actual = millis(); // imprimir imprime(dato, cl); pc.printf("Tiempo: "); pc.printf("%i\n",(int16_t)actual-previo); pc.printf(" mS\n"); } }
that shows error message like this one..
Quote:
Error: Expected a declarator in condition declaration in "main.cpp", Line: 85, Col: 19
Error: Type name is not allowed in "main.cpp", Line: 89, Col: 27
Error: Identifier "millis" is undefined in "main.cpp", Line: 93, Col: 23
Try to modify the main function as follows: