拾瑞 发表于 2013-11-2 17:27:14

"算法"到最后最是数学问题,整理再多,不会用全白搭!

就是这些源码,直接抄,用在你的代码里,你不明白原理,还是白搭!

所以,算法,其实个人认为可以归为数学类,整理代码出来,没有任何意义........

(楼主莫怪,个人见解而已,对楼主的劳动,表示支持)

zhangzhe0617 发表于 2013-11-2 19:18:02

建议编辑一下这个帖子作为滤波专用的,这样大家查起来也方便。下面是卡尔曼滤波,不是扩展的,但是输出平稳的俯仰和滚转应该够了(凑乎用吧我也不是专业写代码的,欢迎大家拍)#include <Wire.h> // I2C library, gyroscope

// Accelerometer ADXL345
#define ACC (0x53)    //ADXL345 ACC address
#define A_TO_READ (6)      //num of bytes we are going to read each time (two bytes for each axis)


// Gyroscope ITG3200
#define GYRO 0x68 // gyro address, binary = 11101000 when AD0 is connected to Vcc (see schematics of your breakout board)
#define G_SMPLRT_DIV 0x15   
#define G_DLPF_FS 0x16   
#define G_INT_CFG 0x17
#define G_PWR_MGM 0x3E

#define G_TO_READ 8 // 2 bytes for each axis x, y, z


// offsets are chip specific.
int a_offx = 0;
int a_offy = 0;
int a_offz = 0;

int g_offx = 0;
int g_offy = 0;
int g_offz = 0;
////////////////////////

////////////////////////
char str;

void initAcc() {
//Turning on the ADXL345
writeTo(ACC, 0x2D, 0);      
writeTo(ACC, 0x2D, 16);
writeTo(ACC, 0x2D, 8);
//by default the device is in +-2g range reading
}

void getAccelerometerData(int* result) {
int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
byte buff;

readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345

//each axis reading comes in 10 bit resolution, ie 2 bytes.Least Significat Byte first!!
//thus we are converting both bytes in to one int
result = (((int)buff) << 8) | buff + a_offx;   
result = (((int)buff) << 8) | buff + a_offy;
result = (((int)buff) << 8) | buff + a_offz;
}

