【IOS】惯性导航详解(包含角度、加速度、修正方式的api分析)

参考文献

iPhone的惯性导航,基于步态。https://www.docin.com/p-811792664.html
Inertial Odometry on Handheld Smartphones: https://arxiv.org/pdf/1703.00154.pdf
惯性导航项目相关代码:https://github.com/topics/inertial-navigation-systems
useracceleration苹果官方文档:https://developer.apple.com/documentation/coremotion/cmdevicemotion/1616149-useracceleration
手机导航https://ieeexplore.ieee.org/document/6139701

give me example that use iOS api to get accelarate in xyz.
I use it in swift. print the acceleration of x y z.
make the data collection part in a class. and show it in CintentView using the data in class.

using a filter in the acceleration data

时间间隔获取

在iOS中进行积分时,您可以选择使用motionManager.accelerometerUpdateInterval(motion.timestamp - lastLocation.timestamp)作为时间间隔。

motionManager.accelerometerUpdateInterval:

motionManager.accelerometerUpdateInterval是CMMotionManager类的属性,表示加速度计更新的时间间隔。
这个值是由您设置的,通常用于控制加速度计数据的采样率。
如果您使用加速度计数据进行积分,可以使用这个时间间隔来估计每个采样点的时间间隔。
例如,如果您的加速度计更新间隔为0.01秒,您可以将其作为每个采样点之间的时间间隔。
这种方法适用于简单的运动情况,但可能受到加速度计的噪声和误差的影响。

(motion.timestamp - lastLocation.timestamp):

motion.timestamp是CMDeviceMotion对象的属性,表示获取数据的时间戳。
lastLocation.timestamp是上一个位置更新的时间戳,如果您使用位置数据进行积分,可以使用这个时间戳来计算时间间隔。
这种方法适用于使用位置数据进行积分的情况,例如计算行进距离。
请注意,这种方法要求您同时获取位置数据,并且需要在每个位置更新时记录时间戳

加速度获取

class MileMeter {let motionManager = CMMotionManager()var referenceAttitude: CMAttitude?func startMotionUpdates() {// 检查设备是否支持加速度计和陀螺仪guard motionManager.isAccelerometerAvailable, motionManager.isGyroAvailable else {print("设备不支持加速度计或陀螺仪")return}// 设置更新频率motionManager.accelerometerUpdateInterval = 1.0 / 100.0 // 启动加速度计和陀螺仪更新motionManager.startAccelerometerUpdates()motionManager.startGyroUpdates()// 获取初始参考姿态if let referenceAttitude = motionManager.deviceMotion?.attitude {self.referenceAttitude = referenceAttitude}// 处理加速度计数据motionManager.startDeviceMotionUpdates(to: .main) { (motionData, error) inguard let motionData = motionData else {print("无法获取加速度计数据: \(error?.localizedDescription ?? "")")return}// 获取用户加速度,而不是混合// 校准加速度计数据
//            if let referenceAttitude = self.referenceAttitude {
//                motionData.acceleration = motionData.acceleration.applying(referenceAttitude.rotationMatrix)
//            }// 进行距离估计let acceleration = motionData.userAcceleration   //用户对设备施加的加速度,而不包括重力的影响// 在此处可以使用估计的速度和位移数据进行进一步的处理print("加速度: \(acceleration)")}}func stopMotionUpdates() {motionManager.stopAccelerometerUpdates()motionManager.stopGyroUpdates()motionManager.stopDeviceMotionUpdates()}
}
在这里插入代码片

角度获取

陀螺仪有两种:var attitude: CMAttitude { get }和var rotationRate: CMRotationRate { get } :

CMAttitude:绝对角度

CMAttitude represents the device’s orientation or attitude in space.
It provides information about the device’s pitch, roll, and yaw angles.
The attitude is expressed as a quaternion, which is a mathematical representation of orientation.
You can access the attitude using the attitude property of a CMMotionManager object.
Example usage: let attitude = motionManager.deviceMotion?.attitude

CMRotationRate:旋转角度

CMRotationRate represents the device’s rotation rate or angular velocity.
It provides information about the device’s rotation speed around each axis (x, y, and z).
The rotation rate is expressed in radians per second.
You can access the rotation rate using the rotationRate property of a CMMotionManager object.
Example usage: let rotationRate = motionManager.deviceMotion?.rotationRate

校准

坐标系变换

