Advertisement
iSeeker

Quaternion Trial

Oct 11th, 2013
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)saveImageToPhotoAlbum
  2. {
  3.     UIImageWriteToSavedPhotosAlbum(self.takenPic, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  4. }
  5.  
  6. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  7. {
  8.     if (error != NULL) {
  9.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Image couldn't be saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
  10.         [alert show];
  11.     }
  12.     else {
  13.         NSLog(@"Saving");
  14.         timerRunning = NO;
  15.         //      savingCompassHeading = YES;
  16.         //      checkingCompassHeading = NO;
  17.         savingGyroOrientation = YES;
  18.         checkingGyroOrientation = NO;
  19.         self.timerLabel.hidden = YES;
  20.         self.orientationOkay.hidden = YES;
  21.         self.centerCircle.hidden = NO;
  22.         self.savingLabel.hidden = NO;
  23.  
  24.         self.deviceStatus = [NSUserDefaults standardUserDefaults];
  25.        
  26.            
  27.             CMDeviceMotion *currDeviceMotion = self.motionManager.deviceMotion;
  28.                
  29.             CMQuaternion cmQuat = currDeviceMotion.attitude.quaternion;
  30.            
  31.             Quaternion *tempQuat = [[Quaternion alloc] init];
  32.            
  33.             tempQuat = [Quaternion quaternionWithRe:cmQuat.w i:cmQuat.x j:cmQuat.y k:cmQuat.z];
  34.            
  35.             //conjugate and normalize
  36.             self.invertedQuat = [tempQuat inverse];
  37.            
  38.             NSLog(@"Reference alpha is %f", acos(self.invertedQuat.Re));
  39.  
  40.             self.startTimerButton.hidden = NO;
  41.  
  42.        
  43.        
  44.     }
  45.    
  46. }
  47.  
  48.  
  49. -(IBAction)startTimerAction:(id)sender{
  50.     self.startTimerButton.hidden = YES;
  51.     self.timerLabel.hidden = NO;
  52.     self.startDate = [NSDate date];
  53.     timerRunning = YES;
  54.     self.clickTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
  55. }
  56.  
  57. -(void)updateTimer:(NSTimer *)timer{
  58.     // Create date from the elapsed time
  59.     NSDate *currentDate = [NSDate date];
  60.     NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate];
  61.     NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
  62.    
  63.     // Create a date formatter
  64.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  65.     [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
  66.     [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
  67.    
  68.     // Format the elapsed time and set it to the label
  69.     NSString *timeString = [dateFormatter stringFromDate:timerDate];
  70.     self.timerLabel.text = timeString;
  71.    
  72.     CMDeviceMotion *currDeviceMotion = self.motionManager.deviceMotion;
  73.    
  74.     NSLog(@"Reference Alpha is %f",acos(self.invertedQuat.Re));
  75.    
  76.     CMQuaternion newQuat = currDeviceMotion.attitude.quaternion;
  77.        
  78.     self.currentQuat = [Quaternion quaternionWithRe:newQuat.w i:newQuat.x j:newQuat.y k:newQuat.z];
  79.    
  80.     NSLog(@"Current alpha is %f", acos(self.currentQuat.Re));
  81.    
  82.     Quaternion *delta = [[Quaternion alloc] init];
  83.    
  84.     delta = [self.invertedQuat multiply:self.currentQuat];
  85.    
  86.     float alpha = acos(delta.Re)*2.0f;
  87.     float xdiff = asin(delta.i);
  88.     float ydiff = asin(delta.j);
  89.     float zdiff = asin(delta.k);
  90.    
  91.     NSLog(@"Delta Alpha is %f",alpha);
  92.    
  93.     if (fabs (alpha) < 0.50) {
  94.         NSLog(@"Matching angle");
  95.     }
  96.  
  97.     if (xdiff < 0.07 && xdiff   > -0.07) {     
  98.         NSLog(@"xdiff in range");
  99.     }
  100.     if (ydiff < 0.07 && ydiff   > -0.07) {
  101.         NSLog(@"ydiff in range");
  102.     }
  103.     if (zdiff < 0.07 && zdiff   > -0.07) {
  104.         NSLog(@"zdiff in range");
  105.     }
  106.  
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement