|
| 1 | + |
| 2 | +#import "RNIKInteractiveImageLibrary.h" |
| 3 | +#import <React/RCTBridge.h> |
| 4 | +#import <React/RCTEventDispatcher.h> |
| 5 | + |
| 6 | +@implementation RNIKInteractiveImageLibrary |
| 7 | + |
| 8 | +- (dispatch_queue_t)methodQueue |
| 9 | +{ |
| 10 | + return dispatch_get_main_queue(); |
| 11 | +} |
| 12 | + |
| 13 | +RCT_EXPORT_MODULE(); |
| 14 | + |
| 15 | +- (id)init { |
| 16 | + if (self = [super init]) { |
| 17 | + self.motionManager = [[CMMotionManager alloc] init]; |
| 18 | + self.motionManager.deviceMotionUpdateInterval = 0.02; // 50 Hz |
| 19 | + } |
| 20 | + return self; |
| 21 | +} |
| 22 | + |
| 23 | +RCT_EXPORT_METHOD(startYawUpdates) { |
| 24 | + self.motionDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(motionRefresh:)]; |
| 25 | + [self.motionDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; |
| 26 | + if ([self.motionManager isDeviceMotionActive]) { |
| 27 | + // To avoid using more CPU than necessary we use |
| 28 | + // `CMAttitudeReferenceFrameXArbitraryZVertical` |
| 29 | + [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical]; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +- (void)motionRefresh:(id)sender { |
| 34 | + CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion; |
| 35 | + double yaw = asin(2*(quat.x*quat.z - quat.w*quat.y)); |
| 36 | + |
| 37 | + if (self.motionLastYaw == 0) { |
| 38 | + self.motionLastYaw = yaw; |
| 39 | + } |
| 40 | + |
| 41 | + // Kalman filtering |
| 42 | + static float q = 0.1; // process noise |
| 43 | + static float r = 0.1; // sensor noise |
| 44 | + static float p = 0.1; // estimated error |
| 45 | + static float k = 0.5; // kalman filter gain |
| 46 | + |
| 47 | + float x = self.motionLastYaw; |
| 48 | + p = p + q; |
| 49 | + k = p / (p + r); |
| 50 | + x = x + k*(yaw - x); |
| 51 | + p = (1 - k)*p; |
| 52 | + self.motionLastYaw = x; |
| 53 | + [self.bridge.eventDispatcher sendDeviceEventWithName:@"MotionManager" |
| 54 | + body:@{@"yaw": [NSNumber numberWithDouble:self.motionLastYaw]}]; |
| 55 | +} |
| 56 | + |
| 57 | +RCT_EXPORT_METHOD(stopYawUpdates) { |
| 58 | + [self.motionDisplayLink invalidate]; |
| 59 | +} |
| 60 | + |
| 61 | +@end |
0 commit comments