source: orbit/iOS/Orbit/Orbit/controllers/AdvancedViewController.m @ 9a24f48

Servo
Last change on this file since 9a24f48 was 9a24f48, checked in by Steve Castellotti <sc@…>, 9 years ago
  • realtime Tilt Sensor control updates fixed
  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[ab9d63b]1//
2//  AdvancedViewController.m
3//  orbit
4//
5//  Copyright (c) 2013 Puzzlebox Productions, LLC. All rights reserved.
[6db346c]6//  Originally created by Jonathon Horsman.
7//
8//  This code is released under the GNU Public License (GPL) version 2
9//  For more information please refer to http://www.gnu.org/copyleft/gpl.html
[ab9d63b]10//
11
12#import "AdvancedViewController.h"
[ce0a7ee]13#import "SignalConverter.h"
14#import "AppDelegate.h"
[ab9d63b]15
[876e42b]16//@interface AdvancedViewController ()
17//
18//@end
[ab9d63b]19
[ce0a7ee]20@implementation AdvancedViewController {
[876e42b]21   SignalConverter *signalConverter;
[ce0a7ee]22}
[ab9d63b]23
24@synthesize pitch, pitchPercent, yaw, yawPercent, throttle, throttlePercent;
25
[ce0a7ee]26- (void)viewDidLoad
27{
[876e42b]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];
[bfa2ba1]36   //   self.motionManager.accelerometerUpdateInterval = .2;
37   //   self.motionManager.gyroUpdateInterval = .2;
38   self.motionManager.accelerometerUpdateInterval = .1;
39   self.motionManager.gyroUpdateInterval = .1;
[876e42b]40   
41   [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
42                                            withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
43                                               [self outputAccelertionData:accelerometerData.acceleration];
44                                               if(error){
45                                                  NSLog(@"%@", error);
46                                               }
47                                            }];
48   
49   [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
50                                   withHandler:^(CMGyroData *gyroData, NSError *error) {
51                                      [self outputRotationData:gyroData.rotationRate];
52                                   }];
[505e3d5]53   
[ce0a7ee]54}
55
[0a45bc8]56- (IBAction) resetButtonPressed:(id)button
57{
[876e42b]58   pitch.value = defaultControlPitch;
59   yaw.value = defaultControlYaw;
60   throttle.value = defaultControlThrottle;
[0a45bc8]61   NSString *zero = @"0%";
62   throttlePercent.text = zero;
63   yawPercent.text = zero;
64   pitchPercent.text = zero;
65   [self adjustValues];
66}
67
[ab9d63b]68- (IBAction) pitchChanged:(id) sender
69{
[876e42b]70   pitchPercent.text = [self percentStringFromSlider:pitch];
71   [self adjustValues];
[ab9d63b]72}
73
74- (IBAction) yawChanged:(id) sender
75{
[876e42b]76   yawPercent.text = [self percentStringFromSlider:yaw];
77   [self adjustValues];
[ab9d63b]78}
79
80- (IBAction) throttleChanged:(id) sender
81{
[876e42b]82   throttlePercent.text = [self percentStringFromSlider:throttle];
83   [self adjustValues];
[ab9d63b]84}
85
86- (NSString *) percentStringFromSlider:(UISlider *) slider
87{
[876e42b]88   return [NSString stringWithFormat:@"%i%%", (int)((slider.value - 0.5) * 200)]; // between -100% and 100%
[ab9d63b]89}
90
[ce0a7ee]91- (void) adjustValues
92{
[876e42b]93   [signalConverter setYaw:[self yawValue]throttle:[self throttleValue] pitch:[self pitchValue]];
[de10cbc]94}
95
96- (int) yawValue
97{
[876e42b]98   return round(yaw.value * 100);
[de10cbc]99}
100
101- (int) pitchValue
102{
[876e42b]103   return round(pitch.value * 100);
[de10cbc]104}
105
106- (int) throttleValue
107{
[876e42b]108   return round(throttle.value * 100);
[de10cbc]109}
110
[0a45bc8]111- (IBAction) hoverButtonPressed:(id)sender {
[7f21a20]112   [self resetButtonPressed:(sender)];
113   
[ce0a7ee]114}
115
[0a45bc8]116- (IBAction) forwardButtonPressed:(id)sender {
[7f21a20]117   pitch.value = 0.8;
118   yaw.value = 0.5;
119   throttle.value = 0.5;
120   [self adjustValues];
121   
[0a45bc8]122}
123
124- (IBAction) leftButtonPressed:(id)sender {
[7f21a20]125   pitch.value = 0.5;
126   yaw.value = 0.1;
127   throttle.value = 0.5;
128   [self adjustValues];
[876e42b]129   
[0a45bc8]130}
131
132- (IBAction) rightButtonPressed:(id)sender {
[7f21a20]133   pitch.value = 0.5;
134   yaw.value = 0.9;
135   throttle.value = 0.5;
136   [self adjustValues];
[876e42b]137   
[0a45bc8]138}
139
[d42b042]140- (IBAction)changeSwitchTiltSensorControl:(id)sender{
[876e42b]141   
142   // With thanks to Kyro Apps
143   // http://kyroapps.weebly.com/xcode-tutorial-452---uiswitch.html
144   
[6db346c]145   if([sender isOn]){
146      NSLog(@"DEBUG: Tilt Sensor Enabled");
[d5f1a91]147      [_switchTiltSensorControlThrottle setEnabled:YES];
[6db346c]148   } else{
149      NSLog(@"DEBUG: Tilt Sensor Disabled");
[d5f1a91]150      [_switchTiltSensorControlThrottle setOn:NO animated:YES];
151      [_switchTiltSensorControlThrottle setEnabled:NO];
[6db346c]152   }
153   
154}
155
[d42b042]156- (IBAction)changeSwitchTiltSensorControlThrottle:(id)sender{
[6db346c]157   
158   if([sender isOn]){
159      NSLog(@"DEBUG: Throttle Tilt Sensor Enabled");
160   } else{
161      NSLog(@"DEBUG: Throttle Tilt Sensor Disabled");
162   }
163   
164}
165
[876e42b]166-(void)outputAccelertionData:(CMAcceleration)acceleration
167{
168   // With thanks to NSCookbook
169   // http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/
170   
[bfa2ba1]171   if ([_switchTiltSensorControl isOn]) {
172     
173      if (! [_switchTiltSensorControlThrottle isOn]) {
174         
175         // Pitch/Yaw Tilt Control
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      } // Pitch/Yaw Tilt Control
192     
193      else {
194         
195         // Throttle Tilt Control
196         
197         double tiltY = acceleration.y * -1;  // invert the Y axis so that negative values equal forward
198         
[9a24f48]199         throttle.value = defaultControlThrottle - (tiltY - referenceTiltY);
[bfa2ba1]200         
201      } // Throttle Tilt Control
202     
[9a24f48]203
204      [self adjustValues];
205     
[bfa2ba1]206   }
[876e42b]207   
208}
209
210-(void)outputRotationData:(CMRotationRate)rotation
211{
212   
213}
214
215- (void)didReceiveMemoryWarning
216{
217   [super didReceiveMemoryWarning];
218   // Dispose of any resources that can be recreated.
219}
220
221
[ab9d63b]222@end
Note: See TracBrowser for help on using the repository browser.