




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/=/ MahonyAHRS.c/=/ Madgwicks implementation of Mayhonys AHRS algorithm./ See: http:/www.x-io.co.uk/node/8#open_source_ahrs_and_imu_algorithms/ DateAuthorNotes/ 29/09/2011SOH Madgwick Initial release/ 02/10/2011SOH MadgwickOptimised for reduced CPU load/=/-/ Header files#include MahonyAHRS.h#include /-/ Definitions#define sampleFreq512.0f/ sample frequency in Hz#define twoKpDef(2.0f * 0.5f)/ 2 * proportional gain#define twoKiDef(2.0f * 0.0f)/ 2 * integral gain/-/ Variable definitionsvolatile float twoKp = twoKpDef;/ 2 * proportional gain (Kp)volatile float twoKi = twoKiDef;/ 2 * integral gain (Ki)volatile float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f;/ quaternion of sensor frame relative to auxiliary framevolatile float integralFBx = 0.0f, integralFBy = 0.0f, integralFBz = 0.0f;/ integral error terms scaled by Ki/-/ Function declarationsfloat invSqrt(float x);/=/ Functions/-/ AHRS algorithm updatevoid MahonyAHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz) float recipNorm; float q0q0, q0q1, q0q2, q0q3, q1q1, q1q2, q1q3, q2q2, q2q3, q3q3; float hx, hy, bx, bz;float halfvx, halfvy, halfvz, halfwx, halfwy, halfwz;float halfex, halfey, halfez;float qa, qb, qc;/ Use IMU algorithm if magnetometer measurement invalid (avoids NaN in magnetometer normalisation)if(mx = 0.0f) & (my = 0.0f) & (mz = 0.0f) MahonyAHRSupdateIMU(gx, gy, gz, ax, ay, az);return;/ Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)if(!(ax = 0.0f) & (ay = 0.0f) & (az = 0.0f) / Normalise accelerometer measurementrecipNorm = invSqrt(ax * ax + ay * ay + az * az);ax *= recipNorm;ay *= recipNorm;az *= recipNorm; / Normalise magnetometer measurementrecipNorm = invSqrt(mx * mx + my * my + mz * mz);mx *= recipNorm;my *= recipNorm;mz *= recipNorm; / Auxiliary variables to avoid repeated arithmetic q0q0 = q0 * q0; q0q1 = q0 * q1; q0q2 = q0 * q2; q0q3 = q0 * q3; q1q1 = q1 * q1; q1q2 = q1 * q2; q1q3 = q1 * q3; q2q2 = q2 * q2; q2q3 = q2 * q3; q3q3 = q3 * q3; / Reference direction of Earths magnetic field hx = 2.0f * (mx * (0.5f - q2q2 - q3q3) + my * (q1q2 - q0q3) + mz * (q1q3 + q0q2); hy = 2.0f * (mx * (q1q2 + q0q3) + my * (0.5f - q1q1 - q3q3) + mz * (q2q3 - q0q1); bx = sqrt(hx * hx + hy * hy); bz = 2.0f * (mx * (q1q3 - q0q2) + my * (q2q3 + q0q1) + mz * (0.5f - q1q1 - q2q2);/ Estimated direction of gravity and magnetic fieldhalfvx = q1q3 - q0q2;halfvy = q0q1 + q2q3;halfvz = q0q0 - 0.5f + q3q3; halfwx = bx * (0.5f - q2q2 - q3q3) + bz * (q1q3 - q0q2); halfwy = bx * (q1q2 - q0q3) + bz * (q0q1 + q2q3); halfwz = bx * (q0q2 + q1q3) + bz * (0.5f - q1q1 - q2q2); / Error is sum of cross product between estimated direction and measured direction of field vectorshalfex = (ay * halfvz - az * halfvy) + (my * halfwz - mz * halfwy);halfey = (az * halfvx - ax * halfvz) + (mz * halfwx - mx * halfwz);halfez = (ax * halfvy - ay * halfvx) + (mx * halfwy - my * halfwx);/ Compute and apply integral feedback if enabledif(twoKi 0.0f) integralFBx += twoKi * halfex * (1.0f / sampleFreq);/ integral error scaled by KiintegralFBy += twoKi * halfey * (1.0f / sampleFreq);integralFBz += twoKi * halfez * (1.0f / sampleFreq);gx += integralFBx;/ apply integral feedbackgy += integralFBy;gz += integralFBz;else integralFBx = 0.0f;/ prevent integral windupintegralFBy = 0.0f;integralFBz = 0.0f;/ Apply proportional feedbackgx += twoKp * halfex;gy += twoKp * halfey;gz += twoKp * halfez;/ Integrate rate of change of quaterniongx *= (0.5f * (1.0f / sampleFreq);/ pre-multiply common factorsgy *= (0.5f * (1.0f / sampleFreq);gz *= (0.5f * (1.0f / sampleFreq);qa = q0;qb = q1;qc = q2;q0 += (-qb * gx - qc * gy - q3 * gz);q1 += (qa * gx + qc * gz - q3 * gy);q2 += (qa * gy - qb * gz + q3 * gx);q3 += (qa * gz + qb * gy - qc * gx); / Normalise quaternionrecipNorm = invSqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);q0 *= recipNorm;q1 *= recipNorm;q2 *= recipNorm;q3 *= recipNorm;/-/ IMU algorithm updatevoid MahonyAHRSupdateIMU(float gx, float gy, float gz, float ax, float ay, float az) float recipNorm;float halfvx, halfvy, halfvz;float halfex, halfey, halfez;float qa, qb, qc;/ Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)if(!(ax = 0.0f) & (ay = 0.0f) & (az = 0.0f) / Normalise accelerometer measurementrecipNorm = invSqrt(ax * ax + ay * ay + az * az);ax *= recipNorm;ay *= recipNorm;az *= recipNorm; / Estimated direction of gravity and vector perpendicular to magnetic fluxhalfvx = q1 * q3 - q0 * q2;halfvy = q0 * q1 + q2 * q3;halfvz = q0 * q0 - 0.5f + q3 * q3;/ Error is sum of cross product between estimated and measured direction of gravityhalfex = (ay * halfvz - az * halfvy);halfey = (az * halfvx - ax * halfvz);halfez = (ax * halfvy - ay * halfvx);/ Compute and apply integral feedback if enabledif(twoKi 0.0f) integralFBx += twoKi * halfex * (1.0f / sampleFreq);/ integral error scaled by KiintegralFBy += twoKi * halfey * (1.0f / sampleFreq);integralFBz += twoKi * halfez * (1.0f / sampleFreq);gx += integralFBx;/ apply integral feedbackgy += integralFBy;gz += integralFBz;else integralFBx = 0.0f;/ prevent integral windupintegralFBy = 0.0f;integralFBz = 0.0f;/ Apply proportional feedbackgx += twoKp * halfex;gy += twoKp * halfey;gz += twoKp * halfez;/ Integrate rate of change of quaterniongx *= (0.5f * (1.0f / sampleFreq);/ pre-multiply common factorsgy *= (0.5f * (1.0f / sampleFreq);gz *= (
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 麦当劳新年店庆活动方案
- 集团游戏活动方案
- 广东软考试题及答案
- 高考试题及答案江苏
- 费用预算编制与成本控制管理模板
- 风险管理评估矩阵模板全面风险识别
- 写景作文迷人的秋色250字10篇范文
- (正式版)DB15∕T 3653-2024 《寒区退化草地改良技术规范》
- 《光合作用机制及其意义:高中生物基础教案》
- 质量控制检查表产品质量检测模板
- 企业所得税汇算清缴政策培训会课件-于永勤
- 《北京市道路桥梁试验检测费用定额》
- 2024年下半年湖北黄冈黄梅县事业单位招聘“三支一扶”服务期满人员17人易考易错模拟试题(共500题)试卷后附参考答案
- 《小儿神经系统查体》课件
- 农业行政执法工作指南
- 小学体育教案《50米快速跑(途中跑)》
- 高职计算机专业人工智能技术应用与开发
- 地铁接触网检修工技能竞赛考试题库500题(含答案)
- 高考3000词分层分类记忆初中1500
- DL∕T 475-2017 接地装置特性参数测量导则
- 公寓物业管理规定
评论
0/150
提交评论