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
- include "mbed.h"
- include "MMA8451Q.h"
- include "FXAS21000.h"
- include "FXOS8700Q.h"
- include "SDFileSystem.h"
- if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) PinName const SDA = PTE25; PinName const SCL = PTE24;
- elif defined (TARGET_KL05Z) PinName const SDA = PTB4; PinName const SCL = PTB3;
- elif defined (TARGET_K20D50M) PinName const SDA = PTB1; PinName const SCL = PTB0;
- else
- error TARGET NOT DEFINED
- endif
- 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);
} }