0

@everyone, I have some problem in reading data form IMU recently. Below is the data I got from My device, it is ASCII, all are chars,and my data size is [122], which is really big, I need convert them to short, and then float, but I dont know why and how.....

unsigned char data[33];
short x,y,z;
float x_fl,y_fl,z_fl,t_fl;
float bias[3]={0,0,0};//array initialization
unsigned char sum_data=0;
int batch=0;
if ( !PurgeComm(file,PURGE_RXCLEAR ))
    cout << "Clearing RX Buffer Error" << endl;//this if two sentence aim to clean the buffer

//---------------- read data from IMU ----------------------
do { ReadFile(file,&data_check,1,&read,NULL);
       //if ((data_check==0x026))
       { ReadFile(file,&data,33,&read,NULL); }

       ///  Wx Values               
       {
          x=(data[8]<<8)+data[9];                               
          x_fl=(float)6.8664e-3*x;                                                  
          bias[0]+=(float)x_fl;                                               
        }

        ///  Wy Values                                      
        {
          y=(data[10]<<8)+data[11];
          y_fl=(float)6.8664e-3*y;
          bias[1]+=(float)y_fl;                                                 
        }

        ///  Wz Values                      
        {
          z=(data[12]<<8)+data[13];
          z_fl=(float)6.8664e-3*z; 
          bias[2]+=(float)z_fl;                                                 
        }

        batch++;

}while(batch<NUM_BATCH_BIAS);

$VNYMR,+049.320,-017.922,-024.946,+00.2829,-00.2734,+00.2735,-02.961,+03.858,-08.325,-00.001267,+00.000213,-00.001214*64

$VNYMR,+049.322,-017.922,-024.948,+00.2829,-00.2714,+00.2735,-02.958,+03.870,-08.323,+00.004923,-00.000783,+00.000290*65

$VNYMR,+049.321,-017.922,-024.949,+00.2821,-00.2655,+00.2724,-02.984,+03.883,-08.321,+00.000648,-00.000391,-00.000485*61

$VNYMR,+049.320,-017.922,-024.947,+00.2830,-00.2665,+00.2756,-02.983,+03.874,-08.347,-00.003416,+00.000437,+00.000252*6C

$VNYMR,+049.323,-017.921,-024.947,+00.2837,-00.2773,+00.2714,-02.955,+03.880,-08.326,+00.002570,-00.001066,+00.000690*67

$VNYMR,+049.325,-017.922,-024.948,+00.2847,-00.2715,+00.2692,-02.944,+03.875,-08.344,-00.002550,+00.000638,+00.000022*6A

$VNYMR,+049.326,-017.921,-024.945,+00.2848,-00.2666,+00.2713,-02.959,+03.876,-08.309,+00.002084,+00.000449,+00.000667*6A

all I want to do is:

  1. extract last 6 numbers separated by commas, btw, I don't need the last 3 chars(like *66).
  2. Save the extracted data to 6 .dat files.

What is the best way to do this?

Since I got this raw data from IMU, and I need the last 6 data, which are accelerations(x,y,z) and gyros(x,y,z).

  1. If someone could tell me how to set a counter to the end of each data stream, that will be perfect, because I need the time stamp of IMU also.

Last word is I am doing data acquisition under windows, c++.

Hope someone could help me, I am freaking out because of so much things to do and that's really annoying!!

flowera
  • 799
  • 8
  • 11
  • Have you tried anything yet? Written any code. Take a look at the manual page for scanf. It lets you detect but not assign some numbers and assign others. It will tell how many it has assigned, so you can check if it found what you expect. – BryanT Feb 13 '16 at 01:10
  • How would you do it if those commas were spaces? – Beta Feb 13 '16 at 01:25
  • scanf will skip white space, – BryanT Feb 13 '16 at 02:26

1 Answers1

0

There's a whole family of scanf functions (fscanf, sscanf and some "secure" ones). Assuming you have read a line into a string:-

sscanf( s, "VNYMR,%*f,%*f,%*f,%*f,%*f,%*f,%f,%f,%f,%f,%f,%f", &accX, &accY, &accZ, &gyroX, &gyroY, &gyroZ )

And assuming I have counted correctly! This will verify that the literal $VNYMR is there, followed by about five floats that you don't assign and finally the six that you care about. &accaX, etc are the addresses of your floats. Test the result - the number of assignments made..

BryanT
  • 412
  • 3
  • 12
  • The problem is I am reading data from device and through each reading loop, I get data from device for 7 batches. Each batch I need to read and store data to a file, and the 6 parameters I need to store them to 6 files. – flowera Feb 13 '16 at 19:46
  • My answer didn't address the major looping and writing. I thought the issue you had was with parsing each line and handling only some of the floats. What have you written so far to try to handle the reading an d writing to file? Please post your code. – BryanT Feb 14 '16 at 19:21
  • do { ReadFile( hPort, &data, 122, &read, NULL); if (batch==0) { for(int i = 0; i< 122; i++){ fprintf(pFile,"%c",data[i]); } – flowera Feb 15 '16 at 16:55
  • Please see updated version above, Thanks very much, the above code is actually calculating the bias, however my goal is to get the acc and gyro, and I believe you can know what is happening if you know serial communication, I set the data size to 122, to get the result attached at the end of the code. – flowera Feb 15 '16 at 17:16
  • T^T it is confusing I know, but you know what I wanna get based on the sscanf you give me, that seems solves the problem if there is only one string.... – flowera Feb 15 '16 at 17:17