This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

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 knn arduino code..

this is the general code from him..

#include "dataset.h"
#include "funcionesKNN.h"

#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){
  
  Serial.println("-----------------------------------------------");
  Serial.print(nomAtr[0]); Serial.print(": "); Serial.print(dato[0],3); Serial.println(" cms");
  Serial.print(nomAtr[1]); Serial.print(": "); Serial.print(dato[1],3); Serial.println(" cms");
  Serial.print(nomAtr[2]); Serial.print(": "); Serial.print(dato[2],3); Serial.println(" cms");
  Serial.print(nomAtr[3]); Serial.print(": "); Serial.print(dato[3],3); Serial.println(" cms");
  Serial.print(" >> Clase: "); Serial.println(clasesSt[clase]);
}

//-----------------------------------------------------------------------
// Configuracion del microcontrolador
//-----------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.println("===============================================");
  Serial.println("===            KKN Classification           ===");
  Serial.println("===============================================");
  delay(500);
}

//-----------------------------------------------------------------------
// Programa principal
//-----------------------------------------------------------------------
void loop() {
  
  // 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);
    Serial.println(cl);

    // tac
    actual = millis();
  
    // imprimir
    imprime(dato, cl);
    Serial.print("Tiempo: "); Serial.print(actual-previo); Serial.println(" mS");
  }
}

in arduino looks like this one..

and then after that, I tried to code using mbed compiler like this one.

#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's error message that show like this one..

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 <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..

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..

thanks..

Parents
  • Hi,

    What board (and microcontroller) are you developing for?

    Correct me if I am wrong, but from what I can see that github project is made for an arduino board which does not use any product from Nordic Semiconductor. If you use an nRF51 SoC from us, then it is probably better to have a look at our nRF5 SDK v12.3.0, which is our latest SDK supporting the nRF51 series.

    In any case: If porting that code from arduino to mbed is what you really want then you may have better luck if you ask those questions on the mbed and/or arduino forums instead.

    Regards,
    Terje

  • thanks for answering before, I'm using nRF51822 core board. Because my first implement before from mbed compiler so I think it's easier to continue that program using mbed but That are several error that must be solve.. if your kindness help me to solve the problem that I explained before it is so much pleasure..

    because it's my progress and I'm newbie from this program c++. so please help me to solve it using mbed compiler please..
    many thanks if you can help me..

Reply
  • thanks for answering before, I'm using nRF51822 core board. Because my first implement before from mbed compiler so I think it's easier to continue that program using mbed but That are several error that must be solve.. if your kindness help me to solve the problem that I explained before it is so much pleasure..

    because it's my progress and I'm newbie from this program c++. so please help me to solve it using mbed compiler please..
    many thanks if you can help me..

Children
  • Hi,

    Unfortunately I do not think I am able to help you with the mbed part of this, but perhaps others on this forum can?

    In any case, I still think you will have better luck if you state your problem on the mbed forums. There are a lot of people there that knows a lot about both mbed and nRF51, and because of that we always recommend to ask the mbed related questions there.

    If you are looking for a k-nearest neighbors algorithm implementation for mbed, you might be able to find that directly instead of having to port an arduino project?

    Regarding the arduino project that you were linking to: I am not convinced that it is a good library. It has variable names and comments in a language that I do not understand, but it does look unfinished and not very well tested. The few translations to English are poor. The project has not been updated after the initial commit three years ago... Finding another implementation (or even implementing an algorithm yourself) may be better.

    Regards,
    Terje

  • after several hours, there was an answer from another forum that says like this..

    I have 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
        }
    }

    but I have several error message that code not completed clear, because this code..

    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's show error like this one..

    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

    please give me best solution to solve that problem..

    thanks

  • Hi,

    It means that the function named millis() is not found anywhere. Perhaps because of a missing header file. A quick google search reveals that it is a standard library function for Arduino. You should see if you find a similar function in mbed.

    Regards,
    Terje

Related