How do you calculate the pitch and roll angles from the x, y and z from the ADXL345.
I have tried using this code from here http://www.den-uijl.nl/electronics/gyro.html
void loop() {
timer = millis();
getGyroscopeReadings(gyroResult);
getAccelerometerReadings(accelResult);
pitchAccel = atan2((accelResult[1] - biasAccelY) / 256, (accelResult[2] - biasAccelZ) / 256) * 360.0 / (2*PI);
pitchGyro = pitchGyro + ((gyroResult[0] - biasGyroX) / 14.375) * timeStep;
pitchPrediction = pitchPrediction + ((gyroResult[0] - biasGyroX) / 14.375) * timeStep;
rollAccel = atan2((accelResult[0] - biasAccelX) / 256, (accelResult[2] - biasAccelZ) / 256) * 360.0 / (2*PI);
rollGyro = rollGyro - ((gyroResult[1] - biasGyroY) / 14.375) * timeStep;
rollPrediction = rollPrediction - ((gyroResult[1] - biasGyroY) / 14.375) * timeStep;
Pxx += timeStep * (2 * Pxv + timeStep * Pvv);
Pxv += timeStep * Pvv;
Pxx += timeStep * giroVar;
Pvv += timeStep * deltaGiroVar;
kx = Pxx * (1 / (Pxx + accelVar));
kv = Pxv * (1 / (Pxx + accelVar));
pitchPrediction += (pitchAccel - pitchPrediction) * kx;
rollPrediction += (rollAccel - rollPrediction) * kx;
Pxx *= (1 - kx);
Pxv *= (1 - kx);
Pvv -= kv * Pxv;
Serial.print(pitchGyro);
Serial.print("\t");
Serial.print(pitchAccel);
Serial.print("\t");
Serial.print(pitchPrediction);
Serial.print("\t");
Serial.print(rollGyro);
Serial.print("\t");
Serial.print(rollAccel);
Serial.print("\t");
Serial.print(rollPrediction);
Serial.print("\n");
timer = millis() - timer;
timer = (timeStep * 1000) - timer;
delay(timer);
}
But it doesn't seem to work - I am getting 0 or 1 for the angles.
Here is my code:
void Adafruit_ADXL345_Unified::getAngles(sensors_event_t &event, uint16_t &nPitchAngle, uint16_t &nRollAngle)
{
float fPitch = 0.0, fRoll = 0.0;
fPitch = atan2((event.acceleration.y - m_fZeroY) / 256, (event.acceleration.z - m_fZeroZ) / 256) * 360.0 / (2*PI);
fRoll = atan2((event.acceleration.x - m_fZeroX) / 256, (event.acceleration.z - m_fZeroZ) / 256) * 360.0 / (2*PI);
nPitchAngle = fPitch;
nRollAngle = fRoll;
}