10 years, 2 months ago.

SD card data write affect the sensor read

In my program i used one accelerometer , Magnetometer, gyroscope sensors, 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, plase help me, 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);

} }

You should call these (gyro.ReadXYZ(gyro_data);

mag.getAxis(mag_data);) before fprintf. After that is there a difference with printf and fprintf values?

posted by Compass Developer 16 Sep 2014

i already tried with that (Like before fprintf)

posted by Deepi R 16 Sep 2014

Now i have a another problem related above when i run in continuous loop that time sensor datas are not read correctly please anyone help me

posted by Deepi R 25 Sep 2014
Be the first to answer this question.