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

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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 = "";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

in arduino looks like this one..

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

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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[]){
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

but there's error message that show like this one..

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

and also from this library funcionesKNN.cpp, there was error too that show like this after I have change my code

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//#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);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

and error message show like this one..

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

  • 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

Reply
  • 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

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

    I have try to modify the extraeDatos function as follows:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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();
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    that's show error like this one..

    Fullscreen
    1
    2
    3
    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
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    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