Returning Hobbyist _ Using 'older' Libraries in Mbed Studio

Hello Everyone,

In the last week I’ve progressed onto some of my ‘older’ more developed programs I put together and here are some of the issues I’m finding with my questions:-
Q1. I noticed in some of the library files wait(0.100); was used. I’ve assumed it is correct for me to replace these statements with thread_sleep_for(100);
Q2: The ‘older’ programme I have compiles successfully in OS3. I originally had errors associated with the SDFileSystem Library but searching through the Forum I have now uploaded the latest associated library…which leads me to this particular question. I made sure all the libraries I used were ‘up to date’ but the ADXL_I2C library throws up this Error:
Unknown type name ‘Serial’
The code is -
<>
Serial pc(USBTX, USBRX);
<
>
within the main.cpp programme of the library file.
Q3: In my main.cpp programme I declare all the header files as:
<>
#include “mbed.h”
#include “TextLCD.h”
#include “ADXL345_I2C.h”
#include “ITG3200.h”
#include “RecordData.h”
#include “GetAccelerometer.h”
#include “GetGyro.h”
<
>
but get this error:- ‘RecordData.h’ file not found
I then get this error:- Use of undeclared identifier ‘accelerometer’ for this code -
<>
if (accelerometer.setPowerControl(0x00)){
//pc.printf(“didn’t intitialize power control\n”);
lcd.printf(“didn’t intitialize power control\n”);
return 0; }
//Full resolution, +/-16g, 4mg/LSB.
thread_sleep_for(100);

 if(accelerometer.setDataFormatControl(0x0B)){
    //pc.printf("didn't set data format\n");
    lcd.printf("didn't set data format\n");
    return 0;  }
 thread_sleep_for(100);

 //3.2kHz data rate.

 if(accelerometer.setDataRate(ADXL345_3200HZ)){
    //pc.printf("didn't set data rate\n");
    lcd.printf("didn't set data rate\n");
    return 0;    }
 thread_sleep_for(100);
 
 //Measurement mode.
 if(accelerometer.setPowerControl(MeasurementMode)) {
    //pc.printf("didn't set the power control to measurement\n");
    lcd.printf("didn't set the power control to measurement\n");
    return 0;   }

<>

If anyone can help please with Q3 I can make the adjustments for the other errors which are similar i.e Error message - Use of undeclared identifier ‘gyro’
<>
gyro.setLpBandwidth(LPFBW_42HZ);
<
>

Please feel free to ask me any questions to understand my code or programme to assist in helping me.
Kind Regards,
Degs

Hello Darek,

yes

For SD card operation you not need external library, it is possible to do it directly with MbedOS now. Through a combination of the two APIs below

That is because the library probably use old Serial API which was splited to two APIs bleow in MbedOS6+

So you have to remake it or downgrade MbedOs to 5.15 or similar. Share a link to affected library and we can look on it.

It seems like, MbedStudio does not see the file in your project. Do you see the file in the project folder?

BR, Jan

Hi Jan,

Thanks for all the help here.
Working through the problems I need to learn about the new API’s for SD card operation as I feel it would be retrograde step to downgrade…bear with me on this one!

The Serial API, I used for TeraTerm to give me an insight to what was happening, please see my RecordData.cpp file here:
<>
#include “RecordData.h”

// Use this if using Tera Term

//Serial pc(USBTX, USBRX);

//void Datarecord(int *ptr_A, int *ptr_G, int size_a, int size_g);

SDFileSystem sd(p5,p6,p7,p8,“sd”); //MOSI, MISO, SCLK, CS

char buffer[42];

// LED’s to know the status what’s happening

//DigitalOut Status_1(LED1);

DigitalOut Status_2(LED2);

DigitalOut Status_3(LED3);

DigitalOut Status_4(LED4);

#define LED_ON 1

#define LED_OFF 0

// create an empty file pointer.

FILE *Gyro_Accelerometer_LogFile = NULL;

//Record the temperature onto SD card with timestamp

void Datarecord(int *ptr_A, int *ptr_G, int size_a, char size_g)//was (float data)

{

// Use this if using Tera Term

Serial pc(USBTX, USBRX);



pc.printf("%2i\n", size_a);

thread_sleep_for(1000);

Status_4 = LED_ON;//Data should have been recorded

time_t seconds = time(&seconds);

strftime(buffer, 42, "%d/%m/%y,%T", localtime(&seconds));

//strftime(buffer, 42, "%I:%M %p\r\n", localtime(&seconds));

//pc.printf("Time as a basic string = %s\r\n", ctime(&seconds));

FILE *fp = fopen("/sd/Gyro_Accelerometer_Datalogger.txt", "a");   // the 'a' appends data to the existing file.

for (int i=0; i<3; i++)

{

    Status_2 = LED_ON;//Data should have been recorded

    //fprintf (fp,"%04.if,",*data_ptr_Accelerometer+i);

    pc.printf("%0.3i,",*(ptr_A+i));

    thread_sleep_for(100);

    //fprintf (fp,"%04.if,",*data_ptr_Gyro+i);

    pc.printf("%s, \r\n",buffer);

    thread_sleep_for(100);

    //fprintf (fp,"%s,\r\n",buffer);

     Status_3 = LED_ON;

    }

/*for(int i=0; i<size_g; i++)

{

    fprintf (fp,"%0.4.if,",*data_ptr_Gyro+i);

    fprintf (fp,"%s,\r\n",buffer);

    }*/                

fclose(fp);

//Status_2 = LED_ON;//Data should have been recorded

}

