MPU 6050- code working but offsets increasing leading to increasing angle without moving in yaw

138 views Asked by At

My mpu 6050 code is working but the problem is that the offsets are increasing continuously which results in increasing angle without moving. Here is my code:

#include<avr/io.h>
float x, y, z, k, bam,angle;
////////////////////////////////
///////////////////////////////////////
float baf;
float j = 0.0, i = 0;
unsigned long current_time = 0;
unsigned long previous_time = 0;
unsigned long time_interval = 0;
float gyro_offset_z = 0.0;
void i2cinit(void)
{
  TWSR = 0x00;
  TWBR = 0x42;
  TWCR = (1 << TWEN);
}
void i2cstart(void)
{
  TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
  while (!(TWCR & (1 << TWINT)));
}

void i2cwrite( unsigned char data)
{
  TWDR = data;
  TWCR = (1 << TWINT) | (1 << TWEN);
  while (!(TWCR & (1 << TWINT)));
}

unsigned int i2c_receive(unsigned char isLast) {
  if (isLast == 0)
  {
    TWCR |= (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
  }
  else
  {
    TWCR = (1 << TWINT) | (1 << TWEN);
  }
  while (!(TWCR & (1 << TWINT)));
  return TWDR;
}

void i2cstop(void)
{
  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);

}

void mpu6050_init(void) {
  i2cstart();
  i2cwrite(0xD0);
  i2cwrite(0x6B);
  i2cwrite(0);
  i2cstop();
}
void mpu6050f()
{
  i2cstart();
  i2cwrite(0xD0);
  i2cwrite(0x1B);
  i2cwrite(0x18);
  i2cstop();
}
unsigned char mpu6050rbyte(int gyro) {
  i2cstart();
  i2cwrite(0xD0);
  i2cwrite(gyro);
  i2cstart();
  i2cwrite(0xD0 | 0x01);
  unsigned char i = i2c_receive(1);
  i2cstop();
  return i;
}
int mpu6050_whole_data(unsigned char a, unsigned char b) {
  unsigned char msb = mpu6050rbyte(a);
  unsigned char lsb = mpu6050rbyte(b);
  int x_gyro = (msb << 8) | lsb;
  return x_gyro;
}

void calibrateGyroOffset() {
  const int numSamples = 1100;
  for (k = 0; k < numSamples; k++) {
    gyro_offset_z += mpu6050_whole_data(0x47, 0x48);
    if ( k < 100)
    {
      gyro_offset_z = 0;
    }
  }
  gyro_offset_z /= 1000;
}

float mpu6050angle(int woho, float gyro) {
  float angular_velocity = (woho - gyro) / 16.4;
  if (angular_velocity > 0.1 || angular_velocity < -0.1)
  {
    angle = (angular_velocity * time_interval) / 1000.0;
  }
  return angle;
}

i used the mpu 6050 for calculating yaw angle using gyro and was expecting the initial readings to be 0. but the angle kept increasing 0.. 0.01... 0.02... and so on please resolve this issue as the offsets are not working properly

1

There are 1 answers

0
dvd280 On

Gyro drift is a built in flaw that every gyro has, using offsets usually wont work, because as the gyro moves, the drift also changes, so the offset will also need to change. This is usually solved by pairing the gyro with an accelerometer, or alternatively using a digital filter, so that only values above/below some level are registered (this way - the small drift values are discarded).

So if you are seeing values like 0.0001 ... 0.0019 etc, you can just decide that anything below 0.01 is not integrated into your angle calculation.