Hi,
I'm trying to get roll & pitch from a ITG3205 gyro and ADXL335 accelerometer and put it into a processing box like a lot of videos are around the net.
From one way I read the accel raw values and tranform it to angles by this: (read from forems and datasheet)
void getAccelAnglesData()
{
//convert read values to degrees -90 to 90 - Needed for atan2
accelAnglesData[0] = map(accelRawData[0], 385.0f, 585.0f, -90.0f, 90.0f);
accelAnglesData[1] = map(accelRawData[1], 380.0f, 580.0f, -90.0f, 90.0f);
accelAnglesData[2] = map(accelRawData[2], 410.0f, 610.0f, -90.0f, 90.0f);
//Caculate 360deg values like so: atan2(yAng, zAng)
//atan2 outputs the value of -π to π (radians)
//Convert it to degrees (* (180 / PI) or * RAD_TO_DEG);
accelAnglesData[0] = atan2(accelAnglesData[0], accelAnglesData[2]) * RAD_TO_DEG;
accelAnglesData[1] = atan2(accelAnglesData[1], accelAnglesData[2]) * RAD_TO_DEG;
accelAnglesData[2] = atan2(accelAnglesData[0], accelAnglesData[1]) * RAD_TO_DEG;
}
With this code I get in stationary position the reading:
X: 180º
Y: 180º
Z: 135º
If I turn it the PCB up 90º (roll), X goes from 180º to 90º, if I turn down X goes from 180º to -90º
The same on Y axis, If I turn left, Y goes from 180º to -90º, right 180º to 90º
The problem is that observing the values of Y when I turn X (in 180º) close to 90º, Y axis falls to 90º too.
(in short, if I roll PCB 90º I get X: 90º, Y 90º, Z 45º)
In other hand, I get gyro data by this:
//Convert RAW data to DEGREE/SEC
void getGyroDSData()
{
gyroDSData[0] = ((double) gyroRawData[0] - gyroOffX) / 14.375;
gyroDSData[1] = ((double) gyroRawData[1] - gyroOffY) / 14.375;
gyroDSData[2] = ((double) gyroRawData[2] - gyroOffZ) / 14.375;
}
After this, I join all with a complementary filter
//Complementary Filter
float ALPHA = 0.98;
void getEstimatedInclination()
{
int currentTime = millis();
compFilterTimerInterval = currentTime - compFilterTimerLastTime;
compFilterTimerLastTime = currentTime;
// Roll
compAngle[0] = ALPHA * (compAngle[0] + gyroDSData[0] * (compFilterTimerInterval / 1000.0f)) + (1 - ALPHA) * accelAnglesData[0];
//Pitch
compAngle[1] = ALPHA * (compAngle[1] + gyroDSData[1] * (compFilterTimerInterval / 1000.0f)) + (1 - ALPHA) * accelAnglesData[1];
compFilterIntervalLog[compFilterIntervalLogIndex] = compFilterTimerInterval;
compFilterIntervalLogIndex++;
if(compFilterIntervalLogIndex > 2) compFilterIntervalLogIndex = 0;
}
And, in order to rotate box I'm using
rotateX(radians(compAngle[0]));
rotateZ(radians(compAngle[1]));
So, when I roll/pitch PCB < 90º, I get a good angle (even both axis), but when I arrive to 90º it turns 90º more.
(I dont talk about yaw because as I read from internet it not possible to get without a magnometer)
Any help will be really appreciate.