获取加速度数据,陀螺仪数据,磁场数据的两种方式

阅读: 评论:0

获取加速度数据,陀螺仪数据,磁场数据的两种方式

获取加速度数据,陀螺仪数据,磁场数据的两种方式

 

下面程序示范了如何通过代码来获取加速度数据、陀螺仪数据、磁场数据。新建一个SingleView Application,该项目包含一个应用程序委托类,一个视图控制器类和Main.storyboard

界面设计文件。

打开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabelgyroLabelmagnetometerLabel这3个IBOutlet属性。

  下面是该示例视图控制器代码:

一、基于代码块方式获取

@interface ViewController ()
{}
@property (weak, nonatomic) IBOutlet UILabel *gyroLabel;
@property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel;
@property (weak, nonatomic) IBOutlet UILabel *magnetometerLabel;
@property (strong,nonatomic)CMMotionManager * motionManager;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.//CFMotionmangaer对象ionManager = [[CMMotionManager alloc]init];NSOperationQueue * queue = [[NSOperationQueue alloc] init];//如果CMMotionManager支持获取加速度数据if (ionManager.accelerometerAvailable) {//设置CMMotionManager的加速度数据更新频率为0.1秒ionManager.accelerometerUpdateInterval = 0.1;//使用代码块开始获取加速度数据[ionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {NSString * labelText;//如果发生了错误,error不为空if (error) {//停止获取加速度数据[ionManager stopAccelerometerUpdates];labelText = [NSString stringWithFormat:@"获取加速度shuu出现错误:%@",error];}else{FKbarllView * barllview = [[FKbarllView alloc] init];barllview.acceleration = accelerometerData.acceleration;//分别获取系统在X,Y,Z轴上的加速度数据labelText = [NSString stringWithFormat:@"加速度为n-----nX轴:%+.2fnY轴:%+.2fnZ轴:%.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];}//在主线程中更新accelerometerLabel的文本,显示加速度数据[self.accelerometerLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO];}];}else{NSLog(@"该设备不支持获取加速度数据!");}//如果CMMotionManager支持获取陀螺仪数据if (Available) {//设置CMMotionManager的陀螺仪数据更新频率为0.1秒[ionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData, NSError *error) {NSString * labelText;//如果发生了错误,error不为空if(error){//停止获取陀螺仪数据[ionManager stopGyroUpdates];labelText = [NSString stringWithFormat:@"获取陀螺仪数据出现错误;%@",error];}else{//分别获取设备烧X,Y,Z轴的转速labelText = [NSString stringWithFormat:@"绕各轴的转速为n-----nX轴:%+.2fnY轴:%+.2fnZ轴:%.2f",ationRate.ationRate.ationRate.z];}//在主线程中更新gyroLabel的文本,显示绕各轴的转速[Label performSelectorOnMainThread:@selector(setText:) withObject:labelText  waitUntilDone:NO];}];}else{NSLog(@"该设备不支持获取陀螺仪数据!");}//如果CMMotionManager支持获取磁场数据if (ionManager.magnetometerAvailable) {//设置CMMot的磁场数据更新频率为0.1秒ionManager.magnetometerUpdateInterval = 0.1f;[ionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData * magnetometerData, NSError *error) {NSString * labelText;if (error) {//停止获取数据[ionManager stopMagnetometerUpdates];labelText = [NSString stringWithFormat:@"获取磁场数据出现错误;%@",error];}else{labelText = [NSString stringWithFormat:@"磁场数据为n-----nX轴:%+.2fnY轴:%+.2fnZ轴:%.2f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z];}//在主线程中更新magnetometerLabel的文本,显示磁场数据[self.magnetometerLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO];}];}else{NSLog(@"该设备不支持获取磁场数据!");}}

 

下面程序示范了如何通过主动请求来获取加速度数据、陀螺仪数据、磁场数据。新建一个SingleView Application,该项目包含一个应用程序委托类,一个视图控制器类和Main.storyboard

界面设计文件。

打 开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数 据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabelgyroLabel magnetometerLabel这3个IBOutlet属性。

  下面是该示例视图控制器代码:

二、主动请求获取

@interface ViewController ()
{NSTimer * updateTimer;}
@property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel;
@property (weak, nonatomic) IBOutlet UILabel *gyroLabel;
@property (weak, nonatomic) IBOutlet UILabel *magnetometerLabel;@property (strong,nonatomic) CMMotionManager* motionMaager;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.//创建CMMotionManager对象ionMaager = [[CMMotionManager alloc] init];//如果CMMotionManager支持获取加速度数据if (ionMaager.accelerometerAvailable) {[ionMaager startAccelerometerUpdates];}else{NSLog(@"该设备不支持获取加速度数据!");}//如果CMMotionManager支持获取陀螺仪数据if (Available) {[ionMaager startGyroUpdates];}else{NSLog(@"该设备不支持获取陀螺仪数据!");}//如果CMMotionManager支持获取磁场数据if (ionMaager.magnetometerAvailable) {[ionMaager startMagnetometerUpdates];}else{NSLog(@"该设备不支持获取磁场数据!");}
}
-(void)viewWillAppear:(BOOL)animated
{[super viewWillAppear:animated];//启动定时器来周期性轮询加速度数据,陀螺仪数据,磁场数据updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateDisplay) userInfo:nil repeats:YES];
}
-(void)updateDisplay
{//如果CMMotionManager的加速度数据可用if (ionMaager.accelerometerAvailable) {//主动请求获取加速度数据CMAccelerometerData * accelerometerData = ionMaager.accelerometerData; = [NSString stringWithFormat:@"加速度为n-nX轴:%+.2fnY轴:%+.2fnZ轴:%+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];}//如果CMMotionManager的陀螺仪数据可用if (Available) {//主动请求获取加速度数据CMGyroData * gyroData = Data;
//         = [NSString stringWithFormat:@"陀螺仪数据为n-nX轴:%+.2fnY轴:%+.2fnZ轴:%+.2f",ationRate.ationRate.ationRate.z];}//如果CMMotionManager的磁场数据可用if (ionMaager.magnetometerAvailable) {//主动请求获取加速度数据CMMagnetometerData * magnetometerData = ionMaager.magnetometerData; = [NSString stringWithFormat:@"加速度为n-nX轴:%+.2fnY轴:%+.2fnZ轴:%+.2f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z];}
}

 ,在真机上运行这两个程序,将可以看到运行结果

转载于:.html

本文发布于:2024-03-12 14:26:08,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1710648008144670.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   陀螺仪   两种   加速度   磁场
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23