//initializes the gyroscope
void initGyro()
{
/*****************************************
* ITG 3200
* power management set to:
* clock select = internal oscillator
*   no reset, no sleep mode
*   no standby mode
* sample rate to = 125Hz
* parameter to +/- 2000 degrees/sec
* low pass filter = 5Hz
* no interrupt
******************************************/
writeTo(GYRO, G_PWR_MGM, 0x00);
writeTo(GYRO, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF
writeTo(GYRO, G_DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19
writeTo(GYRO, G_INT_CFG, 0x00);
}


void getGyroscopeData(int * result)
{
/**************************************
Gyro ITG-3200 I2C
registers:
temp MSB = 1B, temp LSB = 1C
x axis MSB = 1D, x axis LSB = 1E
y axis MSB = 1F, y axis LSB = 20
z axis MSB = 21, z axis LSB = 22
*************************************/

int regAddress = 0x1B;
int temp, x, y, z;
byte buff;

readFrom(GYRO, regAddress, G_TO_READ, buff); //read the gyro data from the ITG3200

result = ((buff << 8) | buff) + g_offx;
result = ((buff << 8) | buff) + g_offy;
result = ((buff << 8) | buff) + g_offz;
result = (buff << 8) | buff; // temperature

}


float xz=0,yx=0,yz=0;
float p_xz=1,p_yx=1,p_yz=1;
float q_xz=0.0025,q_yx=0.0025,q_yz=0.0025;
float k_xz=0,k_yx=0,k_yz=0;
float r_xz=0.25,r_yx=0.25,r_yz=0.25;
//int acc_temp;
//float acc;
int acc;
int gyro;
float Axz;
float Ayx;
float Ayz;
float t=0.025;
void setup()
{
Serial.begin(9600);
Wire.begin();
initAcc();
initGyro();

}

//unsigned long timer = 0;
//float o;
void loop()
{

getAccelerometerData(acc);
getGyroscopeData(gyro);
//timer = millis();
sprintf(str, "%d,%d,%d,%d,%d,%d", acc,acc,acc,gyro,gyro,gyro);

//acc=acc;
//acc=acc;
//acc=acc;
//r=sqrt(acc*acc+acc*acc+acc*acc);
gyro=gyro/ 14.375;
gyro=gyro/ (-14.375);
gyro=gyro/ 14.375;

   
Axz=(atan2(acc,acc))*180/PI;
Ayx=(atan2(acc,acc))*180/PI;
/*if((acc!=0)&&(acc!=0))
    {
      Ayx=(atan2(acc,acc))*180/PI;
    }
    else
    {
      Ayx=t*gyro;
    }*/
Ayz=(atan2(acc,acc))*180/PI;


//kalman filter
calculate_xz();
calculate_yx();
calculate_yz();

//sprintf(str, "%d,%d,%d", xz_1, xy_1, x_1);
//Serial.print(xz);Serial.print(",");
//Serial.print(yx);Serial.print(",");
//Serial.print(yz);Serial.print(",");
//sprintf(str, "%d,%d,%d,%d,%d,%d", acc,acc,acc,gyro,gyro,gyro);
//sprintf(str, "%d,%d,%d",gyro,gyro,gyro);
    Serial.print(Axz);Serial.print(",");
    //Serial.print(Ayx);Serial.print(",");
    //Serial.print(Ayz);Serial.print(",");
//Serial.print(str);
//o=gyro;//w=acc;
//Serial.print(o);Serial.print(",");
//Serial.print(w);Serial.print(",");
Serial.print("\n");


//delay(50);
}
void calculate_xz()
{

xz=xz+t*gyro;
p_xz=p_xz+q_xz;
k_xz=p_xz/(p_xz+r_xz);
xz=xz+k_xz*(Axz-xz);
p_xz=(1-k_xz)*p_xz;
}
void calculate_yx()
{

yx=yx+t*gyro;
p_yx=p_yx+q_yx;
k_yx=p_yx/(p_yx+r_yx);
yx=yx+k_yx*(Ayx-yx);
p_yx=(1-k_yx)*p_yx;

}
void calculate_yz()
{
yz=yz+t*gyro;
p_yz=p_yz+q_yz;
k_yz=p_yz/(p_yz+r_yz);
yz=yz+k_yz*(Ayz-yz);
p_yz=(1-k_yz)*p_yz;

}


//---------------- Functions
//Writes val to address register on ACC
void writeTo(int DEVICE, byte address, byte val) {
   Wire.beginTransmission(DEVICE); //start transmission to ACC
   Wire.write(address);      // send register address
   Wire.write(val);      // send value to write
   Wire.endTransmission(); //end transmission
}


//reads num bytes starting from address register on ACC in to buff array
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
Wire.beginTransmission(DEVICE); //start transmission to ACC
Wire.write(address);      //sends address to read from
Wire.endTransmission(); //end transmission

Wire.beginTransmission(DEVICE); //start transmission to ACC
Wire.requestFrom(DEVICE, num);    // request 6 bytes from ACC

int i = 0;
while(Wire.available())    //ACC may send less than requested (abnormal)
{
    buff = Wire.read(); // receive a byte
    i++;
}
Wire.endTransmission(); //end transmission
}

lylmy 发表于 2013-11-3 01:07:07

这个好,收藏了,滤波用得上

Estel 发表于 2013-11-3 10:26:27

拾瑞 发表于 2013-11-2 17:27 static/image/common/back.gif
"算法"到最后最是数学问题,整理再多,不会用全白搭!

就是这些源码,直接抄,用在你的代码里,你不明白原理,还 ...

Arduino追根究底也是电子电路一种,模块什么,直接抄,组合起来,不明白原理也是白搭。稍有问题,就是抓瞎。“所以,Arduino,其实个人认为可以归为电子类,整理模块,库出来,没有任何意义?”

哈哈,开个玩笑。

进入正题:这些源码,没人整理,也就在哪里扔着,整理起来,后人学习起来方便更多。没有网上那些错误百出的代码,后进者可以少走很多弯路。

bboxer 发表于 2013-11-3 11:50:27

感谢楼主分享,收藏了以后肯定有用

shenhaiyu 发表于 2013-11-4 11:32:19

zhangzhe0617 发表于 2013-11-2 19:18 static/image/common/back.gif
建议编辑一下这个帖子作为滤波专用的,这样大家查起来也方便。下面是卡尔曼滤波,不是扩展的,但是输出平稳 ...

太棒了,感谢分享,我把你的楼层加到帖子的索引里~~~

dreamload 发表于 2013-11-4 11:51:57

灰常好。这才是本网站存在的意义之一。

zhangzhe0617 发表于 2013-11-4 14:36:07

shenhaiyu 发表于 2013-11-4 11:32 static/image/common/back.gif
太棒了,感谢分享,我把你的楼层加到帖子的索引里~~~

希望有问题大家一起进步解决,而不是提问了没人理。。。。。。

shenhaiyu 发表于 2013-11-4 16:36:47

zhangzhe0617 发表于 2013-11-4 14:36 static/image/common/back.gif
希望有问题大家一起进步解决,而不是提问了没人理。。。。。。

是啊~~         

tangmao48 发表于 2013-11-6 23:31:47

卧槽,连卡尔曼滤波都有……

hmjack2008 发表于 2013-11-7 00:12:10

原本看标题真的以为是 “重复发旧帖”, 看到内容真是感激啊 !!

shenhaiyu 发表于 2013-11-7 10:46:02

tangmao48 发表于 2013-11-6 23:31 static/image/common/back.gif
卧槽,连卡尔曼滤波都有……

吼吼,这个要感谢zhagnzhe0617

shenhaiyu 发表于 2013-11-7 10:46:43

hmjack2008 发表于 2013-11-7 00:12 static/image/common/back.gif
原本看标题真的以为是 “重复发旧帖”, 看到内容真是感激啊 !!

这个。。。。我也没那么无聊吧,不会发重复贴的

ldq99 发表于 2013-11-7 17:11:34

好贴必须顶!!

Ned_Flander 发表于 2013-11-7 19:18:29

好帖!留名
页: 1 [2] 3 4 5 6 7 8 9 10 11
查看完整版本: 十大滤波算法程序大全(Arduino精编无错版)