|
|

楼主 |
发表于 2013-6-30 14:02:06
|
显示全部楼层
#include "Wire.h"
#include"Kalman.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
Kalman kalmanX; // Create the Kalman instances
Kalman kalmanY;
Kalman kalmanZ;
int16_t ax, ay, az;
int16_t gx, gy, gz;
uint32_t timer;
double accXangle, accYangle,accZangle; // Angle calculate using the accelerometer
double gyroXangle, gyroYangle,gyroZangle; // Angle calculate using the gyro
double kalAngleX, kalAngleY; // Calculate the angle using a Kalman filter
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(115200);
// initialize device
accelgyro.initialize();
// verify connection
accelgyro.testConnection();
timer=micros();
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
accXangle = (atan2(ay,az)+PI)*RAD_TO_DEG;
accYangle = (atan2(ax,az)+PI)*RAD_TO_DEG;
double gyroXrate = (double)gx/131.0;
double gyroYrate = -((double)gy/131.0);
gyroXangle += gyroXrate*((double)(micros()-timer)/1000000); // Calculate gyro angle without any filter
gyroYangle += gyroYrate*((double)(micros()-timer)/1000000);
kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros()-timer)/1000000); // Calculate the angle using a Kalman filter
kalAngleY = kalmanY.getAngle(accYangle, gyroYrate, (double)(micros()-timer)/1000000);
timer = micros();
// Serial.print(accXangle);Serial.print(",");
// Serial.print(gyroXangle);Serial.print(",");
//Serial.print(kalAngleX);Serial.print("\r\n");
// Serial.print("\t");
//Serial.print(accYangle);Serial.print(",");
//Serial.print(gyroYangle);Serial.print(",");
//Serial.print(kalAngleY);Serial.print("\r\n");
delay(1);
}
这个是代码 只测了X,和Y 的。。。库论坛都有,卡尔曼的库已经发出了链接。。。用Serialchat就能看到波形 |
|