Hi everyone,
My code below is obtaining angles from an MPU6050, but the problem I am having is with the last 10 lines of code where it sets a delay so that the time of one loop is always the same. Having set the loop time to 2 seconds, when you read the serial data port the data is coming through at 40Hz, so the adaptive delay if statement isn't working for some reason and I really don't know why. Thanks!
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
float accelangleY; float accelangleX;
float gyroY; float gyroX; double gyroZ;
float compangleX=0; float compangleY=0; double gyroangleZ = 0;
unsigned long starttime=0;
unsigned long delta;
unsigned long endtime;
long looptime = 2000; long looptimemicros;// Loop time in milli seconds
float gain = 0.9; // complementary filter gain
MPU6050 mpu;
// Offset Values: XA YA ZA XG YG ZG
int MPUOffsets[6] = { -1745, 1704, 1362, 79, -18, 21};
void setup(){
delay(100);
looptimemicros = looptime*1000;
Wire.begin();
Serial.begin(9600);
mpu.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
mpu.setXAccelOffset(-1745);
mpu.setYAccelOffset(1704);
mpu.setZAccelOffset(1362);
mpu.setXGyroOffset(79);
mpu.setYGyroOffset(-18);
mpu.setZGyroOffset(21);
mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_250);
/*
AFS_SEL | Full Scale Range | LSB Sensitivity | Accelerometer Set up
0 | ±2g | 16384 LSB/g |
1 | ±4g | 8192 LSB/g |
2 | ±8g | 4096 LSB/g |
3 | ±16g | 2084 LSB/g |
FS_SEL | Full Scale Range | LSB Sensitivity | Gyro Setup (Leave at default value FS_SEL=0)
0 | ±250 deg/s | 131 LSB/deg/s |
1 | ±500 deg/s | 65.5 LSB/deg/s |
*/
Wire.endTransmission(true);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX = Wire.read()<<8;
AcX |= Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read()<<8;
AcY |= Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read()<<8;
AcZ |= Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read()<<8;
Tmp |= Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read()<<8;
GyX |= Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read()<<8;
GyY |= Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read()<<8;
GyZ |= Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
accelangleY = atan2(AcX, AcZ)*4068/71;
accelangleX = atan2(AcY, AcZ)*4068/71;
gyroX = (GyX-3)/131.00; //turn value into deg/s
gyroY = (GyY)/131.00; //turn value into deg/s
gyroZ = (GyZ)/-131.00; //turn value into deg/s
compangleX = (compangleX + (gyroX*looptime)/1000)*gain + (1-gain)*accelangleX;
compangleY = (compangleY + (gyroY*looptime)/1000)*gain + (1-gain)*accelangleY;
gyroangleZ = (gyroangleZ + (gyroZ*looptime)/1000);
Serial.print(" | gyroangleZ = "); Serial.println(gyroangleZ);
// GET PWM SIGNALS
// PID Code
// Write to Servos/motors
endtime = micros();
delta = endtime - starttime;
Serial.println(delta);
if (delta < looptimemicros)
{
delayMicroseconds( looptimemicros - delta );
}
else
{
Serial.println(" ..... increase delay time ...... "); //DELETE once code is functioning
}
starttime = micros();
} // final bracket