当前位置:首页 > 单片机 > 单片机
[导读]一、卡尔曼滤波九轴融合算法stm32尝试1、Kalman滤波文件[.h已经封装为结构体] 1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics-> All rights reserved-> 2 3 This software may be distributed and modi

一、卡尔曼滤波九轴融合算法stm32尝试



1、Kalman滤波文件[.h已经封装为结构体]



1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics-> All rights reserved->

2

3 This software may be distributed and modified under the terms of the GNU

4 General Public License version 2 (GPL2) as published by the Free Software

5 Foundation and appearing in the file GPL2->TXT included in the packaging of

6 this file-> Please note that GPL2 Section 2[b] requires that all works based

7 on this software must also be made publicly available under the terms of

8 the GPL2 ("Copyleft")->

9

10 Contact information

11 -------------------

12

13 Kristian Lauszus, TKJ Electronics

14 Web : http://www->tkjelectronics->com

15 e-mail : kristianl@tkjelectronics->com

16 */

17

18 #ifndef _Kalman_h

19 #define _Kalman_h

20 struct Kalman {

21 /* Kalman filter variables */

22 double Q_angle; // Process noise variance for the accelerometer

23 double Q_bias; // Process noise variance for the gyro bias

24 double R_measure; // Measurement noise variance - this is actually the variance of the measurement noise

25

26 double angle; // The angle calculated by the Kalman filter - part of the 2x1 state vector

27 double bias; // The gyro bias calculated by the Kalman filter - part of the 2x1 state vector

28 double rate; // Unbiased rate calculated from the rate and the calculated bias - you have to call getAngle to update the rate

29

30 double P[2][2]; // Error covariance matrix - This is a 2x2 matrix

31 double K[2]; // Kalman gain - This is a 2x1 vector

32 double y; // Angle difference

33 double S; // Estimate error

34 };

35

36 void Init(struct Kalman* klm){

37 /* We will set the variables like so, these can also be tuned by the user */

38 klm->Q_angle = 0.001;

39 klm->Q_bias = 0.003;

40 klm->R_measure = 0.03;

41

42 klm->angle = 0; // Reset the angle

43 klm->bias = 0; // Reset bias

44

45 klm->P[0][0] = 0; // Since we assume that the bias is 0 and we know the starting angle (use setAngle), the error covariance matrix is set like so - see: http://en->wikipedia->org/wiki/Kalman_filter#Example_application->2C_technical

46 klm->P[0][1] = 0;

47 klm->P[1][0] = 0;

48 klm->P[1][1] = 0;

49 }

50

51 // The angle should be in degrees and the rate should be in degrees per second and the delta time in seconds

52 double getAngle(struct Kalman * klm, double newAngle, double newRate, double dt) {

53 // KasBot V2 - Kalman filter module - http://www->x-firm->com/?page_id=145

54 // Modified by Kristian Lauszus

55 // See my blog post for more information: http://blog->tkjelectronics->dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it

56

57 // Discrete Kalman filter time update equations - Time Update ("Predict")

58 // Update xhat - Project the state ahead

59 /* Step 1 */

60 klm->rate = newRate - klm->bias;

61 klm->angle += dt * klm->rate;

62

63 // Update estimation error covariance - Project the error covariance ahead

64 /* Step 2 */

65 klm->P[0][0] += dt * (dt*klm->P[1][1] - klm->P[0][1] - klm->P[1][0] + klm->Q_angle);

66 klm->P[0][1] -= dt * klm->P[1][1];

67 klm->P[1][0] -= dt * klm->P[1][1];

68 klm->P[1][1] += klm->Q_bias * dt;

69

70 // Discrete Kalman filter measurement update equations - Measurement Update ("Correct")

71 // Calculate Kalman gain - Compute the Kalman gain

72 /* Step 4 */

73 klm->S = klm->P[0][0] + klm->R_measure;

74 /* Step 5 */

75 klm->K[0] = klm->P[0][0] / klm->S;

76 klm->K[1] = klm->P[1][0] / klm->S;

77

78 // Calculate angle and bias - Update estimate with measurement zk (newAngle)

79 /* Step 3 */

80 klm->y = newAngle - klm->angle;

81 /* Step 6 */

82 klm->angle += klm->K[0] * klm->y;

83 klm->bias += klm->K[1] * klm->y;

84

85 // Calculate estimation error covariance - Update the error covariance

86 /* Step 7 */

87 klm->P[0][0] -= klm->K[0] * klm->P[0][0];

88 klm->P[0][1] -= klm->K[0] * klm->P[0][1];

89 klm->P[1][0] -= klm->K[1] * klm->P[0][0];

90 klm->P[1][1] -= klm->K[1] * klm->P[0][1];

91

92 return klm->angle;

93 }

94

95 void setAngle(struct Kalman* klm, double newAngle) { klm->angle = newAngle; } // Used to set angle, this should be set as the starting angle

96 double getRate(struct Kalman* klm) { return klm->rate; } // Return the unbiased rate

97

98 /* These are used to tune the Kalman filter */

99 void setQangle(struct Kalman* klm, double newQ_angle) { klm->Q_angle = newQ_angle; }

100 void setQbias(struct Kalman* klm, double newQ_bias) { klm->Q_bias = newQ_bias; }

101 void setRmeasure(struct Kalman* klm, double newR_measure) { klm->R_measure = newR_measure; }

102

103 double getQangle(struct Kalman* klm) { return klm->Q_angle; }

104 double getQbias(struct Kalman* klm) { return klm->Q_bias; }

105 double getRmeasure(struct Kalman* klm) { return klm->R_measure; }

106


2、I2C总线代码[这里把MPU和HMC挂接到上面,通过改变SlaveAddress的值来和不同的设备通信]



1 #include "stm32f10x.h"

2

3 /*标志是否读出数据*/

4 char test=0;

5 /*I2C从设备*/

6 unsigned char SlaveAddress;

7 /*模拟IIC端口输出输入定义*/

8 #define SCL_H GPIOB->BSRR = GPIO_Pin_10

9 #define SCL_L GPIOB->BRR = GPIO_Pin_10

10 #define SDA_H GPIOB->BSRR = GPIO_Pin_11

11 #define SDA_L GPIOB->BRR = GPIO_Pin_11

12 #define SCL_read GPIOB->IDR & GPIO_Pin_10

13 #define SDA_read GPIOB->IDR & GPIO_Pin_11

14

15 /*I2C的端口初始化---------------------------------------*/

16 void I2C_GPIO_Config(void)

17 {

18 GPIO_InitTypeDef GPIO_InitStructure;

19

20 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

21 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

22 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;

23 GPIO_Init(GPIOB, &GPIO_InitStructure);

24

25 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

26 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

27 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;

28 GPIO_Init(GPIOB, &GPIO_InitStructure);

29 }

30

31 /*I2C的延时函数-----------------------------------------*/

32 void I2C_delay(void)

33 {

34 u8 i=30; //这里可以优化速度 ,经测试最低到5还能写入

35 while(i)

36 {

37 i--;

38 }

39 }

40

41 /*I2C的等待5ms函数--------------------------------------*/

42 void delay5ms(void)

43 {

44 int i=5000;

45 while(i)

46 {

47 i--;

48 }

49 }

50

51 /*I2C启动函数-------------------------------------------*/

52 bool I2C_Start(void)

53 {

54 SDA_H;

55 SCL_H;

56 I2C_delay();

57 if(!SDA_read)return FALSE; //SDA线为低电平则总线忙,退出

58 SDA_L;

59 I2C_delay();

60 if(SDA_read) return FALSE; //SDA线为高电平则总线出错,退出

61 SDA_L;

62 I2C_delay();

63 return TRUE;

64 }

65

66 /*I2C停止函数-------------------------------------------*/

67 void I2C_Stop(void)

68 {

69 SCL_L;

70 I2C_delay();

71 SDA_L;

72 I2C_delay();

73 SCL_H;

74 I2C_delay();

75 SDA_H;

76 I2C_delay();

77 }

78

7

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

9月2日消息,不造车的华为或将催生出更大的独角兽公司,随着阿维塔和赛力斯的入局,华为引望愈发显得引人瞩目。

关键字: 阿维塔 塞力斯 华为

加利福尼亚州圣克拉拉县2024年8月30日 /美通社/ -- 数字化转型技术解决方案公司Trianz今天宣布,该公司与Amazon Web Services (AWS)签订了...

关键字: AWS AN BSP 数字化

伦敦2024年8月29日 /美通社/ -- 英国汽车技术公司SODA.Auto推出其旗舰产品SODA V,这是全球首款涵盖汽车工程师从创意到认证的所有需求的工具,可用于创建软件定义汽车。 SODA V工具的开发耗时1.5...

关键字: 汽车 人工智能 智能驱动 BSP

北京2024年8月28日 /美通社/ -- 越来越多用户希望企业业务能7×24不间断运行,同时企业却面临越来越多业务中断的风险,如企业系统复杂性的增加,频繁的功能更新和发布等。如何确保业务连续性,提升韧性,成...

关键字: 亚马逊 解密 控制平面 BSP

8月30日消息,据媒体报道,腾讯和网易近期正在缩减他们对日本游戏市场的投资。

关键字: 腾讯 编码器 CPU

8月28日消息,今天上午,2024中国国际大数据产业博览会开幕式在贵阳举行,华为董事、质量流程IT总裁陶景文发表了演讲。

关键字: 华为 12nm EDA 半导体

8月28日消息,在2024中国国际大数据产业博览会上,华为常务董事、华为云CEO张平安发表演讲称,数字世界的话语权最终是由生态的繁荣决定的。

关键字: 华为 12nm 手机 卫星通信

要点: 有效应对环境变化,经营业绩稳中有升 落实提质增效举措,毛利润率延续升势 战略布局成效显著,战新业务引领增长 以科技创新为引领,提升企业核心竞争力 坚持高质量发展策略,塑强核心竞争优势...

关键字: 通信 BSP 电信运营商 数字经济

北京2024年8月27日 /美通社/ -- 8月21日,由中央广播电视总台与中国电影电视技术学会联合牵头组建的NVI技术创新联盟在BIRTV2024超高清全产业链发展研讨会上宣布正式成立。 活动现场 NVI技术创新联...

关键字: VI 传输协议 音频 BSP

北京2024年8月27日 /美通社/ -- 在8月23日举办的2024年长三角生态绿色一体化发展示范区联合招商会上,软通动力信息技术(集团)股份有限公司(以下简称"软通动力")与长三角投资(上海)有限...

关键字: BSP 信息技术
关闭
关闭