void SDreset()

{

// creates an empty file Datalogger.txt on the SD card, old data will be lost.

FILE *fp = fopen("/sd/Gyro_Accelerometer_Datalogger.txt", "w");  

fclose(fp);

}

<>

RecordData.h file is in the project folder
<>
#ifndef RECORD_DATA_H // if RECORD_DATA_H has not previously been defined

#define RECORD_DATA_H // define it now

#include “mbed.h”

#include “SDFileSystem.h”

extern SDFileSystem sd; // allow SD card to be manipulated by other files

void Datarecord(int *ptr_A, int *ptr_G, int size_a, char size_g); // function prototype

void SDreset();

#endif
<
>

Thanks
Degs

When you want to share a code, please be so kind and use these ``` 3 symbols before and after your code like bellow or share a link.

```
// your code
```

OK, if the use case is only debug, then just delete the line with the Serial object and also cut out pc. from all printf calls, like below.

//pc.printf("%2i\n", size_a);
printf("%2i\n", size_a);

Debugging using printf() statements - Debugging and testing | Mbed OS 6 Documentation

About SD card you can look here

BR, Jan

Great thanks Jan, I’ll just cut out the pc.

I’d used the older method I think of <> my code <>.

In future I’ll use


// my code


1 Like

Here is my main.cpp code


/*

*/

#include “mbed.h”

#include “TextLCD.h”

#include “ADXL345_I2C.h”

#include “ITG3200.h”

#include “RecordData.h”

#include “GetAccelerometer.h”

#include “GetGyro.h”

/*

#define g0 9.812865328

//Number of samples to average.

#define SAMPLES 4

//Number of samples to be averaged for a null bias calculation

//during calibration.

#define CALIBRATION_SAMPLES 128

//Convert from radians to degrees.

#define toDegrees(x) (x * 57.2957795)

//Convert from degrees to radians.

#define toRadians(x) (x * 0.01745329252)

//ITG-3200 sensitivity is 14.375 LSB/(degrees/sec).

#define GYROSCOPE_GAIN (1 / 14.375)

//Full scale resolution on the ADXL345 is 4mg/LSB.

#define ACCELEROMETER_GAIN (0.004 * g0)

//Sampling gyroscope at 200Hz.

#define GYRO_RATE 0.002

//Sampling accelerometer at 200Hz.

#define ACC_RATE 0.002

//Updating filter at 100Hz.

#define FILTER_RATE 0.01

#define CORRECTION_MAGNITUDE 50

*/

/*

Ticker accelerometerTicker;

Ticker gyroscopeTicker;

Ticker filterTicker;

Ticker dataTicker;

Ticker algorithmTicker;

DigitalOut led1(LED1, “led1”);

DigitalOut led2(LED2, “led2”);

DigitalOut led4(LED4, “led4”);

*/

// Use this if using Tera Term

//Serial pc(USBTX, USBRX);

//SDFileSystem sd(p5,p6,p7,p8,“sd”); //MOSI, MISO, SCLK, CS

//Set up I2C comms and devices

I2C i2c(p9,p10);//p28,p27

I2C i2c_lcd(p9,p10); // sda, scl

DigitalOut Status_1(LED1);

#define LED_ON 1

#define LED_OFF 0

//float gyro_buffer[480];

int err;

int samples = 120; //(check wait in while loop)

int file_open = 0;

/*

Be sure to read Wim Huiskamps notes about the LCD type you are using

and adjust the function call to match your LCD type display.

*/

TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4); // I2C bus, 2004A Slaveaddress 0x4E, LCD Type

//Function declaration

//void calibrateAccelerometer(void);

void calibrateAccelerometer();

//Global Variables

int readings[3] = {0, 0, 0};

int buffer_a[3] = {0, 0, 0};

int *buffer_aptr;

int buffer_g[] = {0, 0, 0};

int *buffer_gptr;

int a_xBias, a_x = 0;

int a_yBias, a_y = 0;

int a_zBias, a_z = 0;

float gx, gy, gz = 0;

int main() {

// ...... you will need to set time first!

  set_time(1490126860);  // Set RTC time from unixtimestamp.com --1489651200

     

// These are here to test whether any of the initialization fails. It will print the failure

if (accelerometer.setPowerControl(0x00)){ 

     //pc.printf("didn't intitialize power control\n");

     lcd.printf("didn't intitialize power control\n");

     return 0;  }

 //Full resolution, +/-16g, 4mg/LSB.

 thread_sleep_for(100);

 

 if(accelerometer.setDataFormatControl(0x0B)){

    //pc.printf("didn't set data format\n");

    lcd.printf("didn't set data format\n");

    return 0;  }

 thread_sleep_for(100);

 

 //3.2kHz data rate.

 if(accelerometer.setDataRate(ADXL345_3200HZ)){

    //pc.printf("didn't set data rate\n");

    lcd.printf("didn't set data rate\n");

    return 0;    }

 thread_sleep_for(100);

 

 //Measurement mode.

 if(accelerometer.setPowerControl(MeasurementMode)) {

    //pc.printf("didn't set the power control to measurement\n");

    lcd.printf("didn't set the power control to measurement\n");

    return 0;   }

 

//First calibrate your device. The ADXL350 datasheet explains about offset.

calibrateAccelerometer();

   

//GYRO set up starts here!

//Set highest bandwidth.

gyro.setLpBandwidth(LPFBW_42HZ); //was gyro1.seLpBandwidth

//Set LCD integrted backlight to ON

lcd.setBacklight(TextLCD::LightOn);



//t.start();

            

while (1){

            

    accelerometer.getOutput(readings);

    

    //Put the 'raw' values into Ax,Ay and Az vaiables _nb (no bias).

    int Ax_nb = (int16_t)readings[0];

    int Ay_nb = (int16_t)readings[1];

    int Az_nb = (int16_t)readings[2];

    

    //Subtract the offset from the non biased raw values

    int Ax = Ax_nb - a_xBias;

    int Ay = Ay_nb - a_yBias;

    int Az = Az_nb - a_zBias;

    

    //Put results in a buffer

    buffer_a[0] = Ax;

    buffer_a[1] = Ay;

    buffer_a[2] = Az;

            

    buffer_aptr = &buffer_a[0];

    /*pc.printf("Ax%2.2i\n",buffer_aptr[0]);

    wait(3);

    buffer_aptr = &buffer_a[1];

    pc.printf("Ay%2.2i\n",buffer_aptr[1]);

    wait(3);

    buffer_aptr = &buffer_a[2];

    pc.printf("Az%2.2i\n",buffer_aptr[2]);

    */

    //wait(3);        

    /*

     To get acceleration due to gravity in the 3 axis multiply the 

     the offset adjusted raw value by the resolution 4mg per LSB

     noting 250 is 1g.

    */

    gx = Ax * 0.004;

    gy = Ay * 0.004;

    gz = Az * 0.004;

            

    int Gx = GyroReading_x();

    int Gy = GyroReading_y();

    int Gz = GyroReading_z();

    buffer_g[0] = Gx;

    buffer_g[1] = Gy;

    buffer_g[2] = Gz;

                  

    buffer_gptr = &buffer_g[0];

    

    Datarecord(buffer_aptr, buffer_gptr, sizeof (buffer_a), sizeof (buffer_g));

    Status_1 = LED_ON;                

                        

    //Lets look at some results if they make any sense!

    lcd.cls();

    

    //lcd.printf("ITG3200 DevID 0x%02X\n",gyro.getWhoAmI()); //was gyro1.getWhoAmI()

    //lcd.printf("ADXL DevID is: 0x%02X\n", accelerometer.getDeviceID());

    

    //Output of ADXL345 without calibration function

    lcd.locate(0,1);

    //lcd.printf("Ax%i Ay%i Az%i\n", Ax_nb, Ay_nb, Az_nb);

    //lcd.locate(0,2);

    //lcd.printf("xB%i yB%i zB%i\n",a_xBias, a_yBias, a_zBias);

    //lcd.locate(0,3);

    //Output of ADXL345 with calibration function

    lcd.printf("AxB%i AyB%i AzB%i\n", Ax, Ay, Az);

    lcd.locate(0,3);

    /*

     Outut of each axis in terms of force due to gravity. By looking at the

     reference document AN-177 and orientating the chip to match you should

     see each of the axis display 1g aginst its orientation.

    */ 

    lcd.printf("gx%03.2f gy%03.2f gz%03.2f\r\n", gx, gy, gz);

    //lcd.printf("gz%04.3f\n",gz);

    //lcd.printf("Gx%i Gy%i Gz%i\n\r", Gx, Gy, Gz);

    

    /*

     This temperature is included for completeness refer to the ADXL350

     datasheet for temperature effects.

    */  

    //lcd.printf("Temperature %2.1f \n",gyro.getTemperature());

    

    thread_sleep_for(1000);

                    

    }

}


RecordData.h is clearly in my project folder.
I now get the following Errors I never had before?
Use of undeclared identifier ‘accelerometer’
Use of undeclared identifier ‘gyro’
Use of undeclared identifier ‘GyroReading_x’

Sturggling to find a reason why under OS6.15.1?

Regards,
Degs

It depends what a tool reports this error. Compiler(AC6 or ARM GCC) or Intelisence (clangd)?

BR, Jan

Hi Jan,
Then I’m assuming its Intelisence as the full error reads:
Use of undeclared identifier ‘accelerometer’ cland(undeclared_var_use) [94,9]

Any idea of a solution? between something that compiles and builds in OS3.

Thanks
Degs

I suppose the error is present because you do not have declaration of those objects in the main, but in another .h file. I am not sure, but that are rules what are set in the clangd, and can probably be ignored if the code can be compiled. Of course best option is rewrite it to standard class.

Br, Jan