I am reading data from a gyroscope (MPU6050). This gives me angular speed. I am trying to integrate this data to know the angle of the gyro. The way I do this is to multiply the read data with the elapsed time and continuously add these results, i.e. integration:

But it does not work, this is my code and the output I get:
#include <stdio.h> #include <stdint.h> #include <time.h> #include <sys/types.h> #include <wiringPi.h> #include <wiringPiI2C.h> #define A_SCALE (16384.0) #define ANG_SCALE (131.0) int main(int argc, char *argv[]) { int fd; int data; int i=0; float gyroXangle=0; float gyroYangle=0; float gyroZangle=0; float gyroOffset = 151; float gyroScale = 0.02; struct timeval startTime, stopTime; long timeDifference; int i=0; wiringPiSetup(); fd = wiringPiI2CSetup(0x68); wiringPiI2CWriteReg8(fd, 0x6b, 0); if(fd==-1) { printf("can't setup device\n"); return -1; } else { printf("successfully setup device\n"); while(1) { msb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+8); lsb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+9); short gyroX = msb << 8 | lsb; msb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+10); lsb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+11); short gyroY = msb << 8 | lsb; msb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+12); lsb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+13); short gyroZ = msb << 8 | lsb; //to know elapsed time between two successive readings if(i%2==0) { gettimeofday(&startTime, NULL); } else { gettimeofday(&stopTime, NULL); } if(i>=1) { timeDifference = abs((int)(stopTime.tv_sec - startTime.tv_sec)*1000000 + (stopTime.tv_usec - startTime.tv_usec)); printf("timeDifference: %d\n", timeDifference); } i++; gyroXangle = ((gyroX/ANG_SCALE) * timeDifference); printf("gyro x: %fx angle: %f \n", gyroX/ANG_SCALE, gyroXangle); sleep(1); } } return 0; }
the corresponding output when the gyroscope simply lays on the table:
gyro x: -0.442748 x angle: -0.000000 timeDifference: 1006761 gyro x: -0.389313 x angle: -391945.125000 timeDifference: 1006744 gyro x: -0.389313 x angle: -391938.500000 timeDifference: 1006755 gyro x: -0.419847 x angle: -422683.406250 timeDifference: 1006731 gyro x: -0.351145 x angle: -353508.593750 timeDifference: 1006861 gyro x: -0.267176 x angle: -269008.656250 timeDifference: 1006716 gyro x: -0.343511 x angle: -345818.468750
Can someone explain to me what I can do wrong?
EDIT
updated code: #include <stdio.h> #include <stdint.h> #include <time.h> #include <sys/types.h> #include <wiringPi.h> #include <wiringPiI2C.h> #define MPU6050_REG_DATA_START 0x3b #define A_SCALE (16384.0) #define ANG_SCALE (131.0) int main(int argc, char *argv[]) { int fd; int data; int i=0; float gyroXangle=0; float gyroYangle=0; float gyroZangle=0; float gyroOffset = 151; float gyroScale = 0.02; struct timeval startTime, stopTime; double timeDifference; wiringPiSetup(); fd = wiringPiI2CSetup(0x68); wiringPiI2CWriteReg8(fd, 0x6b, 0); if(fd==-1) { printf("can't setup device\n"); return -1; } else { printf("successfully setup device\n"); while(1) { //start_time = gettime_now.tv_nsec; uint8_t msb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+8); uint8_t lsb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+9); short gyroX = msb << 8 | lsb; msb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+10); lsb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+11); short gyroY = msb << 8 | lsb; msb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+12); lsb = wiringPiI2CReadReg8(fd, MPU6050_REG_DATA_START+13); short gyroZ = msb << 8 | lsb; if(i%2==0){ gettimeofday(&startTime, NULL); } else{ gettimeofday(&stopTime, NULL); } if(i>=1) { timeDifference = abs((stopTime.tv_sec - startTime.tv_sec)+ (stopTime.tv_usec - startTime.tv_usec)/10.0e6); printf("timeDifference: %d\n", timeDifference); } i++; gyroXangle += ((gyroX/ANG_SCALE) * timeDifference); printf("gyro x: %fx angle: %f \n", gyroX/ANG_SCALE, gyroXangle); //sleep(1); } } return 0; }
corresponding output:
gyro x: -0.442748 x angle: 0 timeDifference: 0 gyro x: -0.389313 x angle: 0 timeDifference: 0 gyro x: -0.389313 x angle: 0 timeDifference: 0 gyro x: -0.419847 x angle: 0 timeDifference: 0 gyro x: -0.351145 x angle: 0 timeDifference: 0 gyro x: -0.267176 x angle: 0 timeDifference: 0 gyro x: -0.343511 x angle: 0