Hello, I am making my first arduino steps this week, although I have a sufficient knowledge in C++ programing.
I've got the rev3 Uno, with an Pololu AltIMU-10 chip, currenty using the gyro L3GD20 only for testing purposes.
I use the default library, supplied by Pololu.
My aim is to take the gyro, rotate it around its three axis, and when I lay it down on the table 10 seconds later, each axis to be within 2 degrees error.
Sounds perfect in theory, almost perfect in practise, in terms of gyro drift.
The gyro is set to 190 Hz, 500dps, 70 Hz cutoff. (The last one is described on page 32 of the gyro pdf, not quite sure how it works...)
I've done the dps calculations, the trapezoid integration with the delta time, microseconds.
When I rotate it around a single axis (flat on the table, Z axis), it works like a charm, not more than a 0.5deg error for the said axis.
But if I try multi-axis movements, it gets out of hand. Easily a 20-40deg offset for a 3sec movement, at least on one of the axes.
If I get it in the air and try to rotate it mainly on one axis, this axis gets perfect readings, while the others, although barely changing, show large errors as described prior.
It is like somehow the signals influence each other, although only when all 3 are getting readings...
I've experimented with loop times, currently 10ms delay + 2ms calculations.
I have a setup() zero-value measurement, plus a static drift remover. (Both not the key to the problem.)
My code is bellow, changed a couple of times, and yet getting the error...please take a look and advise me what to do, this will be a crucial part of my quadcopter project...
(I know I need the accelerometer too, this topic is about getting a short-term drift-free gyro readings.)
#include <Wire.h>
#include <L3G.h>
L3G gyro;
int xZ=0,yZ=0,zZ=0;
void setup() {
Serial.begin(19200);
Wire.begin();
if (!gyro.init())
{
Serial.println("Failed to autodetect gyro type!");
while (1);
}
//gyro.enableDefault();
gyro.writeReg(L3G_CTRL_REG1, 0b01111111); //2 output 2 bandwidth 4 on-off controls
gyro.writeReg(L3G_CTRL_REG4, 0b00010000); //2 reserved 2 dps scale 4 reserved
for(int i=0;i<50;i++){ //get 50 samples
gyro.read();
xZ+=gyro.g.x;
yZ+=gyro.g.y;
zZ+=gyro.g.z;
delayMicroseconds(10000);
}
xZ/=50; //get the digital zero values
yZ/=50;
zZ/=50;
}
float x=0,y=0,z=0,x1=0,y1=0,z1=0,x2=0,y2=0,z2=0; //I need those global, not single loop only
long int t1=micros(); //initial gyro delta time
void loop() {
delayMicroseconds(10000);
long int t2=micros(); //time for loop
gyro.read();
if(gyro.g.x*17.5/1000>400)while(1); //if the reading gets out of hand
if(gyro.g.y*17.5/1000>400)while(1); //crash it, to indicate
if(gyro.g.z*17.5/1000>400)while(1); //400 dps is way too much, dont worry
x2=(float)(gyro.g.x-xZ)*17.5/1000/(1000000.0/(micros()-(t1+128))); //calculate the angle
y2=(float)(gyro.g.y-yZ)*17.5/1000/(1000000.0/(micros()-(t1+256))); //128 us for each calc.
z2=(float)(gyro.g.z-zZ)*17.5/1000/(1000000.0/(micros()-(t1+384)));
t1=micros(); //delta time for gyro
if(abs(gyro.g.x-xZ)>20){ //if not stationaly, 20 in binary, not dps
x+=(x2+x1)/2; //trapezoid integration
}
x1=x2;
if(abs(gyro.g.y-yZ)>20){
y+=(y2+y1)/2;
}
y1=y2;
if(abs(gyro.g.z-zZ)>20){
z+=(z2+z1)/2;
}
z1=z2;
//}
if(t1%20==0){ //once every 20 times to print some info
Serial.print(" G ");
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.print(z);
Serial.print(" ");
Serial.println(micros()-t2);
}
}