iOSセンサー編:データ取得
6995 ワード
iOSセンサー編:CoreMotion初探査
使用
CMAltimeter
きあつけい
インポート
使用
1.CMMotionManagerオブジェクトの作成
2.CMMotionManagerに加速度データ、ヘリカルデータ、磁界データ周波数を設定し、通常xxxUpdateInterval属性を設定し、単位時間は秒である
3.CMMotionManger startXXXXUpdatesToQueue:queue withHanlder:メソッドを呼び出して周期的にデータを取得し、
4.エラーが発生した場合、これらのデータの取得を中止しようとするstopXXXXUpdatesメソッドの停止
ヘリカルデータの取得
磁場データの取得
アクティブリクエスト加速度データ、ジャイロデータ、磁場データの取得
アクティブ要求加速度データ
プロアクティブ要求ジャイロデータ
アクティブリクエスト磁場データ
センシングデバイスの移動
歩数計
デバイスステータス
使用
CMAltimeter
きあつけい
#import
//
NSLog(@"%d",[CMAltimeter isRelativeAltitudeAvailable]);
//
self.altimeter = [[CMAltimeter alloc]init];
[self.altimeter startRelativeAltitudeUpdatesToQueue:NSOperationQueue.mainQueue withHandler:^(CMAltitudeData * _Nullable altitudeData, NSError * _Nullable error) {
NSLog(@"%lf",[altitudeData.relativeAltitude floatValue]);
NSLog(@"%@",error);
}];
インポート
import
@property(strong,nonatomic)CMMotionManager *Manager;
使用
1.CMMotionManagerオブジェクトの作成
2.CMMotionManagerに加速度データ、ヘリカルデータ、磁界データ周波数を設定し、通常xxxUpdateInterval属性を設定し、単位時間は秒である
// XXX :accelerometer( )
// gyro( )
// magnetometer( )
3.CMMotionManger startXXXXUpdatesToQueue:queue withHanlder:メソッドを呼び出して周期的にデータを取得し、
4.エラーが発生した場合、これらのデータの取得を中止しようとするstopXXXXUpdatesメソッドの停止
self.Manager = [[CMMotionManager alloc]init];
NSOperationQueue *queque = [[NSOperationQueue alloc]init];
if (self.Manager.accelerometerActive) {
// CMMotionManager 0.1s
self.Manager.accelerometerUpdateInterval = 0.1;
//
[self.Manager startAccelerometerUpdatesToQueue:queque withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
if(error){
[self.Manager stopAccelerometerUpdates];
NSLog(@" %@",error);
}else{
// x y z
NSLog(@"x-----------%lf",accelerometerData.acceleration.x);
NSLog(@"y-----------%lf",accelerometerData.acceleration.y);
NSLog(@"x-----------%lf",accelerometerData.acceleration.z);
}
}];
}else{
NSLog(@" ");
}
ヘリカルデータの取得
if (self.Manager.gyroActive) {
//
self.Manager.gyroUpdateInterval = 0.1;
[self.Manager startGyroUpdatesToQueue:queque withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
if (error) {
[self.Manager stopGyroUpdates];
}else{
NSLog(@"x-----------%lf",gyroData.rotationRate.x);
NSLog(@"y-----------%lf",gyroData.rotationRate.y);
NSLog(@"z-----------%lf",gyroData.rotationRate.z);
}
}];
}else{
NSLog(@" ");
}
磁場データの取得
if (self.Manager.magnetometerActive) {
//
self.Manager.magnetometerUpdateInterval = 0.1;
[self.Manager startMagnetometerUpdatesToQueue:queque withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
if (error) {
[self.Manager stopMagnetometerUpdates];
}else{
NSLog(@"x-----------%lf",magnetometerData.magneticField.x);
NSLog(@"y-----------%lf",magnetometerData.magneticField.y);
NSLog(@"z-----------%lf",magnetometerData.magneticField.z);
}
}];
}else{
NSLog(@" ");
}
アクティブリクエスト加速度データ、ジャイロデータ、磁場データの取得
アクティブ要求加速度データ
if (self.Manager.accelerometerAvailable) {
CMAccelerometerData *AccelerometerData = self.Manager.accelerometerData;
NSLog(@"x-----------%lf",AccelerometerData.acceleration.x);
}
プロアクティブ要求ジャイロデータ
if (self.Manager.gyroAvailable) {
CMGyroData *gyroData = self.Manager.gyroData;
NSLog(@"x-----------%lf",gyroData.rotationRate.x);
}
アクティブリクエスト磁場データ
if (self.Manager.magnetometerAvailable) {
CMMagnetometerData *magnetometerData = self.Manager.magnetometerData;
NSLog(@"x-----------%lf",magnetometerData.magneticField.x);
}
センシングデバイスの移動
//
if (self.Manager.deviceMotionAvailable) {
//
[self.Manager startDeviceMotionUpdates];
}else{
NSLog(@" deviceMotion ");
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(Update) userInfo:nil repeats:YES];
}
-(void)Update{
if (self.Manager.deviceMotionAvailable) {
CMDeviceMotion *deviceMotion = self.Manager.deviceMotion;
NSLog(@" (Z ):%lf",deviceMotion.attitude.yaw);
NSLog(@" (X ):%lf",deviceMotion.attitude.pitch);
NSLog(@" :(Y )%lf",deviceMotion.attitude.roll);
//
NSLog(@"%lf",deviceMotion.rotationRate.x);
NSLog(@"%lf",deviceMotion.rotationRate.y);
NSLog(@"%lf",deviceMotion.rotationRate.z);
//
NSLog(@"%lf",deviceMotion.gravity.x);
NSLog(@"%lf",deviceMotion.gravity.y);
NSLog(@"%lf",deviceMotion.gravity.z);
// ( )
NSLog(@"%d",deviceMotion.magneticField.accuracy);
//xyz
NSLog(@"%lf",deviceMotion.magneticField.field.x);
NSLog(@"%lf",deviceMotion.magneticField.field.y);
NSLog(@"%lf",deviceMotion.magneticField.field.z);
}
歩数計
@property(nonatomic,strong)CMStepCounter *stepCounter;
//
if ([CMStepCounter isStepCountingAvailable]) {
self.stepCounter = [[CMStepCounter alloc]init];
NSOperationQueue *queue1 = [[NSOperationQueue alloc]init];
//updateOn : 5
[self.stepCounter startStepCountingUpdatesToQueue:queue1 updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {
NSLog(@" %ld",numberOfSteps);
}];
}else{
NSLog(@" ");
}
デバイスステータス
#import
// CMMotionActivityManager
// , , , ,
NSOperationQueue *queue2= [[NSOperationQueue alloc]init];
[self.motionActivityManager startActivityUpdatesToQueue:queue2 withHandler:^(CMMotionActivity * _Nullable activity) {
NSLog(@" :%d",activity.walking);
NSLog(@" :%d",activity.running);
NSLog(@" :%d",activity.automotive);
NSLog(@" :%d",activity.stationary);
NSLog(@" :%d",activity.unknown);
}];