Hi all, i am trying to get the data from Yaw rotation with kalman filter and my coding does not giving the correct result to me, which is when i rotate to yaw and the angle in the serial monitor is NOT respond
BUT when i move to Pitch rotation the result in Yaw is responded.
my question is how to correct the coding that is have in order for me to get the result when rotate in YAW.
my Coding
#include <Wire.h>
#include <L3G4200D.h>
#include <ADXL345.h>
#include <KalmanFilter.h>
L3G4200D gyroscope;
ADXL345 accelerometer;
KalmanFilter kalmanX(0.001, 0.003, 0.03);
KalmanFilter kalmanY(0.001, 0.003, 0.03);
KalmanFilter kalmanZ(0.001, 0.003, 0.03);
float accPitch = 0;
float accRoll = 0;
float accYaw = 0;
float kalPitch = 0;
float kalRoll = 0;
float kalYaw = 0;
int red = 6;
void setup()
{
Serial.begin(115200);
pinMode(red, OUTPUT);
// Initialize ADXL345
while(!accelerometer.begin())
{
delay(500);
}
accelerometer.setRange(ADXL345_RANGE_2G);
// Initialize L3G4200D
while(!gyroscope.begin(L3G4200D_SCALE_2000DPS, L3G4200D_DATARATE_400HZ_50))
{
delay(500);
}
// Calibrate gyroscope. The calibration must be at rest.
// If you don't want calibrate, comment this line.
gyroscope.calibrate(100);
}
void loop()
{
data();
led();
}
void data()
{
Vector acc = accelerometer.readNormalize();
Vector gyr = gyroscope.readNormalize();
// Calculate Pitch & Roll from accelerometer (deg)
accPitch = -(atan2(acc.XAxis, sqrt(acc.YAxisacc.YAxis + acc.ZAxisacc.ZAxis))180.0)/M_PI;
accRoll = (atan2(acc.YAxis, acc.ZAxis)180.0)/M_PI;
accYaw = 180 * atan (acc.ZAxis/sqrt(acc.XAxisacc.XAxis + acc.ZAxisacc.ZAxis))/M_PI;
// Kalman filter
kalPitch = kalmanY.update(accPitch, gyr.YAxis);
kalRoll = kalmanX.update(accRoll, gyr.XAxis);
kalYaw = kalmanZ.update(accYaw, gyr.ZAxis);
Serial.print(accPitch);
Serial.print(":");
Serial.print(accRoll);
Serial.print(":");
Serial.print(kalPitch);
Serial.print(":");
Serial.print(kalRoll);
Serial.print(":");
Serial.print(kalYaw);
Serial.println();
}
void led()
{
if (kalYaw >= -180 && kalYaw <=-20)
{digitalWrite(red, HIGH);
}
else {digitalWrite (red,LOW);
}
}
kalman___led.ino (1.78 KB)