开发者

Accelerometer get me 0000 all time

I get 0 0 0 0 0 0 0 0 0 0 0 0

 - (void)applicationDidFinishLaunching:(UIApplication *)application {resultValues.text = @"";
[[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1.0 / kUpdateFrequency];
[[UIAccelerometer sharedAccelerometer] setDelegat开发者_开发知识库e:self];
 }
  - (void)accelerometer:(UIAccelerometer*)accelerometer      didAccelerate:(UIAcceleration*)acceleration 
 {
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
printf("\n%d", x);
printf("\n%d", y);
printf("\n%d", z);

}


The problem is with your printf statements.

The UIAcceleration class defines its properties like so:

@property(nonatomic, readonly) UIAccelerationValue x

And UIAccelerationValue is typedef'ed like so:

typedef double UIAccelerationValue

The d printf specifier is for ints. You want one for doubles. You want something like so:

printf("\n%g %g %g", acceleration.x, acceleration.y, acceleration.z);

(You can use f or e as well. See: http://en.wikipedia.org/wiki/Printf#printf_format_placeholders )


The acceleration values are doubles, and further expressed in units of earth gravity. e.g. 1g. It is not surprising that when you assign them to ints and print the values they are below zero, as the acceleration vector's component would be distributed over the xyz axis unless you were holding one of them precisely parallel to Earth's acceleration vector.

Treat them like the floating point values that they are, and you should see changes as you manipulate your device.

The only time the accelerometer will give legitimate all zero values is when you are in free fall.


x,y,z are floating points, not integers.

change printf("\n%d", x); to printf("\n%f", x);


printf("\n%.5f", x);
printf("\n%.5f", y);
printf("\n%.5f", z);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