Wednesday, December 11, 2019

ADXL345 Collecting data and plotting with GNU Octave



int main () {
FILE * fp;
....


while (1) {
....

fp = fopen ("mpu6050.dat", "a");

fprintf(fp," %f, %f, %f %f, %f, %f, %f \n", xa, ya,za,xg,yg,zg,T);
fclose(
fp);
sleep(1);
}


Complete code 
 
// Build with:     gcc -o adxl345 adxl345.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <linux/i2c-dev.h>

    #define ADXL345_I2C_ADDR 0x53

    void selectDevice(int fd, int addr, char * name)
    {
       if (ioctl(fd, I2C_SLAVE, addr) < 0)
       {
          fprintf(stderr, "%s not present\n", name);
          //exit(1);
       }
    }

    void selectRegister(int fd, int reg)
    {
       char buf[1];
       buf[0]=reg;
       if (write(fd, buf, 1) != 1)  {
          fprintf(stderr, "Can't write to i2c slave\n");
       }
    }

   
    void writeToDevice(int fd, int reg, int val)
    {
       char buf[2];
       buf[0]=reg; buf[1]=val;
       if (write(fd, buf, 2) != 2)
       {
          fprintf(stderr, "Can't write to ADXL345\n");
       }
    }

    int main(int argc, char **argv)
    {

     FILE * fp;
       unsigned int range;
       int count, b;
       short x, y, z;
       float xa, ya, za;
       int fd;
       unsigned char buf[16];
      
       if ((fd = open("/dev/i2c-1", O_RDWR)) < 0)
       {
          // Open port for reading and writing
          fprintf(stderr, "Failed to open i2c bus\n");
          exit(1);
       }
      
       selectDevice(fd, ADXL345_I2C_ADDR, "ADXL345");

       writeToDevice(fd, 0x2d, 0);
       writeToDevice(fd, 0x2d, 16);
       writeToDevice(fd, 0x2d, 8);
       writeToDevice(fd, 0x31, 0);
       writeToDevice(fd, 0x31, 11);

       while (1)
       {  
          selectDevice(fd, ADXL345_I2C_ADDR, "ADXL345");
          selectRegister(fd,0x32 );
     
          if (read(fd, buf, 6) != 6)   {   //  X, Y, Z accelerations
             fprintf(stderr, "Unable to read from ADXL345\n");
          }
          else
          {
             x = buf[1]<<8| buf[0];
             y = buf[3]<<8| buf[2];
             z = buf[5]<<8| buf[4];
             printf("x: %d y: %d z: %d\n", x, y, z);

     fp = fopen ("adxl345.dat", "a");

            fprintf(fp,"  %d  %d  %d\n", x, y, z);
       fclose(fp);




          }
          sleep(1);
       }
      
       return 0;
    }

################################################


can run the code in background with the command

./mpu6050 &

Because you are running the data collection code inside a NFS mounted directory, the data file generated will be directly visible from the PC. In the PC, you can see the data with the command

cat mpu6050.dat


Bus this is not a very convenient way to understand data. A better way is to plot graphs.

Plotting data

Many data plotting packages are available for visualizing data. One available in your computers is Octave.

Octave is a package very similar to Matlab. Many Matlab m-files can be executed in Octave without any change.

For plotting your data in Octave, start Octave and change directory to your NFS shared directory. Then the following commands can be used to plot the three acceleration components on the same graph.

s=load("adxl345.dat");
plot(s(:,1));
hold on
plot(s(:,2));
plot(s(:,3));


ADXL345: If you are using one of these modules, plot the three components of acceleration in one graph.

No comments:

Search This Blog