Ignore:
Timestamp:
12/30/13 13:23:09 (7 years ago)
Author:
Steve Castellotti <sc@…>
Branches:
master, Servo
Children:
bfa2ba1
Parents:
7f21a20
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • iOS/Orbit/Orbit/controllers/AdvancedViewController.m

    r7f21a20 r876e42b  
    1414#import "AppDelegate.h" 
    1515 
    16 @interface AdvancedViewController () 
    17  
    18 @end 
     16//@interface AdvancedViewController () 
     17// 
     18//@end 
    1919 
    2020@implementation AdvancedViewController { 
    21     SignalConverter *signalConverter; 
     21   SignalConverter *signalConverter; 
    2222} 
    2323 
     
    2626- (void)viewDidLoad 
    2727{ 
    28     [super viewDidLoad]; 
    29     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    30     signalConverter = appDelegate.signalConverter; 
    31     
    32 //   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Help Wanted" message:@"Please join our Open Source development community to help us port this feature! See link to Forums under Support tab" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    33 //   [alert show]; 
    34       
     28   [super viewDidLoad]; 
     29   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
     30   signalConverter = appDelegate.signalConverter; 
     31    
     32   //   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Help Wanted" message:@"Please join our Open Source development community to help us port this feature! See link to Forums under Support tab" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     33   //   [alert show]; 
     34    
     35   self.motionManager = [[CMMotionManager alloc] init]; 
     36   self.motionManager.accelerometerUpdateInterval = .2; 
     37   self.motionManager.gyroUpdateInterval = .2; 
     38    
     39   [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] 
     40                                            withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
     41                                               [self outputAccelertionData:accelerometerData.acceleration]; 
     42                                               if(error){ 
     43                                                  NSLog(@"%@", error); 
     44                                               } 
     45                                            }]; 
     46    
     47   [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] 
     48                                   withHandler:^(CMGyroData *gyroData, NSError *error) { 
     49                                      [self outputRotationData:gyroData.rotationRate]; 
     50                                   }]; 
     51    
    3552} 
    3653 
    3754- (IBAction) resetButtonPressed:(id)button 
    3855{ 
    39    pitch.value = 0.5; 
    40    yaw.value = 0.5; 
    41    throttle.value = 0.5; 
     56   pitch.value = defaultControlPitch; 
     57   yaw.value = defaultControlYaw; 
     58   throttle.value = defaultControlThrottle; 
    4259   NSString *zero = @"0%"; 
    4360   throttlePercent.text = zero; 
     
    4966- (IBAction) pitchChanged:(id) sender 
    5067{ 
    51     pitchPercent.text = [self percentStringFromSlider:pitch]; 
    52     [self adjustValues]; 
     68   pitchPercent.text = [self percentStringFromSlider:pitch]; 
     69   [self adjustValues]; 
    5370} 
    5471 
    5572- (IBAction) yawChanged:(id) sender 
    5673{ 
    57     yawPercent.text = [self percentStringFromSlider:yaw]; 
    58     [self adjustValues]; 
     74   yawPercent.text = [self percentStringFromSlider:yaw]; 
     75   [self adjustValues]; 
    5976} 
    6077 
    6178- (IBAction) throttleChanged:(id) sender 
    6279{ 
    63     throttlePercent.text = [self percentStringFromSlider:throttle]; 
    64     [self adjustValues]; 
     80   throttlePercent.text = [self percentStringFromSlider:throttle]; 
     81   [self adjustValues]; 
    6582} 
    6683 
    6784- (NSString *) percentStringFromSlider:(UISlider *) slider 
    6885{ 
    69     return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; // between -100% and 100% 
     86   return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; // between -100% and 100% 
    7087} 
    7188 
    7289- (void) adjustValues 
    7390{ 
    74     [signalConverter setYaw:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]]; 
     91   [signalConverter setYaw:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]]; 
    7592} 
    7693 
    7794- (int) yawValue 
    7895{ 
    79     return round(yaw.value * 100); 
     96   return round(yaw.value * 100); 
    8097} 
    8198 
    8299- (int) pitchValue 
    83100{ 
    84     return round(pitch.value * 100); 
     101   return round(pitch.value * 100); 
    85102} 
    86103 
    87104- (int) throttleValue 
    88105{ 
    89     return round(throttle.value * 100); 
     106   return round(throttle.value * 100); 
    90107} 
    91108 
     
    96113 
    97114- (IBAction) forwardButtonPressed:(id)sender { 
    98    NSLog(@"DEBUG: forwardButtonPressed"); 
    99     
    100115   pitch.value = 0.8; 
    101116   yaw.value = 0.5; 
     
    106121 
    107122- (IBAction) leftButtonPressed:(id)sender { 
    108    NSLog(@"DEBUG: leftButtonPressed"); 
    109123   pitch.value = 0.5; 
    110124   yaw.value = 0.1; 
    111125   throttle.value = 0.5; 
    112126   [self adjustValues]; 
    113  
     127    
    114128} 
    115129 
    116130- (IBAction) rightButtonPressed:(id)sender { 
    117    NSLog(@"DEBUG: rightButtonPressed"); 
    118131   pitch.value = 0.5; 
    119132   yaw.value = 0.9; 
    120133   throttle.value = 0.5; 
    121134   [self adjustValues]; 
    122  
    123 } 
    124  
    125  
     135    
     136} 
    126137 
    127138- (IBAction)changeSwitchTiltSensorControl:(id)sender{ 
    128  
     139    
     140   // With thanks to Kyro Apps 
     141   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html 
     142    
    129143   if([sender isOn]){ 
    130144      NSLog(@"DEBUG: Tilt Sensor Enabled"); 
     
    148162} 
    149163 
     164-(void)outputAccelertionData:(CMAcceleration)acceleration 
     165{ 
     166   // With thanks to NSCookbook 
     167   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/ 
     168    
     169   //   // Sensor.TYPE_GRAVITY 
     170   //   if (checkBoxTiltSensorControlThrottle.isChecked()) { 
     171   //      seekBarThrottle.setProgress(defaultControlThrottle - (int) ((tiltX * 2) - (referenceTiltX * 2))); 
     172   //   } else { 
     173   //      seekBarYaw.setProgress(defaultControlYaw + (int) (tiltY - referenceTiltY)); 
     174   //      seekBarPitch.setProgress(defaultControlPitch + (int) (tiltX - referenceTiltX)); 
     175   //   } 
     176    
     177   double tiltX = acceleration.x; 
     178   double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal left 
     179    
     180   NSLog(@"DEBUG: tiltX:%f tiltY:%f", tiltX, tiltY); 
     181 
     182   double newYaw = defaultControlYaw + (tiltX - referenceTiltX); 
     183   double newPitch = defaultControlPitch - (tiltY - referenceTiltY); 
     184    
     185   NSLog(@"DEBUG: yaw:%f pitch:%f", yaw.value, pitch.value); 
     186   NSLog(@"DEBUG: newYaw:%f newPitch:%f", newYaw, newPitch); 
     187    
     188   yaw.value = newYaw; 
     189   pitch.value = newPitch; 
     190    
     191   [self adjustValues]; 
     192    
     193} 
     194 
     195-(void)outputRotationData:(CMRotationRate)rotation 
     196{ 
     197    
     198} 
     199 
     200- (void)didReceiveMemoryWarning 
     201{ 
     202   [super didReceiveMemoryWarning]; 
     203   // Dispose of any resources that can be recreated. 
     204} 
     205 
     206 
    150207@end 
Note: See TracChangeset for help on using the changeset viewer.