AnsweredAssumed Answered

ADIS 16475 Output Problems Using Raspberry Pi CM3

Question asked by IvanZhu on Jan 22, 2018
Latest reply on Jan 25, 2018 by NevadaMark
Branched to a new discussion

Hello everyone,

 

NevadaMark, hello NevedaMark, may I take up you for a few minutes ^_^) 

 

I am trying to set up an ADIS 16475-1BMLZ imu using a Raspberry Pi Compute Module 3 as the master spi controller, using the GPIO pins on the pi as SPI pins.  I have successfully set up and attached the imu to the pi. However I am getting some weird outputs when I try to read anything from the imu.  I am using the bcm2835 driver (found here) to control the gpio pins on the pi with the following C code:

 

#include <bcm2835.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>

int initialize(void){
    if (!bcm2835_init()){
        return -1;
    }
    bcm2835_spi_begin();  //initializing spi mode on rpi
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //setting MSB read/write first
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); //setting CPOL=1; CPHA=1.
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_512); //Setting the clock frequency to 781.25khz
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);  //selecting which pin to use for cs line
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0,LOW); //setting chip select pin to be active on low signal
    return 0;

}

int getdata(double *ax, double *ay, double  *az, double *gx, double *gy, double *gz){

    char buf1[]={0x10, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00};
    bcm2835_spi_transfern(buf1,sizeof(buf1));  //Sending command to read XACCL and receiving output
    usleep(50);

    char buf2[]={0x14, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00};
    bcm2835_spi_transfern(buf2,sizeof(buf2));  //Sending command to read YACCL and receiving output
    usleep(50);

    char buf3[]={0x18, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00};
    bcm2835_spi_transfern(buf3,sizeof(buf3));  //Sending command to read ZACCL and receiving output
    usleep(50);


    char buf4[]={0x04, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00};
    bcm2835_spi_transfern(buf4,sizeof(buf4));  //Sending command to read XGYRO and receiving output
    usleep(50);

    char buf5[]={0x08, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00};
    bcm2835_spi_transfern(buf5,sizeof(buf5));  //Sending command to read YGYRO and receiving output
    usleep(50);

    char buf6[]={0x0C, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00};
    bcm2835_spi_transfern(buf6,sizeof(buf6));  //Sending command to read ZGYRO and receiving output
    usleep(50);


    uint32_t temp1=((uint32_t)buf1[6]<<24)+((uint32_t)buf1[7]<<16)+((uint32_t)buf1[3]<<8)+(uint32_t)buf1[4];
    int32_t output1=((int32_t)(temp1<<2)>>2);
    *ax=output1*0.25/pow(2,16)*9.8/1000;
    //if(abs(output1/300.3*9.8)>20) *ax=0;

    uint32_t temp2=((uint32_t)buf2[6]<<24)+((uint32_t)buf2[7]<<16)+((uint32_t)buf2[3]<<8)+(uint32_t)buf2[4];

    int32_t output2=((int32_t)(temp2<<2)>>2);
    *ay=output2*0.25/pow(2,16)*9.8/1000;
    //if(abs(output2/300.3*9.8>20)) *ay=0;

    uint32_t temp3=((uint32_t)buf3[6]<<24)+((uint32_t)buf3[7]<<16)+((uint32_t)buf3[3]<<8)+(uint32_t)buf3[4];

    int32_t output3=((int32_t)(temp3<<2)>>2);
    *az=output3*0.25/pow(2,16)*9.8/1000;
    //if(abs(output3/300.3*9.8>20)) *az=0;

    uint32_t temp4=((uint32_t)buf4[6]<<24)+((uint32_t)buf4[7]<<16)+((uint32_t)buf4[3]<<8)+(uint32_t)buf4[4];

    int32_t output4=((int32_t)(temp4<<2)>>2);
    *gx=output4*0.00625/pow(2,16);
    //if(abs(output1/300.3*9.8)>20) *ax=0;

    uint32_t temp5=((uint32_t)buf5[6]<<24)+((uint32_t)buf5[7]<<16)+((uint32_t)buf5[3]<<8)+(uint32_t)buf5[4];

    int32_t output5=((int32_t)(temp5<<2)>>2);
    *gy=output5*0.00625/pow(2,16);
    //if(abs(output2/300.3*9.8>20)) *ay=0;

    uint32_t temp6=((uint32_t)buf6[6]<<24)+((uint32_t)buf6[7]<<16)+((uint32_t)buf6[3]<<8)+(uint32_t)buf6[4];

    int32_t output6=((int32_t)(temp6<<2)>>2);
    *gz=output6*0.00625/pow(2,16);
    //if(abs(output3/300.3*9.8>20)) *az=0;

return 0;

}

int diag(int *x){

    char buf[]={0x72, 0x00, 0x00, 0x00, 0x00};
    bcm2835_spi_transfern(buf,sizeof(buf));  //Sending command to read PROD_ID(16475) and receiving output
    usleep(50);


    uint16_t temp=((uint16_t)buf[3]<<8)+(uint16_t)buf[4];
    //int16_t output=((int16_t)(temp<<2)>>2);
    *x = temp;


return 0;
}

int reset(void){
     //char buf[] ={0xE8, 0x80, 0x00, 0xE9, 0x00}; //Command to software reset
     char buf0[] ={0xE8, 0x80, 0xE9, 0x00};

     bcm2835_spi_transfern(buf0,sizeof(buf0));  //Sending command to ADIS16475 and receiving output $
     sleep(1);


     char buf2[]={0xCC, 0x04, 0xCD, 0x00};  //set filter
     bcm2835_spi_transfern(buf2,sizeof(buf2));

     sleep(1);

return 0;
}

int main(int argc, char **argv){
     int suc=initialize();
     if (suc!=0){ //checking to see if the settings were successfully applied
          return -1;

     }

     reset();

     int diagx;
     double ax,ay,az,gx,gy,gz;
     while(1){
        //diag(&diagx);
        getdata(&ax, &ay, &az, &gx, &gy, &gz);

        printf("The output is: %f %f %f %f %f %f. \n", ax, ay, az, gx, gy, gz);
        usleep(500000);
        //diag(&diagx);

     }


return 0;
}

The output is like:

The output is: 0.000427 0.112700 0.002209 0.025000 0.002809 -0.300000.
The output is: 0.002278 0.098000 0.002017 0.118750 0.005752 -0.181250.
The output is: 0.002340 0.122500 0.000444 -0.031250 0.004882 -0.181250.

……

Make no sense at all!

But dramatically when I uncomment  'diag(&diagx)' in the main():

int main(int argc, char **argv){
     int suc=initialize();
     if (suc!=0){ //checking to see if the settings were successfully applied
          return -1;

     }

     reset();

     int diagx;
     double ax,ay,az,gx,gy,gz;
     while(1){
        diag(&diagx);
        getdata(&ax, &ay, &az, &gx, &gy, &gz);
        printf("The output is: %f %f %f %f %f %f. \n", ax, ay, az, gx, gy, gz);
        usleep(500000);
        diag(&diagx);

     }

return 0;
}

The output becomes:

The output is: -0.570850 0.002362 9.800000 0.005357 -0.337500 0.003840.
The output is: -0.575750 0.000137 9.787750 0.002891 -0.412500 0.002506.
The output is: -0.573300 0.001218 9.780400 0.003049 -0.437500 0.000574.
The output is: -0.575750 0.000076 9.790200 0.003477 -0.387500 0.001208.
The output is: -0.575750 0.001771 9.787750 0.004292 -0.393750 0.000924.
The output is: -0.583100 0.000693 9.777950 0.002596 -0.443750 0.004066.
The output is: -0.583100 0.002202 9.790200 0.004016 -0.400000 0.003093.
The output is: -0.580650 0.000213 9.773050 0.003613 -0.412500 0.005186.
The output is: -0.583100 0.000749 9.773050 0.003560 -0.418750 0.003802.
The output is: -0.580650 0.000726 9.780400 0.005325 -0.362500 0.000077.
The output is: -0.573300 0.000115 9.785300 0.003564 -0.412500 0.004130.
The output is: -0.597800 0.000532 9.777950 0.003987 -0.375000 0.006085.
The output is: -0.575750 0.000465 9.787750 0.002644 -0.418750 0.002522.
The output is: -0.565950 0.000326 9.777950 0.002632 -0.406250 0.000168.
The output is: -0.570850 0.002197 9.797550 0.003444 -0.456250 0.002804.
The output is: -0.595350 0.002364 9.753450 0.004531 -0.306250 0.004525.
The output is: -0.573300 0.001321 9.768150 0.004684 -0.387500 0.000177.
The output is: -0.585550 0.001747 9.741200 0.002841 -0.543750 0.002825.
The output is: -0.632100 0.000392 9.741200 0.003959 -0.350000 0.004181.
The output is: -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000.
The output is: 0.002353 0.098000 0.000079 -0.068750 0.003597 -0.237500.
The output is: 0.000592 0.115150 0.001486 -0.006250 0.003185 -0.187500.
The output is: 0.001745 0.144550 0.000434 -0.018750 0.005471 -0.262500.
The output is: 0.001870 0.093100 0.001577 0.037500 0.003724 -0.125000.
The output is: 0.001627 0.110250 0.000347 -0.081250 0.003763 -0.225000.
The output is: 0.000959 0.129850 0.000526 -0.100000 0.002289 -0.275000.

It seems to be right for the first several lines, but finally dead……

Does anyone have any ideas as to why the output is so weird and some way I can fix it?

 

Thanks so much,

Ivan

Outcomes