            // 校准加速度计数据
//            if let referenceAttitude = self.referenceAttitude {
//                motionData.acceleration = motionData.acceleration.applying(referenceAttitude.rotationMatrix)
//            }

低通滤波器

func lowPassFilter(_ x: Double) -> Double {if abs(x) < 0.01 {return 0} else {return x}
}

卡尔曼滤波

import Foundationstruct KalmanFilter {var state: Doublevar covariance: Doublelet processNoise: Doublelet measurementNoise: Doublemutating func update(measurement: Double) {// Prediction steplet predictedState = statelet predictedCovariance = covariance + processNoise// Update steplet kalmanGain = predictedCovariance / (predictedCovariance + measurementNoise)state = predictedState + kalmanGain * (measurement - predictedState)covariance = (1 - kalmanGain) * predictedCovariance}
}// Example usage
var filter = KalmanFilter(state: 0, covariance: 1, processNoise: 0.1, measurementNoise: 1)let measurements = [1.2, 1.4, 1.6, 1.8, 2.0]for measurement in measurements {filter.update(measurement: measurement)print("Filtered measurement: \(filter.state)")
}

最终代码

class DistanceCalculator {private let motionManager = CMMotionManager()@Published var totalDistance: Double = 0.0@Published var lastLocation: CMDeviceMotion?@Published var ax: Double = 0.0 // 距离@Published var ay: Double = 0.0@Published var az: Double = 0.0@Published var acc_ax: Double = 0.0 // 加速度@Published var acc_ay: Double = 0.0@Published var acc_az: Double = 0.0@Published var m_acc_ax: Double = 0.0 // 修正后的加速度@Published var m_acc_ay: Double = 0.0@Published var m_acc_az: Double = 0.0@Published var m_dis_ax: Double = 0.0 // 修正后的路程@Published var m_dis_ay: Double = 0.0@Published var m_dis_az: Double = 0.0@Published var m_totalDistance: Double = 0.0init() {// 检查设备是否支持运动数据的获取guard motionManager.isDeviceMotionAvailable else {print("设备不支持运动数据")return}// 设置更新间隔 官方推荐100hz以上motionManager.deviceMotionUpdateInterval = 0.01// 开始获取设备运动数据motionManager.startDeviceMotionUpdates(to: .main) { [weak self] (motion, error) inguard let motion = motion else {return}if let lastLocation = self?.lastLocation {self?.acc_ax = motion.userAcceleration.xself?.acc_ay = motion.userAcceleration.yself?.acc_az = motion.userAcceleration.zlet velocity_x = motion.userAcceleration.x * (motion.timestamp - lastLocation.timestamp)let velocity_y = motion.userAcceleration.y * (motion.timestamp - lastLocation.timestamp)let velocity_z = motion.userAcceleration.z * (motion.timestamp - lastLocation.timestamp)self?.ax = velocity_xself?.ay += velocity_yself?.az += velocity_z// sqrt 有问题?变成nanself?.totalDistance += abs(velocity_x*velocity_x)
//                self?.totalDistance += sqrt(velocity_x*velocity_x + velocity_y+velocity_y + velocity_z*velocity_z)print("欧式距离里程:\(self?.totalDistance ?? 0)") //如果可选类型的值为nil,空合运算符会返回它的右侧的默认值(在这里是0)print("加速度:\(motion.userAcceleration)")// 使用低通滤波器let _m_acc_x = lowPassFilter(motion.userAcceleration.x)self?.m_acc_ax = _m_acc_xlet _m_acc_y = lowPassFilter(motion.userAcceleration.y)self?.m_acc_ay = _m_acc_ylet _m_acc_z = lowPassFilter(motion.userAcceleration.z)self?.m_acc_az = _m_acc_zlet m_velocity_x = _m_acc_x * (motion.timestamp - lastLocation.timestamp)let m_velocity_y = _m_acc_y * (motion.timestamp - lastLocation.timestamp)let m_velocity_z = _m_acc_z * (motion.timestamp - lastLocation.timestamp)self?.m_dis_ax = m_velocity_xself?.m_dis_ay += m_velocity_yself?.m_dis_az += m_velocity_zself?.m_totalDistance += abs(m_velocity_x)}self?.lastLocation = motion}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/613624.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

openssl3.2 - 官方dmeo学习 - 索引贴

文章目录 openssl3.2 - 官方dmeo学习 - 索引贴概述笔记工程的搭建和调试环境BIOBIO - client-arg.cBIO - client-conf.cBIO - saccept.cBIO - sconnect.cBIO - server-arg.cBIO - server-cmod.cBIO - server-conf.cBIO - 总结certsEND openssl3.2 - 官方dmeo学习 - 索引贴 概述…

长尾分布定义,举个物种长尾分布和词频长尾分布的例子。

问题描述&#xff1a;长尾分布定义&#xff0c;举个物种长尾分布和词频长尾分布的例子。 问题解答&#xff1a; 长尾分布是一种概率分布的类型&#xff0c;它描述的是一种极端事件或者稀有事件的发生概率。具体来说&#xff0c;长尾分布描述的是少量的类别占据了大部分的样本…

SpringIOC之support模块GenericXmlApplicationContext

博主介绍&#xff1a;✌全网粉丝5W&#xff0c;全栈开发工程师&#xff0c;从事多年软件开发&#xff0c;在大厂呆过。持有软件中级、六级等证书。可提供微服务项目搭建与毕业项目实战&#xff0c;博主也曾写过优秀论文&#xff0c;查重率极低&#xff0c;在这方面有丰富的经验…

STM32(HAL库) CubeMX+Keil5 建立工程

STM32&#xff08;HAL库&#xff09; CubeMXKeil5 建立工程 目标选择 菜单栏 File 新建工程打开工程退出软件 Window 输出窗口的开启软件字体设置 Help 软件帮助文档检查软件更新管理MCU 已存在工程&#xff08;Existing Projects&#xff09; 最近打开过的工程(Recent Open…

2024年值得关注的10种自动化测试趋势

超级自动化测试这是利用人工智能&#xff08;AI&#xff09;和机器学习&#xff08;ML&#xff09;来自动化测试任务。超级自动化测试可以帮助减少手动测试的需求&#xff0c;提高测试的效率&#xff0c;并在开发生命周期的早期发现缺陷。 TestOps集成TestOps是将DevOps实践扩…

PyPDF2 3.0.0更新,一些函数被弃用,需要重新写

1.PdfFileWriter is deprecated and was removed in PyPDF2 3.0.0. Use PdfWriter instead. 这错误表明你正在使用的 PyPDF2 版本中已经移除了 PdfFileWriter&#xff0c;并在版本 3.0.0 中被替代为 PdfWriter。这是因为在 PyPDF2 的更新中&#xff0c;一些 API 被重新组织和更…

中霖教育:专业不对口,能考会计师吗?

学的不是会计专业&#xff0c;专业不对口&#xff0c;能不能考会计师? 从学历要求来看&#xff0c;考会计师并无硬性规定必须具备哪个专业的学历。所以只要符合报考条件&#xff0c;非会计专业的人也可以报考会计师。 除了基本条件外&#xff0c;报名参加中级会计考试的人员…

html学习之路:简述html文档头部 <meta> 的 http-equiv 属性

&#x1f9cb;当输入网址打开网页时&#xff0c;设置html头部meta的http-equiv属性&#xff0c;可以帮助浏览器更加精确和正常却的显示网页内容&#xff0c;比如设置网页多久自动刷新&#xff0c;设置网页在浏览器缓存中的时限&#xff0c;设置多少事件跳转到指定的网页地址&am…

leetcode-合并两个有序数组

88. 合并两个有序数组 题解&#xff1a; 这是一个经典的双指针问题&#xff0c;我们可以使用两个指针分别指向nums1和nums2的最后一个元素&#xff0c;然后比较两个指针所指向的元素大小&#xff0c;将较大的元素放入nums1的末尾&#xff0c;并将对应的指针向前移动一位。重复…

Redis系列-15.Redis的IO多路复用原理解析

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱吃芝士的土豆倪&#xff0c;24届校招生Java选手&#xff0c;很高兴认识大家&#x1f4d5;系列专栏&#xff1a;Spring源码、JUC源码、Kafka原理、分布式技术原理、数据库技术&#x1f525;如果感觉博主的文章还不错的…

[DM8] 达梦8配置兼容Oracle

查看版本信息 select *&#xff0c;id_code from v$version; 查询解释&#xff1a; DM Database Server 64 V8 1-1-190-21.03.12-136419-ENT 64 版本位数标识&#xff0c;64表示为64位版本&#xff0c;无64则表示为32位版本 V8 大版本号&#xff0c;目前主要是V7、V8 1-1-190…

智慧医院之定位导航解决方案

移动端LBS应用 通过绘制院方各楼栋各层平面图,利用无线/蓝牙技术可对终端进行实时定位,方便病人、家属等就医,提高就医体验,减少工作人员工作量,减少医患冲突,打造智慧医院。 移动端的LBS位置应用,可分为医院的室内地图展现、室内地图搜索、室内导航、室内定位、室内位…

【JaveWeb教程】(18) MySQL数据库开发之 MySQL数据库设计-DDL 如何查询、创建、使用、删除数据库数据表 详细代码示例讲解

目录 2. 数据库设计-DDL2.1 项目开发流程2.2 数据库操作2.2.1 查询数据库2.2.2 创建数据库2.2.3 使用数据库2.2.4 删除数据库 2.3 图形化工具2.3.1 介绍2.3.2 安装2.3.3 使用2.2.3.1 连接数据库2.2.3.2 操作数据库 2.3 表操作2.3.1 创建2.3.1.1 语法2.3.1.2 约束2.3.1.3 数据类…

KAFKA高级应用

kafka高级应用 一些kafka的基础使用以及说明请参考上一篇文章kafka的基础入门。这篇文章主要是写kafka的一些高级特性、存储结构以及原理。 1.kafka副本同步机制 高可用是很多分布式系统中必备的特征之一&#xff0c;Kafka的高可用是通过基于 leader-follower的多副本同步实…

Qt QComboBox组合框控件

文章目录 1 属性和方法1.1 文本1.2 图标1.3 插入和删除1.4 信号和槽 2 实例2.1 布局2.2 代码实现 Qt中的组合框是集按钮和下拉列表体的控件&#xff0c;&#xff0c;它占用的屏幕空间很小&#xff0c;对应的类是QComboBox 1 属性和方法 QComboBox有很多属性&#xff0c;完整的…

Java编程避坑指南之关键字专题

1、Java常见关键字 1&#xff09;、48个关键字&#xff1a; abstract、assert、boolean、break、byte、case、catch、char、class、continue、default、do、double、else、enum、extends、final、finally、float、for、if、implements、import、int、interface、instanceof、l…

C++入门【27-C++ 引用】

引用变量是一个别名&#xff0c;也就是说&#xff0c;它是某个已存在变量的另一个名字。一旦把引用初始化为某个变量&#xff0c;就可以使用该引用名称或变量名称来指向变量。 C 引用 vs 指针 引用很容易与指针混淆&#xff0c;它们之间有三个主要的不同&#xff1a; 不存在…

编程语言的未来:创新与发展

编程语言的未来&#xff1f; 编程语言是计算机软件的基础&#xff0c;它们为程序员提供了一种沟通和指导计算机的方式。随着软件需求的不断增长和技术的进步&#xff0c;编程语言也需要不断演化和创新&#xff0c;以满足不断变化的需求。未来的编程语言将更加强大、灵活、易用…

航空服务市场分析:预计2024年客运总量将达40亿人次

在政策的引导和市场发展下&#xff0c;支线航空发展机遇在于在一些具备需求的区域&#xff0c;持续推进"航空服务大众化"。此前&#xff0c;美国实行"普遍航空服务"计划&#xff0c;我国也需要加快推进"国家基本航空服务计划"政策体系。国民经济…

springboot第46集:Nginx,Sentinel,计算机硬件的介绍

image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png 什么是单点容错率低&#xff1a; 单点容错率低指的是系统中存在某个关键节点&#xff0c;一旦这个节点发生故障或崩…