SD card write function affect the sensor datas in KL25Z

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SD card write function affect the sensor datas in KL25Z

Jump to solution
1,437 Views
deepir
Contributor I

SD card data write affect the sensor read:  In my program i used one accelerometer(MMA8451Q) , Magnetometer(FXOS8700Q), gyroscope sensor(FXAS21000) and KL25Z, I read all the datas from sensors perfectly, Now i try to store the datas in sd card, In that time i use fopen function, that time sensor datas are not read correctly(Gyroscope and magnetometer In that time also Accelerometer read correct value), please help me, In my project i used FRDM-KL25Z and FRDM-FXS-MULTI-Rev C.

I insert the code below

  1. include "mbed.h"
  2. include "MMA8451Q.h"
  3. include "FXAS21000.h"
  4. include "FXOS8700Q.h"
  5. include "SDFileSystem.h"
  1. if  defined (TARGET_KL25Z) || defined (TARGET_KL46Z) PinName const SDA = PTE25; PinName const SCL = PTE24;
  2. elif defined (TARGET_KL05Z) PinName const SDA = PTB4; PinName const SCL = PTB3;
  3. elif defined (TARGET_K20D50M) PinName const SDA = PTB1; PinName const SCL = PTB0;
  4. else
  5. error TARGET NOT DEFINED
  6. endif
  1. define MMA8451_I2C_ADDRESS (0x1d<<1)

FXOS8700Q_mag mag( A4, A5, FXOS8700CQ_SLAVE_ADDR0); Proper Ports and I2C address for Freescale Multi Axis shield

MotionSensorDataUnits mag_data;

MotionSensorDataCounts mag_raw;

FXAS21000 gyro( A4, A5); Serial pc(USBTX, USBRX);

SDFileSystem sd(D11, D12, D13, A2, "sd"); MOSI, MISO, SCK, CS FILE *fp;

int main(void) { float gyro_data[3]; float x, y, z; int i; pc.baud(115200);

  MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); mag.enable(); printf("MMA8451 ID: %d\n", acc.getWhoAmI());

  fp = fopen("/sd/straightLine_CalInertialAndMag.csv", "r"); remove("/sd/straightLine_CalInertialAndMag.csv");   fp = fopen("/sd/straightLine_CalInertialAndMag.csv", "a");   fprintf(fp,"Packet number,Gyroscope X (deg/s),Gyroscope Y (deg/s),Gyroscope Z (deg/s),Accelerometer X (g),Accelerometer Y (g),Accelerometer Z (g),Magnetometer X (G),Magnetometer Y (G),Magnetometer Z (G)\n\r"); fclose(fp); for(i=0;i<250;i++) { fp = fopen("/sd/straightLine_CalInertialAndMag1.csv", "a"); fprintf(fp,"%4.2f,%4.2f,%4.2f,%1.2f,%1.2f,%1.2f,%4.1f,%4.1f,%4.1f,\n\r",gyro_data[0], gyro_data[1], gyro_data[2],x,y,z,mag_data.x, mag_data.y, mag_data.z); fclose(fp);

  x = abs(acc.getAccX()); y = abs(acc.getAccY()); z = abs(acc.getAccZ());

  gyro.ReadXYZ(gyro_data);

  mag.getAxis(mag_data); wait(0.2);

   if (fp == NULL) { pc.printf("Unable to write the file\r\n"); } else { fclose(fp); 

   } printf("%1.2f,%1.2f,%1.2f,%4.2f,%4.2f,%4.2f,%4.1f,%4.1f,%4.1f,\n\r", x,y,z,gyro_data[0],gyro_data[1],gyro_data[2],mag_data.x,mag_data.y,mag_data.z); wait(0.1);

  } }

Labels (1)
0 Kudos
Reply
1 Solution
1,049 Views
Amit_Kumar1
Senior Contributor II

Hi Deepi

Based on your inputs, There are few suggestions from my side,

1. FXOS8700CQ can be used for extracting accelerometer + magnetometer data. As the no. of devices increases on I2C bus , there are chances of Bus faults. If you use a combined sensor then the data coming out will be much synchronized.

2.If you look at page 47 of Datasheet of FXAS21000 Gyro  ERRATA A.1. There it is mentioned that It needs to be connected to a dedicated I2C bus (Without sharing with other sensors as it will give erroneous data) which in your case is happening.

    Workaround:-

    connect it to different I2C /SPI without sharing line with any other devices.

3. If you are using FATFs while each time closing/opening files takes around 2-3 milliseconds which makes the program to hang for that duration. Due to this you will miss some entries.

   workaround:-

    use fopen at the beginning of the program and fclose at the end , each time after fwrite, use "f_lseek" ,which will take less time. Or you can buffer the readings and use f_lseek at some regular time intervals so that data is not lost if accidentally the card is removed/ power failure.

4. If using FATFs , It is clearly mentioned that it doesn't support floating point data write

   workaround:-

   use sprintf and convert floating point to string and print it as a string.

I faced some of these issues and this is how I resolved.

Hope It Helps!

Kind Regards

Amit

View solution in original post

0 Kudos
Reply
5 Replies
1,049 Views
Amit_Kumar1
Senior Contributor II

Hi Deepi

I have few questions, I hope answering these ques will help you to resolve the issues faster

1. At what rate you are extracting the data from sensor?

2. Are all the sensors sharing the I2c/SPI bus?

3. Are you creating and closing the file each time writing the data?

4. Data being stored is int or float ?

Regards

Amit

0 Kudos
Reply
1,049 Views
deepir
Contributor I

1.I am using the data rate at 100khz.

2.All sensors sharing I2C

3.Yes

4.Float

0 Kudos
Reply
1,050 Views
Amit_Kumar1
Senior Contributor II

Hi Deepi

Based on your inputs, There are few suggestions from my side,

1. FXOS8700CQ can be used for extracting accelerometer + magnetometer data. As the no. of devices increases on I2C bus , there are chances of Bus faults. If you use a combined sensor then the data coming out will be much synchronized.

2.If you look at page 47 of Datasheet of FXAS21000 Gyro  ERRATA A.1. There it is mentioned that It needs to be connected to a dedicated I2C bus (Without sharing with other sensors as it will give erroneous data) which in your case is happening.

    Workaround:-

    connect it to different I2C /SPI without sharing line with any other devices.

3. If you are using FATFs while each time closing/opening files takes around 2-3 milliseconds which makes the program to hang for that duration. Due to this you will miss some entries.

   workaround:-

    use fopen at the beginning of the program and fclose at the end , each time after fwrite, use "f_lseek" ,which will take less time. Or you can buffer the readings and use f_lseek at some regular time intervals so that data is not lost if accidentally the card is removed/ power failure.

4. If using FATFs , It is clearly mentioned that it doesn't support floating point data write

   workaround:-

   use sprintf and convert floating point to string and print it as a string.

I faced some of these issues and this is how I resolved.

Hope It Helps!

Kind Regards

Amit

0 Kudos
Reply
1,049 Views
deepir
Contributor I

Thank you for ur wonderful help

0 Kudos
Reply
1,049 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Deepi R,

I've reviewed your question and let me make your question clearly.

The demo read the these sensor values well, the SD operation also works well by itself. On other hand, it can't read correct sensor value when they work together.

I think you must use the SPI interface to operate the SD card, so I want to know whether the SPI interface also share with the sensors or not.

And I'd like to suggest that you can try this method that after you had read all sensor values successful, then create a file in SD card.

Hope this reply help.
Have a great day,
(my name)

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply