陀螺仪更新核心运动

时间:2012-02-06 16:45:25

标签: ios gyroscope core-motion

我是核心动议的新手,经过大量搜索而没有得到满意的答案,我决定在这里发布我的问题,看看是否有人可以帮助我。 我正试图从iPhone陀螺仪获得偏航角度:

    motionManager = [[CMMotionManager alloc] init];

    if (motionManager.gyroAvailable) {

        motionManager.gyroUpdateInterval = 1.0/60.0;
        [motionManager 
         startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
         toQueue:[NSOperationQueue currentQueue]
         withHandler: ^(CMDeviceMotion *motion, NSError *error) {

            CMAttitude *attitude = motion.attitude;
            NSLog(@"%f %f %f", attitude.roll, attitude.pitch, attitude.yaw);

        }];

    } else {
        NSLog(@"No gyroscope on device.");
    }

无论手机的方向如何,此代码都会打印几乎不变的值:

2012-02-06 17:38:29.074 test[11279:707] 0.284117 0.536363 -0.078897
2012-02-06 17:38:29.517 test[11279:707] 0.282726 0.535011 -0.079389
2012-02-06 17:38:30.145 test[11279:707] 0.281268 0.535169 -0.080593
2012-02-06 17:38:30.887 test[11279:707] 0.282435 0.543171 -0.084084
2012-02-06 17:38:31.671 test[11279:707] 0.281040 0.547520 -0.086938
2012-02-06 17:38:31.852 test[11279:707] 0.279655 0.553691 -0.090453
2012-02-06 17:38:32.512 test[11279:707] 0.279519 0.561513 -0.095231
2012-02-06 17:38:32.613 test[11279:707] 0.278557 0.567429 -0.099968
2012-02-06 17:38:32.717 test[11279:707] 0.279050 0.574920 -0.105910
2012-02-06 17:38:33.317 test[11279:707] 0.280330 0.583690 -0.112861
2012-02-06 17:38:34.086 test[11279:707] 0.281029 0.592159 -0.120220
2012-02-06 17:38:34.784 test[11279:707] 0.280887 0.599839 -0.127748
2012-02-06 17:38:35.407 test[11279:707] 0.280940 0.606983 -0.135865
2012-02-06 17:38:36.025 test[11279:707] 0.280652 0.612754 -0.144395
2012-02-06 17:38:36.651 test[11279:707] 0.279496 0.616934 -0.153258
2012-02-06 17:38:37.251 test[11279:707] 0.277919 0.619977 -0.162749
2012-02-06 17:38:37.856 test[11279:707] 0.274963 0.621073 -0.171352
2012-02-06 17:38:38.446 test[11279:707] 0.272552 0.621618 -0.179002
2012-02-06 17:38:39.086 test[11279:707] 0.269863 0.621692 -0.184768
2012-02-06 17:38:39.749 test[11279:707] 0.265449 0.620685 -0.187640
2012-02-06 17:38:40.015 test[11279:707] 0.256092 0.614860 -0.186004
2012-02-06 17:38:40.547 test[11279:707] 0.252883 0.613316 -0.186697
2012-02-06 17:38:41.314 test[11279:707] 0.237640 0.600663 -0.179584
2012-02-06 17:38:42.064 test[11279:707] 0.228467 0.593559 -0.175179
2012-02-06 17:38:42.627 test[11279:707] 0.225964 0.591553 -0.173761
2012-02-06 17:38:43.299 test[11279:707] 0.219047 0.585079 -0.169377
2012-02-06 17:38:43.857 test[11279:707] 0.213861 0.578946 -0.165632
2012-02-06 17:38:44.466 test[11279:707] 0.212410 0.575619 -0.163787
2012-02-06 17:38:45.122 test[11279:707] 0.209166 0.568359 -0.161083
2012-02-06 17:38:45.640 test[11279:707] 0.205685 0.560641 -0.158335
2012-02-06 17:38:46.196 test[11279:707] 0.203486 0.556160 -0.156266
2012-02-06 17:38:46.680 test[11279:707] 0.200355 0.548807 -0.153427
2012-02-06 17:38:47.139 test[11279:707] 0.196721 0.541516 -0.150096
2012-02-06 17:38:47.603 test[11279:707] 0.193479 0.534318 -0.146664
2012-02-06 17:38:48.953 test[11279:707] 0.190706 0.528044 -0.143307
2012-02-06 17:38:49.420 test[11279:707] 0.188027 0.521882 -0.140167
2012-02-06 17:38:49.891 test[11279:707] 0.186110 0.518000 -0.137604
2012-02-06 17:38:50.772 test[11279:707] 0.183170 0.511420 -0.134743
2012-02-06 17:38:51.235 test[11279:707] 0.180418 0.504996 -0.132144
2012-02-06 17:38:51.698 test[11279:707] 0.177734 0.498595 -0.129568
2012-02-06 17:38:52.154 test[11279:707] 0.175299 0.492050 -0.127031
2012-02-06 17:38:52.624 test[11279:707] 0.172230 0.485532 -0.124196
2012-02-06 17:38:53.081 test[11279:707] 0.170063 0.481812 -0.122000
2012-02-06 17:38:53.583 test[11279:707] 0.166253 0.476152 -0.119293
2012-02-06 17:38:54.025 test[11279:707] 0.160787 0.468770 -0.116094
2012-02-06 17:38:54.482 test[11279:707] 0.158701 0.466625 -0.114797
2012-02-06 17:38:54.940 test[11279:707] 0.155296 0.462999 -0.113401
2012-02-06 17:38:55.456 test[11279:707] 0.152151 0.460157 -0.112659
2012-02-06 17:38:55.492 test[11279:707] 0.150581 0.459592 -0.112931
2012-02-06 17:38:55.614 test[11279:707] 0.148270 0.457763 -0.112954
2012-02-06 17:38:56.004 test[11279:707] 0.147790 0.455452 -0.113850
2012-02-06 17:38:56.505 test[11279:707] 0.146853 0.455745 -0.114760
2012-02-06 17:38:56.984 test[11279:707] 0.146299 0.453550 -0.116355
2012-02-06 17:38:57.482 test[11279:707] 0.144864 0.450247 -0.118949
2012-02-06 17:38:57.949 test[11279:707] 0.141946 0.450065 -0.122539
2012-02-06 17:38:58.414 test[11279:707] 0.138981 0.448612 -0.127861
2012-02-06 17:38:59.003 test[11279:707] 0.135581 0.449638 -0.134634
2012-02-06 17:38:59.484 test[11279:707] 0.131823 0.452456 -0.142817
2012-02-06 17:39:00.034 test[11279:707] 0.127508 0.456594 -0.152322
2012-02-06 17:39:00.191 test[11279:707] 0.122910 0.462254 -0.163025
2012-02-06 17:39:00.207 test[11279:707] 0.118614 0.469168 -0.175117
2012-02-06 17:39:00.578 test[11279:707] 0.114704 0.477054 -0.188226
2012-02-06 17:39:00.616 test[11279:707] 0.110742 0.486018 -0.201588
2012-02-06 17:39:00.748 test[11279:707] 0.106248 0.495900 -0.214898
2012-02-06 17:39:01.665 test[11279:707] 0.102002 0.506241 -0.228094
2012-02-06 17:39:02.336 test[11279:707] 0.099557 0.514980 -0.239528
2012-02-06 17:39:02.407 test[11279:707] 0.098438 0.520571 -0.247081
2012-02-06 17:39:02.495 test[11279:707] 0.097672 0.523262 -0.250384
2012-02-06 17:39:02.552 test[11279:707] 0.097222 0.524001 -0.250785
2012-02-06 17:39:02.922 test[11279:707] 0.097418 0.523620 -0.249829
2012-02-06 17:39:03.414 test[11279:707] 0.099227 0.521513 -0.249166
2012-02-06 17:39:03.929 test[11279:707] 0.101716 0.519924 -0.249055
2012-02-06 17:39:04.679 test[11279:707] 0.102946 0.519034 -0.248954
2012-02-06 17:39:05.250 test[11279:707] 0.104410 0.518250 -0.250017
2012-02-06 17:39:05.339 test[11279:707] 0.105101 0.518529 -0.251809
2012-02-06 17:39:05.414 test[11279:707] 0.103954 0.517553 -0.253460
2012-02-06 17:39:05.834 test[11279:707] 0.102850 0.517375 -0.255460
2012-02-06 17:39:05.853 test[11279:707] 0.100568 0.516271 -0.256589
2012-02-06 17:39:05.871 test[11279:707] 0.096929 0.513702 -0.256523
2012-02-06 17:39:06.405 test[11279:707] 0.092045 0.510893 -0.255013
2012-02-06 17:39:07.029 test[11279:707] 0.086092 0.507567 -0.252255
2012-02-06 17:39:07.538 test[11279:707] 0.081380 0.504508 -0.249655
2012-02-06 17:39:08.699 test[11279:707] 0.074794 0.500937 -0.246108
2012-02-06 17:39:10.308 test[11279:707] 0.068511 0.497140 -0.243295
2012-02-06 17:39:10.469 test[11279:707] 0.063528 0.493209 -0.241749
2012-02-06 17:39:11.196 test[11279:707] 0.057956 0.487372 -0.240436
2012-02-06 17:39:11.328 test[11279:707] 0.053526 0.481475 -0.240096
2012-02-06 17:39:11.342 test[11279:707] 0.049141 0.475907 -0.239913
2012-02-06 17:39:12.003 test[11279:707] 0.044934 0.470173 -0.239304
2012-02-06 17:39:12.643 test[11279:707] 0.041000 0.464876 -0.238245
2012-02-06 17:39:12.738 test[11279:707] 0.037876 0.461354 -0.237112
2012-02-06 17:39:12.786 test[11279:707] 0.034908 0.458204 -0.235784
2012-02-06 17:39:13.987 test[11279:707] 0.032735 0.456382 -0.234687
2012-02-06 17:39:14.130 test[11279:707] 0.031751 0.454415 -0.234166
2012-02-06 17:39:14.201 test[11279:707] 0.030873 0.452816 -0.233771
2012-02-06 17:39:14.608 test[11279:707] 0.031080 0.450683 -0.233894
2012-02-06 17:39:15.214 test[11279:707] 0.030725 0.447874 -0.233945
2012-02-06 17:39:16.000 test[11279:707] 0.031100 0.444689 -0.234371
2012-02-06 17:39:16.567 test[11279:707] 0.031405 0.440659 -0.234729
2012-02-06 17:39:17.143 test[11279:707] 0.030912 0.436343 -0.234610
2012-02-06 17:39:17.802 test[11279:707] 0.029276 0.432026 -0.233608
2012-02-06 17:39:18.601 test[11279:707] 0.026838 0.428396 -0.232015
2012-02-06 17:39:18.714 test[11279:707] 0.024792 0.425108 -0.230611
2012-02-06 17:39:19.335 test[11279:707] 0.022407 0.421895 -0.229250
2012-02-06 17:39:20.006 test[11279:707] 0.020278 0.418510 -0.228097
2012-02-06 17:39:20.920 test[11279:707] 0.018179 0.414834 -0.227015
2012-02-06 17:39:21.818 test[11279:707] 0.016243 0.410729 -0.225862
2012-02-06 17:39:21.954 test[11279:707] 0.014359 0.406511 -0.224585
2012-02-06 17:39:22.119 test[11279:707] 0.012452 0.402080 -0.223064
2012-02-06 17:39:22.614 test[11279:707] 0.010601 0.397233 -0.221162
2012-02-06 17:39:22.692 test[11279:707] 0.008699 0.392451 -0.218747
2012-02-06 17:39:22.818 test[11279:707] 0.007168 0.388804 -0.216152
2012-02-06 17:39:23.190 test[11279:707] 0.005118 0.384335 -0.213227
2012-02-06 17:39:23.658 test[11279:707] 0.002705 0.380201 -0.210206
2012-02-06 17:39:24.124 test[11279:707] 0.001094 0.377416 -0.207627
2012-02-06 17:39:24.607 test[11279:707] -0.002178 0.373076 -0.204743
2012-02-06 17:39:25.117 test[11279:707] -0.004245 0.369853 -0.202551
2012-02-06 17:39:25.669 test[11279:707] -0.005570 0.367699 -0.200901
2012-02-06 17:39:26.279 test[11279:707] -0.006612 0.364938 -0.199402
2012-02-06 17:39:26.483 test[11279:707] -0.007071 0.362445 -0.197858
2012-02-06 17:39:26.514 test[11279:707] -0.006780 0.360407 -0.196251
2012-02-06 17:39:27.038 test[11279:707] -0.006128 0.359039 -0.194405
2012-02-06 17:39:27.605 test[11279:707] -0.005377 0.358899 -0.192268
2012-02-06 17:39:28.153 test[11279:707] -0.003901 0.359926 -0.190265
2012-02-06 17:39:28.751 test[11279:707] -0.002602 0.361514 -0.188276
2012-02-06 17:39:29.318 test[11279:707] -0.001312 0.363152 -0.186416
2012-02-06 17:39:29.886 test[11279:707] 0.000524 0.364863 -0.184941
2012-02-06 17:39:30.436 test[11279:707] 0.002012 0.366129 -0.183612
2012-02-06 17:39:31.009 test[11279:707] 0.004049 0.366699 -0.182729
2012-02-06 17:39:31.605 test[11279:707] 0.005820 0.367657 -0.181845
kill
quit

我不知道为什么会发生这种情况,理论上它应该以弧度给出数字(pi / 2是最大值,约为1.57)。

感谢阅读。

AA

2 个答案:

答案 0 :(得分:1)

 motionManager= [[CMMotionManager alloc] init];

    if (motionManager.gyroAvailable) {

        motionManager.gyroUpdateInterval = 1.0/60.0;
        [motionManager 
         startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler: ^(CMDeviceMotion *motion, NSError *error) {

             CMAttitude *attitude = motion.attitude;
             NSLog(@"%f %f %f", attitude.roll, attitude.pitch, attitude.yaw);

         }];

尝试我的代码我保证这是有效的,至少在我的设备上。最小/最大是-Pi / + Pi

实际上经过几次快速更改后..您的代码也可以在我的设备上运行而不会出现问题。你在测试什么设备?

答案 1 :(得分:0)

尝试创建新的操作队列:

[motionManager startDeviceMotionUpdatesToQueue:[[[NSOperationQueue alloc] init] autorelease] withHandler: ^(CMDeviceMotion *motion, NSError *error) { ...

我遇到了这个问题,因为我的应用程序仅以25 FPS运行。